implemented tiebreakers and postseason tournament

This commit is contained in:
Sakimori 2021-01-15 02:12:30 -05:00
parent 121d92885a
commit 19072f2111
2 changed files with 96 additions and 3 deletions

View File

@ -44,6 +44,41 @@ class league_structure(object):
def day_to_series_num(self, day): def day_to_series_num(self, day):
return math.ceil((self.day)/self.series_length) return math.ceil((self.day)/self.series_length)
def tiebreaker_required(self):
standings = {}
matchups = []
tournaments = []
for team_name, wins, losses, run_diff in league_db.get_standings(self.name):
standings[team_name] = {"wins" : wins, "losses" : losses, "run_diff" : run_diff}
for subleague in iter(self.league.keys()):
team_dic = {}
division_leaders = []
subleague_array = []
wildcard_leaders = []
for division in iter(self.league[subleague].keys()):
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")
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}
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.build_bracket(by_wins = True)
tournaments.append(tourney)
return tournaments
def find_team(self, team_name): def find_team(self, team_name):
for subleague in iter(self.league.keys()): for subleague in iter(self.league.keys()):
for division in iter(self.league[subleague].keys()): for division in iter(self.league[subleague].keys()):
@ -256,6 +291,35 @@ class league_structure(object):
this_embed.set_footer(text=f"Wildcard standings as of day {self.day-1}") this_embed.set_footer(text=f"Wildcard standings as of day {self.day-1}")
return this_embed return this_embed
def champ_series(self):
tournaments = []
standings = {}
for team_name, wins, losses, run_diff in league_db.get_standings(self.name):
standings[team_name] = {"wins" : wins, "losses" : losses, "run_diff" : run_diff}
for subleague in iter(self.league.keys()):
team_dic = {}
division_leaders = []
subleague_array = []
wildcard_leaders = []
for division in iter(self.league[subleague].keys()):
division_leaders += self.division_standings(self.league[subleague][division], standings)[:self.constraints["division_leaders"]]
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_leaders = self.division_standings(subleague_array, standings)[:self.constraints["wild_cards"]]
for this_team, wins, losses, diff, gb in divison_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)
tournaments.append(subleague_tournament)
return tournaments
class tournament(object): class tournament(object):
def __init__(self, name, team_dic, series_length = 5, finals_series_length = 7, max_innings = 9, id = None, secs_between_games = 300, secs_between_rounds = 600): def __init__(self, name, team_dic, series_length = 5, finals_series_length = 7, max_innings = 9, id = None, secs_between_games = 300, secs_between_rounds = 600):
self.name = name self.name = name
@ -270,6 +334,7 @@ class tournament(object):
self.round_delay = secs_between_rounds self.round_delay = secs_between_rounds
self.finals = False self.finals = False
self.id = id self.id = id
self.winner = None
if id is None: if id is None:
self.id = random.randint(1111,9999) self.id = random.randint(1111,9999)

View File

@ -1255,6 +1255,7 @@ async def tourney_round_watcher(channel, tourney, games_list, filter_url, finals
if finals: #if this last round was finals if finals: #if this last round was finals
embed = discord.Embed(color = discord.Color.dark_purple(), title = f"{winner_list[0]} win the {tourney.name} finals!") embed = discord.Embed(color = discord.Color.dark_purple(), title = f"{winner_list[0]} win the {tourney.name} finals!")
await channel.send(embed=embed) await channel.send(embed=embed)
tourney.winner = get_team_fuzzy_search(winner_list[0])
active_tournaments.pop(active_tournaments.index(tourney)) active_tournaments.pop(active_tournaments.index(tourney))
return return
@ -1644,15 +1645,44 @@ async def league_day_watcher(channel, league, games_list, filter_url, last = Fal
league.active = False league.active = False
if last: #if last game of the season
embed = league.standings_embed()
embed.set_footer(text="Final Standings")
await channel.send(embed=embed)
tiebreakers = league.tiebreaker_required()
if tiebreakers != []:
await channel.send("Tiebreakers required!")
await asyncio.gather(*[start_tournament_round(tourney) for tourney in tiebreakers])
for tourney in tiebreakers:
league.update_standings({tourney.winner.name : {"wins" : 1}})
leagues.save_league(league)
await channel.send("Setting up postseason...")
tourneys = league.champ_series()
await asyncio.gather(*[start_tournament_round(tourney) for tourney in tourneys])
champs = {}
for tourney in tourneys:
for team in tourney.teams.keys():
if team.name == tourney.winner.name:
champs[tourney.winner] = {"wins" : tourney.teams[team]["wins"]}
world_series = leagues.tournament(f"{league.name} Championship Series", champs, series_length=7, secs_between_games=int(3600/league.games_per_hour), secs_between_rounds=int(7200/league.games_per_hour))
world_series.build_bracket(by_wins = True)
await start_tournament_round(channel, world_series)
return
if last or league.autoplay == 0: #if this series was the last of the season OR number of series to autoplay has been reached
if league.autoplay == 0: #if number of series to autoplay has been reached
await channel.send(embed=league.standings_embed()) await channel.send(embed=league.standings_embed())
await channel.send(f"The {league.name} is no longer autoplaying.") await channel.send(f"The {league.name} is no longer autoplaying.")
leagues.save_league(league) leagues.save_league(league)
active_leagues.pop(active_leagues.index(league)) active_leagues.pop(active_leagues.index(league))
return return
now = datetime.datetime.now() now = datetime.datetime.now()
validminutes = [int((60 * div)/league.games_per_hour) for div in range(0,league.games_per_hour)] validminutes = [int((60 * div)/league.games_per_hour) for div in range(0,league.games_per_hour)]
@ -1704,6 +1734,4 @@ async def continue_league_series(league, queue, games_list, series_results):
client.run(config()["token"]) client.run(config()["token"])