X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=carousel.js;h=35b02883c7d91809e89e5ec891e10733c9595c83;hb=3c3a5c028bed3922840a2b5ad7ab861fca092582;hp=841ae6824964528aef09ce187766fadaf495719f;hpb=593375711224535e21e359480489883ad6bb0660;p=ultimatescore diff --git a/carousel.js b/carousel.js index 841ae68..35b0288 100644 --- a/carousel.js +++ b/carousel.js @@ -69,11 +69,13 @@ function make_teams_to_idx(teams) let teams_to_idx = []; for (let i = 0; i < teams.length; i++) { teams_to_idx[teams[i].name] = i; + teams_to_idx[teams[i].mediumname] = i; + teams_to_idx[teams[i].shortname] = i; } return teams_to_idx; } -function partition_by_beat(games, teams) +function partition_by_beat(teams, fill_beatmatrix) { // Head-to-head score by way of components. First construct the beat matrix. let n = teams.length; @@ -85,18 +87,7 @@ function partition_by_beat(games, teams) beat[i][j] = 0; } } - for (let i = 0; i < games.length; ++i) { - let idx1 = teams_to_idx[games[i].name1]; - let idx2 = teams_to_idx[games[i].name2]; - if (idx1 !== undefined && idx2 !== undefined) { - if (games[i].score1 > games[i].score2) { - beat[idx1][idx2] = 1; - } - if (games[i].score1 < games[i].score2) { - beat[idx2][idx1] = 1; - } - } - } + fill_beatmatrix(beat, teams_to_idx); // Floyd-Warshall for transitive closure. for (let k = 0; k < n; ++k) { for (let i = 0; i < n; ++i) { @@ -135,11 +126,11 @@ function partition_by_beat(games, teams) } let result = []; if (better_than_pivot.length > 0) { - result = partition_by_beat(games, better_than_pivot); + result = partition_by_beat(better_than_pivot, fill_beatmatrix); } result.push(equal); // Obviously can't be partitioned further. if (worse_than_pivot.length > 0) { - result = result.concat(partition_by_beat(games, worse_than_pivot)); + result = result.concat(partition_by_beat(worse_than_pivot, fill_beatmatrix)); } return result; } @@ -165,7 +156,22 @@ function rank(games, teams, start_rank, tiebreakers) { } // Rule #1: Head-to-head wins. - let beat_parts = partition_by_beat(games, teams); + let num_relevant_games = 0; + let beat_parts = partition_by_beat(teams, function(beat, teams_to_idx) { + for (let i = 0; i < games.length; ++i) { + let idx1 = teams_to_idx[games[i].name1]; + let idx2 = teams_to_idx[games[i].name2]; + if (idx1 !== undefined && idx2 !== undefined) { + if (games[i].score1 > games[i].score2) { + beat[idx1][idx2] = 1; + ++num_relevant_games; + } else if (games[i].score1 < games[i].score2) { + beat[idx2][idx1] = 1; + ++num_relevant_games; + } + } + } + }); if (beat_parts.length > 1) { tiebreakers.push(explain_tiebreaker(beat_parts, 'head-to-head')); return subrank_partitions(games, beat_parts, start_rank, tiebreakers); @@ -180,50 +186,113 @@ function rank(games, teams, start_rank, tiebreakers) { return subrank_partitions(games, nplayed_parts, start_rank, tiebreakers); } - // Rule #3: Head-to-head goal difference. + // Rule #3: Head-to-head goal difference (if all have played). let teams_to_idx = make_teams_to_idx(teams); - for (let i = 0; i < teams.length; i++) { - teams[i].h2h_gd = 0; - teams[i].h2h_goals = 0; + if (num_relevant_games >= teams.length * (teams.length - 1) / 2) { + for (let i = 0; i < teams.length; i++) { + teams[i].h2h_gd = 0; + teams[i].h2h_goals = 0; + } + for (let i = 0; i < games.length; ++i) { + let idx1 = teams_to_idx[games[i].name1]; + let idx2 = teams_to_idx[games[i].name2]; + if (idx1 !== undefined && idx2 !== undefined && + !isNaN(games[i].score1) && !isNaN(games[i].score2)) { + teams[idx1].h2h_gd += games[i].score1; + teams[idx1].h2h_gd -= games[i].score2; + teams[idx2].h2h_gd += games[i].score2; + teams[idx2].h2h_gd -= games[i].score1; + + teams[idx1].h2h_goals += games[i].score1; + teams[idx2].h2h_goals += games[i].score2; + } + } + let h2h_gd_parts = partition(teams, function(a, b) { return b.h2h_gd - a.h2h_gd }); + if (h2h_gd_parts.length > 1) { + tiebreakers.push(explain_tiebreaker(h2h_gd_parts, 'head-to-head goal difference')); + return subrank_partitions(games, h2h_gd_parts, start_rank, tiebreakers); + } } + + // Rule #4: Goal difference against common opponents. + var results = {}; for (let i = 0; i < games.length; ++i) { - let idx1 = teams_to_idx[games[i].name1]; - let idx2 = teams_to_idx[games[i].name2]; - if (idx1 !== undefined && idx2 !== undefined && - !isNaN(games[i].score1) && isNaN(games[i].score2)) { - teams[idx1].h2h_gd += games[i].score1; - teams[idx1].h2h_gd -= games[i].score2; - teams[idx2].h2h_gd += games[i].score2; - teams[idx2].h2h_gd -= games[i].score1; - - teams[idx1].h2h_goals += games[i].score1; - teams[idx2].h2h_goals += games[i].score2; + if (results[games[i].name1] === undefined) { + results[games[i].name1] = {}; } - } - let h2h_gd_parts = partition(teams, function(a, b) { return b.h2h_gd - a.h2h_gd }); - if (h2h_gd_parts.length > 1) { - tiebreakers.push(explain_tiebreaker(h2h_gd_parts, 'head-to-head goal difference')); - return subrank_partitions(games, h2h_gd_parts, start_rank, tiebreakers); - } + if (results[games[i].name2] === undefined) { + results[games[i].name2] = {}; + } + results[games[i].name1][games[i].name2] = [ games[i].score1, games[i].score2 ]; + results[games[i].name2][games[i].name1] = [ games[i].score2, games[i].score1 ]; + } + let gd_parts = partition_by_beat(teams, function(beat, teams_to_idx) { + for (const team_i of Object.keys(teams_to_idx)) { + let i = teams_to_idx[team_i]; + for (const team_j of Object.keys(teams_to_idx)) { + let j = teams_to_idx[team_j]; + let results_i = results[team_i], results_j = results[team_j]; + let gd_i = 0, gd_j = 0; + + // See if the two teams have both played a third team k. + for (let k in results_i) { + if (!results_i.hasOwnProperty(k)) continue; + if (results_j !== undefined && results_j[k] !== undefined) { + gd_i += results_i[k][0] - results_i[k][1]; + gd_j += results_j[k][0] - results_j[k][1]; + } + } - // Rule #4: Global goal difference. (Well, not strictly, but good enough.) - let gd_parts = partition(teams, function(a, b) { return b.gd - a.gd }); + if (gd_i > gd_j) { + beat[i][j] = 1; + } else if (gd_i < gd_j) { + beat[j][i] = 1; + } + } + } + }); if (gd_parts.length > 1) { - tiebreakers.push(explain_tiebreaker(gd_parts, 'overall goal difference')); + tiebreakers.push(explain_tiebreaker(gd_parts, 'goal difference versus common opponents')); return subrank_partitions(games, gd_parts, start_rank, tiebreakers); } - // Rule #5: Head-to-head scored goals. - let h2h_goals_parts = partition(teams, function(a, b) { return b.h2h_goals - a.h2h_goals }); - if (h2h_goals_parts.length > 1) { - tiebreakers.push(explain_tiebreaker(h2h_goals_parts, 'head-to-head scored goals')); - return subrank_partitions(games, h2h_goals_parts, start_rank, tiebreakers); + // Rule #5: Head-to-head scored goals (if all have played). + if (num_relevant_games >= teams.length * (teams.length - 1) / 2) { + let h2h_goals_parts = partition(teams, function(a, b) { return b.h2h_goals - a.h2h_goals }); + if (h2h_goals_parts.length > 1) { + tiebreakers.push(explain_tiebreaker(h2h_goals_parts, 'head-to-head scored goals')); + return subrank_partitions(games, h2h_goals_parts, start_rank, tiebreakers); + } } - // Rule #6: Overall scored goals. (Same caveat as #4.) - let goals_parts = partition(teams, function(a, b) { return b.goals - a.goals }); + // Rule #6: Goals scored against common opponents. + let goals_parts = partition_by_beat(teams, function(beat, teams_to_idx) { + for (const team_i of Object.keys(teams_to_idx)) { + let i = teams_to_idx[team_i]; + for (const team_j of Object.keys(teams_to_idx)) { + let j = teams_to_idx[team_j]; + let results_i = results[team_i], results_j = results[team_j]; + let goals_i = 0, goals_j = 0; + + // See if the two teams have both played a third team k. + for (let k in results_i) { + if (!results_i.hasOwnProperty(k)) continue; + if (results_j !== undefined && results_j[k] !== undefined) { + goals_i += results_i[k][0]; + goals_j += results_j[k][0]; + } + } + + if (goals_i > goals_j) { + beat[i][j] = 1; + } else if (goals_i < goals_j) { + beat[j][i] = 1; + } + } + } + }); if (goals_parts.length > 1) { - tiebreakers.push(explain_tiebreaker(goals_parts, 'scored goals')); + tiebreakers.push(explain_tiebreaker(goals_parts, 'goals scored against common opponents')); return subrank_partitions(games, goals_parts, start_rank, tiebreakers); } @@ -283,17 +352,8 @@ function parse_games_from_spreadsheet(response, group_name, include_unplayed) { return games; }; -function display_group(response, group_name) +function apply_games_to_teams(games, teams) { - let teams = parse_teams_from_spreadsheet(response); - let games = parse_games_from_spreadsheet(response, group_name, false); - display_group_parsed(teams, games, group_name); -}; - -function display_group_parsed(teams, games, group_name) -{ - document.getElementById('entire-bug').style.display = 'none'; - let teams_to_idx = make_teams_to_idx(teams); for (let i = 0; i < games.length; ++i) { let idx1 = teams_to_idx[games[i].name1]; @@ -318,14 +378,20 @@ function display_group_parsed(teams, games, group_name) teams[idx2].pts += 2; } } +} +function display_group_parsed(teams, games, group_name) +{ + document.getElementById('entire-bug').style.display = 'none'; + + apply_games_to_teams(games, teams); let tiebreakers = []; teams = rank(games, teams, 1, tiebreakers); let carousel = document.getElementById('carousel'); clear_carousel(carousel); - addheading(carousel, 5, "Current standings, Trøndisk 2017
" + group_name); + addheading(carousel, 5, "Current standings
" + group_name); let tr = document.createElement("tr"); tr.className = "subfooter"; addth(tr, "rank", ""); @@ -361,7 +427,7 @@ function display_group_parsed(teams, games, group_name) let footer_tr = document.createElement("tr"); footer_tr.className = "footer"; let td = document.createElement("td"); - td.appendChild(document.createTextNode("www.trondheimfrisbeeklubb.no | #trøndisk")); + td.appendChild(document.createTextNode("Norwegian Ultimate Championships 2018 | #ultimatenm")); td.setAttribute("colspan", "5"); footer_tr.appendChild(td); carousel.appendChild(footer_tr); @@ -403,7 +469,7 @@ function clear_carousel(table) }; // Stream schedule -let max_list_len = 8; +let max_list_len = 7; function display_stream_schedule(response, group_name) { let teams = parse_teams_from_spreadsheet(response); @@ -475,7 +541,7 @@ function display_stream_schedule_parsed(teams, games, page) { let carousel = document.getElementById('carousel'); clear_carousel(carousel); - addheading(carousel, 3, "Stream schedule, Trøndisk 2017
" + covered_days.join('/') + " (all times CET)"); + addheading(carousel, 3, "Match schedule
" + covered_days.join('/') + " (all times CET)"); let teams_to_idx = make_teams_to_idx(teams); row_num = 0; @@ -515,19 +581,25 @@ function get_group(group_name, cb) req.onload = function(e) { cb(JSON.parse(req.responseText), group_name); }; - req.open('GET', 'https://sheets.googleapis.com/v4/spreadsheets/122tIwrXTi5ug0Vv6Np5w3pVwEWE2KkjWxtzQQfGtOZA/values/\'' + group_name + '\'!A1:J50?key=AIzaSyAuP9yQn8g0bSay6r_RpGtpFeIbwprH1TU'); + req.open('GET', 'https://sheets.googleapis.com/v4/spreadsheets/1uh7kr5v_hyD072b1G2tbQlhqd_8ldS_6j30CBocQ-4E/values/\'' + group_name + '\'!A1:J50?key=AIzaSyAuP9yQn8g0bSay6r_RpGtpFeIbwprH1TU'); req.send(); -}; +} function showgroup(group_name) { - get_group(group_name, display_group); -}; + get_group(group_name, function(response, group_name) { + let teams = parse_teams_from_spreadsheet(response); + let games = parse_games_from_spreadsheet(response, group_name, false); + display_group_parsed(teams, games, group_name); + }); + publish_group_rank(group_name); // Update the spreadsheet in the background. +} + function showgroup_from_state() { showgroup(state['group_name']); -}; +} let carousel_timeout = null;