]> git.sesse.net Git - ultimatescore/blobdiff - update_sheets.js
Add a third clock.
[ultimatescore] / update_sheets.js
index 2115b8ce24ac2489fa0bca2d6e46c2ddf10dbd55..641c94db10e41597d28eb5af48f65e4f05ed2f36 100644 (file)
@@ -88,7 +88,7 @@ function publish_group_rank(group_name)
                for (let i = 0; i < teams.length; ++i) {
                        let row = ultimateconfig['ranking_list_start_row'] + i;
                        updates.push({ "range": cols[0] + row, "values": [ [ teams[i].rank ] ] });
-                       updates.push({ "range": cols[1] + row, "values": [ [ teams[i].shortname ] ] });
+                       updates.push({ "range": cols[1] + row, "values": [ [ teams[i].mediumname ] ] });
                        updates.push({ "range": cols[2] + row, "values": [ [ teams[i].pts ] ] });
                }
 
@@ -112,10 +112,77 @@ function publish_group_rank(group_name)
 function publish_group_ranks() {
        publish_group_rank('Group A');
        publish_group_rank('Group B');
+       publish_group_rank('Group C');
+}
+
+function get_ranked(response, group_name) {
+       let teams = parse_teams_from_spreadsheet(response);
+       let games = parse_games_from_spreadsheet(response, group_name, false);
+       apply_games_to_teams(games, teams);
+       let tiebreakers = [];
+       teams = rank(games, teams, 1, tiebreakers);
+       return teams;
+}
+
+// Pick out everything that is at rank N _or_ avoids rank N by lack of tiebreakers only.
+function pick_out_rank(teams, rank, candidates) {
+       let lowest_rank = teams[rank - 1].rank;
+
+       let count = 0;
+       for (const team of teams) {
+               if (team.rank >= lowest_rank && team.rank <= rank) {
+                       ++count;
+               }
+       }
+
+       if (count >= teams.length / 2) {
+               // We have no info yet, ignore this group.
+               return;
+       }
+
+       for (const team of teams) {
+               if (team.rank >= lowest_rank && team.rank <= rank) {
+                       candidates.push(team);
+               }
+       }
+}
+
+function publish_best_thirds() {
+       get_group('Group A', function(response_a) {
+               get_group('Group B', function(response_b) {
+                       get_group('Group C', function(response_c) {
+                               let A = get_ranked(response_a, 'Group A');
+                               let B = get_ranked(response_b, 'Group B');
+                               let C = get_ranked(response_c, 'Group C');
+
+                               let candidates = [];
+                               pick_out_rank(A, 3, candidates);
+                               pick_out_rank(B, 3, candidates);
+                               pick_out_rank(C, 3, candidates);
+
+                               let tiebreakers = [];
+                               let text = "";
+                               if (candidates.length >= 2) {
+                                       let ranked = rank_thirds([], candidates, 1, tiebreakers);
+                                       text = "Best thirds: " + ranked[0].mediumname + ", " + ranked[1].mediumname + "\n" + tiebreakers.join("\n");
+                               }
+                               let updates = [];
+                               updates.push({ "range": ultimateconfig['explain_third_cell'], "values": [ [ text ] ] });
+                               let json = {
+                                       "valueInputOption": "USER_ENTERED",
+                                       "data": updates 
+                               };
+                               possibly_update_oauth_key(function() {
+                                       post_json('https://sheets.googleapis.com/v4/spreadsheets/' + ultimateconfig['score_sheet_id'] + '/values:batchUpdate?key=' + ultimateconfig['api_key'], json, function(response) {}, current_oauth_access_token);
+                               });
+                       });
+               });
+       });
 }
 
 update_oauth_key();
 setTimeout(function() {
        publish_group_ranks();
-       setInterval(function() { publish_group_ranks(); }, 60000);
+       publish_best_thirds();
+       setInterval(function() { publish_group_ranks(); publish_best_thirds(); }, 60000);
 }, 5000);