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) {
29 for (let i = 0; i < parts.length; ++i) {
30 let part = rank(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;
76 function partition_by_beat(teams, fill_beatmatrix)
78 // Head-to-head score by way of components. First construct the beat matrix.
80 let beat = new Array(n);
81 let teams_to_idx = make_teams_to_idx(teams);
82 for (let i = 0; i < n; i++) {
83 beat[i] = new Array(n);
84 for (let j = 0; j < n; j++) {
88 fill_beatmatrix(beat, teams_to_idx);
89 // Floyd-Warshall for transitive closure.
90 for (let k = 0; k < n; ++k) {
91 for (let i = 0; i < n; ++i) {
92 for (let j = 0; j < n; ++j) {
93 if (beat[i][k] && beat[k][j]) {
100 // See if we can find any team that is comparable to all others.
101 for (let pivot_idx = 0; pivot_idx < n; pivot_idx++) {
102 let incomparable = false;
103 for (let i = 0; i < n; ++i) {
104 if (i != pivot_idx && beat[pivot_idx][i] == 0 && beat[i][pivot_idx] == 0) {
110 // Split the teams into three partitions:
111 let better_than_pivot = [], equal = [], worse_than_pivot = [];
112 for (let i = 0; i < n; ++i) {
113 let we_beat = (beat[pivot_idx][i] == 1);
114 let they_beat = (beat[i][pivot_idx] == 1);
115 if ((i == pivot_idx) || (we_beat && they_beat)) {
116 equal.push(teams[i]);
117 } else if (we_beat && !they_beat) {
118 worse_than_pivot.push(teams[i]);
119 } else if (they_beat && !we_beat) {
120 better_than_pivot.push(teams[i]);
122 console.log("this shouldn't happen");
126 if (better_than_pivot.length > 0) {
127 result = partition_by_beat(better_than_pivot, fill_beatmatrix);
129 result.push(equal); // Obviously can't be partitioned further.
130 if (worse_than_pivot.length > 0) {
131 result = result.concat(partition_by_beat(worse_than_pivot, fill_beatmatrix));
137 // No usable pivot was found, so the graph is inherently
138 // disconnected, and we cannot partition it.
142 // Takes in an array, gives every element a rank starting with 1, and returns.
143 function rank(games, teams, start_rank, tiebreakers) {
144 if (teams.length <= 1) {
145 // Only one team, so trivial.
146 teams[0].rank = start_rank;
150 // Rule #0: Partition the teams by score.
151 let score_parts = partition(teams, function(a, b) { return b.pts - a.pts });
152 if (score_parts.length > 1) {
153 return subrank_partitions(games, score_parts, start_rank, tiebreakers);
156 // Rule #1: Head-to-head wins.
157 let num_relevant_games = 0;
158 let beat_parts = partition_by_beat(teams, function(beat, teams_to_idx) {
159 for (let i = 0; i < games.length; ++i) {
160 let idx1 = teams_to_idx[games[i].name1];
161 let idx2 = teams_to_idx[games[i].name2];
162 if (idx1 !== undefined && idx2 !== undefined) {
163 if (games[i].score1 > games[i].score2) {
164 beat[idx1][idx2] = 1;
165 ++num_relevant_games;
166 } else if (games[i].score1 < games[i].score2) {
167 beat[idx2][idx1] = 1;
168 ++num_relevant_games;
173 if (beat_parts.length > 1) {
174 tiebreakers.push(explain_tiebreaker(beat_parts, 'head-to-head'));
175 return subrank_partitions(games, beat_parts, start_rank, tiebreakers);
178 // Rule #2: Number of games played (fewer is better).
179 // Actually the rule says “fewest losses”, but fewer games is equivalent
180 // as long as teams have the same amount of points and ties don't exist.
181 let nplayed_parts = partition(teams, function(a, b) { return a.nplayed - b.nplayed });
182 if (nplayed_parts.length > 1) {
183 tiebreakers.push(explain_tiebreaker(nplayed_parts, 'fewer losses'));
184 return subrank_partitions(games, nplayed_parts, start_rank, tiebreakers);
187 // Rule #3: Head-to-head goal difference (if all have played).
188 let teams_to_idx = make_teams_to_idx(teams);
189 if (num_relevant_games >= teams.length * (teams.length - 1) / 2) {
190 for (let i = 0; i < teams.length; i++) {
192 teams[i].h2h_goals = 0;
194 for (let i = 0; i < games.length; ++i) {
195 let idx1 = teams_to_idx[games[i].name1];
196 let idx2 = teams_to_idx[games[i].name2];
197 if (idx1 !== undefined && idx2 !== undefined &&
198 !isNaN(games[i].score1) && !isNaN(games[i].score2)) {
199 teams[idx1].h2h_gd += games[i].score1;
200 teams[idx1].h2h_gd -= games[i].score2;
201 teams[idx2].h2h_gd += games[i].score2;
202 teams[idx2].h2h_gd -= games[i].score1;
204 teams[idx1].h2h_goals += games[i].score1;
205 teams[idx2].h2h_goals += games[i].score2;
208 let h2h_gd_parts = partition(teams, function(a, b) { return b.h2h_gd - a.h2h_gd });
209 if (h2h_gd_parts.length > 1) {
210 tiebreakers.push(explain_tiebreaker(h2h_gd_parts, 'head-to-head goal difference'));
211 return subrank_partitions(games, h2h_gd_parts, start_rank, tiebreakers);
215 // Rule #4: Goal difference against common opponents.
217 for (let i = 0; i < games.length; ++i) {
218 if (results[games[i].name1] === undefined) {
219 results[games[i].name1] = {};
221 if (results[games[i].name2] === undefined) {
222 results[games[i].name2] = {};
224 results[games[i].name1][games[i].name2] = [ games[i].score1, games[i].score2 ];
225 results[games[i].name2][games[i].name1] = [ games[i].score2, games[i].score1 ];
227 let gd_parts = partition_by_beat(teams, function(beat, teams_to_idx) {
228 for (const team_i of Object.keys(teams_to_idx)) {
229 let i = teams_to_idx[team_i];
230 for (const team_j of Object.keys(teams_to_idx)) {
231 let j = teams_to_idx[team_j];
232 let results_i = results[team_i], results_j = results[team_j];
233 let gd_i = 0, gd_j = 0;
235 // See if the two teams have both played a third team k.
236 for (let k in results_i) {
237 if (!results_i.hasOwnProperty(k)) continue;
238 if (results_j[k] !== undefined) {
239 gd_i += results_i[k][0] - results_i[k][1];
240 gd_j += results_j[k][0] - results_j[k][1];
246 } else if (gd_i < gd_j) {
252 if (gd_parts.length > 1) {
253 tiebreakers.push(explain_tiebreaker(gd_parts, 'goal difference versus common opponents'));
254 return subrank_partitions(games, gd_parts, start_rank, tiebreakers);
257 // Rule #5: Head-to-head scored goals (if all have played).
258 if (num_relevant_games >= teams.length * (teams.length - 1) / 2) {
259 let h2h_goals_parts = partition(teams, function(a, b) { return b.h2h_goals - a.h2h_goals });
260 if (h2h_goals_parts.length > 1) {
261 tiebreakers.push(explain_tiebreaker(h2h_goals_parts, 'head-to-head scored goals'));
262 return subrank_partitions(games, h2h_goals_parts, start_rank, tiebreakers);
266 // Rule #6: Goals scored against common opponents.
267 let goals_parts = partition_by_beat(teams, function(beat, teams_to_idx) {
268 for (const team_i of Object.keys(teams_to_idx)) {
269 let i = teams_to_idx[team_i];
270 for (const team_j of Object.keys(teams_to_idx)) {
271 let j = teams_to_idx[team_j];
272 let results_i = results[team_i], results_j = results[team_j];
273 let goals_i = 0, goals_j = 0;
275 // See if the two teams have both played a third team k.
276 for (let k in results_i) {
277 if (!results_i.hasOwnProperty(k)) continue;
278 if (results_j[k] !== undefined) {
279 goals_i += results_i[k][0];
280 goals_j += results_j[k][0];
284 if (goals_i > goals_j) {
286 } else if (goals_i < goals_j) {
292 if (goals_parts.length > 1) {
293 tiebreakers.push(explain_tiebreaker(goals_parts, 'goals scored against common opponents'));
294 return subrank_partitions(games, goals_parts, start_rank, tiebreakers);
297 // OK, it's a tie. Give them all the same rank.
299 for (let i = 0; i < teams.length; ++i) {
300 result.push(teams[i]);
301 result[i].rank = start_rank;
306 function parse_teams_from_spreadsheet(response) {
308 for (let i = 2; response.values[i].length >= 1; ++i) {
310 "name": response.values[i][0],
311 "mediumname": response.values[i][1],
312 "shortname": response.values[i][2],
313 "tags": response.values[i][3],
323 function parse_games_from_spreadsheet(response, group_name, include_unplayed) {
326 for (i = 0; i < response.values.length; ++i) {
327 if (response.values[i][0] === 'Results') {
333 for ( ; response.values[i] !== undefined && response.values[i].length >= 1; ++i) {
334 if ((response.values[i][2] && response.values[i][3]) || include_unplayed) {
335 let real_group_name = response.values[i][9];
336 if (real_group_name === undefined) {
337 real_group_name = group_name;
340 "name1": response.values[i][0],
341 "name2": response.values[i][1],
342 "score1": parseInt(response.values[i][2]),
343 "score2": parseInt(response.values[i][3]),
344 "streamday": response.values[i][7],
345 "streamtime": response.values[i][8],
346 "group_name": real_group_name
353 function display_group(response, group_name)
355 let teams = parse_teams_from_spreadsheet(response);
356 let games = parse_games_from_spreadsheet(response, group_name, false);
357 display_group_parsed(teams, games, group_name);
360 function display_group_parsed(teams, games, group_name)
362 document.getElementById('entire-bug').style.display = 'none';
364 let teams_to_idx = make_teams_to_idx(teams);
365 for (let i = 0; i < games.length; ++i) {
366 let idx1 = teams_to_idx[games[i].name1];
367 let idx2 = teams_to_idx[games[i].name2];
368 if (games[i].score1 === undefined || games[i].score2 === undefined ||
369 isNaN(games[i].score1) || isNaN(games[i].score2) ||
370 idx1 === undefined || idx2 === undefined ||
371 games[i].score1 == games[i].score2) {
374 ++teams[idx1].nplayed;
375 ++teams[idx2].nplayed;
376 teams[idx1].goals += games[i].score1;
377 teams[idx2].goals += games[i].score2;
378 teams[idx1].gd += games[i].score1;
379 teams[idx2].gd += games[i].score2;
380 teams[idx1].gd -= games[i].score2;
381 teams[idx2].gd -= games[i].score1;
382 if (games[i].score1 > games[i].score2) {
383 teams[idx1].pts += 2;
385 teams[idx2].pts += 2;
389 let tiebreakers = [];
390 teams = rank(games, teams, 1, tiebreakers);
392 let carousel = document.getElementById('carousel');
393 clear_carousel(carousel);
395 addheading(carousel, 5, "Current standings, Trøndisk 2017<br />" + group_name);
396 let tr = document.createElement("tr");
397 tr.className = "subfooter";
398 addth(tr, "rank", "");
399 addth(tr, "team", "");
400 addth(tr, "nplayed", "P");
401 addth(tr, "gd", "GD");
402 addth(tr, "pts", "Pts");
403 carousel.appendChild(tr);
406 for (let i = 0; i < teams.length; ++i) {
407 let tr = document.createElement("tr");
409 addth(tr, "rank", teams[i].rank);
410 addtd(tr, "team", teams[i].name);
411 addtd(tr, "nplayed", teams[i].nplayed);
412 addtd(tr, "gd", teams[i].gd.toString().replace(/-/, '−'));
413 addtd(tr, "pts", teams[i].pts);
415 carousel.appendChild(tr);
418 if (tiebreakers.length > 0) {
419 let tie_tr = document.createElement("tr");
420 tie_tr.className = "footer";
421 let td = document.createElement("td");
422 td.appendChild(document.createTextNode("Tiebreaks applied: " + tiebreakers.join(', ')));
423 td.setAttribute("colspan", "5");
424 tie_tr.appendChild(td);
425 carousel.appendChild(tie_tr);
428 let footer_tr = document.createElement("tr");
429 footer_tr.className = "footer";
430 let td = document.createElement("td");
431 td.appendChild(document.createTextNode("www.trondheimfrisbeeklubb.no | #trøndisk"));
432 td.setAttribute("colspan", "5");
433 footer_tr.appendChild(td);
434 carousel.appendChild(footer_tr);
436 fade_in_rows(carousel);
438 carousel.style.display = 'table';
441 function fade_in_rows(table)
443 let trs = table.getElementsByTagName("tr");
444 for (let i = 1; i < trs.length; ++i) { // The header already has its own fade-in.
445 if (trs[i].className === "footer") {
446 trs[i].style = "-webkit-animation: fade-in 1.0s ease; -webkit-animation-delay: " + (0.25 * i) + "s; -webkit-animation-fill-mode: both;";
448 trs[i].style = "-webkit-animation: fade-in 2.0s ease; -webkit-animation-delay: " + (0.25 * i) + "s; -webkit-animation-fill-mode: both;";
453 function fade_out_rows(table)
455 let trs = table.getElementsByTagName("tr");
456 for (let i = 0; i < trs.length; ++i) {
457 if (trs[i].className === "footer") {
458 trs[i].style = "-webkit-animation: fade-out 1.0s ease; -webkit-animation-delay: " + (0.125 * i) + "s; -webkit-animation-fill-mode: both;";
460 trs[i].style = "-webkit-animation: fade-out 1.0s ease; -webkit-animation-delay: " + (0.125 * i) + "s; -webkit-animation-fill-mode: both;";
465 function clear_carousel(table)
467 while (table.childNodes.length > 0) {
468 table.removeChild(table.firstChild);
473 let max_list_len = 8;
475 function display_stream_schedule(response, group_name) {
476 let teams = parse_teams_from_spreadsheet(response);
477 let games = parse_games_from_spreadsheet(response, group_name, true);
478 display_stream_schedule_parsed(teams, games, 0);
481 function sort_game_list(games) {
482 games = games.filter(function(game) { return game.streamtime !== undefined && game.streamtime.match(/[0-9]+:[0-9]+/) != null; });
483 games.sort(function(a, b) {
484 if (a.streamday !== b.streamday) {
485 return a.streamday - b.streamday;
488 let m1 = a.streamtime.match(/([0-9]+):([0-9]+)/);
489 let m2 = b.streamtime.match(/([0-9]+):([0-9]+)/);
490 return (m1[1] * 60 + m1[2]) - (m2[1] * 60 + m2[2]);
495 function find_game_start_idx(games) {
496 // Pick out a reasonable place to start the list. We'll show the last
497 // completed match and start from there.
498 let start_idx = games.length - 1;
499 for (let i = 0; i < games.length; ++i) {
500 if (isNaN(games[i].score1) || isNaN(games[i].score2) &&
501 games[i].score1 === games[i].score2) {
506 if (start_idx > 0) start_idx--;
507 if (games.length >= max_list_len) {
508 start_idx = Math.min(start_idx, games.length - max_list_len);
513 function find_num_pages(games) {
514 games = sort_game_list(games);
515 let start_idx = find_game_start_idx(games);
516 return Math.ceil((games.length - start_idx) / max_list_len);
519 function display_stream_schedule_parsed(teams, games, page) {
520 document.getElementById('entire-bug').style.display = 'none';
522 games = sort_game_list(games);
523 let start_idx = find_game_start_idx(games);
525 start_idx += page * max_list_len;
526 if (start_idx >= games.length) {
531 let days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"];
532 let shortdays = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"];
533 let today = days[(new Date).getDay()];
535 let covered_days = [];
537 for (let i = start_idx; i < games.length && row_num++ < max_list_len; ++i) {
538 if (i == start_idx || games[i].streamday != games[i - 1].streamday) {
539 covered_days.push(days[games[i].streamday]);
543 let carousel = document.getElementById('carousel');
544 clear_carousel(carousel);
545 addheading(carousel, 3, "Stream schedule, Trøndisk 2017<br />" + covered_days.join('/') + " (all times CET)");
547 let teams_to_idx = make_teams_to_idx(teams);
549 for (let i = start_idx; i < games.length && row_num < max_list_len; ++i) {
550 let tr = document.createElement("tr");
552 let name1 = teams[teams_to_idx[games[i].name1]].mediumname;
553 let name2 = teams[teams_to_idx[games[i].name2]].mediumname;
555 addtd(tr, "matchup", name1 + "–" + name2);
556 addtd(tr, "group", games[i].group_name);
558 if (!isNaN(games[i].score1) && !isNaN(games[i].score2) &&
559 games[i].score1 !== games[i].score2) {
560 addtd(tr, "streamtime", games[i].score1 + "–" + games[i].score2);
562 let streamtime = games[i].streamtime;
563 let streamday = days[games[i].streamday];
564 if (streamday !== today) {
565 streamtime = shortdays[games[i].streamday] + " " + streamtime;
567 addth(tr, "streamtime", streamtime);
571 carousel.appendChild(tr);
574 fade_in_rows(carousel);
576 carousel.style.display = 'table';
579 function get_group(group_name, cb)
581 let req = new XMLHttpRequest();
582 req.onload = function(e) {
583 cb(JSON.parse(req.responseText), group_name);
585 req.open('GET', 'https://sheets.googleapis.com/v4/spreadsheets/122tIwrXTi5ug0Vv6Np5w3pVwEWE2KkjWxtzQQfGtOZA/values/\'' + group_name + '\'!A1:J50?key=AIzaSyAuP9yQn8g0bSay6r_RpGtpFeIbwprH1TU');
589 function showgroup(group_name)
591 get_group(group_name, display_group);
594 function showgroup_from_state()
596 showgroup(state['group_name']);
599 let carousel_timeout = null;
603 fade_out_rows(document.getElementById('carousel'));
606 function showschedule(page)
612 let cb = function(response, group_name) {
613 teams = teams.concat(parse_teams_from_spreadsheet(response));
614 games = games.concat(parse_games_from_spreadsheet(response, group_name, true));
615 if (--num_left == 0) {
616 display_stream_schedule_parsed(teams, games, 0);
620 get_group('Group A', cb);
621 get_group('Group B', cb);
622 get_group('Playoffs', cb);
625 function do_series(series)
627 do_series_internal(series, 0);
630 function do_series_internal(series, idx)
633 if (idx + 1 < series.length) {
634 carousel_timeout = setTimeout(function() { do_series_internal(series, idx + 1); }, series[idx][0]);
638 function showcarousel()
640 let teams_per_group = [];
641 let games_per_group = [];
642 let combined_teams = [];
643 let combined_games = [];
646 let cb = function(response, group_name) {
647 let teams = parse_teams_from_spreadsheet(response);
648 let games = parse_games_from_spreadsheet(response, group_name, true);
649 teams_per_group[group_name] = teams;
650 games_per_group[group_name] = games;
652 combined_teams = combined_teams.concat(teams);
653 combined_games = combined_games.concat(games);
654 if (--num_left == 0) {
656 [ 13000, function() { display_group_parsed(teams_per_group['Group A'], games_per_group['Group A'], 'Group A'); } ],
657 [ 2000, function() { hidetable(); } ],
658 [ 13000, function() { display_group_parsed(teams_per_group['Group B'], games_per_group['Group B'], 'Group B'); } ],
659 [ 2000, function() { hidetable(); } ]
661 let num_pages = find_num_pages(combined_games);
662 for (let page = 0; page < num_pages; ++page) {
663 series.push([ 13000, function() { display_stream_schedule_parsed(combined_teams, combined_games, page); } ]);
664 series.push([ 2000, function() { hidetable(); } ]);
671 get_group('Group A', cb);
672 get_group('Group B', cb);
673 get_group('Playoffs', cb);
676 function stopcarousel()
678 if (carousel_timeout !== null) {
680 clearTimeout(carousel_timeout);
681 carousel_timeout = null;
685 function hidescorebug()
687 document.getElementById('entire-bug').style.display = 'none';
690 function showscorebug()
692 document.getElementById('entire-bug').style.display = null;