]> git.sesse.net Git - ultimatescore/blobdiff - carousel.js
Implement correct tiebreak rules for #4 and #6.
[ultimatescore] / carousel.js
index 841ae6824964528aef09ce187766fadaf495719f..4d8e2ec27ec4b04329777ba10570494fcc0f3128 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,20 @@ function rank(games, teams, start_rank, tiebreakers) {
        }
 
        // Rule #1: Head-to-head wins.
-       let beat_parts = partition_by_beat(games, teams);
+       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;
+                               }
+                               if (games[i].score1 < games[i].score2) {
+                                       beat[idx2][idx1] = 1;
+                               }
+                       }
+               }
+       });
        if (beat_parts.length > 1) {
                tiebreakers.push(explain_tiebreaker(beat_parts, 'head-to-head'));
                return subrank_partitions(games, beat_parts, start_rank, tiebreakers);
@@ -206,10 +208,45 @@ function rank(games, teams, start_rank, tiebreakers) {
                return subrank_partitions(games, h2h_gd_parts, start_rank, tiebreakers);
        }
 
-       // Rule #4: Global goal difference. (Well, not strictly, but good enough.)
-       let gd_parts = partition(teams, function(a, b) { return b.gd - a.gd });
+       // Rule #4: Goal difference against common opponents.
+       var results = {};
+       for (let i = 0; i < games.length; ++i) {
+               if (results[games[i].name1] === undefined) {
+                       results[games[i].name1] = {};
+               }
+               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];
+                                       }
+                               }
+
+                               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);
        }
 
@@ -220,10 +257,34 @@ function rank(games, teams, start_rank, tiebreakers) {
                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);
        }