-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse-basketball.js
124 lines (119 loc) · 7.27 KB
/
parse-basketball.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
var getDataLoc = window.location.href + 'livescore.parse.php';
function parseGameData() {
// live updates from feed parse
$.getJSON(getDataLoc, function(data) {
hometeamid = awayteamid = hometeamname = awayteamname = '';
//set up team names and logos
if (data["team-sport-content"]["league-content"]["competition"] && data["team-sport-content"]["league-content"]["competition"]["home-team-content"] && data["team-sport-content"]["league-content"]["competition"]["away-team-content"]) {
hometeamid = data["team-sport-content"]["league-content"]["competition"]["home-team-content"]["team"]["name"][1];
awayteamid = data["team-sport-content"]["league-content"]["competition"]["away-team-content"]["team"]["name"][1];
hometeamname = data["team-sport-content"]["league-content"]["competition"]["home-team-content"]["team"]["name"][0];
awayteamname = data["team-sport-content"]["league-content"]["competition"]["away-team-content"]["team"]["name"][0];
$('#scores > div.teams').html('<h2>' + awayteamid.toUpperCase() + ' <span class="vs">vs</span> ' + hometeamid.toUpperCase() + '</h2>');
if ( $('#away > img').length == 0) {
$('#away').prepend('<img src="./img/logo-' + awayteamid.toLowerCase() + '.png" alt="' + awayteamname + ' logo" />');
}
if ( $('#home > img').length == 0) {
$('#home').prepend('<img src="./img/logo-' + hometeamid.toLowerCase() + '.png" alt="' + hometeamname + ' logo" />');
}
} else if (data["custom-content"]["heading"]) {
$('#scores > div.teams').html('<h2>' + data["custom-content"]["heading"]) + '</h2>';
}
//update home team score
var homescore = '';
if (data["team-sport-content"]["league-content"]["competition"]["home-team-content"]) {
$(data["team-sport-content"]["league-content"]["competition"]["home-team-content"]["stat-group"]).each(function(index, element){
if (element["scope"]["@attributes"]["type"] == 'competition') {
homescore = element["stat"]["@attributes"]["num"];
}
})
}
$('#homescore').html(homescore);
//update away team score
var awayscore = '';
if (data["team-sport-content"]["league-content"]["competition"]["away-team-content"]) {
$(data["team-sport-content"]["league-content"]["competition"]["away-team-content"]["stat-group"]).each(function(index, element){
if (element["scope"]["@attributes"]["type"] == 'competition') {
awayscore = element["stat"]["@attributes"]["num"];
}
})
}
$('#awayscore').html(awayscore);
// get the start time/quarter/overtime/etc
// start time
var startTime = new Date(data["team-sport-content"]["league-content"]["competition"]["start-date"]);
function formatAMPM(date) {
var hours = startTime.getHours();
var minutes = startTime.getMinutes();
var ampm = hours >= 12 ? ' p.m.' : ' a.m.';
hours = hours % 12;
hours = hours ? hours : 12; // the hour '0' should be '12'
minutes = minutes < 10 ? '0'+minutes : minutes;
var strTime = hours + ':' + minutes + ampm;
return strTime;
}
var timeDisplay = (startTime.getMonth() + 1) + '/' + startTime.getDate() + ', ' + formatAMPM(startTime);
// parse and beautify the period/overtime (no code for shootout yet)
if (data["team-sport-content"]["league-content"]["competition"]["result-scope"]) {
// if the game isn't over
var quarterOut = '';
// quarters
var quarter = data["team-sport-content"]["league-content"]["competition"]["result-scope"]["scope"]["@attributes"]["num"];
var overtime = data["team-sport-content"]["league-content"]["competition"]["result-scope"]["scope"]["@attributes"]["type"];
var quarterover = data["team-sport-content"]["league-content"]["competition"]["result-scope"]["scope-status"];
if (overtime == 'period') {
if (quarter == 1) {
quarterOut = (quarterover == 'complete') ? 'End of 1st' : '1st quarter';
} else if (quarter == 2) {
quarterOut = (quarterover == 'complete') ? 'Halftime' : '2nd quarter';
} else if (quarter == 3) {
quarterOut = (quarterover == 'complete') ? 'End of 3rd' : '3rd quarter';
} else if (quarter == 4) {
quarterOut = (quarterover == 'complete' && (homescore != awayscore)) ? 'Final' : '4th quarter';
}
} else if (overtime == 'overtime') {
if (quarter == 1) {
quarterOut = (quarterover == 'complete' && (homescore != awayscore)) ? 'Final (OT)' : '1st overtime';
} else if (quarter == 2) {
quarterOut = (quarterover == 'complete' && (homescore != awayscore)) ? 'Final (2 OT)' : '2nd overtime';
} else if (quarter == 3) {
quarterOut = (quarterover == 'complete' && (homescore != awayscore)) ? 'Final (3 OT)' : '3rd overtime';
} else if (quarter == 4) {
quarterOut = (quarterover == 'complete') ? 'Final (4 OT)' : '4th overtime';
}
}
}
if (data["team-sport-content"]["league-content"]["competition"]["result-scope"]) {
// time remaining only if game still running
if (data["team-sport-content"]["league-content"]["competition"]["result-scope"]["competition-status"] != 'complete' && data["team-sport-content"]["league-content"]["competition"]["result-scope"]["scope-status"] != 'complete') {
var timeRemain = data["team-sport-content"]["league-content"]["competition"]["result-scope"]["clock"];
var timeChunks = timeRemain.split(':');
var timeRemaining = parseInt(timeChunks[1],10) + ':' + timeChunks[2];
}
}
// if we have a quarter we'll display it, otherwise we'll put in the start time
if (quarter != null) {
$('#quarter').html(quarterOut);
if (data["team-sport-content"]["league-content"]["competition"]["result-scope"]) {
if (data["team-sport-content"]["league-content"]["competition"]["result-scope"]["competition-status"] == 'in-progress') {
if (timeRemaining) { $('#bullet').html('•'); } else { $('#bullet').html(''); }
var timeToShow = (timeRemaining) ? timeRemaining : '';
$('#remaining').html(timeToShow);
} else if (data["team-sport-content"]["league-content"]["competition"]["result-scope"]["competition-status"] == 'complete') {
$('#quarter').html(quarterOut);
}
}
} else {
$('#quarter').html(timeDisplay);
var inputtime = new Date(data["custom-content"]["unixtime"]*1000);
if (!startedCount) startCountdown(inputtime);
}
});
}
parseGameData();
$(document).ready(function() {
$.ajaxSetup({ cache: false });
setInterval(function() {
parseGameData();
}, 10000);
});