X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=carousel.js;h=0dda4750cfa8f57b2a5809ba4ab03bb1f2b58ad6;hb=5653c007bb2f6095b9f538bbae472256be0a7bc9;hp=0a3d707e641d0b2a39e4eece0ef1c020b3e8340f;hpb=fe24af7266193f4908d73c49403387af8c819acc;p=ultimatescore diff --git a/carousel.js b/carousel.js index 0a3d707..0dda475 100644 --- a/carousel.js +++ b/carousel.js @@ -1,5 +1,21 @@ 'use strict'; +function jsonclone(x) +{ + return JSON.parse(JSON.stringify(x)); +} + +// Log with deep clone, so that the browser will show the object at time of log, +// instead of what it looks like at time of view. +function dlog() +{ + let args = []; + for (const arg of arguments) { + args.push(jsonclone(arg)); + } + console.log(args); +} + function addheading(carousel, colspan, content) { let thead = document.createElement("thead"); @@ -10,24 +26,26 @@ function addheading(carousel, colspan, content) tr.appendChild(th); thead.appendChild(tr); carousel.appendChild(thead); -}; +} + function addtd(tr, className, content) { let td = document.createElement("td"); td.appendChild(document.createTextNode(content)); td.className = className; tr.appendChild(td); -}; +} + function addth(tr, className, content) { let th = document.createElement("th"); th.appendChild(document.createTextNode(content)); th.className = className; tr.appendChild(th); -}; +} -function subrank_partitions(games, parts, start_rank, tiebreakers) { +function subrank_partitions(games, parts, start_rank, tiebreakers, func) { let result = []; for (let i = 0; i < parts.length; ++i) { - let part = rank(games, parts[i], start_rank, tiebreakers); + let part = func(games, parts[i], start_rank, tiebreakers); for (let j = 0; j < part.length; ++j) { result.push(part[j]); } @@ -152,7 +170,7 @@ function rank(games, teams, start_rank, tiebreakers) { // Rule #0: Partition the teams by score. let score_parts = partition(teams, function(a, b) { return b.pts - a.pts }); if (score_parts.length > 1) { - return subrank_partitions(games, score_parts, start_rank, tiebreakers); + return subrank_partitions(games, score_parts, start_rank, tiebreakers, rank); } // Rule #1: Head-to-head wins. @@ -174,7 +192,7 @@ function rank(games, teams, start_rank, tiebreakers) { }); if (beat_parts.length > 1) { tiebreakers.push(explain_tiebreaker(beat_parts, 'head-to-head')); - return subrank_partitions(games, beat_parts, start_rank, tiebreakers); + return subrank_partitions(games, beat_parts, start_rank, tiebreakers, rank); } // Rule #2: Number of games played (fewer is better). @@ -183,7 +201,7 @@ function rank(games, teams, start_rank, tiebreakers) { let nplayed_parts = partition(teams, function(a, b) { return a.nplayed - b.nplayed }); if (nplayed_parts.length > 1) { tiebreakers.push(explain_tiebreaker(nplayed_parts, 'fewer losses')); - return subrank_partitions(games, nplayed_parts, start_rank, tiebreakers); + return subrank_partitions(games, nplayed_parts, start_rank, tiebreakers, rank); } // Rule #3: Head-to-head goal difference (if all have played). @@ -210,7 +228,7 @@ function rank(games, teams, start_rank, tiebreakers) { 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); + return subrank_partitions(games, h2h_gd_parts, start_rank, tiebreakers, rank); } } @@ -253,7 +271,7 @@ function rank(games, teams, start_rank, tiebreakers) { }); if (gd_parts.length > 1) { tiebreakers.push(explain_tiebreaker(gd_parts, 'goal difference versus common opponents')); - return subrank_partitions(games, gd_parts, start_rank, tiebreakers); + return subrank_partitions(games, gd_parts, start_rank, tiebreakers, rank); } // Rule #5: Head-to-head scored goals (if all have played). @@ -261,7 +279,7 @@ function rank(games, teams, start_rank, tiebreakers) { 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); + return subrank_partitions(games, h2h_goals_parts, start_rank, tiebreakers, rank); } } @@ -293,7 +311,45 @@ function rank(games, teams, start_rank, tiebreakers) { }); if (goals_parts.length > 1) { tiebreakers.push(explain_tiebreaker(goals_parts, 'goals scored against common opponents')); - return subrank_partitions(games, goals_parts, start_rank, tiebreakers); + return subrank_partitions(games, goals_parts, start_rank, tiebreakers, rank); + } + + // OK, it's a tie. Give them all the same rank. + let result = []; + for (let i = 0; i < teams.length; ++i) { + result.push(teams[i]); + result[i].rank = start_rank; + } + return result; +}; + +// Same, but with the simplified rules for ranking thirds. games isn't used and can be empty. +function rank_thirds(games, teams, start_rank, tiebreakers) { + if (teams.length <= 1) { + // Only one team, so trivial. + teams[0].rank = start_rank; + return teams; + } + + // Rule #1: Partition the teams by score. + let score_parts = partition(teams, function(a, b) { return b.pts - a.pts }); + if (score_parts.length > 1) { + tiebreakers.push(explain_tiebreaker(score_parts, 'most games won')); + return subrank_partitions(games, score_parts, start_rank, tiebreakers, rank_thirds); + } + + // Rule #2: Goal difference against common opponents. + let gd_parts = partition(teams, function(a, b) { return b.gd - a.gd }); + if (gd_parts.length > 1) { + tiebreakers.push(explain_tiebreaker(gd_parts, 'goal difference')); + return subrank_partitions(games, gd_parts, start_rank, tiebreakers, rank_thirds); + } + + // Rule #3: Goals scored. + let goal_parts = partition(teams, function(a, b) { return b.goals - a.goals }); + if (goal_parts.length > 1) { + tiebreakers.push(explain_tiebreaker(goal_parts, 'goals scored')); + return subrank_partitions(games, goal_parts, start_rank, tiebreakers, rank_thirds); } // OK, it's a tie. Give them all the same rank. @@ -312,7 +368,8 @@ function parse_teams_from_spreadsheet(response) { "name": response.values[i][0], "mediumname": response.values[i][1], "shortname": response.values[i][2], - "tags": response.values[i][3], + //"tags": response.values[i][3], + "ngames": 0, "nplayed": 0, "gd": 0, "pts": 0, @@ -352,9 +409,21 @@ function parse_games_from_spreadsheet(response, group_name, include_unplayed) { return games; }; -function apply_games_to_teams(games, teams) +function apply_games_to_teams(games, teams, ignored_teams, ret_ignored_games) { let teams_to_idx = make_teams_to_idx(teams); + let ignored_teams_idx; + if (ignored_teams === undefined) { + ignored_teams_idx = []; + } else { + ignored_teams_idx = make_teams_to_idx(ignored_teams); + } + for (let i = 0; i < teams.length; ++i) { + teams[i].nplayed = 0; + teams[i].goals = 0; + teams[i].gd = 0; + teams[i].pts = 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]; @@ -364,6 +433,23 @@ function apply_games_to_teams(games, teams) games[i].score1 == games[i].score2) { continue; } + + let ignored_idx1 = ignored_teams_idx[games[i].name1]; + let ignored_idx2 = ignored_teams_idx[games[i].name2]; + if (ignored_idx1 !== undefined || ignored_idx2 !== undefined) { + if (ret_ignored_games !== undefined) { + // Figure out whether the fifth we're ignoring was only picked out arbitrarily + // (ie., there's a tie for 5th); if so, mark it as such. + let arbitrary = false; + if (ignored_idx1 !== undefined && ignored_teams[ignored_idx1].rank < 5) { + arbitrary = true; + } else if (ignored_idx2 !== undefined && ignored_teams[ignored_idx2].rank < 5) { + arbitrary = true; + } + ret_ignored_games.push([teams[idx1].shortname, teams[idx2].shortname, arbitrary]); + } + continue; + } ++teams[idx1].nplayed; ++teams[idx2].nplayed; teams[idx1].goals += games[i].score1; @@ -380,6 +466,24 @@ function apply_games_to_teams(games, teams) } } +// So that we can just have one team list, and let membership be defined by games. +function filter_teams(teams, response) +{ + let teams_to_idx = make_teams_to_idx(teams); + let games = parse_games_from_spreadsheet(response, 'irrelevant group name', true); + 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) { + ++teams[idx1].ngames; // FIXME: shouldn't nplayed be just as good? + } + if (idx2 !== undefined) { + ++teams[idx2].ngames; + } + } + return teams.filter(function(team) { return team.ngames > 0; }); +} + function display_group_parsed(teams, games, group_name) { document.getElementById('entire-bug').style.display = 'none'; @@ -391,7 +495,7 @@ function display_group_parsed(teams, games, group_name) let carousel = document.getElementById('carousel'); clear_carousel(carousel); - addheading(carousel, 5, "Current standings, Trøndisk 2018
" + group_name); + addheading(carousel, 5, "Current standings, " + ultimateconfig['tournament_title'] + "
" + group_name); let tr = document.createElement("tr"); tr.className = "subfooter"; addth(tr, "rank", ""); @@ -427,7 +531,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(ultimateconfig['tournament_footer'])); td.setAttribute("colspan", "5"); footer_tr.appendChild(td); carousel.appendChild(footer_tr); @@ -541,7 +645,7 @@ function display_stream_schedule_parsed(teams, games, page) { let carousel = document.getElementById('carousel'); clear_carousel(carousel); - addheading(carousel, 3, "Stream schedule, Trøndisk 2018
" + covered_days.join('/') + " (all times CET)"); + addheading(carousel, 3, "Stream schedule, " + ultimateconfig['tournament_title'] + "
" + covered_days.join('/') + " (all times CET)"); let teams_to_idx = make_teams_to_idx(teams); row_num = 0; @@ -581,7 +685,7 @@ 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/1DSRRcV6d6Dx9KbCZgOSFF0MX9wKRex9nHcUjjOG4DNE/values/\'' + group_name + '\'!A1:J50?key=AIzaSyAuP9yQn8g0bSay6r_RpGtpFeIbwprH1TU'); + req.open('GET', 'https://sheets.googleapis.com/v4/spreadsheets/' + ultimateconfig['score_sheet_id'] + '/values/\'' + group_name + '\'!A1:J50?key=' + ultimateconfig['api_key']); req.send(); } @@ -590,9 +694,10 @@ function showgroup(group_name) 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); + teams = filter_teams(teams, response); display_group_parsed(teams, games, group_name); + publish_group_rank(response, group_name); // Update the spreadsheet in the background. }); - publish_group_rank(group_name); // Update the spreadsheet in the background. } @@ -612,7 +717,7 @@ function showschedule(page) { let teams = []; let games = []; - let num_left = 3; + let num_left = 5; let cb = function(response, group_name) { teams = teams.concat(parse_teams_from_spreadsheet(response)); @@ -624,7 +729,9 @@ function showschedule(page) get_group('Group A', cb); get_group('Group B', cb); + get_group('Group C', cb); get_group('Playoffs', cb); + get_group('Playoffs 9th-13th', cb); }; function do_series(series) @@ -646,11 +753,12 @@ function showcarousel() let games_per_group = []; let combined_teams = []; let combined_games = []; - let num_left = 3; + let num_left = 5; let cb = function(response, group_name) { let teams = parse_teams_from_spreadsheet(response); let games = parse_games_from_spreadsheet(response, group_name, true); + teams = filter_teams(teams, response); teams_per_group[group_name] = teams; games_per_group[group_name] = games; @@ -661,6 +769,10 @@ function showcarousel() [ 13000, function() { display_group_parsed(teams_per_group['Group A'], games_per_group['Group A'], 'Group A'); } ], [ 2000, function() { hidetable(); } ], [ 13000, function() { display_group_parsed(teams_per_group['Group B'], games_per_group['Group B'], 'Group B'); } ], + [ 2000, function() { hidetable(); } ], + [ 13000, function() { display_group_parsed(teams_per_group['Group C'], games_per_group['Group C'], 'Group C'); } ], + [ 2000, function() { hidetable(); } ], + [ 13000, function() { display_group_parsed(teams_per_group['Playoffs 9th-13th'], games_per_group['Playoffs 9th-13th'], 'Playoffs 9th–13th'); } ], [ 2000, function() { hidetable(); } ] ]; let num_pages = find_num_pages(combined_games); @@ -675,6 +787,8 @@ function showcarousel() get_group('Group A', cb); get_group('Group B', cb); + get_group('Group C', cb); + get_group('Playoffs 9th-13th', cb); get_group('Playoffs', cb); }; @@ -710,3 +824,17 @@ function hidematch2() document.getElementById('scorebug2').style = css; document.getElementById('clockbug2').style = css; } + +function showmatch3() +{ + let css = "-webkit-animation: fade-in 1.0s ease; -webkit-animation-fill-mode: both;"; + document.getElementById('scorebug3').style = css; + document.getElementById('clockbug3').style = css; +} + +function hidematch3() +{ + let css = "-webkit-animation: fade-out 1.0s ease; -webkit-animation-fill-mode: both;"; + document.getElementById('scorebug3').style = css; + document.getElementById('clockbug3').style = css; +}