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