X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=update_sheets.js;h=f96b223a83054a4b732369c1cac52ed0f90c07dd;hb=62acd54d7e0d27c056de5e7a106be803c3dc2f4e;hp=b03c48dbe4ef224b0ad1907069fac35a0f53afd7;hpb=64c11ffc84930a031dffd586d403d7a1faf7d9cb;p=ultimatescore diff --git a/update_sheets.js b/update_sheets.js index b03c48d..f96b223 100644 --- a/update_sheets.js +++ b/update_sheets.js @@ -68,16 +68,20 @@ function possibly_update_oauth_key(cb) { function publish_group_rank(response, group_name) { let updates = []; - let cols = ultimateconfig['score_sheet_cols'][group_name]; + let config = ultimateconfig['group_cells'][group_name]; + let cols = config['score_sheet_cols']; let teams = parse_teams_from_spreadsheet(response); let games = parse_games_from_spreadsheet(response, group_name, false); apply_games_to_teams(games, teams); + teams = filter_teams(teams, response); // Write the points total to the unsorted columns. - for (let i = 0; i < teams.length; ++i) { - let row = ultimateconfig['point_total_start_row'] + i; - updates.push({ "range": cols[2] + row, "values": [ [ teams[i].pts ] ] }); + if (config['point_total_start_row'] !== null) { + for (let i = 0; i < teams.length; ++i) { + let row = config['point_total_start_row'] + i; + updates.push({ "range": cols[2] + row, "values": [ [ teams[i].pts ] ] }); + } } let tiebreakers = []; @@ -85,7 +89,7 @@ function publish_group_rank(response, group_name) // Write the ranking table, from scratch. for (let i = 0; i < teams.length; ++i) { - let row = ultimateconfig['ranking_list_start_row'] + i; + let row = config['ranking_list_start_row'] + i; updates.push({ "range": cols[0] + row, "values": [ [ teams[i].rank ] ] }); updates.push({ "range": cols[1] + row, "values": [ [ teams[i].mediumname ] ] }); updates.push({ "range": cols[2] + row, "values": [ [ teams[i].pts ] ] }); @@ -95,7 +99,7 @@ function publish_group_rank(response, group_name) if (tiebreakers.length != 0) { tb_str = tiebreakers.join("\n"); } - updates.push({ "range": cols[0] + ultimateconfig['ranking_list_explain_row'], "values": [ [ tb_str ] ]}); + updates.push({ "range": config['ranking_list_explain_cell'], "values": [ [ tb_str ] ]}); let json = { "valueInputOption": "USER_ENTERED", @@ -252,6 +256,13 @@ function names_for_team(team, expansions) { return [ longteam, longteam, team ]; } +function expand_mediumname_if_single_team(team, expansions) { + if (expansions.hasOwnProperty(team)) { + return expansions[team][1]; + } + return team; +} + function do_replacements(str, replacements) { for (const r of replacements) { str = str.replace(r[0], r[1]); @@ -270,24 +281,38 @@ function fill_playoff(replacements, teams) { let games = ultimateconfig['playoff_games']; get_results('Results', function(response) { - let updates = []; + let updates = [], meta_updates = []; let game_num = 0; for (const game of games) { let team1 = do_replacements(game[0], replacements); let team2 = do_replacements(game[1], replacements); + let team1_mediumname = expand_mediumname_if_single_team(team1, team_expansions); + let team2_mediumname = expand_mediumname_if_single_team(team2, team_expansions); let row = ultimateconfig['playoff_games_start_row'] + game[3]; let cols = ultimateconfig['playoff_games_cols'][game[2]]; let cell_team1 = "Results!" + String.fromCharCode(cols[0] + 65) + row; let cell_score1 = "Results!" + String.fromCharCode(cols[1] + 65) + row; let cell_score2 = "Results!" + String.fromCharCode(cols[2] + 65) + row; let cell_team2 = "Results!" + String.fromCharCode(cols[3] + 65) + row; - updates.push({ "range": cell_team1, "values": [ [ team1 ] ] }); - updates.push({ "range": cell_team2, "values": [ [ team2 ] ] }); + updates.push({ "range": cell_team1, "values": [ [ team1_mediumname ] ] }); + updates.push({ "range": cell_team2, "values": [ [ team2_mediumname ] ] }); let score1 = response['values'][row - 1][cols[1]]; let score2 = response['values'][row - 1][cols[2]]; let game_name = game[4]; let game_name2 = game_name.replace("Semi", "semi"); + let game_day = game[7]; + if (game_day === undefined) { + game_day = 7; // Sunday. + } + + let range = { + "sheetId": ultimateconfig['score_sheet_index'], + "startColumnIndex": cols[1], + "endColumnIndex": cols[2] + 1, + "startRowIndex": row - 1, + "endRowIndex": row + }; if (parseInt(score1) >= 0 && parseInt(score2) >= 0 && score1 != score2) { if (parseInt(score1) > parseInt(score2)) { @@ -301,16 +326,21 @@ function fill_playoff(replacements, teams) { replacements.unshift(["W " + game_name2, team2]); replacements.unshift(["L " + game_name2, team1]); } + meta_updates.push({ "unmergeCells": { "range": range }}); } else if (game[5]) { + // No score yet, so write the name of the game (e.g. “L-semi 1”) + // where the score would normally be, to mark what this game is called. + // This is useful with the limited space on the tablet. score1 = score2 = ""; updates.push({ "range": cell_score1, "values": [ [ game[4] ] ] }); + meta_updates.push({ "mergeCells": { "range": range, "mergeType": "MERGE_ALL" }}); } if (game[2] == 0) { // Stream field. // Game. updates.push({ "range": "Playoffs!A" + (game_num + 32) + ":J" + (game_num + 32), - "values": [ [ team1, team2, score1, score2, "", "", "", 7, response['values'][row - 1][1].replace(".",":"), game[6] ] ] + "values": [ [ team1, team2, score1, score2, "", "", "", game_day, response['values'][row - 1][1].replace(".",":"), game[6] ] ] }); // Team codes. @@ -330,8 +360,14 @@ function fill_playoff(replacements, teams) { "valueInputOption": "USER_ENTERED", "data": updates }; + let meta_json = { + "requests": meta_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); + post_json('https://sheets.googleapis.com/v4/spreadsheets/' + ultimateconfig['score_sheet_id'] + '/values:batchUpdate?key=' + ultimateconfig['api_key'], json, function(response) { + get_group('Playoffs 9th-13th', function(response_l) { publish_group_rank(response_l, 'Playoffs 9th-13th'); }); + }, current_oauth_access_token); + 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); }); }); } @@ -368,6 +404,7 @@ 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); + teams = filter_teams(teams, response); let tiebreakers = []; teams = rank(games, teams, 1, tiebreakers); return teams;