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