/
nfl_game_stats-2.0.2.js
146 lines (134 loc) · 3.9 KB
/
nfl_game_stats-2.0.2.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/**
* nfl_game_stats.js
*
* Creates JSON object of NFL's week games given in XML format.
*
* @author Eric Metcalf
* @requires DayHours.js
*/
/**
* Parses XML and returns week game stats.
*/
function getWeekGameStats(xmlWeekGameStats) {
parser = new DOMParser();
xmlDoc = parser.parseFromString(xmlWeekGameStats, "text/xml");
return new WeekGameStats(xmlDoc);
}
/**
* Class containing week game stats for given XML.
*/
function WeekGameStats(xmlDoc) {
var weekData = xmlDoc.getElementsByTagName("gms")[0];
this.week = weekData.getAttribute("w");
this.year = weekData.getAttribute("y");
this.gameStats = getGameStats(xmlDoc);
}
/**
* Returns game stats for fiven XML.
*/
function getGameStats(xmlDoc) {
var gameStats = [];
var games = xmlDoc.getElementsByTagName("g");
for (i = 0; i < games.length; i++) {
gameStats.push(new GameStats(
games[i].getAttribute("d"),
games[i].getAttribute("t"),
games[i].getAttribute("q"),
games[i].getAttribute("k"),
games[i].getAttribute("v"),
games[i].getAttribute("vnn"),
games[i].getAttribute("vs"),
games[i].getAttribute("h"),
games[i].getAttribute("hnn"),
games[i].getAttribute("hs"),
games[i].getAttribute("p"),
games[i].getAttribute("rz"),
games[i].getAttribute("ga"),
games[i].getAttribute("gt"),
games[i].getAttribute("eid")
));
}
return gameStats;
}
/**
* Class containing stats for an NFL game.
*/
function GameStats(gameDay, gameTime, abbrGameStatus, timeInQuarter, awayTeam, awayTeamMascot, awayTeamScore, homeTeam, homeTeamMascot, homeTeamScore, teamWithPossession, isInRedZone, gameAlert, gameType, id) {
this.gameDay = gameDay;
this.gameTime = getOffsetGameTime(gameTime);
this.gameStatus = getGameStatus(abbrGameStatus);
this.timeInQuarter = timeInQuarter;
this.awayTeam = new Team(awayTeam, capitalize(awayTeamMascot));
this.awayTeamScore = awayTeamScore;
this.homeTeam = new Team(homeTeam, capitalize(homeTeamMascot));
this.homeTeamScore = homeTeamScore;
this.teamWithPossession = teamWithPossession;
this.isInRedZone = isInRedZone;
this.gameAlert = gameAlert;
this.gameType = gameType;
this.id = id;
}
/**
* Returns local time given eastern time in HH:MM format.
*
* @return {string} Local time in HH:MM format
*/
function getOffsetGameTime(gameTime) {
var splitGameTime = gameTime.split(":");
// TODO move the code that gets timezoneOffsetFromEastern so it's not calculated multiple times.
var localTime = new Date();
var localTimeaoneOffset = -(localTime.getTimezoneOffset() / 60);
var easternTimezoneOffset = -5; // Time zone given to us from JSON.
var timezoneOffsetFromEastern = localTimeaoneOffset + -(easternTimezoneOffset);
var dayHours = new DayHours(parseInt(splitGameTime[0]));
if (timezoneOffsetFromEastern < 0) {
dayHours.subtractHours(Math.abs(timezoneOffsetFromEastern));
} else {
dayHours.addHours(timezoneOffsetFromEastern);
}
return (dayHours.currentHour + ":" + splitGameTime[1]);
}
/**
* Returns game status in human readable text.
*/
function getGameStatus(abbrGameStatus) {
var gameStatus;
switch(abbrGameStatus) {
case "P":
gameStatus = "Pregame";
break;
case "H":
gameStatus = "Halftime";
break;
case "5":
gameStatus = "Overtime";
break;
case "F":
gameStatus = "Final";
break;
case "FO":
gameStatus = "Final Overtime";
break;
default:
gameStatus = abbrGameStatus;
}
return gameStatus;
}
/**
* Returns string with first letter capitalized.
*/
function capitalize(str) {
if (str != null) {
return str.charAt(0).toUpperCase() + str.slice(1);
}
}
/**
* Class containing attributes of an NFL team.
*/
function Team(abbrCityName, mascotName) {
this.abbrCityname = abbrCityName;
this.mascotName = mascotName;
}
Team.prototype.toString = function teamToString() {
return this.abbrCityname;
}