def __init__(self, lobby_id_cell): self.lobby_id = lobby_id_cell self.lobby_id.value_type = str self.teams = [] self.referee = Cell(-1, -1, "") self.date = Cell(-1, -1, "") self.time = Cell(-1, -1, "")
def get_first_blank_fields(players_spreadsheet): range_to_use = None if players_spreadsheet.range_team_name: range_to_use = players_spreadsheet.range_team_name else: range_to_use = players_spreadsheet.range_team cells = players_spreadsheet.spreadsheet.get_range(range_to_use) if not cells: worksheet, range_to_use = players_spreadsheet.spreadsheet.get_worksheet_and_range( range_to_use) splitted_range = range_to_use.split(":")[0] column, row, _ = re.split( r"(\d+)", splitted_range) # TODO: handle all kind of ranges cells = [[Cell(from_letter_base(column), int(row) - 1, "")]] worksheet.cells = cells used = False for row in cells: used = False for cell in row: if cell: used = True break if not used: break if used: worksheet, _ = players_spreadsheet.spreadsheet.get_worksheet_and_range( range_to_use) cells = worksheet.cells row = [Cell(row[0].x, row[0].y + 1, "")] cells.append(row) if players_spreadsheet.range_team_name: return TeamInfo.from_team_name_cell(players_spreadsheet, row[0]) else: return TeamInfo.from_player_cell(players_spreadsheet, row[0])
def create_worksheet(index, name, values): cells = [] for y, row in enumerate(values): cells.append([]) for x, value in enumerate(row): cells[y].append(Cell(x, y, value)) return Worksheet(index, name, cells)
def __init__( self, name, discord=None, discord_id=None, rank=None, bws_rank=None, osu_id=None, pp=None, country=None, is_captain=False, ): self.name = name self.name.value_type = str self.discord = discord if isinstance(discord, Cell) else Cell( -1, -1, "") self.discord.value_type = str self.discord_id = discord_id if isinstance( discord_id, Cell) else Cell(-1, -1, 0) self.discord_id.value_type = int self.rank = rank if isinstance(rank, Cell) else Cell(-1, -1, "") self.rank.value_type = str self.bws_rank = bws_rank if isinstance(bws_rank, Cell) else Cell( -1, -1, "") self.bws_rank.value_type = str self.osu_id = osu_id if isinstance(osu_id, Cell) else Cell( -1, -1, "") self.osu_id.value_type = str self.pp = pp if isinstance(pp, Cell) else Cell(-1, -1, "") self.pp.value_type = str self.country = country if isinstance(country, Cell) else Cell( -1, -1, "") self.country.value_type = str self.is_captain = is_captain
def __init__(self, match_id_cell): self.match_id = match_id_cell self.match_id.value_type = str self.team1 = Cell(-1, -1, "") self.team2 = Cell(-1, -1, "") self.score_team1 = Cell(-1, -1, "") self.score_team2 = Cell(-1, -1, "") self.date = Cell(-1, -1, "") self.time = Cell(-1, -1, "") self.referees = [] self.streamers = [] self.commentators = [] self.mp_links = []
def retrieve_spreadsheet(spreadsheet_id): with open(SPREADSHEET_RESOURCES_DIR + spreadsheet_id + ".json", "r") as f: spreadsheet_data = json.loads(f.read()) spreadsheet = SpreadsheetMock(spreadsheet_data["spreadsheet_id"]) for index, sheet in enumerate(spreadsheet_data["worksheets"]): cells = [] for y, row in enumerate(sheet["cells"]): cells.append([]) for x, cell in enumerate(row): cells[y].append(Cell(x, y, cell)) spreadsheet.worksheets.append(Worksheet(index, sheet["name"], cells)) spreadsheet._spreadsheet = spreadsheet return spreadsheet
def from_team_name_cell(players_spreadsheet, team_name_cell): team_info = TeamInfo(team_name_cell) players_data = [] players_data.append( find_corresponding_cells_best_effort_from_range( players_spreadsheet.spreadsheet, players_spreadsheet.range_team, team_name_cell, max_difference_with_base=players_spreadsheet. max_range_for_teams, )) players_data.append( find_corresponding_cells_best_effort_from_range( players_spreadsheet.spreadsheet, players_spreadsheet.range_discord, team_name_cell, max_difference_with_base=players_spreadsheet. max_range_for_teams, )) players_data.append( find_corresponding_cells_best_effort_from_range( players_spreadsheet.spreadsheet, players_spreadsheet.range_discord_id, team_name_cell, max_difference_with_base=players_spreadsheet. max_range_for_teams, )) players_data.append( find_corresponding_cells_best_effort_from_range( players_spreadsheet.spreadsheet, players_spreadsheet.range_rank, team_name_cell, max_difference_with_base=players_spreadsheet. max_range_for_teams, )) players_data.append( find_corresponding_cells_best_effort_from_range( players_spreadsheet.spreadsheet, players_spreadsheet.range_bws_rank, team_name_cell, max_difference_with_base=players_spreadsheet. max_range_for_teams, )) players_data.append( find_corresponding_cells_best_effort_from_range( players_spreadsheet.spreadsheet, players_spreadsheet.range_osu_id, team_name_cell, max_difference_with_base=players_spreadsheet. max_range_for_teams, )) players_data.append( find_corresponding_cells_best_effort_from_range( players_spreadsheet.spreadsheet, players_spreadsheet.range_pp, team_name_cell, max_difference_with_base=players_spreadsheet. max_range_for_teams, )) players_data.append( find_corresponding_cells_best_effort_from_range( players_spreadsheet.spreadsheet, players_spreadsheet.range_country, team_name_cell, max_difference_with_base=players_spreadsheet. max_range_for_teams, )) # TODO: only one cell needed / maybe a new function needed ? timezone_cells = find_corresponding_cells_best_effort_from_range( players_spreadsheet.spreadsheet, players_spreadsheet.range_timezone, team_name_cell, max_difference_with_base=players_spreadsheet.max_range_for_teams, ) if timezone_cells: team_info.timezone = timezone_cells[0] max_len = len(players_data[0]) for player_data in players_data: while len(player_data) < max_len: player_data.append(Cell(-1, -1, "")) for name, discord, discord_id, rank, bws_rank, osu_id, pp, country in zip( *players_data): team_info.add_player( TeamInfo.PlayerInfo(name, discord, discord_id, rank, bws_rank, osu_id, pp, country)) return team_info
def __init__(self, osu_id_cell): self.osu_id = osu_id_cell self.score = Cell(-1, -1, "")
def set_timezones(self, timezones): while len(timezones) < len(self.players): timezones.append(Cell(-1, -1, None)) self.timezones = timezones
def set_pps(self, pps): while len(pps) < len(self.players): pps.append(Cell(-1, -1, None)) self.pps = pps
def set_bws_ranks(self, bws_ranks): while len(bws_ranks) < len(self.players): bws_ranks.append(Cell(-1, -1, None)) self.bws_ranks = bws_ranks
def set_ranks(self, ranks): while len(ranks) < len(self.players): ranks.append(Cell(-1, -1, None)) self.ranks = ranks
def set_discord_ids(self, discord_ids): while len(discord_ids) < len(self.players): discord_ids.append(Cell(-1, -1, None)) self.discord_ids = discord_ids
class MatchInfo: """Contains all info about a match.""" def __init__(self, match_id_cell): self.match_id = match_id_cell self.match_id.value_type = str self.team1 = Cell(-1, -1, "") self.team2 = Cell(-1, -1, "") self.score_team1 = Cell(-1, -1, "") self.score_team2 = Cell(-1, -1, "") self.date = Cell(-1, -1, "") self.time = Cell(-1, -1, "") self.referees = [] self.streamers = [] self.commentators = [] self.mp_links = [] def set_team1(self, team1_cell): self.team1 = team1_cell self.team1.value_type = str def set_team2(self, team2_cell): self.team2 = team2_cell self.team2.value_type = str def set_score_team1(self, score_team1_cell): self.score_team1 = score_team1_cell self.score_team1.value_type = str def set_score_team2(self, score_team2_cell): self.score_team2 = score_team2_cell self.score_team2.value_type = str def set_date(self, date_cell): self.date = date_cell if self.date.value_type == int or self.date.value_type == float: self.date.value_type = int return if self.date.value_type != str: raise DateIsNotString("date") def set_time(self, time_cell): self.time = time_cell if self.time.value_type == float or self.time.value_type == int: self.time.value_type = float return if self.time.value_type != str: raise DateIsNotString("time") def set_referees(self, referee_cells): self.referees = referee_cells for referee_cell in self.referees: referee_cell.value_type = str def set_streamers(self, streamer_cells): self.streamers = streamer_cells for streamer_cell in self.streamers: streamer_cell.value_type = str def set_commentators(self, commentator_cells): self.commentators = commentator_cells for commentator_cell in self.commentators: commentator_cell.value_type = str def set_mp_links(self, mp_link_cells): self.mp_links = mp_link_cells for mp_link_cell in self.mp_links: mp_link_cell.value_type = str def get_datetime(self): if self.date.value_type == int: date = (datetime.datetime(1970, 1, 1) + datetime.timedelta( days=self.date.get() - 25569)).strftime("%d %B") else: date = self.date.get() if self.time.value_type == float: time_value = self.time.get() time_hour_floor = math.floor(time_value * 24) time = str(time_hour_floor) + ":" + str( round((time_value * 24 - time_hour_floor) * 60)) else: time = self.time.get() return " ".join(filter(None, [date, time])) def set_datetime(self, schedules_spreadsheet, new_date, date_format): new_date_value = None new_time_value = None if schedules_spreadsheet.range_date and schedules_spreadsheet.range_time: new_date_value = new_date.strftime(date_format) new_time_value = new_date.strftime("%H:%M") elif schedules_spreadsheet.range_date: new_date_value = new_date.strftime(date_format + " %H:%M") elif schedules_spreadsheet.range_time: new_time_value = new_date.strftime(date_format + " %H:%M") else: raise exceptions.UnknownError("No date range") if new_date_value: if self.date.value_type == int: new_date_value = (dateparser.parse( new_date_value, date_formats=list(filter(None, [date_format]))) - datetime.datetime(1970, 1, 1)).days + 25569 self.date.set(new_date_value) if new_time_value: if self.time.value_type == float: new_time_value = float( new_time_value.split(":")[0]) / 24.0 + float( new_time_value.split(":")[1]) / (60.0 * 24.0) self.time.set(new_time_value) @staticmethod def from_id(schedules_spreadsheet, match_id, filled_only=True): match_id_cells = schedules_spreadsheet.spreadsheet.get_range( schedules_spreadsheet.range_match_id) corresponding_match_id_cells = schedules_spreadsheet.spreadsheet.find_cells( match_id_cells, match_id, False) if not corresponding_match_id_cells: raise MatchIdNotFound(match_id) if len(corresponding_match_id_cells) > 1: raise DuplicateMatchId(match_id) match_id_cell = corresponding_match_id_cells[0] return MatchInfo.from_match_id_cell(schedules_spreadsheet, match_id_cell, filled_only) @staticmethod def from_match_id_cell(schedules_spreadsheet, match_id_cell, filled_only=True): match_info = MatchInfo(match_id_cell) spreadsheet = schedules_spreadsheet.spreadsheet match_info.set_team1( find_corresponding_cell_best_effort_from_range( spreadsheet, schedules_spreadsheet.range_team1, match_id_cell, )) match_info.set_team2( find_corresponding_cell_best_effort_from_range( spreadsheet, schedules_spreadsheet.range_team2, match_id_cell, )) match_info.set_score_team1( find_corresponding_cell_best_effort_from_range( spreadsheet, schedules_spreadsheet.range_score_team1, match_id_cell, )) match_info.set_score_team2( find_corresponding_cell_best_effort_from_range( spreadsheet, schedules_spreadsheet.range_score_team2, match_id_cell, )) match_info.set_date( find_corresponding_cell_best_effort_from_range( spreadsheet, schedules_spreadsheet.range_date, match_id_cell, )) match_info.set_time( find_corresponding_cell_best_effort_from_range( spreadsheet, schedules_spreadsheet.range_time, match_id_cell, )) if schedules_spreadsheet.use_range: match_info.set_referees( find_corresponding_cells_best_effort_from_range( spreadsheet, schedules_spreadsheet.range_referee, match_id_cell, filled_only=filled_only, )) match_info.set_streamers( find_corresponding_cells_best_effort_from_range( spreadsheet, schedules_spreadsheet.range_streamer, match_id_cell, filled_only=filled_only, )) match_info.set_commentators( find_corresponding_cells_best_effort_from_range( spreadsheet, schedules_spreadsheet.range_commentator, match_id_cell, filled_only=filled_only, )) match_info.set_mp_links( find_corresponding_cells_best_effort_from_range( spreadsheet, schedules_spreadsheet.range_mp_links, match_id_cell, filled_only=filled_only, )) else: match_info.set_referees([ find_corresponding_cell_best_effort_from_range( spreadsheet, schedules_spreadsheet.range_referee, match_id_cell, ) ]) match_info.set_streamers([ find_corresponding_cell_best_effort_from_range( spreadsheet, schedules_spreadsheet.range_streamer, match_id_cell, ) ]) match_info.set_commentators([ find_corresponding_cell_best_effort_from_range( spreadsheet, schedules_spreadsheet.range_commentator, match_id_cell, ) ]) match_info.set_mp_links([ find_corresponding_cell_best_effort_from_range( spreadsheet, schedules_spreadsheet.range_mp_links, match_id_cell, ) ]) return match_info
def set_osu_ids(self, osu_ids): while len(osu_ids) < len(self.players): osu_ids.append(Cell(-1, -1, None)) self.osu_ids = osu_ids
def __init__(self, team_name_cell): self.team_name = team_name_cell self.team_name.value_type = str self.players = [] self.timezone = Cell(-1, -1, "")
class LobbyInfo: """Contains all info about a lobby.""" def __init__(self, lobby_id_cell): self.lobby_id = lobby_id_cell self.lobby_id.value_type = str self.teams = [] self.referee = Cell(-1, -1, "") self.date = Cell(-1, -1, "") self.time = Cell(-1, -1, "") def set_teams(self, team_cells): self.teams = team_cells for team in self.teams: team.value_type = str def set_referee(self, referee_cell): self.referee = referee_cell self.referee.value_type = str def set_date(self, date_cell): self.date = date_cell if self.date.value_type == int or self.date.value_type == float: self.date.value_type = int return if self.date.value_type != str: raise DateIsNotString("date") def set_time(self, time_cell): self.time = time_cell if self.time.value_type == float or self.time.value_type == int: self.time.value_type = float return if self.time.value_type != str: raise DateIsNotString("time") def get_datetime(self): if self.date.value_type == int: date = (datetime.datetime(1970, 1, 1) + datetime.timedelta( days=self.date.get() - 25569)).strftime("%d %B") else: date = self.date.get() if self.time.value_type == float: time_value = self.time.get() time_hour_floor = math.floor(time_value * 24) time = str(time_hour_floor) + ":" + str( round((time_value * 24 - time_hour_floor) * 60)) else: time = self.time.get() return " ".join(filter(None, [date, time])) @staticmethod def from_id(qualifiers_spreadsheet, lobby_id, filled_only=True): lobby_id_cells = qualifiers_spreadsheet.spreadsheet.get_range( qualifiers_spreadsheet.range_lobby_id) corresponding_lobby_id_cells = qualifiers_spreadsheet.spreadsheet.find_cells( lobby_id_cells, lobby_id, False) if not corresponding_lobby_id_cells: raise LobbyIdNotFound(lobby_id) if len(corresponding_lobby_id_cells) > 1: raise DuplicateLobbyId(lobby_id) lobby_id_cell = corresponding_lobby_id_cells[0] return LobbyInfo.from_lobby_id_cell(qualifiers_spreadsheet, lobby_id_cell, filled_only) @staticmethod def from_lobby_id_cell(qualifiers_spreadsheet, lobby_id_cell, filled_only=True): lobby_info = LobbyInfo(lobby_id_cell) spreadsheet = qualifiers_spreadsheet.spreadsheet lobby_info.set_teams( find_corresponding_qualifier_cells_best_effort( spreadsheet, spreadsheet.get_range(qualifiers_spreadsheet.range_teams), lobby_id_cell, qualifiers_spreadsheet.max_teams_in_row, filled_only, )) lobby_info.set_referee( find_corresponding_cell_best_effort( spreadsheet.get_range(qualifiers_spreadsheet.range_referee), lobby_id_cell, qualifiers_spreadsheet.max_teams_in_row, )) lobby_info.set_date( find_corresponding_cell_best_effort( spreadsheet.get_range(qualifiers_spreadsheet.range_date), lobby_id_cell, qualifiers_spreadsheet.max_teams_in_row, )) lobby_info.set_time( find_corresponding_cell_best_effort( spreadsheet.get_range(qualifiers_spreadsheet.range_time), lobby_id_cell, qualifiers_spreadsheet.max_teams_in_row, )) return lobby_info