]> git.sesse.net Git - ultimatescore/blob - carousel.js
Add a FIXME.
[ultimatescore] / carousel.js
1 'use strict';
2
3 // Log with deep clone, so that the browser will show the object at time of log,
4 // instead of what it looks like at time of view.
5 function dlog()
6 {
7         let args = [];
8         for (const arg of arguments) {
9                 args.push(JSON.parse(JSON.stringify(arg)));
10         }
11         console.log(args);
12 }
13
14 function addheading(carousel, colspan, content)
15 {
16         let thead = document.createElement("thead");
17         let tr = document.createElement("tr");
18         let th = document.createElement("th");
19         th.innerHTML = content;
20         th.setAttribute("colspan", colspan);
21         tr.appendChild(th);
22         thead.appendChild(tr);
23         carousel.appendChild(thead);
24 }
25
26 function addtd(tr, className, content) {
27         let td = document.createElement("td");
28         td.appendChild(document.createTextNode(content));
29         td.className = className;
30         tr.appendChild(td);
31 }
32
33 function addth(tr, className, content) {
34         let th = document.createElement("th");
35         th.appendChild(document.createTextNode(content));
36         th.className = className;
37         tr.appendChild(th);
38 }
39
40 function subrank_partitions(games, parts, start_rank, tiebreakers, func) {
41         let result = [];
42         for (let i = 0; i < parts.length; ++i) {
43                 let part = func(games, parts[i], start_rank, tiebreakers);
44                 for (let j = 0; j < part.length; ++j) {
45                         result.push(part[j]);
46                 }
47                 start_rank += part.length;
48         }
49         return result;
50 };
51
52 function partition(teams, compare)
53 {
54         teams.sort(compare);
55
56         let parts = [];
57         let curr_part = [teams[0]];
58         for (let i = 1; i < teams.length; ++i) {
59                 if (compare(teams[i], curr_part[0]) != 0) {
60                         parts.push(curr_part);
61                         curr_part = [];
62                 }
63                 curr_part.push(teams[i]);
64         }
65         if (curr_part.length != 0) {
66                 parts.push(curr_part);
67         }
68         return parts;
69 };
70
71 function explain_tiebreaker(parts, rule_name)
72 {
73         let result = [];
74         for (let i = 0; i < parts.length; ++i) {
75                 result.push(parts[i].map(function(x) { return x.shortname; }).join("/"));
76         }
77         return result.join(" > ") + " (" + rule_name + ")";
78 }
79
80 function make_teams_to_idx(teams)
81 {
82         let teams_to_idx = [];
83         for (let i = 0; i < teams.length; i++) {
84                 teams_to_idx[teams[i].name] = i;
85                 teams_to_idx[teams[i].mediumname] = i;
86                 teams_to_idx[teams[i].shortname] = i;
87         }
88         return teams_to_idx;
89 }
90
91 function partition_by_beat(teams, fill_beatmatrix)
92 {
93         // Head-to-head score by way of components. First construct the beat matrix.
94         let n = teams.length;
95         let beat = new Array(n);
96         let teams_to_idx = make_teams_to_idx(teams);
97         for (let i = 0; i < n; i++) {
98                 beat[i] = new Array(n);
99                 for (let j = 0; j < n; j++) {
100                         beat[i][j] = 0;
101                 }
102         }
103         fill_beatmatrix(beat, teams_to_idx);
104         // Floyd-Warshall for transitive closure.
105         for (let k = 0; k < n; ++k) {
106                 for (let i = 0; i < n; ++i) {
107                         for (let j = 0; j < n; ++j) {
108                                 if (beat[i][k] && beat[k][j]) {
109                                         beat[i][j] = 1;
110                                 }
111                         }
112                 }
113         }
114
115         // See if we can find any team that is comparable to all others.
116         for (let pivot_idx = 0; pivot_idx < n; pivot_idx++) {
117                 let incomparable = false;
118                 for (let i = 0; i < n; ++i) {
119                         if (i != pivot_idx && beat[pivot_idx][i] == 0 && beat[i][pivot_idx] == 0) {
120                                 incomparable = true;
121                                 break;
122                         }
123                 }
124                 if (!incomparable) {
125                         // Split the teams into three partitions:
126                         let better_than_pivot = [], equal = [], worse_than_pivot = [];
127                         for (let i = 0; i < n; ++i) {
128                                 let we_beat = (beat[pivot_idx][i] == 1);
129                                 let they_beat = (beat[i][pivot_idx] == 1);
130                                 if ((i == pivot_idx) || (we_beat && they_beat)) {
131                                         equal.push(teams[i]);
132                                 } else if (we_beat && !they_beat) {
133                                         worse_than_pivot.push(teams[i]);
134                                 } else if (they_beat && !we_beat) {
135                                         better_than_pivot.push(teams[i]);
136                                 } else {
137                                         console.log("this shouldn't happen");
138                                 }
139                         } 
140                         let result = [];
141                         if (better_than_pivot.length > 0) {
142                                 result = partition_by_beat(better_than_pivot, fill_beatmatrix);
143                         }
144                         result.push(equal);  // Obviously can't be partitioned further.
145                         if (worse_than_pivot.length > 0) {
146                                 result = result.concat(partition_by_beat(worse_than_pivot, fill_beatmatrix));
147                         }
148                         return result;
149                 }
150         }
151
152         // No usable pivot was found, so the graph is inherently
153         // disconnected, and we cannot partition it.
154         return [teams];
155 }
156
157 // Takes in an array, gives every element a rank starting with 1, and returns.
158 function rank(games, teams, start_rank, tiebreakers) {
159         if (teams.length <= 1) {
160                 // Only one team, so trivial.
161                 teams[0].rank = start_rank;
162                 return teams;
163         }
164
165         // Rule #0: Partition the teams by score.
166         let score_parts = partition(teams, function(a, b) { return b.pts - a.pts });
167         if (score_parts.length > 1) {
168                 return subrank_partitions(games, score_parts, start_rank, tiebreakers, rank);
169         }
170
171         // Rule #1: Head-to-head wins.
172         let num_relevant_games = 0;
173         let beat_parts = partition_by_beat(teams, function(beat, teams_to_idx) {
174                 for (let i = 0; i < games.length; ++i) {
175                         let idx1 = teams_to_idx[games[i].name1];
176                         let idx2 = teams_to_idx[games[i].name2];
177                         if (idx1 !== undefined && idx2 !== undefined) {
178                                 if (games[i].score1 > games[i].score2) {
179                                         beat[idx1][idx2] = 1;
180                                         ++num_relevant_games;
181                                 } else if (games[i].score1 < games[i].score2) {
182                                         beat[idx2][idx1] = 1;
183                                         ++num_relevant_games;
184                                 }
185                         }
186                 }
187         });
188         if (beat_parts.length > 1) {
189                 tiebreakers.push(explain_tiebreaker(beat_parts, 'head-to-head'));
190                 return subrank_partitions(games, beat_parts, start_rank, tiebreakers, rank);
191         }
192
193         // Rule #2: Number of games played (fewer is better).
194         // Actually the rule says “fewest losses”, but fewer games is equivalent
195         // as long as teams have the same amount of points and ties don't exist.
196         let nplayed_parts = partition(teams, function(a, b) { return a.nplayed - b.nplayed });
197         if (nplayed_parts.length > 1) {
198                 tiebreakers.push(explain_tiebreaker(nplayed_parts, 'fewer losses'));
199                 return subrank_partitions(games, nplayed_parts, start_rank, tiebreakers, rank);
200         }
201
202         // Rule #3: Head-to-head goal difference (if all have played).
203         let teams_to_idx = make_teams_to_idx(teams);
204         if (num_relevant_games >= teams.length * (teams.length - 1) / 2) {
205                 for (let i = 0; i < teams.length; i++) {
206                         teams[i].h2h_gd = 0;
207                         teams[i].h2h_goals = 0;
208                 }
209                 for (let i = 0; i < games.length; ++i) {
210                         let idx1 = teams_to_idx[games[i].name1];
211                         let idx2 = teams_to_idx[games[i].name2];
212                         if (idx1 !== undefined && idx2 !== undefined &&
213                             !isNaN(games[i].score1) && !isNaN(games[i].score2)) {
214                                 teams[idx1].h2h_gd += games[i].score1;
215                                 teams[idx1].h2h_gd -= games[i].score2;
216                                 teams[idx2].h2h_gd += games[i].score2;
217                                 teams[idx2].h2h_gd -= games[i].score1;
218
219                                 teams[idx1].h2h_goals += games[i].score1;
220                                 teams[idx2].h2h_goals += games[i].score2;
221                         }
222                 }
223                 let h2h_gd_parts = partition(teams, function(a, b) { return b.h2h_gd - a.h2h_gd });
224                 if (h2h_gd_parts.length > 1) {
225                         tiebreakers.push(explain_tiebreaker(h2h_gd_parts, 'head-to-head goal difference'));
226                         return subrank_partitions(games, h2h_gd_parts, start_rank, tiebreakers, rank);
227                 }
228         }
229
230         // Rule #4: Goal difference against common opponents.
231         var results = {};
232         for (let i = 0; i < games.length; ++i) {
233                 if (results[games[i].name1] === undefined) {
234                         results[games[i].name1] = {};
235                 }
236                 if (results[games[i].name2] === undefined) {
237                         results[games[i].name2] = {};
238                 }
239                 results[games[i].name1][games[i].name2] = [ games[i].score1, games[i].score2 ];
240                 results[games[i].name2][games[i].name1] = [ games[i].score2, games[i].score1 ];
241         }
242         let gd_parts = partition_by_beat(teams, function(beat, teams_to_idx) {
243                 for (const team_i of Object.keys(teams_to_idx)) {
244                         let i = teams_to_idx[team_i];
245                         for (const team_j of Object.keys(teams_to_idx)) {
246                                 let j = teams_to_idx[team_j];
247                                 let results_i = results[team_i], results_j = results[team_j];
248                                 let gd_i = 0, gd_j = 0;
249
250                                 // See if the two teams have both played a third team k.
251                                 for (let k in results_i) {
252                                         if (!results_i.hasOwnProperty(k)) continue;
253                                         if (results_j !== undefined && results_j[k] !== undefined) {
254                                                 gd_i += results_i[k][0] - results_i[k][1];
255                                                 gd_j += results_j[k][0] - results_j[k][1];
256                                         }
257                                 }
258
259                                 if (gd_i > gd_j) {
260                                         beat[i][j] = 1;
261                                 } else if (gd_i < gd_j) {
262                                         beat[j][i] = 1;
263                                 }
264                         }
265                 }
266         });
267         if (gd_parts.length > 1) {
268                 tiebreakers.push(explain_tiebreaker(gd_parts, 'goal difference versus common opponents'));
269                 return subrank_partitions(games, gd_parts, start_rank, tiebreakers, rank);
270         }
271
272         // Rule #5: Head-to-head scored goals (if all have played).
273         if (num_relevant_games >= teams.length * (teams.length - 1) / 2) {
274                 let h2h_goals_parts = partition(teams, function(a, b) { return b.h2h_goals - a.h2h_goals });
275                 if (h2h_goals_parts.length > 1) {
276                         tiebreakers.push(explain_tiebreaker(h2h_goals_parts, 'head-to-head scored goals'));
277                         return subrank_partitions(games, h2h_goals_parts, start_rank, tiebreakers, rank);
278                 }
279         }
280
281         // Rule #6: Goals scored against common opponents.
282         let goals_parts = partition_by_beat(teams, function(beat, teams_to_idx) {
283                 for (const team_i of Object.keys(teams_to_idx)) {
284                         let i = teams_to_idx[team_i];
285                         for (const team_j of Object.keys(teams_to_idx)) {
286                                 let j = teams_to_idx[team_j];
287                                 let results_i = results[team_i], results_j = results[team_j];
288                                 let goals_i = 0, goals_j = 0;
289
290                                 // See if the two teams have both played a third team k.
291                                 for (let k in results_i) {
292                                         if (!results_i.hasOwnProperty(k)) continue;
293                                         if (results_j !== undefined && results_j[k] !== undefined) {
294                                                 goals_i += results_i[k][0];
295                                                 goals_j += results_j[k][0];
296                                         }
297                                 }
298
299                                 if (goals_i > goals_j) {
300                                         beat[i][j] = 1;
301                                 } else if (goals_i < goals_j) {
302                                         beat[j][i] = 1;
303                                 }
304                         }
305                 }
306         });
307         if (goals_parts.length > 1) {
308                 tiebreakers.push(explain_tiebreaker(goals_parts, 'goals scored against common opponents'));
309                 return subrank_partitions(games, goals_parts, start_rank, tiebreakers, rank);
310         }
311
312         // OK, it's a tie. Give them all the same rank.
313         let result = [];
314         for (let i = 0; i < teams.length; ++i) {
315                 result.push(teams[i]);
316                 result[i].rank = start_rank;
317         }
318         return result; 
319 }; 
320
321 // Same, but with the simplified rules for ranking thirds. games isn't used and can be empty.
322 function rank_thirds(games, teams, start_rank, tiebreakers) {
323         if (teams.length <= 1) {
324                 // Only one team, so trivial.
325                 teams[0].rank = start_rank;
326                 return teams;
327         }
328
329         // Rule #1: Partition the teams by score.
330         let score_parts = partition(teams, function(a, b) { return b.pts - a.pts });
331         if (score_parts.length > 1) {
332                 tiebreakers.push(explain_tiebreaker(score_parts, 'most games won'));
333                 return subrank_partitions(games, score_parts, start_rank, tiebreakers, rank_thirds);
334         }
335
336         // Rule #2: Goal difference against common opponents.
337         let gd_parts = partition(teams, function(a, b) { return b.gd - a.gd });
338         if (gd_parts.length > 1) {
339                 tiebreakers.push(explain_tiebreaker(gd_parts, 'goal difference'));
340                 return subrank_partitions(games, gd_parts, start_rank, tiebreakers, rank_thirds);
341         }
342         
343         // Rule #3: Goals scored.
344         let goal_parts = partition(teams, function(a, b) { return b.goals - a.goals });
345         if (goal_parts.length > 1) {
346                 tiebreakers.push(explain_tiebreaker(goal_parts, 'goals scored'));
347                 return subrank_partitions(games, goal_parts, start_rank, tiebreakers, rank_thirds);
348         }
349
350         // OK, it's a tie. Give them all the same rank.
351         let result = [];
352         for (let i = 0; i < teams.length; ++i) {
353                 result.push(teams[i]);
354                 result[i].rank = start_rank;
355         }
356         return result; 
357 }; 
358
359 function parse_teams_from_spreadsheet(response) {
360         let teams = [];
361         for (let i = 2; response.values[i].length >= 1; ++i) {
362                 teams.push({
363                         "name": response.values[i][0],
364                         "mediumname": response.values[i][1],
365                         "shortname": response.values[i][2],
366                         //"tags": response.values[i][3],
367                         "ngames": 0,
368                         "nplayed": 0,
369                         "gd": 0,
370                         "pts": 0,
371                         "goals": 0
372                 });
373         }
374         return teams;
375 };
376
377 function parse_games_from_spreadsheet(response, group_name, include_unplayed) {
378         let games = [];
379         let i;
380         for (i = 0; i < response.values.length; ++i) {
381                 if (response.values[i][0] === 'Results') {
382                         i += 2;
383                         break;
384                 }
385         }
386
387         for ( ; response.values[i] !== undefined && response.values[i].length >= 1; ++i) {
388                 if ((response.values[i][2] && response.values[i][3]) || include_unplayed) {
389                         let real_group_name = response.values[i][9];
390                         if (real_group_name === undefined) {
391                                 real_group_name = group_name;
392                         }
393                         games.push({
394                                 "name1": response.values[i][0],
395                                 "name2": response.values[i][1],
396                                 "score1": parseInt(response.values[i][2]),
397                                 "score2": parseInt(response.values[i][3]),
398                                 "streamday": response.values[i][7],
399                                 "streamtime": response.values[i][8],
400                                 "group_name": real_group_name
401                         });
402                 }
403         }
404         return games;
405 };
406
407 function apply_games_to_teams(games, teams)
408 {
409         let teams_to_idx = make_teams_to_idx(teams);
410         for (let i = 0; i < games.length; ++i) {
411                 let idx1 = teams_to_idx[games[i].name1];
412                 let idx2 = teams_to_idx[games[i].name2];
413                 if (games[i].score1 === undefined || games[i].score2 === undefined ||
414                     isNaN(games[i].score1) || isNaN(games[i].score2) ||
415                     idx1 === undefined || idx2 === undefined ||
416                     games[i].score1 == games[i].score2) {
417                         continue;
418                 }
419                 ++teams[idx1].nplayed;
420                 ++teams[idx2].nplayed;
421                 teams[idx1].goals += games[i].score1;
422                 teams[idx2].goals += games[i].score2;
423                 teams[idx1].gd += games[i].score1;
424                 teams[idx2].gd += games[i].score2;
425                 teams[idx1].gd -= games[i].score2;
426                 teams[idx2].gd -= games[i].score1;
427                 if (games[i].score1 > games[i].score2) {
428                         teams[idx1].pts += 2;
429                 } else {
430                         teams[idx2].pts += 2;
431                 }
432         }
433 }
434
435 // So that we can just have one team list, and let membership be defined by games.
436 function filter_teams(teams, response)
437 {
438         let teams_to_idx = make_teams_to_idx(teams);
439         let games = parse_games_from_spreadsheet(response, 'irrelevant group name', true);
440         for (let i = 0; i < games.length; ++i) {
441                 let idx1 = teams_to_idx[games[i].name1];
442                 let idx2 = teams_to_idx[games[i].name2];
443                 if (idx1 !== undefined) {
444                         ++teams[idx1].ngames;  // FIXME: shouldn't nplayed be just as good?
445                 }
446                 if (idx2 !== undefined) {
447                         ++teams[idx2].ngames;
448                 }
449         }
450         return teams.filter(function(team) { return team.ngames > 0; });
451 }
452
453 function display_group_parsed(teams, games, group_name)
454 {
455         document.getElementById('entire-bug').style.display = 'none';
456
457         apply_games_to_teams(games, teams);
458         let tiebreakers = [];
459         teams = rank(games, teams, 1, tiebreakers);
460
461         let carousel = document.getElementById('carousel');
462         clear_carousel(carousel);
463
464         addheading(carousel, 5, "Current standings, " + ultimateconfig['tournament_title'] + "<br />" + group_name);
465         let tr = document.createElement("tr");
466         tr.className = "subfooter";
467         addth(tr, "rank", "");
468         addth(tr, "team", "");
469         addth(tr, "nplayed", "P");
470         addth(tr, "gd", "GD");
471         addth(tr, "pts", "Pts");
472         carousel.appendChild(tr);
473
474         let row_num = 2;
475         for (let i = 0; i < teams.length; ++i) {
476                 let tr = document.createElement("tr");
477
478                 addth(tr, "rank", teams[i].rank);
479                 addtd(tr, "team", teams[i].name);
480                 addtd(tr, "nplayed", teams[i].nplayed);
481                 addtd(tr, "gd", teams[i].gd.toString().replace(/-/, '−'));
482                 addtd(tr, "pts", teams[i].pts);
483
484                 carousel.appendChild(tr);
485         }
486
487         if (tiebreakers.length > 0) {
488                 let tie_tr = document.createElement("tr");
489                 tie_tr.className = "footer";
490                 let td = document.createElement("td");
491                 td.appendChild(document.createTextNode("Tiebreaks applied: " + tiebreakers.join(', ')));
492                 td.setAttribute("colspan", "5");
493                 tie_tr.appendChild(td);
494                 carousel.appendChild(tie_tr);
495         }
496
497         let footer_tr = document.createElement("tr");
498         footer_tr.className = "footer";
499         let td = document.createElement("td");
500         td.appendChild(document.createTextNode(ultimateconfig['tournament_footer']));
501         td.setAttribute("colspan", "5");
502         footer_tr.appendChild(td);
503         carousel.appendChild(footer_tr);
504
505         fade_in_rows(carousel);
506
507         carousel.style.display = 'table';
508 };
509
510 function fade_in_rows(table)
511 {
512         let trs = table.getElementsByTagName("tr");
513         for (let i = 1; i < trs.length; ++i) {  // The header already has its own fade-in.
514                 if (trs[i].className === "footer") {
515                         trs[i].style = "-webkit-animation: fade-in 1.0s ease; -webkit-animation-delay: " + (0.25 * i) + "s; -webkit-animation-fill-mode: both;";
516                 } else {
517                         trs[i].style = "-webkit-animation: fade-in 2.0s ease; -webkit-animation-delay: " + (0.25 * i) + "s; -webkit-animation-fill-mode: both;";
518                 }
519         }
520 };
521
522 function fade_out_rows(table)
523 {
524         let trs = table.getElementsByTagName("tr");
525         for (let i = 0; i < trs.length; ++i) {
526                 if (trs[i].className === "footer") {
527                         trs[i].style = "-webkit-animation: fade-out 1.0s ease; -webkit-animation-delay: " + (0.125 * i) + "s; -webkit-animation-fill-mode: both;";
528                 } else {
529                         trs[i].style = "-webkit-animation: fade-out 1.0s ease; -webkit-animation-delay: " + (0.125 * i) + "s; -webkit-animation-fill-mode: both;";
530                 }
531         }
532 };
533
534 function clear_carousel(table)
535 {
536         while (table.childNodes.length > 0) {
537                 table.removeChild(table.firstChild);
538         }
539 };
540
541 // Stream schedule
542 let max_list_len = 7;
543
544 function display_stream_schedule(response, group_name) {
545         let teams = parse_teams_from_spreadsheet(response);
546         let games = parse_games_from_spreadsheet(response, group_name, true);
547         display_stream_schedule_parsed(teams, games, 0);
548 };
549
550 function sort_game_list(games) {
551         games = games.filter(function(game) { return game.streamtime !== undefined && game.streamtime.match(/[0-9]+:[0-9]+/) != null; });
552         games.sort(function(a, b) {
553                 if (a.streamday !== b.streamday) {
554                         return a.streamday - b.streamday;
555                 }
556
557                 let m1 = a.streamtime.match(/([0-9]+):([0-9]+)/);
558                 let m2 = b.streamtime.match(/([0-9]+):([0-9]+)/);
559                 return (m1[1] * 60 + m1[2]) - (m2[1] * 60 + m2[2]);
560         });
561         return games;
562 }
563
564 function find_game_start_idx(games) {
565         // Pick out a reasonable place to start the list. We'll show the last
566         // completed match and start from there.
567         let start_idx = games.length - 1;
568         for (let i = 0; i < games.length; ++i) {
569                 if (isNaN(games[i].score1) || isNaN(games[i].score2) &&
570                     games[i].score1 === games[i].score2) {
571                         start_idx = i;
572                         break;
573                 }
574         }
575         if (start_idx > 0) start_idx--;
576         if (games.length >= max_list_len) {
577                 start_idx = Math.min(start_idx, games.length - max_list_len);
578         }
579         return start_idx;
580 }
581
582 function find_num_pages(games) {
583         games = sort_game_list(games);
584         let start_idx = find_game_start_idx(games);
585         return Math.ceil((games.length - start_idx) / max_list_len);
586 }
587
588 function display_stream_schedule_parsed(teams, games, page) {
589         document.getElementById('entire-bug').style.display = 'none';
590
591         games = sort_game_list(games);
592         let start_idx = find_game_start_idx(games);
593
594         start_idx += page * max_list_len;
595         if (start_idx >= games.length) {
596                 // Error.
597                 return;
598         }
599
600         let days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"];
601         let shortdays = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"];
602         let today = days[(new Date).getDay()];
603
604         let covered_days = [];
605         let row_num = 0;
606         for (let i = start_idx; i < games.length && row_num++ < max_list_len; ++i) {
607                 if (i == start_idx || games[i].streamday != games[i - 1].streamday) {
608                         covered_days.push(days[games[i].streamday]);
609                 }
610         }
611         
612         let carousel = document.getElementById('carousel');
613         clear_carousel(carousel);
614         addheading(carousel, 3, "Stream schedule, " + ultimateconfig['tournament_title'] + "<br />" + covered_days.join('/') + " (all times CET)");
615
616         let teams_to_idx = make_teams_to_idx(teams);
617         row_num = 0;
618         for (let i = start_idx; i < games.length && row_num < max_list_len; ++i) {
619                 let tr = document.createElement("tr");
620
621                 let name1 = teams[teams_to_idx[games[i].name1]].mediumname;
622                 let name2 = teams[teams_to_idx[games[i].name2]].mediumname;
623
624                 addtd(tr, "matchup", name1 + "–" + name2);
625                 addtd(tr, "group", games[i].group_name);
626
627                 if (!isNaN(games[i].score1) && !isNaN(games[i].score2) &&
628                     games[i].score1 !== games[i].score2) {
629                         addtd(tr, "streamtime", games[i].score1 + "–" + games[i].score2);
630                 } else {
631                         let streamtime = games[i].streamtime;
632                         let streamday = days[games[i].streamday];
633                         if (streamday !== today) {
634                                 streamtime = shortdays[games[i].streamday] + " " + streamtime;
635                         }
636                         addth(tr, "streamtime", streamtime);
637                 }
638
639                 row_num++;
640                 carousel.appendChild(tr);
641         }
642
643         fade_in_rows(carousel);
644
645         carousel.style.display = 'table';
646 };
647
648 function get_group(group_name, cb)
649 {
650         let req = new XMLHttpRequest();
651         req.onload = function(e) {
652                 cb(JSON.parse(req.responseText), group_name);
653         };
654         req.open('GET', 'https://sheets.googleapis.com/v4/spreadsheets/' + ultimateconfig['score_sheet_id'] + '/values/\'' + group_name + '\'!A1:J50?key=' + ultimateconfig['api_key']);
655         req.send();
656 }
657
658 function showgroup(group_name)
659 {
660         get_group(group_name, function(response, group_name) {
661                 let teams = parse_teams_from_spreadsheet(response);
662                 let games = parse_games_from_spreadsheet(response, group_name, false);
663                 teams = filter_teams(teams, response);
664                 display_group_parsed(teams, games, group_name);
665                 publish_group_rank(response, group_name);  // Update the spreadsheet in the background.
666         });
667 }
668
669
670 function showgroup_from_state()
671 {
672         showgroup(state['group_name']);
673 }
674
675 let carousel_timeout = null;
676
677 function hidetable()
678 {
679         fade_out_rows(document.getElementById('carousel'));
680 };
681
682 function showschedule(page)
683 {
684         let teams = [];
685         let games = [];
686         let num_left = 5;
687
688         let cb = function(response, group_name) {
689                 teams = teams.concat(parse_teams_from_spreadsheet(response));
690                 games = games.concat(parse_games_from_spreadsheet(response, group_name, true));
691                 if (--num_left == 0) {
692                         display_stream_schedule_parsed(teams, games, 0);
693                 }
694         };
695
696         get_group('Group A', cb);
697         get_group('Group B', cb);
698         get_group('Group C', cb);
699         get_group('Playoffs', cb);
700         get_group('Playoffs 9th-13th', cb);
701 };
702
703 function do_series(series)
704 {
705         do_series_internal(series, 0);
706 };
707
708 function do_series_internal(series, idx)
709 {
710         (series[idx][1])();
711         if (idx + 1 < series.length) {
712                 carousel_timeout = setTimeout(function() { do_series_internal(series, idx + 1); }, series[idx][0]);
713         }
714 };
715
716 function showcarousel()
717 {
718         let teams_per_group = [];
719         let games_per_group = [];
720         let combined_teams = [];
721         let combined_games = [];
722         let num_left = 5;
723
724         let cb = function(response, group_name) {
725                 let teams = parse_teams_from_spreadsheet(response);
726                 let games = parse_games_from_spreadsheet(response, group_name, true);
727                 teams = filter_teams(teams, response);
728                 teams_per_group[group_name] = teams;
729                 games_per_group[group_name] = games;
730
731                 combined_teams = combined_teams.concat(teams);
732                 combined_games = combined_games.concat(games);
733                 if (--num_left == 0) {
734                         let series = [
735                                 [ 13000, function() { display_group_parsed(teams_per_group['Group A'], games_per_group['Group A'], 'Group A'); } ],
736                                 [ 2000, function() { hidetable(); } ],
737                                 [ 13000, function() { display_group_parsed(teams_per_group['Group B'], games_per_group['Group B'], 'Group B'); } ],
738                                 [ 2000, function() { hidetable(); } ],
739                                 [ 13000, function() { display_group_parsed(teams_per_group['Group C'], games_per_group['Group C'], 'Group C'); } ],
740                                 [ 2000, function() { hidetable(); } ],
741                                 [ 13000, function() { display_group_parsed(teams_per_group['Playoffs 9th-13th'], games_per_group['Playoffs 9th-13th'], 'Playoffs 9th–13th'); } ],
742                                 [ 2000, function() { hidetable(); } ]
743                         ];
744                         let num_pages = find_num_pages(combined_games);
745                         for (let page = 0; page < num_pages; ++page) {
746                                 series.push([ 13000, function() { display_stream_schedule_parsed(combined_teams, combined_games, page); } ]);
747                                 series.push([ 2000, function() { hidetable(); } ]);
748                         }
749
750                         do_series(series);
751                 }
752         };
753
754         get_group('Group A', cb);
755         get_group('Group B', cb);
756         get_group('Group C', cb);
757         get_group('Playoffs 9th-13th', cb);
758         get_group('Playoffs', cb);
759 };
760
761 function stopcarousel()
762 {
763         if (carousel_timeout !== null) {
764                 hidetable();
765                 clearTimeout(carousel_timeout);
766                 carousel_timeout = null;
767         }
768 };
769
770 function hidescorebug()
771 {
772         document.getElementById('entire-bug').style.display = 'none';
773 }
774
775 function showscorebug()
776 {
777         document.getElementById('entire-bug').style.display = null;
778 }
779
780 function showmatch2()
781 {
782         let css = "-webkit-animation: fade-in 1.0s ease; -webkit-animation-fill-mode: both;";
783         document.getElementById('scorebug2').style = css;
784         document.getElementById('clockbug2').style = css;
785 }
786
787 function hidematch2()
788 {
789         let css = "-webkit-animation: fade-out 1.0s ease; -webkit-animation-fill-mode: both;";
790         document.getElementById('scorebug2').style = css;
791         document.getElementById('clockbug2').style = css;
792 }
793
794 function showmatch3()
795 {
796         let css = "-webkit-animation: fade-in 1.0s ease; -webkit-animation-fill-mode: both;";
797         document.getElementById('scorebug3').style = css;
798         document.getElementById('clockbug3').style = css;
799 }
800
801 function hidematch3()
802 {
803         let css = "-webkit-animation: fade-out 1.0s ease; -webkit-animation-fill-mode: both;";
804         document.getElementById('scorebug3').style = css;
805         document.getElementById('clockbug3').style = css;
806 }