+function names_for_team(team, expansions) {
+ if (expansions.hasOwnProperty(team)) {
+ return expansions[team];
+ }
+ let longteam = team.replace("W ", "Win. ").replace("L ", "Los. ");
+ 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]);
+ }
+ return str;
+}
+
+function fill_playoff(replacements, teams) {
+ let team_expansions = {};
+ for (const group of teams) {
+ for (const team of group) {
+ team_expansions[team.name] = team_expansions[team.mediumname] = team_expansions[team.shortname] =
+ [ team.name, team.mediumname, team.shortname ];
+ }
+ }
+
+ let games = ultimateconfig['playoff_games'];
+ get_results('Results', function(response) {
+ let 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_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");
+
+ if (parseInt(score1) >= 0 && parseInt(score2) >= 0 && score1 != score2) {
+ if (parseInt(score1) > parseInt(score2)) {
+ replacements.unshift(["W " + game_name, team1]);
+ replacements.unshift(["L " + game_name, team2]);
+ replacements.unshift(["W " + game_name2, team1]);
+ replacements.unshift(["L " + game_name2, team2]);
+ } else {
+ replacements.unshift(["W " + game_name, team2]);
+ replacements.unshift(["L " + game_name, team1]);
+ replacements.unshift(["W " + game_name2, team2]);
+ replacements.unshift(["L " + game_name2, team1]);
+ }
+ } 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] ] ] });
+ }
+
+ 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] ] ]
+ });
+
+ // Team codes.
+ updates.push({
+ "range": "Playoffs!A" + (2 * game_num + 3) + ":C" + (2 * game_num + 3),
+ "values": [ names_for_team(team1, team_expansions) ]
+ });
+ updates.push({
+ "range": "Playoffs!A" + (2 * game_num + 4) + ":C" + (2 * game_num + 4),
+ "values": [ names_for_team(team2, team_expansions) ]
+ });
+
+ ++game_num;
+ }
+ }