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 beat_parts = partition_by_beat(teams, function(beat, teams_to_idx) {
158 for (let i = 0; i < games.length; ++i) {
159 let idx1 = teams_to_idx[games[i].name1];
160 let idx2 = teams_to_idx[games[i].name2];
161 if (idx1 !== undefined && idx2 !== undefined) {
162 if (games[i].score1 > games[i].score2) {
163 beat[idx1][idx2] = 1;
165 if (games[i].score1 < games[i].score2) {
166 beat[idx2][idx1] = 1;
171 if (beat_parts.length > 1) {
172 tiebreakers.push(explain_tiebreaker(beat_parts, 'head-to-head'));
173 return subrank_partitions(games, beat_parts, start_rank, tiebreakers);
176 // Rule #2: Number of games played (fewer is better).
177 // Actually the rule says “fewest losses”, but fewer games is equivalent
178 // as long as teams have the same amount of points and ties don't exist.
179 let nplayed_parts = partition(teams, function(a, b) { return a.nplayed - b.nplayed });
180 if (nplayed_parts.length > 1) {
181 tiebreakers.push(explain_tiebreaker(nplayed_parts, 'fewer losses'));
182 return subrank_partitions(games, nplayed_parts, start_rank, tiebreakers);
185 // Rule #3: Head-to-head goal difference.
186 let teams_to_idx = make_teams_to_idx(teams);
187 for (let i = 0; i < teams.length; i++) {
189 teams[i].h2h_goals = 0;
191 for (let i = 0; i < games.length; ++i) {
192 let idx1 = teams_to_idx[games[i].name1];
193 let idx2 = teams_to_idx[games[i].name2];
194 if (idx1 !== undefined && idx2 !== undefined &&
195 !isNaN(games[i].score1) && isNaN(games[i].score2)) {
196 teams[idx1].h2h_gd += games[i].score1;
197 teams[idx1].h2h_gd -= games[i].score2;
198 teams[idx2].h2h_gd += games[i].score2;
199 teams[idx2].h2h_gd -= games[i].score1;
201 teams[idx1].h2h_goals += games[i].score1;
202 teams[idx2].h2h_goals += games[i].score2;
205 let h2h_gd_parts = partition(teams, function(a, b) { return b.h2h_gd - a.h2h_gd });
206 if (h2h_gd_parts.length > 1) {
207 tiebreakers.push(explain_tiebreaker(h2h_gd_parts, 'head-to-head goal difference'));
208 return subrank_partitions(games, h2h_gd_parts, start_rank, tiebreakers);
211 // Rule #4: Goal difference against common opponents.
213 for (let i = 0; i < games.length; ++i) {
214 if (results[games[i].name1] === undefined) {
215 results[games[i].name1] = {};
217 if (results[games[i].name2] === undefined) {
218 results[games[i].name2] = {};
220 results[games[i].name1][games[i].name2] = [ games[i].score1, games[i].score2 ];
221 results[games[i].name2][games[i].name1] = [ games[i].score2, games[i].score1 ];
223 let gd_parts = partition_by_beat(teams, function(beat, teams_to_idx) {
224 for (const team_i of Object.keys(teams_to_idx)) {
225 let i = teams_to_idx[team_i];
226 for (const team_j of Object.keys(teams_to_idx)) {
227 let j = teams_to_idx[team_j];
228 let results_i = results[team_i], results_j = results[team_j];
229 let gd_i = 0, gd_j = 0;
231 // See if the two teams have both played a third team k.
232 for (let k in results_i) {
233 if (!results_i.hasOwnProperty(k)) continue;
234 if (results_j[k] !== undefined) {
235 gd_i += results_i[k][0] - results_i[k][1];
236 gd_j += results_j[k][0] - results_j[k][1];
242 } else if (gd_i < gd_j) {
248 if (gd_parts.length > 1) {
249 tiebreakers.push(explain_tiebreaker(gd_parts, 'goal difference versus common opponents'));
250 return subrank_partitions(games, gd_parts, start_rank, tiebreakers);
253 // Rule #5: Head-to-head scored goals.
254 let h2h_goals_parts = partition(teams, function(a, b) { return b.h2h_goals - a.h2h_goals });
255 if (h2h_goals_parts.length > 1) {
256 tiebreakers.push(explain_tiebreaker(h2h_goals_parts, 'head-to-head scored goals'));
257 return subrank_partitions(games, h2h_goals_parts, start_rank, tiebreakers);
260 // Rule #6: Goals scored against common opponents.
261 let goals_parts = partition_by_beat(teams, function(beat, teams_to_idx) {
262 for (const team_i of Object.keys(teams_to_idx)) {
263 let i = teams_to_idx[team_i];
264 for (const team_j of Object.keys(teams_to_idx)) {
265 let j = teams_to_idx[team_j];
266 let results_i = results[team_i], results_j = results[team_j];
267 let goals_i = 0, goals_j = 0;
269 // See if the two teams have both played a third team k.
270 for (let k in results_i) {
271 if (!results_i.hasOwnProperty(k)) continue;
272 if (results_j[k] !== undefined) {
273 goals_i += results_i[k][0];
274 goals_j += results_j[k][0];
278 if (goals_i > goals_j) {
280 } else if (goals_i < goals_j) {
286 if (goals_parts.length > 1) {
287 tiebreakers.push(explain_tiebreaker(goals_parts, 'goals scored against common opponents'));
288 return subrank_partitions(games, goals_parts, start_rank, tiebreakers);
291 // OK, it's a tie. Give them all the same rank.
293 for (let i = 0; i < teams.length; ++i) {
294 result.push(teams[i]);
295 result[i].rank = start_rank;
300 function parse_teams_from_spreadsheet(response) {
302 for (let i = 2; response.values[i].length >= 1; ++i) {
304 "name": response.values[i][0],
305 "mediumname": response.values[i][1],
306 "shortname": response.values[i][2],
307 "tags": response.values[i][3],
317 function parse_games_from_spreadsheet(response, group_name, include_unplayed) {
320 for (i = 0; i < response.values.length; ++i) {
321 if (response.values[i][0] === 'Results') {
327 for ( ; response.values[i] !== undefined && response.values[i].length >= 1; ++i) {
328 if ((response.values[i][2] && response.values[i][3]) || include_unplayed) {
329 let real_group_name = response.values[i][9];
330 if (real_group_name === undefined) {
331 real_group_name = group_name;
334 "name1": response.values[i][0],
335 "name2": response.values[i][1],
336 "score1": parseInt(response.values[i][2]),
337 "score2": parseInt(response.values[i][3]),
338 "streamday": response.values[i][7],
339 "streamtime": response.values[i][8],
340 "group_name": real_group_name
347 function display_group(response, group_name)
349 let teams = parse_teams_from_spreadsheet(response);
350 let games = parse_games_from_spreadsheet(response, group_name, false);
351 display_group_parsed(teams, games, group_name);
354 function display_group_parsed(teams, games, group_name)
356 document.getElementById('entire-bug').style.display = 'none';
358 let teams_to_idx = make_teams_to_idx(teams);
359 for (let i = 0; i < games.length; ++i) {
360 let idx1 = teams_to_idx[games[i].name1];
361 let idx2 = teams_to_idx[games[i].name2];
362 if (games[i].score1 === undefined || games[i].score2 === undefined ||
363 isNaN(games[i].score1) || isNaN(games[i].score2) ||
364 idx1 === undefined || idx2 === undefined ||
365 games[i].score1 == games[i].score2) {
368 ++teams[idx1].nplayed;
369 ++teams[idx2].nplayed;
370 teams[idx1].goals += games[i].score1;
371 teams[idx2].goals += games[i].score2;
372 teams[idx1].gd += games[i].score1;
373 teams[idx2].gd += games[i].score2;
374 teams[idx1].gd -= games[i].score2;
375 teams[idx2].gd -= games[i].score1;
376 if (games[i].score1 > games[i].score2) {
377 teams[idx1].pts += 2;
379 teams[idx2].pts += 2;
383 let tiebreakers = [];
384 teams = rank(games, teams, 1, tiebreakers);
386 let carousel = document.getElementById('carousel');
387 clear_carousel(carousel);
389 addheading(carousel, 5, "Current standings, Trøndisk 2017<br />" + group_name);
390 let tr = document.createElement("tr");
391 tr.className = "subfooter";
392 addth(tr, "rank", "");
393 addth(tr, "team", "");
394 addth(tr, "nplayed", "P");
395 addth(tr, "gd", "GD");
396 addth(tr, "pts", "Pts");
397 carousel.appendChild(tr);
400 for (let i = 0; i < teams.length; ++i) {
401 let tr = document.createElement("tr");
403 addth(tr, "rank", teams[i].rank);
404 addtd(tr, "team", teams[i].name);
405 addtd(tr, "nplayed", teams[i].nplayed);
406 addtd(tr, "gd", teams[i].gd.toString().replace(/-/, '−'));
407 addtd(tr, "pts", teams[i].pts);
409 carousel.appendChild(tr);
412 if (tiebreakers.length > 0) {
413 let tie_tr = document.createElement("tr");
414 tie_tr.className = "footer";
415 let td = document.createElement("td");
416 td.appendChild(document.createTextNode("Tiebreaks applied: " + tiebreakers.join(', ')));
417 td.setAttribute("colspan", "5");
418 tie_tr.appendChild(td);
419 carousel.appendChild(tie_tr);
422 let footer_tr = document.createElement("tr");
423 footer_tr.className = "footer";
424 let td = document.createElement("td");
425 td.appendChild(document.createTextNode("www.trondheimfrisbeeklubb.no | #trøndisk"));
426 td.setAttribute("colspan", "5");
427 footer_tr.appendChild(td);
428 carousel.appendChild(footer_tr);
430 fade_in_rows(carousel);
432 carousel.style.display = 'table';
435 function fade_in_rows(table)
437 let trs = table.getElementsByTagName("tr");
438 for (let i = 1; i < trs.length; ++i) { // The header already has its own fade-in.
439 if (trs[i].className === "footer") {
440 trs[i].style = "-webkit-animation: fade-in 1.0s ease; -webkit-animation-delay: " + (0.25 * i) + "s; -webkit-animation-fill-mode: both;";
442 trs[i].style = "-webkit-animation: fade-in 2.0s ease; -webkit-animation-delay: " + (0.25 * i) + "s; -webkit-animation-fill-mode: both;";
447 function fade_out_rows(table)
449 let trs = table.getElementsByTagName("tr");
450 for (let i = 0; i < trs.length; ++i) {
451 if (trs[i].className === "footer") {
452 trs[i].style = "-webkit-animation: fade-out 1.0s ease; -webkit-animation-delay: " + (0.125 * i) + "s; -webkit-animation-fill-mode: both;";
454 trs[i].style = "-webkit-animation: fade-out 1.0s ease; -webkit-animation-delay: " + (0.125 * i) + "s; -webkit-animation-fill-mode: both;";
459 function clear_carousel(table)
461 while (table.childNodes.length > 0) {
462 table.removeChild(table.firstChild);
467 let max_list_len = 8;
469 function display_stream_schedule(response, group_name) {
470 let teams = parse_teams_from_spreadsheet(response);
471 let games = parse_games_from_spreadsheet(response, group_name, true);
472 display_stream_schedule_parsed(teams, games, 0);
475 function sort_game_list(games) {
476 games = games.filter(function(game) { return game.streamtime !== undefined && game.streamtime.match(/[0-9]+:[0-9]+/) != null; });
477 games.sort(function(a, b) {
478 if (a.streamday !== b.streamday) {
479 return a.streamday - b.streamday;
482 let m1 = a.streamtime.match(/([0-9]+):([0-9]+)/);
483 let m2 = b.streamtime.match(/([0-9]+):([0-9]+)/);
484 return (m1[1] * 60 + m1[2]) - (m2[1] * 60 + m2[2]);
489 function find_game_start_idx(games) {
490 // Pick out a reasonable place to start the list. We'll show the last
491 // completed match and start from there.
492 let start_idx = games.length - 1;
493 for (let i = 0; i < games.length; ++i) {
494 if (isNaN(games[i].score1) || isNaN(games[i].score2) &&
495 games[i].score1 === games[i].score2) {
500 if (start_idx > 0) start_idx--;
501 if (games.length >= max_list_len) {
502 start_idx = Math.min(start_idx, games.length - max_list_len);
507 function find_num_pages(games) {
508 games = sort_game_list(games);
509 let start_idx = find_game_start_idx(games);
510 return Math.ceil((games.length - start_idx) / max_list_len);
513 function display_stream_schedule_parsed(teams, games, page) {
514 document.getElementById('entire-bug').style.display = 'none';
516 games = sort_game_list(games);
517 let start_idx = find_game_start_idx(games);
519 start_idx += page * max_list_len;
520 if (start_idx >= games.length) {
525 let days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"];
526 let shortdays = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"];
527 let today = days[(new Date).getDay()];
529 let covered_days = [];
531 for (let i = start_idx; i < games.length && row_num++ < max_list_len; ++i) {
532 if (i == start_idx || games[i].streamday != games[i - 1].streamday) {
533 covered_days.push(days[games[i].streamday]);
537 let carousel = document.getElementById('carousel');
538 clear_carousel(carousel);
539 addheading(carousel, 3, "Stream schedule, Trøndisk 2017<br />" + covered_days.join('/') + " (all times CET)");
541 let teams_to_idx = make_teams_to_idx(teams);
543 for (let i = start_idx; i < games.length && row_num < max_list_len; ++i) {
544 let tr = document.createElement("tr");
546 let name1 = teams[teams_to_idx[games[i].name1]].mediumname;
547 let name2 = teams[teams_to_idx[games[i].name2]].mediumname;
549 addtd(tr, "matchup", name1 + "–" + name2);
550 addtd(tr, "group", games[i].group_name);
552 if (!isNaN(games[i].score1) && !isNaN(games[i].score2) &&
553 games[i].score1 !== games[i].score2) {
554 addtd(tr, "streamtime", games[i].score1 + "–" + games[i].score2);
556 let streamtime = games[i].streamtime;
557 let streamday = days[games[i].streamday];
558 if (streamday !== today) {
559 streamtime = shortdays[games[i].streamday] + " " + streamtime;
561 addth(tr, "streamtime", streamtime);
565 carousel.appendChild(tr);
568 fade_in_rows(carousel);
570 carousel.style.display = 'table';
573 function get_group(group_name, cb)
575 let req = new XMLHttpRequest();
576 req.onload = function(e) {
577 cb(JSON.parse(req.responseText), group_name);
579 req.open('GET', 'https://sheets.googleapis.com/v4/spreadsheets/122tIwrXTi5ug0Vv6Np5w3pVwEWE2KkjWxtzQQfGtOZA/values/\'' + group_name + '\'!A1:J50?key=AIzaSyAuP9yQn8g0bSay6r_RpGtpFeIbwprH1TU');
583 function showgroup(group_name)
585 get_group(group_name, display_group);
588 function showgroup_from_state()
590 showgroup(state['group_name']);
593 let carousel_timeout = null;
597 fade_out_rows(document.getElementById('carousel'));
600 function showschedule(page)
606 let cb = function(response, group_name) {
607 teams = teams.concat(parse_teams_from_spreadsheet(response));
608 games = games.concat(parse_games_from_spreadsheet(response, group_name, true));
609 if (--num_left == 0) {
610 display_stream_schedule_parsed(teams, games, 0);
614 get_group('Group A', cb);
615 get_group('Group B', cb);
616 get_group('Playoffs', cb);
619 function do_series(series)
621 do_series_internal(series, 0);
624 function do_series_internal(series, idx)
627 if (idx + 1 < series.length) {
628 carousel_timeout = setTimeout(function() { do_series_internal(series, idx + 1); }, series[idx][0]);
632 function showcarousel()
634 let teams_per_group = [];
635 let games_per_group = [];
636 let combined_teams = [];
637 let combined_games = [];
640 let cb = function(response, group_name) {
641 let teams = parse_teams_from_spreadsheet(response);
642 let games = parse_games_from_spreadsheet(response, group_name, true);
643 teams_per_group[group_name] = teams;
644 games_per_group[group_name] = games;
646 combined_teams = combined_teams.concat(teams);
647 combined_games = combined_games.concat(games);
648 if (--num_left == 0) {
650 [ 13000, function() { display_group_parsed(teams_per_group['Group A'], games_per_group['Group A'], 'Group A'); } ],
651 [ 2000, function() { hidetable(); } ],
652 [ 13000, function() { display_group_parsed(teams_per_group['Group B'], games_per_group['Group B'], 'Group B'); } ],
653 [ 2000, function() { hidetable(); } ]
655 let num_pages = find_num_pages(combined_games);
656 for (let page = 0; page < num_pages; ++page) {
657 series.push([ 13000, function() { display_stream_schedule_parsed(combined_teams, combined_games, page); } ]);
658 series.push([ 2000, function() { hidetable(); } ]);
665 get_group('Group A', cb);
666 get_group('Group B', cb);
667 get_group('Playoffs', cb);
670 function stopcarousel()
672 if (carousel_timeout !== null) {
674 clearTimeout(carousel_timeout);
675 carousel_timeout = null;
679 function hidescorebug()
681 document.getElementById('entire-bug').style.display = 'none';
684 function showscorebug()
686 document.getElementById('entire-bug').style.display = null;