]> git.sesse.net Git - ultimatescore/blob - update_sheets.js
Fix the display of the points in each group.
[ultimatescore] / update_sheets.js
1 // Updates back to the Google spreadsheet.
2 // There's basically zero error handling here, but OK, missing some updates is fine, really.
3
4 let jwt_key = {
5         "type": "service_account",
6         "project_id": "solskogen-cubemap",
7         "private_key_id": "9eaf56bb4d6b688c3c73bd532fecdde943eea718",
8         "private_key": "-----BEGIN PRIVATE KEY-----\nMIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCPlcoZuj+tiaiw\nH5tjcZmCAFuCS3LhND+4WgA7BPvA3yrHvgm23T9aYVhhntA/uv2MSNjbZLj9Bk5z\nTfhSF6X6mr6JdtK05X5FXOiZdk8/36FT+aLANFqhyTD4WGXQVaHVjp1i6YNm9NvH\nUCt+R99VSteuvEyMQbqQtqTgTAisCmiO6bMssK90xKH9hwc5Zew/OUEaxa+ivgPR\nbgD3cTTQrPh0SKrZQFvdxx9ikGI/7rTZUazGU8r+VHBRCTMExCx1uQa2QzMd3REM\nngQCQ9TIkS0fNsE6NE8omrbbDJK1ob5tm4Jm6O7a7cN+yCel8sWoqW4fHdUb1z5n\n9IOUgHgFAgMBAAECggEAAWYJL2scghpvzbNf+JlbdO1a9tRvvIaK+cbvOBGqST7e\nqynVf2O0KqEh91MsMIq6O/Gl/fWns1fPBoc10zZOwcnugeb8LbcLZwlqtbtjo8wi\nV8sgn1kVfKDwjvT/LyuHgPI7mqbTxp7iGN36ZnnZLB9wkxjJKBe6YPznl8yROeoK\n4BdLaTWSv5w9mp1wnPG5RVsS5oAkoSFyDY0U7gAetsUjNf7bdlGtLvobw3kOpa7W\nm8WdN6jGbbyxmpe5Ql66/DhTBI4giNDDVvhf6fcRCOO+aAWNzZ5R6SgrdSYHbuBQ\npzGI7nBmBg3Nu1EYpm42wrUpoh6czy1Uf0F6VDmIkQKBgQDEh1Ado/QICWGQu6MB\nP0tX+APhN10x9Oq511Fd9SnTLZz2yzUN5Sshor+nevpes1Ljf8hS2FSV0fl0nRg3\nb7uMRt6EZNmzJJlPCQeBHNevVg5Z3kn3cpGE2cIr5JWB3r+EVd9wTOz7ihOVOboY\nt0yREiMeRuVmrwPi98hyoaNiHQKBgQC7CQzLGUHQDORBlQA0V6NFyamgrpkbSdML\nIZ3VThxbSxFGROC6W8At914F3XXTeP4f/kU1jjYOYhKpQy2RpOg6oLyCIx78sC/J\nkZS5eMeqv/hLSLt5eebAx0tVpDO++z/MWbbr/EpPMwQlSMMFlU2HqUK3XnAlSv9Q\njBxrs1sJCQKBgGy1GFi85vBHGCOx1rGK7EcllifOsws+GVRgyM47HT6FvYw5zQf5\nmokJeA/RE4qskI3skcdZiDgzJFQfzVRkxo4KaW08R7sy5GZ2bSM67Ac9h8SoE6v/\nQIUG2sPitdxXdQJjaau5sWBV+Q0TGGAxi/W23ZwSxTOuXWz/eG4IANL1AoGBAJdc\nmpLejMk/NZXxbGnvpn161yDnS5au5vEyMlYGUaJ8HK2+XhPS3rMUZm3erFUIrLfd\ngcr2nL6FFc8PQ5iDWUDhBc1XeONL/lBk1XRHz2Za1yit4rJLObg3ULstGIdtM1NA\nI23VDZoMkkVOHi2th0HLc+eLsLwtdnOMABAU5Q5pAoGBAKoZY3MflCEIj1S2hKQB\ncmz68DcwwXiwwuwE4zXoTWO95xApl7IP9ElNr1LFjYEhRp0VKyeZJ8UASKLN0nKF\ncD36qa71rd9VvKsNOiiKwbNy/E9WQ2B5rfovPbg2xSr8AQJxwZww2iv0zsP/Z+fG\nWYKJbvIPySmSrXhg9seBoSOL\n-----END PRIVATE KEY-----\n",
9         "client_email": "ultimate-nm-2018@solskogen-cubemap.iam.gserviceaccount.com",
10         "client_id": "102636658655884526659",
11         "auth_uri": "https://accounts.google.com/o/oauth2/auth",
12         "token_uri": "https://accounts.google.com/o/oauth2/token",
13         "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
14         "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/ultimate-nm-2018%40solskogen-cubemap.iam.gserviceaccount.com"
15 };
16
17 function post_data(url, contents, content_type, cb, auth) {
18         let req = new XMLHttpRequest();
19         req.onload = function(e) {
20                 cb(req.responseText);
21         };
22         req.open('POST', url);
23         req.setRequestHeader("Content-type", content_type);
24         if (auth !== undefined) {
25                 req.setRequestHeader("Authorization", "Bearer " + auth);
26         }
27         req.send(contents);
28 }
29
30 function post_json(url, json, cb, auth) {
31         post_data(url, JSON.stringify(json), "application/json;charset=UTF-8", cb, auth);
32 }
33
34 let current_oauth_access_token = null;
35 let oauth_expire = 0;
36
37 function update_oauth_key(cb) {
38         let now = Math.floor(new Date().getTime() / 1000);
39         let jwt = {
40                 "iss": jwt_key.client_email,
41                 "scope": "https://www.googleapis.com/auth/spreadsheets",
42                 "aud":"https://www.googleapis.com/oauth2/v4/token",
43                 "exp": now + 1800,
44                 "iat": now,
45         };
46         let sJWS = KJUR.jws.JWS.sign(null, {"alg": "RS256"}, jwt, jwt_key.private_key);
47         post_data('https://www.googleapis.com/oauth2/v4/token',
48                 "grant_type=urn%3Aietf%3Aparams%3Aoauth%3Agrant-type%3Ajwt-bearer&assertion=" + sJWS,
49                 "application/x-www-form-urlencoded",
50                 function(response) {
51                         current_oauth_access_token = JSON.parse(response)['access_token'];
52                         console.log("Got new OAuth key.");
53                         oauth_expire = now + 1800;
54                         if (cb !== undefined) { cb(); }
55                 });
56 }
57
58 function possibly_update_oauth_key(cb) {
59         let now = Math.floor(new Date().getTime() / 1000);
60         if (oauth_expire - now < 60) {
61                 console.log("Getting new OAuth key...");
62                 update_oauth_key(cb);
63         } else {
64                 cb();
65         }
66 }
67
68 function publish_group_rank(teams, games, group_name)
69 {
70         let updates = [];
71         let config = ultimateconfig['group_cells'][group_name];
72         let cols = config['score_sheet_cols'];
73
74         apply_games_to_teams(games, teams, group_name);
75         teams.sort(function(a, b) { return a.seeding - b.seeding });
76
77         // Write the points total to the unsorted columns.
78         if (config['point_total_start_row'] !== null) {
79                 for (let i = 0; i < teams.length; ++i) {
80                         let row = config['point_total_start_row'] + i;
81                         updates.push({ "range": cols[2] + row, "values": [ [ teams[i].pts ] ] });
82                 }
83         }
84
85         let tiebreakers = [];
86         teams = rank(games, teams, 1, tiebreakers);
87
88         // Write the ranking table, from scratch.
89         for (let i = 0; i < teams.length; ++i) {
90                 let row = config['ranking_list_start_row'] + i;
91                 updates.push({ "range": cols[0] + row, "values": [ [ teams[i].rank + config['rank_offset'] - 1] ] });
92                 updates.push({ "range": cols[1] + row, "values": [ [ teams[i].mediumname ] ] });
93                 updates.push({ "range": cols[2] + row, "values": [ [ teams[i].pts ] ] });
94         }
95
96         let tb_str = "";
97         if (tiebreakers.length != 0) {
98                 tb_str = tiebreakers.join("\n");
99         }
100         updates.push({ "range": config['ranking_list_explain_cell'], "values": [ [ tb_str ] ]});
101
102         let json = {
103                 "valueInputOption": "USER_ENTERED",
104                 "data": updates 
105         };
106         possibly_update_oauth_key(function() {
107                 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);
108         });
109 }
110
111 function montecarlo(all_teams, groups, games, groups_to_calc) {
112         let pseudo_group_names = ['X', 'Y', 'Z'];
113         let real_group_names = ['A', 'B', 'C'];  // Better be corresponding to groups_to_calc...
114         let teams_to_idx = [];
115
116         let third_groups = [];
117         let busted_thirds = false;
118
119         // Split teams by group.
120         let teams = [];
121         for (let group_idx = 0; group_idx < groups_to_calc.length; ++group_idx) {
122                 let teams_group = filter_teams_by_group(all_teams, groups, groups_to_calc[group_idx]);
123                 teams.push(teams_group);
124                 teams_to_idx.push(make_teams_to_idx(teams_group));
125         }
126
127         for (let simulation_idx = 0; simulation_idx < 100; ++simulation_idx) {  // 100 seems to be enough.
128                 let thirds = [], ignoreds = [];
129                 let calc_groups = [];
130
131                 for (let group_idx = 0; group_idx < groups_to_calc.length; ++group_idx) {
132                         let teams_copy = [];
133                         for (const team of teams[group_idx]) {
134                                 teams_copy.push(Object.assign({}, team));
135                         }
136
137                         // Fill in random results. We deliberately use a uniform [-13,+13]
138                         // model here, since we are interested in the extremal results.
139                         // Of course, not all real games go to 13, but the risk of that
140                         // influencing the tiebreakers is very slim.
141                         let games_copy = [], games_with_synth = [];
142                         for (const game of games) {
143                                 games_copy.push(Object.assign({}, game));
144                         }
145
146                         for (let i = 0; i < games_copy.length; ++i) {
147                                 let idx1 = teams_to_idx[group_idx][games_copy[i].name1];
148                                 let idx2 = teams_to_idx[group_idx][games_copy[i].name2];
149                                 if (idx1 === undefined || idx2 === undefined) continue;
150                                 if (games_copy[i].score1 === undefined || games_copy[i].score2 === undefined ||
151                                     isNaN(games_copy[i].score1) || isNaN(games_copy[i].score2) ||
152                                     games_copy[i].score1 == games_copy[i].score2) {
153                                         // These were skipped by apply_games_to_teams() earlier.
154                                         let score1 = 0, score2 = 0;
155                                         let r = Math.floor(Math.random() * 26);
156                                         if (r < 13) {
157                                                 score1 = 13;
158                                                 score2 = r;
159                                                 teams_copy[idx1].pts += 2;
160                                         } else {
161                                                 score1 = r - 13;
162                                                 score2 = 13;
163                                                 teams_copy[idx2].pts += 2;
164                                         }
165                                         games_copy[i].score1 = score1;
166                                         games_copy[i].score2 = score2;
167                                         ++teams_copy[idx1].nplayed;
168                                         ++teams_copy[idx2].nplayed;
169                                         teams_copy[idx1].goals += score1;
170                                         teams_copy[idx2].goals += score2;
171                                         teams_copy[idx1].gd += score1;
172                                         teams_copy[idx2].gd += score2;
173                                         teams_copy[idx1].gd -= score2;
174                                         teams_copy[idx2].gd -= score1;
175
176                                         games_with_synth.push({
177                                                 "name1": games_copy[i].name1,
178                                                 "name2": games_copy[i].name2,
179                                                 "score1": score1,
180                                                 "score2": score2
181                                         });
182                                 } else {
183                                         games_with_synth.push(games_copy[i]);
184                                         continue;
185                                 }
186                         }
187                         
188                         // Now rank according to the simulation.
189                         let tiebreakers = [];
190                         teams_copy = rank(games_copy, teams_copy, 1, tiebreakers);
191
192                         // See if we have conflicting information with other simulations.
193                         if (simulation_idx == 0) {
194                                 for (let i = 0; i < teams[group_idx].length; ++i) {
195                                         let idx = teams_to_idx[group_idx][teams_copy[i].name];
196                                         teams[group_idx][idx].simulated_rank = teams_copy[i].rank;
197                                 }
198                         } else {
199                                 for (let i = 0; i < teams[group_idx].length; ++i) {
200                                         let idx = teams_to_idx[group_idx][teams_copy[i].name];
201                                         if (teams[group_idx][idx].simulated_rank !== teams_copy[i].rank) {
202                                                 teams[group_idx][idx].simulated_rank = null;
203                                         }
204                                 }
205                         }
206
207                         if (!busted_thirds) {
208                                 let any_third_found = false;
209                                 for (let i = 0; i < teams[group_idx].length; ++i) {
210                                         // Store the third.
211                                         if (i == 2 || teams_copy[i].rank == 3) {
212                                                 if (any_third_found) {
213                                                         busted_thirds = true;
214                                                 } else {
215                                                         teams_copy[i].group_idx = group_idx;
216                                                         thirds.push(teams_copy[i]);
217                                                         any_third_found = true;
218                                                 }
219                                         }
220                                 }
221                                 if (ultimateconfig['kick_fifth_from_third'] && teams_copy.length >= 5) {
222                                         if (teams_copy[4].rank != 5) {
223                                                 // A real tie for fifth; the rules are unclear, so just give up.
224                                                 busted_thirds = true;
225                                         } else {
226                                                 ignoreds.push(teams_copy[4]);
227                                         }
228                                 }
229                         }
230
231                         calc_groups.push({
232                                 "games": games_with_synth,
233                                 "teams": teams_copy
234                         });
235                 }
236
237                 // Also rank thirds.
238                 if (!busted_thirds) {
239                         let tiebreakers = [];
240                         if (ultimateconfig['kick_fifth_from_third']) {
241                                 // Recompute scores (but not ranks!) without the ignored games. (thirds point to these objects.)
242                                 for (let group_idx = 0; group_idx < groups_to_calc.length; ++group_idx) {
243                                         apply_games_to_teams(calc_groups[group_idx].games, calc_groups[group_idx].teams, groups_to_calc[group_idx], ignoreds);
244                                 }
245                         }
246                         let ranked = rank_thirds([], thirds, 1, tiebreakers);
247                         if (simulation_idx == 0) {
248                                 third_groups = ranked;
249                         } else {
250                                 for (let i = 0; i < groups_to_calc.length; ++i) {
251                                         if (third_groups[i].group_idx !== ranked[i].group_idx) {
252                                                 third_groups[i].group_idx = null;
253                                         }
254                                 }
255                         }
256                 }
257         }
258
259         let replacements = [];
260         for (let group_idx = 0; group_idx < groups_to_calc.length; ++group_idx) {
261                 if (third_groups[group_idx].group_idx !== null) {
262                         replacements.push([ pseudo_group_names[group_idx], real_group_names[third_groups[group_idx].group_idx] ]);
263                 }
264         }
265
266         // These are pretty hard-coded, but that's probably fine. Must come after we've concretized X, Y, etc.
267         for (const group_name of real_group_names) {
268                 let teams = filter_teams_by_group(all_teams, groups, 'Group ' + group_name);
269                 if (teams.length >= 5) {
270                         for (const other_group_name of real_group_names) {
271                                 replacements.push([ group_name + other_group_name + '5', group_name + '5' ]);
272                         }
273                         for (const other_group_name of pseudo_group_names) {
274                                 replacements.push([ group_name + other_group_name + '5', group_name + '5' ]);
275                         }
276                 } else {  // Perhaps a bit overkill.
277                         for (const other_group_name of real_group_names) {
278                                 replacements.push([ group_name + other_group_name + '5', other_group_name + '5' ]);
279                         }
280                         for (const other_group_name of pseudo_group_names) {
281                                 replacements.push([ group_name + other_group_name + '5', other_group_name + '5' ]);
282                         }
283                 }
284         }
285
286         for (let group_idx = 0; group_idx < groups_to_calc.length; ++group_idx) {
287                 for (let i = 0; i < teams[group_idx].length; ++i) {
288                         if (teams[group_idx][i].simulated_rank !== null) {
289                                 replacements.push([ real_group_names[group_idx] + teams[group_idx][i].simulated_rank, teams[group_idx][i].shortname ]);
290                         }
291                 }
292         }
293
294         return replacements;
295 }
296
297 function names_for_team(team, expansions) {
298         if (expansions.hasOwnProperty(team)) {
299                 return expansions[team];
300         }
301         let longteam = team.replace("W ", "Win. ").replace("L ", "Los. ");
302         return [ longteam, longteam, team ];
303 }
304
305 function expand_mediumname_if_single_team(team, expansions) {
306         if (expansions.hasOwnProperty(team)) {
307                 return expansions[team][1];
308         }
309         return team;
310 }
311
312 function do_replacements(str, replacements) {
313         for (const r of replacements) {
314                 str = str.replace(r[0], r[1]);
315         }
316         return str;
317 }
318
319 function fill_playoff(all_teams, groups, replacements, teams) {
320         let team_expansions = {};
321         for (const team of all_teams) {
322                 team_expansions[team.name] = team_expansions[team.mediumname] = team_expansions[team.shortname] =
323                         [ team.name, team.mediumname, team.shortname ];
324         }
325
326         let games = ultimateconfig['playoff_games'];
327         get_results('Results', function(response) {
328                 let updates = [], meta_updates = [];
329                 for (const game of games) {
330                         let team1 = do_replacements(game[0], replacements);
331                         let team2 = do_replacements(game[1], replacements);
332                         let team1_mediumname = expand_mediumname_if_single_team(team1, team_expansions);
333                         let team2_mediumname = expand_mediumname_if_single_team(team2, team_expansions);
334                         let row = ultimateconfig['playoff_games_start_row'] + game[3];
335                         let cols = ultimateconfig['playoff_games_cols'][game[2]];
336                         let cell_team1 = "Results!" + String.fromCharCode(cols[0] + 65) + row;
337                         let cell_score1 = "Results!" + String.fromCharCode(cols[1] + 65) + row;
338                         let cell_score2 = "Results!" + String.fromCharCode(cols[2] + 65) + row;
339                         let cell_team2 = "Results!" + String.fromCharCode(cols[3] + 65) + row;
340                         updates.push({ "range": cell_team1, "values": [ [ team1_mediumname ] ] });
341                         updates.push({ "range": cell_team2, "values": [ [ team2_mediumname ] ] });
342
343                         let score1 = response['values'][row - 1][cols[1]];
344                         let score2 = response['values'][row - 1][cols[2]];
345                         let game_name = game[4];
346                         let game_name2 = game_name.replace("Semi", "semi");
347                         let game_day = game[7];
348                         if (game_day === undefined) {
349                                 game_day = 7;  // Sunday.
350                         }
351
352                         let range = {
353                                 "sheetId": ultimateconfig['score_sheet_index'],
354                                 "startColumnIndex": cols[1],
355                                 "endColumnIndex": cols[2] + 1,
356                                 "startRowIndex": row - 1,
357                                 "endRowIndex": row
358                         };
359
360                         if (parseInt(score1) >= 0 && parseInt(score2) >= 0 && score1 != score2) {
361                                 if (parseInt(score1) > parseInt(score2)) {
362                                         replacements.unshift(["W " + game_name, team1]);
363                                         replacements.unshift(["L " + game_name, team2]);
364                                         replacements.unshift(["W " + game_name2, team1]);
365                                         replacements.unshift(["L " + game_name2, team2]);
366                                 } else {
367                                         replacements.unshift(["W " + game_name, team2]);
368                                         replacements.unshift(["L " + game_name, team1]);
369                                         replacements.unshift(["W " + game_name2, team2]);
370                                         replacements.unshift(["L " + game_name2, team1]);
371                                 }
372                                 meta_updates.push({ "unmergeCells": { "range": range }});
373                         } else if (game[5]) {
374                                 // No score yet, so write the name of the game (e.g. “L-semi 1”)
375                                 // where the score would normally be, to mark what this game is called.
376                                 // This is useful with the limited space on the tablet.
377                                 score1 = score2 = "";
378                                 updates.push({ "range": cell_score1, "values": [ [ game[4] ] ] });
379                                 meta_updates.push({ "mergeCells": { "range": range, "mergeType": "MERGE_ALL" }});
380                         }
381                 }
382                 let json = {
383                         "valueInputOption": "USER_ENTERED",
384                         "data": updates 
385                 };
386                 let meta_json = {
387                         "requests": meta_updates
388                 };
389                 possibly_update_oauth_key(function() {
390                         if (updates.length > 0) {
391                                 post_json('https://sheets.googleapis.com/v4/spreadsheets/' + ultimateconfig['score_sheet_id'] + '/values:batchUpdate?key=' + ultimateconfig['api_key'], json, function(response) {
392                                         get_all_group_games(all_teams, groups, function(group_games) {
393                                                 // NOTE: filter_teams_by_group will be delayed by one cycle
394                                                 // after W P1 etc. becomes determined for the first time.
395                                                 let teams_l1 = filter_teams_by_group(all_teams, groups, 'Playoffs 9th–11th');
396                                                 let teams_l2 = filter_teams_by_group(all_teams, groups, 'Playoffs 12th–14th');
397                                                 publish_group_rank(teams_l1, group_games, 'Playoffs 9th–11th');
398                                                 publish_group_rank(teams_l2, group_games, 'Playoffs 12th–14th');
399                                         });
400                                 }, current_oauth_access_token);
401                         }
402                         if (meta_updates.length > 0) {
403                                 post_json('https://sheets.googleapis.com/v4/spreadsheets/' + ultimateconfig['score_sheet_id'] + ':batchUpdate?key=' + ultimateconfig['api_key'], meta_json, function(response) {}, current_oauth_access_token);
404                         }
405                 });
406         });
407 }
408
409 function get_results(sheet_name, cb)
410 {
411         let req = new XMLHttpRequest();
412         req.onload = function(e) {
413                 cb(JSON.parse(req.responseText), sheet_name);
414         };
415         req.open('GET', 'https://sheets.googleapis.com/v4/spreadsheets/' + ultimateconfig['score_sheet_id'] + '/values/\'' + sheet_name + '\'!A1:Q50?key=' + ultimateconfig['api_key']);
416         req.send();
417 }
418
419 function publish_group_ranks() {
420         get_teams(function(teams) {
421                 get_groups(function(groups) {
422                         get_all_group_games(teams, groups, function(games) {
423                                 let teams_a = filter_teams_by_group(teams, groups, 'Group A');
424                                 let teams_b = filter_teams_by_group(teams, groups, 'Group B');
425                                 let teams_c = filter_teams_by_group(teams, groups, 'Group C');
426                                 publish_group_rank(teams_a, games, 'Group A');
427                                 publish_group_rank(teams_b, games, 'Group B');
428                                 publish_group_rank(teams_c, games, 'Group C');
429
430                                 let replacements = montecarlo(teams, groups, games, ['Group A', 'Group B', 'Group C']);
431                                 fill_playoff(teams, groups, replacements, [teams_a, teams_b, teams_c]);
432                         });
433                 });
434         });
435 }
436
437 function get_all_playoff_games(teams, groups, group_games, cb) {
438         let replacements = montecarlo(teams, groups, group_games, ['Group A', 'Group B', 'Group C']);
439         let games = ultimateconfig['playoff_games'];
440         get_results('Results', function(response) {
441                 let playoff_games = [];
442                 for (const game of games) {
443                         let team1 = do_replacements(game[0], replacements);
444                         let team2 = do_replacements(game[1], replacements);
445                         let row = ultimateconfig['playoff_games_start_row'] + game[3];
446                         let cols = ultimateconfig['playoff_games_cols'][game[2]];
447                         let score1 = response['values'][row - 1][cols[1]];
448                         let score2 = response['values'][row - 1][cols[2]];
449                         let streamday = game[7];
450                         if (streamday === undefined && game[2] === 0) {  // Stream field is by default on stream.
451                                 streamday = 7;
452                         }
453                         playoff_games.push({
454                                 "name1": team1,
455                                 "name2": team2,
456                                 "score1": parseInt(score1),
457                                 "score2": parseInt(score2),
458                                 "streamday": streamday,
459                                 "streamtime": response['values'][row - 1][1].replace('.', ':'),
460                                 "group_name": game[6]
461                         });
462                 }
463                 cb(playoff_games);
464         });
465 }
466
467 function get_ranked(teams, games, group_name) {
468         apply_games_to_teams(games, teams, group_name);
469         let tiebreakers = [];
470         teams = rank(games, teams, 1, tiebreakers);
471         return teams;
472 }
473
474 // Pick out everything that is at rank N _or_ avoids rank N by lack of tiebreakers only.
475 function pick_out_rank(teams, rank, candidates) {
476         if (teams.length < rank) {
477                 return;
478         }
479
480         let lowest_rank = teams[rank - 1].rank;
481
482         let count = 0;
483         for (const team of teams) {
484                 if (team.rank >= lowest_rank && team.rank <= rank) {
485                         ++count;
486                 }
487         }
488
489         if (count >= teams.length / 2) {
490                 // We have no info yet, ignore this group.
491                 return;
492         }
493
494         for (const team of teams) {
495                 if (team.rank >= lowest_rank && team.rank <= rank) {
496                         candidates.push(team);
497                 }
498         }
499 }
500
501 function addsign(x)
502 {
503         if (x < 0) {
504                 return "−" + (-x);
505         } else if (x == 0) {
506                 return "=0";
507         } else {
508                 return "+" + x;
509         }
510 }
511
512 function publish_best_thirds() {
513         if (!ultimateconfig['best_thirds']) return;
514         get_teams(function(teams) {
515                 get_groups(function(groups) {
516                         get_all_group_games(teams, groups, function(games) {
517                                 let teams_a = filter_teams_by_group(teams, groups, 'Group A');
518                                 let teams_b = filter_teams_by_group(teams, groups, 'Group B');
519                                 let teams_c = filter_teams_by_group(teams, groups, 'Group C');
520                                 let A = get_ranked(teams_a, games, 'Group A');
521                                 let B = get_ranked(teams_b, games, 'Group B');
522                                 let C = get_ranked(teams_c, games, 'Group C');
523
524                                 let candidates = [];
525                                 pick_out_rank(A, 3, candidates);
526                                 pick_out_rank(B, 3, candidates);
527                                 pick_out_rank(C, 3, candidates);
528
529                                 let ignoreds = [];
530                                 let ignored_games = [], ignored_games_expl = [];
531                                 if (ultimateconfig['kick_fifth_from_third']) {
532                                         let ignoreds_A = [], ignoreds_B = [], ignoreds_C = [];
533                                         pick_out_rank(A, 5, ignoreds_A);
534                                         pick_out_rank(B, 5, ignoreds_B);
535                                         pick_out_rank(C, 5, ignoreds_C);
536
537                                         if (ignoreds_A.length >= 2) {
538                                                 ignoreds_A = [ ignoreds_A[ignoreds_A.length - 1] ];
539                                         }
540                                         if (ignoreds_B.length >= 2) {
541                                                 ignoreds_B = [ ignoreds_B[ignoreds_B.length - 1] ];
542                                         }
543                                         if (ignoreds_C.length >= 2) {
544                                                 ignoreds_C = [ ignoreds_C[ignoreds_C.length - 1] ];
545                                         }
546                                         ignoreds = ignoreds_A.concat(ignoreds_B).concat(ignoreds_C);
547
548                                         // Protect the “candidates” array, so that apply_games_to_teams() further down
549                                         // doesn't modify it (we want to compare old and new).
550                                         A = jsonclone(A);
551                                         B = jsonclone(B);
552                                         C = jsonclone(C);
553
554                                         // Recompute scores (but not ranks!) without the ignored games.
555                                         apply_games_to_teams(games, A, 'Group A', ignoreds, ignored_games);
556                                         apply_games_to_teams(games, B, 'Group B', ignoreds, ignored_games);
557                                         apply_games_to_teams(games, C, 'Group C', ignoreds, ignored_games);
558
559                                         // Filter out ignored games involving the candidate thirds.
560                                         let candidates_to_idx = make_teams_to_idx(candidates);
561                                         for (const game of ignored_games) {
562                                                 if (candidates_to_idx[game[0]] !== undefined ||
563                                                     candidates_to_idx[game[1]] !== undefined) {
564                                                         if (game[2]) {
565                                                                 ignored_games_expl.push("Ignoring (arbitrarily) " + game[0] + "–" + game[1]);
566                                                         } else {
567                                                                 ignored_games_expl.push("Ignoring " + game[0] + "–" + game[1]);
568                                                         }
569                                                 }
570                                         }
571
572                                         let new_teams = A.concat(B).concat(C);
573                                         let new_teams_to_idx = make_teams_to_idx(new_teams);
574
575                                         // Move back the scores (points, gd, goals).
576                                         for (let cand of candidates) {
577                                                 let new_version = new_teams[new_teams_to_idx[cand.shortname]];
578                                                 if (cand.pts != new_version.pts ||
579                                                     cand.gd != new_version.gd ||
580                                                     cand.goals != new_version.goals) {
581                                                         cand.pts = new_version.pts;
582                                                         cand.gd = new_version.gd;
583                                                         cand.goals = new_version.goals;
584                                                         ignored_games_expl.push(cand.shortname + " at " + cand.pts + " pts, " + addsign(new_version.gd) + " GD");
585                                                 }
586                                         }
587                                 }
588
589                                 let tiebreakers = [];
590                                 let text = "";
591                                 if (candidates.length >= 2) {
592                                         let ranked = rank_thirds([], candidates, 1, tiebreakers);
593                                         let best_thirds = ranked.filter(function(team) { return team.rank <= 2; });
594                                         if (best_thirds.length == 2) {
595                                                 text = "Best thirds: " + best_thirds.map(function(team) { return team.mediumname }).join(', ') + "\n";
596                                                 if (ignored_games_expl.length > 0) {
597                                                         text += ignored_games_expl.join("; ") + "\n";
598                                                 }
599                                                 text += tiebreakers.join("\n");
600                                         }
601                                 }
602                                 let updates = [];
603                                 updates.push({ "range": ultimateconfig['explain_third_cell'], "values": [ [ text ] ] });
604                                 let json = {
605                                         "valueInputOption": "USER_ENTERED",
606                                         "data": updates 
607                                 };
608                                 possibly_update_oauth_key(function() {
609                                         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);
610                                 });
611                         });
612                 });
613         });
614 }
615
616 update_oauth_key();
617 setTimeout(function() {
618         publish_group_ranks();
619         publish_best_thirds();
620         setInterval(function() { publish_group_ranks(); publish_best_thirds(); }, 60000);
621 }, 5000);