]> git.sesse.net Git - ultimatescore/blobdiff - carousel.js
Fix tally for SBS.
[ultimatescore] / carousel.js
index 841ae6824964528aef09ce187766fadaf495719f..ac48db40c81cff50b5eb96ad8c1f08b727042eac 100644 (file)
@@ -73,7 +73,7 @@ function make_teams_to_idx(teams)
        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 +85,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 +124,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 +154,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 +184,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[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[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);
        }