diff --git a/leagues.py b/leagues.py index 14c0cdf..02761d5 100644 --- a/leagues.py +++ b/leagues.py @@ -52,30 +52,30 @@ class league_structure(object): standings[team_name] = {"wins" : wins, "losses" : losses, "run_diff" : run_diff} for subleague in iter(self.league.keys()): - team_dic = {} - division_leaders = [] + team_dic = {} subleague_array = [] wildcard_leaders = [] for division in iter(self.league[subleague].keys()): + division_standings = [] division_standings += self.division_standings(self.league[subleague][division], standings) - for division_team in division_standings: - if division_team.name != division_standings[self.constraints["division_leaders"]-1].name and standings[division_team.name]["wins"] == standings[division_standings[self.constraints["division_leaders"]-1].name]["wins"]: - matchups.append((division_team, division_standings[self.constraints["division_leaders"]-1]), f"{division} Tiebreaker") + division_leaders = division_standings[:self.constraints["division_leaders"]] + for division_team, wins, losses, diff, gb in division_standings[self.constraints["division_leaders"]:]: + if division_team.name != division_leaders[-1][0].name and standings[division_team.name]["wins"] == standings[division_leaders[-1][0].name]["wins"]: + matchups.append((division_team, division_standings[self.constraints["division_leaders"]-1][0], f"{division} Tiebreaker")) this_div_wildcard = [this_team for this_team, wins, losses, diff, gb in self.division_standings(self.league[subleague][division], standings)[self.constraints["division_leaders"]:]] subleague_array += this_div_wildcard if self.constraints["wild_cards"] > 0: wildcard_standings = self.division_standings(subleague_array, standings) - for wildcard_team in wildcard_standings: - if wildcard_team.name != wildcard_standings[self.constraints["wild_cards"]-1].name and standings[wildcard_team.name]["wins"] == standings[wildcard_standings[self.constraints["wild_cards"]-1].name]["wins"]: - matchups.append((wildcard_team, wildcard_standings[self.constraints["wild_cards"]-1]), f"{subleague} Wildcard Tiebreaker") - - for this_team, wins, losses, diff, gb in divison_leaders + wildcard_leaders: - team_dic[this_team] = {"wins" : wins} + wildcard_leaders = wildcard_standings[:self.constraints["wild_cards"]] + for wildcard_team, wins, losses, diff, gb in wildcard_standings[self.constraints["wild_cards"]:]: + if wildcard_team.name != wildcard_leaders[-1][0].name and standings[wildcard_team.name]["wins"] == standings[wildcard_leaders[-1][0].name]["wins"]: + matchups.append((wildcard_team, wildcard_standings[self.constraints["wild_cards"]-1][0], f"{subleague} Wildcard Tiebreaker")) for team_a, team_b, type in matchups: - tourney = tournament(f"{league.name} {type}",{team_a : {"wins" : 1}, team_b : {"wins" : 0}}, secs_between_games=int(3600/self.games_per_hour), secs_between_rounds=int(7200/self.games_per_hour)) + tourney = tournament(f"{self.name} {type}",{team_a : {"wins" : 1}, team_b : {"wins" : 0}}, finals_series_length=1, secs_between_games=int(3600/self.games_per_hour), secs_between_rounds=int(7200/self.games_per_hour)) tourney.build_bracket(by_wins = True) + tourney.league = self tournaments.append(tourney) return tournaments @@ -310,11 +310,12 @@ class league_structure(object): if self.constraints["wild_cards"] > 0: wildcard_leaders = self.division_standings(subleague_array, standings)[:self.constraints["wild_cards"]] - for this_team, wins, losses, diff, gb in divison_leaders + wildcard_leaders: + for this_team, wins, losses, diff, gb in division_leaders + wildcard_leaders: team_dic[this_team] = {"wins" : wins} subleague_tournament = tournament(f"{self.name} {subleague} Championship Series", team_dic, secs_between_games=int(3600/self.games_per_hour), secs_between_rounds=int(7200/self.games_per_hour)) subleague_tournament.build_bracket(by_wins = True) + subleague_tournament.league = self tournaments.append(subleague_tournament) return tournaments @@ -334,6 +335,7 @@ class tournament(object): self.round_delay = secs_between_rounds self.finals = False self.id = id + self.league = None self.winner = None if id is None: diff --git a/the_prestige.py b/the_prestige.py index bd98b32..01950de 100644 --- a/the_prestige.py +++ b/the_prestige.py @@ -809,7 +809,7 @@ Plays a league with a given name, provided that league has been saved on the web elif league.day % league.series_length == 1: await start_league_day(msg.channel, league) else: - + await start_league_day(msg.channel, league, partial = True) else: await msg.channel.send("Couldn't find that league, boss. Did you save it on the website?") @@ -1156,7 +1156,15 @@ async def start_tournament_round(channel, tourney, seeding = None): for pair in games_to_start: if pair[0] is not None and pair[1] is not None: - this_game = games.game(pair[0].prepare_for_save().finalize(), pair[1].prepare_for_save().finalize(), length = tourney.game_length) + team_a = get_team_fuzzy_search(pair[0].name) + team_b = get_team_fuzzy_search(pair[1].name) + + if tourney.league is not None: + team_a.set_pitcher(rotation_slot = tourney.league.day) + team_b.set_pitcher(rotation_slot = tourney.league.day) + league.day += 1 + + this_game = games.game(team_a.finalize(), team_b.finalize(), length = tourney.game_length) this_game, state_init = prepare_game(this_game) state_init["is_league"] = True @@ -1187,6 +1195,12 @@ async def continue_tournament_series(tourney, queue, games_list, wins_in_series) for oldgame in queue: away_team = games.get_team(oldgame.teams["away"].name) home_team = games.get_team(oldgame.teams["home"].name) + + if tourney.league is not None: + away_team.set_pitcher(rotation_slot = tourney.league.day) + home_team.set_pitcher(rotation_slot = tourney.league.day) + league.day += 1 + this_game = games.game(away_team.finalize(), home_team.finalize(), length = tourney.game_length) this_game, state_init = prepare_game(this_game) @@ -1250,8 +1264,32 @@ async def tourney_round_watcher(channel, tourney, games_list, filter_url, finals if len(queued_games) > 0: - await channel.send(f"The next batch of games for {tourney.name} will start in {int(tourney.delay/60)} minutes.") - await asyncio.sleep(tourney.delay) + + if tourney.league is not None: + now = datetime.datetime.now() + validminutes = [int((60 * div)/tourney.league.games_per_hour) for div in range(0,tourney.league.games_per_hour)] + for i in range(0, len(validminutes)): + if now.minute > validminutes[i]: + if i <= len(validminutes)-3: + if validminutes[i+1] == now.minute: + delta = datetime.timedelta(minutes= (validminutes[i+2] - now.minute)) + else: + delta = datetime.timedelta(minutes= (validminutes[i+1] - now.minute)) + elif i <= len(validminutes)-2: + if validminutes[i+1] == now.minute: + delta = datetime.timedelta(minutes= (60 - now.minute)) + else: + delta = datetime.timedelta(minutes= (validminutes[i+1] - now.minute)) + else: + delta = datetime.timedelta(minutes= (60 - now.minute)) + + next_start = (now + delta).replace(microsecond=0) + wait_seconds = (next_start - now).seconds + await channel.send(f"The next batch of games for {tourney.name} will start in {math.ceil(wait_seconds/60)} minutes.") + await asyncio.sleep(wait_seconds) + else: + await channel.send(f"The next batch of games for {tourney.name} will start in {int(tourney.delay/60)} minutes.") + await asyncio.sleep(tourney.delay) await channel.send(f"{len(queued_games)} games for {tourney.name}, starting at {filter_url}") games_list = await continue_tournament_series(tourney, queued_games, games_list, wins_in_series) else: @@ -1269,11 +1307,37 @@ async def tourney_round_watcher(channel, tourney, games_list, filter_url, finals winners_string = "" for game in tourney.bracket.get_bottom_row(): winners_string += f"{game[0].name}\n{game[1].name}\n" - await channel.send(f""" + + if tourney.league is not None: + now = datetime.datetime.now() + validminutes = [int((60 * div)/tourney.league.games_per_hour) for div in range(0,tourney.league.games_per_hour)] + for i in range(0, len(validminutes)): + if now.minute > validminutes[i]: + if i <= len(validminutes)-3: + if validminutes[i+1] == now.minute: + delta = datetime.timedelta(minutes= (validminutes[i+2] - now.minute)) + else: + delta = datetime.timedelta(minutes= (validminutes[i+1] - now.minute)) + elif i <= len(validminutes)-2: + if validminutes[i+1] == now.minute: + delta = datetime.timedelta(minutes= (60 - now.minute)) + else: + delta = datetime.timedelta(minutes= (validminutes[i+1] - now.minute)) + else: + delta = datetime.timedelta(minutes= (60 - now.minute)) + + next_start = (now + delta).replace(microsecond=0) + wait_seconds = (next_start - now).seconds + await channel.send(f"""This round of games for {tourney.name} is now complete! The next round will start in {math.ceil(wait_seconds/60)} minutes. +Advancing teams: +{winners_string}""") + await asyncio.sleep(wait_seconds) + else: + await channel.send(f""" This round of games for {tourney.name} is now complete! The next round will be starting in {int(tourney.round_delay/60)} minutes. Advancing teams: {winners_string}""") - await asyncio.sleep(tourney.round_delay) + await asyncio.sleep(tourney.round_delay) await start_tournament_round(channel, tourney) @@ -1658,6 +1722,27 @@ async def league_day_watcher(channel, league, games_list, filter_url, last = Fal if last: #if last game of the season + now = datetime.datetime.now() + validminutes = [int((60 * div)/league.games_per_hour) for div in range(0,league.games_per_hour)] + for i in range(0, len(validminutes)): + if now.minute > validminutes[i]: + if i <= len(validminutes)-3: + if validminutes[i+1] == now.minute: + delta = datetime.timedelta(minutes= (validminutes[i+2] - now.minute)) + else: + delta = datetime.timedelta(minutes= (validminutes[i+1] - now.minute)) + elif i <= len(validminutes)-2: + if validminutes[i+1] == now.minute: + delta = datetime.timedelta(minutes= (60 - now.minute)) + else: + delta = datetime.timedelta(minutes= (validminutes[i+1] - now.minute)) + else: + delta = datetime.timedelta(minutes= (60 - now.minute)) + + next_start = (now + delta).replace(microsecond=0) + wait_seconds = (next_start - now).seconds + await channel.send(f"This {league.name} season is now over! The postseason (with any necessary tiebreakers) will be starting in {math.ceil(wait_seconds/60)} minutes.") + await asyncio.sleep(wait_seconds) await league_postseason(channel, league) #need to reset league to new season here @@ -1732,14 +1817,36 @@ async def league_postseason(channel, league): tiebreakers = league.tiebreaker_required() if tiebreakers != []: await channel.send("Tiebreakers required!") - await asyncio.gather(*[start_tournament_round(tourney) for tourney in tiebreakers]) + await asyncio.gather(*[start_tournament_round(channel, tourney) for tourney in tiebreakers]) for tourney in tiebreakers: league.update_standings({tourney.winner.name : {"wins" : 1}}) leagues.save_league(league) + now = datetime.datetime.now() + + validminutes = [int((60 * div)/league.games_per_hour) for div in range(0,league.games_per_hour)] + for i in range(0, len(validminutes)): + if now.minute > validminutes[i]: + if i <= len(validminutes)-3: + if validminutes[i+1] == now.minute: + delta = datetime.timedelta(minutes= (validminutes[i+2] - now.minute)) + else: + delta = datetime.timedelta(minutes= (validminutes[i+1] - now.minute)) + elif i <= len(validminutes)-2: + if validminutes[i+1] == now.minute: + delta = datetime.timedelta(minutes= (60 - now.minute)) + else: + delta = datetime.timedelta(minutes= (validminutes[i+1] - now.minute)) + else: + delta = datetime.timedelta(minutes= (60 - now.minute)) + + next_start = (now + delta).replace(microsecond=0) + wait_seconds = (next_start - now).seconds + await channel.send(f"Tiebreakers complete! Postseason starting in {math.ceil(wait_seconds/60)} minutes.") + await asyncio.sleep(wait_seconds) - await channel.send("Setting up postseason...") + tourneys = league.champ_series() - await asyncio.gather(*[start_tournament_round(tourney) for tourney in tourneys]) + await asyncio.gather(*[start_tournament_round(channel, tourney) for tourney in tourneys]) champs = {} for tourney in tourneys: for team in tourney.teams.keys():