X-Git-Url: https://git.sesse.net/?p=ultimatescore;a=blobdiff_plain;f=carousel.js;h=ac48db40c81cff50b5eb96ad8c1f08b727042eac;hp=fcf5cc5178862d82dc88ebfbd1db9e47686e0c0c;hb=6639e908e5a74d691a93edf1a16217911f4e4eb4;hpb=e6f6d97706538a174b9f3e0539ed68ca4d28cc71 diff --git a/carousel.js b/carousel.js index fcf5cc5..ac48db4 100644 --- a/carousel.js +++ b/carousel.js @@ -154,6 +154,7 @@ function rank(games, teams, start_rank, tiebreakers) { } // Rule #1: Head-to-head wins. + 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]; @@ -161,9 +162,10 @@ function rank(games, teams, start_rank, tiebreakers) { if (idx1 !== undefined && idx2 !== undefined) { if (games[i].score1 > games[i].score2) { beat[idx1][idx2] = 1; - } - if (games[i].score1 < games[i].score2) { + ++num_relevant_games; + } else if (games[i].score1 < games[i].score2) { beat[idx2][idx1] = 1; + ++num_relevant_games; } } } @@ -182,30 +184,32 @@ 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; - } - 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 (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); } - } - 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. @@ -250,11 +254,13 @@ function rank(games, teams, start_rank, tiebreakers) { 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: Goals scored against common opponents.