3 function addheading(carousel, colspan, content)
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);
11 thead.appendChild(tr);
12 carousel.appendChild(thead);
14 function addtd(tr, className, content) {
15 let td = document.createElement("td");
16 td.appendChild(document.createTextNode(content));
17 td.className = className;
20 function addth(tr, className, content) {
21 let th = document.createElement("th");
22 th.appendChild(document.createTextNode(content));
23 th.className = className;
27 function subrank_partitions(games, parts, start_rank, tiebreakers, func) {
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) {
34 start_rank += part.length;
39 function partition(teams, compare)
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);
50 curr_part.push(teams[i]);
52 if (curr_part.length != 0) {
53 parts.push(curr_part);
58 function explain_tiebreaker(parts, rule_name)
61 for (let i = 0; i < parts.length; ++i) {
62 result.push(parts[i].map(function(x) { return x.shortname; }).join("/"));
64 return result.join(" > ") + " (" + rule_name + ")";
67 function make_teams_to_idx(teams)
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;
78 function partition_by_beat(teams, fill_beatmatrix)
80 // Head-to-head score by way of components. First construct the beat matrix.
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++) {
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]) {
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) {
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]);
124 console.log("this shouldn't happen");
128 if (better_than_pivot.length > 0) {
129 result = partition_by_beat(better_than_pivot, fill_beatmatrix);
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));
139 // No usable pivot was found, so the graph is inherently
140 // disconnected, and we cannot partition it.
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;
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);
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;
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);
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);
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++) {
194 teams[i].h2h_goals = 0;
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;
206 teams[idx1].h2h_goals += games[i].score1;
207 teams[idx2].h2h_goals += games[i].score2;
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);
217 // Rule #4: Goal difference against common opponents.
219 for (let i = 0; i < games.length; ++i) {
220 if (results[games[i].name1] === undefined) {
221 results[games[i].name1] = {};
223 if (results[games[i].name2] === undefined) {
224 results[games[i].name2] = {};
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 ];
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;
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];
248 } else if (gd_i < gd_j) {
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);
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);
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;
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];
286 if (goals_i > goals_j) {
288 } else if (goals_i < goals_j) {
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);
299 // OK, it's a tie. Give them all the same rank.
301 for (let i = 0; i < teams.length; ++i) {
302 result.push(teams[i]);
303 result[i].rank = start_rank;
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;
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);
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);
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);
337 // OK, it's a tie. Give them all the same rank.
339 for (let i = 0; i < teams.length; ++i) {
340 result.push(teams[i]);
341 result[i].rank = start_rank;
346 function parse_teams_from_spreadsheet(response) {
348 for (let i = 2; response.values[i].length >= 1; ++i) {
350 "name": response.values[i][0],
351 "mediumname": response.values[i][1],
352 "shortname": response.values[i][2],
353 "tags": response.values[i][3],
363 function parse_games_from_spreadsheet(response, group_name, include_unplayed) {
366 for (i = 0; i < response.values.length; ++i) {
367 if (response.values[i][0] === 'Results') {
373 for ( ; response.values[i] !== undefined && response.values[i].length >= 1; ++i) {
374 if ((response.values[i][2] && response.values[i][3]) || include_unplayed) {
375 let real_group_name = response.values[i][9];
376 if (real_group_name === undefined) {
377 real_group_name = group_name;
380 "name1": response.values[i][0],
381 "name2": response.values[i][1],
382 "score1": parseInt(response.values[i][2]),
383 "score2": parseInt(response.values[i][3]),
384 "streamday": response.values[i][7],
385 "streamtime": response.values[i][8],
386 "group_name": real_group_name
393 function apply_games_to_teams(games, teams)
395 let teams_to_idx = make_teams_to_idx(teams);
396 for (let i = 0; i < games.length; ++i) {
397 let idx1 = teams_to_idx[games[i].name1];
398 let idx2 = teams_to_idx[games[i].name2];
399 if (games[i].score1 === undefined || games[i].score2 === undefined ||
400 isNaN(games[i].score1) || isNaN(games[i].score2) ||
401 idx1 === undefined || idx2 === undefined ||
402 games[i].score1 == games[i].score2) {
405 ++teams[idx1].nplayed;
406 ++teams[idx2].nplayed;
407 teams[idx1].goals += games[i].score1;
408 teams[idx2].goals += games[i].score2;
409 teams[idx1].gd += games[i].score1;
410 teams[idx2].gd += games[i].score2;
411 teams[idx1].gd -= games[i].score2;
412 teams[idx2].gd -= games[i].score1;
413 if (games[i].score1 > games[i].score2) {
414 teams[idx1].pts += 2;
416 teams[idx2].pts += 2;
421 function display_group_parsed(teams, games, group_name)
423 document.getElementById('entire-bug').style.display = 'none';
425 apply_games_to_teams(games, teams);
426 let tiebreakers = [];
427 teams = rank(games, teams, 1, tiebreakers);
429 let carousel = document.getElementById('carousel');
430 clear_carousel(carousel);
432 addheading(carousel, 5, "Current standings, " + ultimateconfig['tournament_title'] + "<br />" + group_name);
433 let tr = document.createElement("tr");
434 tr.className = "subfooter";
435 addth(tr, "rank", "");
436 addth(tr, "team", "");
437 addth(tr, "nplayed", "P");
438 addth(tr, "gd", "GD");
439 addth(tr, "pts", "Pts");
440 carousel.appendChild(tr);
443 for (let i = 0; i < teams.length; ++i) {
444 let tr = document.createElement("tr");
446 addth(tr, "rank", teams[i].rank);
447 addtd(tr, "team", teams[i].name);
448 addtd(tr, "nplayed", teams[i].nplayed);
449 addtd(tr, "gd", teams[i].gd.toString().replace(/-/, '−'));
450 addtd(tr, "pts", teams[i].pts);
452 carousel.appendChild(tr);
455 if (tiebreakers.length > 0) {
456 let tie_tr = document.createElement("tr");
457 tie_tr.className = "footer";
458 let td = document.createElement("td");
459 td.appendChild(document.createTextNode("Tiebreaks applied: " + tiebreakers.join(', ')));
460 td.setAttribute("colspan", "5");
461 tie_tr.appendChild(td);
462 carousel.appendChild(tie_tr);
465 let footer_tr = document.createElement("tr");
466 footer_tr.className = "footer";
467 let td = document.createElement("td");
468 td.appendChild(document.createTextNode(ultimateconfig['tournament_footer']));
469 td.setAttribute("colspan", "5");
470 footer_tr.appendChild(td);
471 carousel.appendChild(footer_tr);
473 fade_in_rows(carousel);
475 carousel.style.display = 'table';
478 function fade_in_rows(table)
480 let trs = table.getElementsByTagName("tr");
481 for (let i = 1; i < trs.length; ++i) { // The header already has its own fade-in.
482 if (trs[i].className === "footer") {
483 trs[i].style = "-webkit-animation: fade-in 1.0s ease; -webkit-animation-delay: " + (0.25 * i) + "s; -webkit-animation-fill-mode: both;";
485 trs[i].style = "-webkit-animation: fade-in 2.0s ease; -webkit-animation-delay: " + (0.25 * i) + "s; -webkit-animation-fill-mode: both;";
490 function fade_out_rows(table)
492 let trs = table.getElementsByTagName("tr");
493 for (let i = 0; i < trs.length; ++i) {
494 if (trs[i].className === "footer") {
495 trs[i].style = "-webkit-animation: fade-out 1.0s ease; -webkit-animation-delay: " + (0.125 * i) + "s; -webkit-animation-fill-mode: both;";
497 trs[i].style = "-webkit-animation: fade-out 1.0s ease; -webkit-animation-delay: " + (0.125 * i) + "s; -webkit-animation-fill-mode: both;";
502 function clear_carousel(table)
504 while (table.childNodes.length > 0) {
505 table.removeChild(table.firstChild);
510 let max_list_len = 7;
512 function display_stream_schedule(response, group_name) {
513 let teams = parse_teams_from_spreadsheet(response);
514 let games = parse_games_from_spreadsheet(response, group_name, true);
515 display_stream_schedule_parsed(teams, games, 0);
518 function sort_game_list(games) {
519 games = games.filter(function(game) { return game.streamtime !== undefined && game.streamtime.match(/[0-9]+:[0-9]+/) != null; });
520 games.sort(function(a, b) {
521 if (a.streamday !== b.streamday) {
522 return a.streamday - b.streamday;
525 let m1 = a.streamtime.match(/([0-9]+):([0-9]+)/);
526 let m2 = b.streamtime.match(/([0-9]+):([0-9]+)/);
527 return (m1[1] * 60 + m1[2]) - (m2[1] * 60 + m2[2]);
532 function find_game_start_idx(games) {
533 // Pick out a reasonable place to start the list. We'll show the last
534 // completed match and start from there.
535 let start_idx = games.length - 1;
536 for (let i = 0; i < games.length; ++i) {
537 if (isNaN(games[i].score1) || isNaN(games[i].score2) &&
538 games[i].score1 === games[i].score2) {
543 if (start_idx > 0) start_idx--;
544 if (games.length >= max_list_len) {
545 start_idx = Math.min(start_idx, games.length - max_list_len);
550 function find_num_pages(games) {
551 games = sort_game_list(games);
552 let start_idx = find_game_start_idx(games);
553 return Math.ceil((games.length - start_idx) / max_list_len);
556 function display_stream_schedule_parsed(teams, games, page) {
557 document.getElementById('entire-bug').style.display = 'none';
559 games = sort_game_list(games);
560 let start_idx = find_game_start_idx(games);
562 start_idx += page * max_list_len;
563 if (start_idx >= games.length) {
568 let days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"];
569 let shortdays = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"];
570 let today = days[(new Date).getDay()];
572 let covered_days = [];
574 for (let i = start_idx; i < games.length && row_num++ < max_list_len; ++i) {
575 if (i == start_idx || games[i].streamday != games[i - 1].streamday) {
576 covered_days.push(days[games[i].streamday]);
580 let carousel = document.getElementById('carousel');
581 clear_carousel(carousel);
582 addheading(carousel, 3, "Stream schedule, " + ultimateconfig['tournament_title'] + "<br />" + covered_days.join('/') + " (all times CET)");
584 let teams_to_idx = make_teams_to_idx(teams);
586 for (let i = start_idx; i < games.length && row_num < max_list_len; ++i) {
587 let tr = document.createElement("tr");
589 let name1 = teams[teams_to_idx[games[i].name1]].mediumname;
590 let name2 = teams[teams_to_idx[games[i].name2]].mediumname;
592 addtd(tr, "matchup", name1 + "–" + name2);
593 addtd(tr, "group", games[i].group_name);
595 if (!isNaN(games[i].score1) && !isNaN(games[i].score2) &&
596 games[i].score1 !== games[i].score2) {
597 addtd(tr, "streamtime", games[i].score1 + "–" + games[i].score2);
599 let streamtime = games[i].streamtime;
600 let streamday = days[games[i].streamday];
601 if (streamday !== today) {
602 streamtime = shortdays[games[i].streamday] + " " + streamtime;
604 addth(tr, "streamtime", streamtime);
608 carousel.appendChild(tr);
611 fade_in_rows(carousel);
613 carousel.style.display = 'table';
616 function get_group(group_name, cb)
618 let req = new XMLHttpRequest();
619 req.onload = function(e) {
620 cb(JSON.parse(req.responseText), group_name);
622 req.open('GET', 'https://sheets.googleapis.com/v4/spreadsheets/' + ultimateconfig['score_sheet_id'] + '/values/\'' + group_name + '\'!A1:J50?key=' + ultimateconfig['api_key']);
626 function showgroup(group_name)
628 get_group(group_name, function(response, group_name) {
629 let teams = parse_teams_from_spreadsheet(response);
630 let games = parse_games_from_spreadsheet(response, group_name, false);
631 display_group_parsed(teams, games, group_name);
633 publish_group_rank(group_name); // Update the spreadsheet in the background.
637 function showgroup_from_state()
639 showgroup(state['group_name']);
642 let carousel_timeout = null;
646 fade_out_rows(document.getElementById('carousel'));
649 function showschedule(page)
655 let cb = function(response, group_name) {
656 teams = teams.concat(parse_teams_from_spreadsheet(response));
657 games = games.concat(parse_games_from_spreadsheet(response, group_name, true));
658 if (--num_left == 0) {
659 display_stream_schedule_parsed(teams, games, 0);
663 get_group('Group A', cb);
664 get_group('Group B', cb);
665 get_group('Group C', cb);
666 get_group('Playoffs', cb);
669 function do_series(series)
671 do_series_internal(series, 0);
674 function do_series_internal(series, idx)
677 if (idx + 1 < series.length) {
678 carousel_timeout = setTimeout(function() { do_series_internal(series, idx + 1); }, series[idx][0]);
682 function showcarousel()
684 let teams_per_group = [];
685 let games_per_group = [];
686 let combined_teams = [];
687 let combined_games = [];
690 let cb = function(response, group_name) {
691 let teams = parse_teams_from_spreadsheet(response);
692 let games = parse_games_from_spreadsheet(response, group_name, true);
693 teams_per_group[group_name] = teams;
694 games_per_group[group_name] = games;
696 combined_teams = combined_teams.concat(teams);
697 combined_games = combined_games.concat(games);
698 if (--num_left == 0) {
700 [ 13000, function() { display_group_parsed(teams_per_group['Group A'], games_per_group['Group A'], 'Group A'); } ],
701 [ 2000, function() { hidetable(); } ],
702 [ 13000, function() { display_group_parsed(teams_per_group['Group B'], games_per_group['Group B'], 'Group B'); } ],
703 [ 2000, function() { hidetable(); } ],
704 [ 13000, function() { display_group_parsed(teams_per_group['Group C'], games_per_group['Group C'], 'Group C'); } ],
705 [ 2000, function() { hidetable(); } ]
707 let num_pages = find_num_pages(combined_games);
708 for (let page = 0; page < num_pages; ++page) {
709 series.push([ 13000, function() { display_stream_schedule_parsed(combined_teams, combined_games, page); } ]);
710 series.push([ 2000, function() { hidetable(); } ]);
717 get_group('Group A', cb);
718 get_group('Group B', cb);
719 get_group('Group C', cb);
720 get_group('Playoffs', cb);
723 function stopcarousel()
725 if (carousel_timeout !== null) {
727 clearTimeout(carousel_timeout);
728 carousel_timeout = null;
732 function hidescorebug()
734 document.getElementById('entire-bug').style.display = 'none';
737 function showscorebug()
739 document.getElementById('entire-bug').style.display = null;
742 function showmatch2()
744 let css = "-webkit-animation: fade-in 1.0s ease; -webkit-animation-fill-mode: both;";
745 document.getElementById('scorebug2').style = css;
746 document.getElementById('clockbug2').style = css;
749 function hidematch2()
751 let css = "-webkit-animation: fade-out 1.0s ease; -webkit-animation-fill-mode: both;";
752 document.getElementById('scorebug2').style = css;
753 document.getElementById('clockbug2').style = css;
756 function showmatch3()
758 let css = "-webkit-animation: fade-in 1.0s ease; -webkit-animation-fill-mode: both;";
759 document.getElementById('scorebug3').style = css;
760 document.getElementById('clockbug3').style = css;
763 function hidematch3()
765 let css = "-webkit-animation: fade-out 1.0s ease; -webkit-animation-fill-mode: both;";
766 document.getElementById('scorebug3').style = css;
767 document.getElementById('clockbug3').style = css;