]> git.sesse.net Git - ultimatescore/commitdiff
Calculate goal difference and points locally instead of relying on the cloud.
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Thu, 19 Oct 2017 16:33:01 +0000 (18:33 +0200)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Thu, 19 Oct 2017 16:33:01 +0000 (18:33 +0200)
carousel.js

index 8f57ff3376fb26cf6259d559c303b0eed494a440..5a56039c9bfb7fc5591dd938081561c5a7ffcdbf 100644 (file)
@@ -173,7 +173,6 @@ rank = function(games, teams, start_rank, tiebreakers) {
        for (var i = 0; i < teams.length; i++) {
                teams[i].h2h_gd = 0;
                teams[i].h2h_goals = 0;
-               teams[i].goals = 0;
        }
        for (i = 0; i < games.length; ++i) {
                var idx1 = teams_to_idx[games[i].name1];
@@ -187,12 +186,6 @@ rank = function(games, teams, start_rank, tiebreakers) {
                        teams[idx1].h2h_goals += games[i].score1;
                        teams[idx2].h2h_goals += games[i].score2;
                }
-               if (idx1 !== undefined) {
-                       teams[idx1].goals += games[i].score1;
-               }
-               if (idx2 !== undefined) {
-                       teams[idx2].goals += games[i].score2;
-               }
        }
        var h2h_gd_parts = partition(teams, function(a, b) { return b.h2h_gd - a.h2h_gd });
        if (h2h_gd_parts.length > 1) {
@@ -240,9 +233,10 @@ req.onload = function(e) {
                teams.push({
                        "name": response.values[i][0],
                        "shortname": response.values[i][1],
-                       "nplayed": parseInt(response.values[i][2]),
-                       "gd": parseInt(response.values[i][3].replace(/−/, '-')),
-                       "pts": parseInt(response.values[i][4])
+                       "nplayed": 0,
+                       "gd": 0,
+                       "pts": 0,
+                       "goals": 0
                });
        }
        var games = [];
@@ -258,6 +252,30 @@ req.onload = function(e) {
        }
        console.log(games);
 
+       var teams_to_idx = make_teams_to_idx(teams);
+       for (i = 0; i < games.length; ++i) {
+               var idx1 = teams_to_idx[games[i].name1];
+               var idx2 = teams_to_idx[games[i].name2];
+               if (games[i].score1 === undefined || games[i].score2 === undefined ||
+                   idx1 === undefined || idx2 === undefined ||
+                   games[i].score1 == games[i].score2) {
+                       continue;
+               }
+               ++teams[idx1].nplayed;
+               ++teams[idx2].nplayed;
+               teams[idx1].goals += games[i].score1;
+               teams[idx2].goals += games[i].score2;
+               teams[idx1].gd += games[i].score1;
+               teams[idx2].gd += games[i].score2;
+               teams[idx1].gd -= games[i].score2;
+               teams[idx2].gd -= games[i].score1;
+               if (games[i].score1 > games[i].score2) {
+                       teams[idx1].pts += 2;
+               } else {
+                       teams[idx2].pts += 2;
+               }
+       }
+
        tiebreakers = [];
        teams = rank(games, teams, 1, tiebreakers);
        console.log(tiebreakers. join(", "));