Update standings calculations

This commit is contained in:
Cal Corum 2023-08-11 09:32:35 -05:00
parent 03956191ab
commit 85c8b441fa
2 changed files with 187 additions and 183 deletions

View File

@ -141,13 +141,15 @@ class Division(BaseModel):
# div_teams[x].div_e_num = e_number(div_teams[1], div_teams[x])
# Used for one playoff team per division
if x == 0:
div_teams[0].div_gb = -games_back(div_teams[0], div_teams[1])
div_teams[0].div_gb = None
div_teams[0].div_e_num = None
div_teams[0].wc_gb = None
div_teams[0].wc_e_num = None
else:
div_teams[x].div_gb = games_back(div_teams[0], div_teams[x])
div_teams[x].div_e_num = e_number(div_teams[0], div_teams[x])
div_teams[x].wc_gb = 99
div_teams[x].wc_e_num = 99
div_teams[x].save()
@ -162,15 +164,15 @@ class Division(BaseModel):
for x in range(len(league_teams)):
# Special calculations for two wildcard teams
if x == 0:
league_teams[0].wc_gb = -games_back(league_teams[0], league_teams[2])
league_teams[0].wc_e_num = None
elif x == 1:
league_teams[1].wc_gb = 0
league_teams[1].wc_e_num = None
if x < 4:
league_teams[x].wc_gb = -games_back(league_teams[0], league_teams[4])
league_teams[x].wc_e_num = None
# elif x == 3:
# league_teams[x].wc_gb = 0
# league_teams[x].wc_e_num = None
else:
league_teams[x].wc_gb = games_back(league_teams[1], league_teams[x])
league_teams[x].wc_e_num = e_number(league_teams[1], league_teams[x])
league_teams[x].wc_gb = games_back(league_teams[3], league_teams[x])
league_teams[x].wc_e_num = e_number(league_teams[3], league_teams[x])
league_teams[x].save()
@ -525,31 +527,24 @@ class Team(BaseModel):
def run_pythag_last8(self):
team_stan = Standings.get_or_none(Standings.team == self)
runs_scored_home = Result.select(fn.SUM(Result.homescore).alias('runs')).where(
Result.hometeam == self
)[0].runs
runs_scored_away = Result.select(fn.SUM(Result.awayscore).alias('runs')).where(
Result.awayteam == self
)[0].runs
runs_allowed_home = Result.select(fn.SUM(Result.homescore).alias('runs')).where(
Result.awayteam == self
)[0].runs
runs_allowed_away = Result.select(fn.SUM(Result.awayscore).alias('runs')).where(
Result.hometeam == self
)[0].runs
runs_scored, runs_allowed = 0, 0
away_games = StratGame.select(
fn.SUM(StratGame.away_score).alias('r_us'), fn.SUM(StratGame.home_score).alias('r_them')
).where(StratGame.away_team == self & StratGame.game_num.is_null(False))
if away_games.count() > 0:
runs_scored += away_games[0].r_us
runs_allowed += away_games[0].r_them
if not runs_scored_home:
runs_scored_home = 0
if not runs_scored_away:
runs_scored_away = 0
if not runs_allowed_home:
runs_allowed_home = 0
if not runs_allowed_away:
runs_allowed_away = 0
home_games = StratGame.select(
fn.SUM(StratGame.home_score).alias('r_us'), fn.SUM(StratGame.away_score).alias('r_them')
).where(StratGame.home_team == self & StratGame.game_num.is_null(False))
if away_games.count() > 0:
runs_scored += home_games[0].r_them
runs_allowed += home_games[0].r_us
runs_scored = runs_scored_home + runs_scored_away
runs_allowed = runs_allowed_home + runs_allowed_away
if runs_allowed == 0:
pythag_win_pct = 1
elif runs_scored == 0:
pythag_win_pct = 0
else:
pythag_win_pct = runs_scored ** 1.83 / ((runs_scored ** 1.83) + (runs_allowed ** 1.83))
@ -558,18 +553,20 @@ class Team(BaseModel):
team_stan.pythag_wins = round(games_played * pythag_win_pct)
team_stan.pythag_losses = games_played - team_stan.pythag_wins
last_games = Result.select_season(self.season).where(
(Result.hometeam == self) | (Result.awayteam == self)
).order_by(-Result.id).limit(8)
last_games = StratGame.select().where(
((StratGame.home_team == self) | (StratGame.away_team == self)) & (StratGame.game_num.is_null(False))
).order_by(
-StratGame.season, -StratGame.week, -StratGame.game_num
).limit(8)
for game in last_games:
if game.homescore > game.awayscore:
if game.hometeam == self:
if game.home_score > game.away_score:
if game.home_team == self:
team_stan.last8_wins += 1
else:
team_stan.last8_losses += 1
else:
if game.hometeam == self:
if game.home_team == self:
team_stan.last8_losses += 1
else:
team_stan.last8_wins += 1
@ -613,149 +610,149 @@ class Result(BaseModel):
def select_season(num):
return Result.select().where(Result.season == num)
def update_standings(self):
away_stan = Standings.get_season(self.awayteam)
home_stan = Standings.get_season(self.hometeam)
away_div = Division.get_by_id(self.awayteam.division.id)
home_div = Division.get_by_id(self.hometeam.division.id)
if self.homescore > self.awayscore:
# - generic w/l & home/away w/l
home_stan.wins += 1
home_stan.home_wins += 1
away_stan.losses += 1
away_stan.away_losses += 1
# - update streak wl and num
if home_stan.streak_wl == 'w':
home_stan.streak_num += 1
else:
home_stan.streak_wl = 'w'
home_stan.streak_num = 1
if away_stan.streak_wl == 'l':
away_stan.streak_num += 1
else:
away_stan.streak_wl = 'l'
away_stan.streak_num = 1
# - if 1-run, tally accordingly
if self.homescore == self.awayscore + 1:
home_stan.one_run_wins += 1
away_stan.one_run_losses += 1
# Used for one league with 3 divisions
# - update record v division
# if away_div.division_abbrev == 'BE':
# def update_standings(self):
# away_stan = Standings.get_season(self.awayteam)
# home_stan = Standings.get_season(self.hometeam)
# away_div = Division.get_by_id(self.awayteam.division.id)
# home_div = Division.get_by_id(self.hometeam.division.id)
#
# if self.homescore > self.awayscore:
# # - generic w/l & home/away w/l
# home_stan.wins += 1
# home_stan.home_wins += 1
# away_stan.losses += 1
# away_stan.away_losses += 1
#
# # - update streak wl and num
# if home_stan.streak_wl == 'w':
# home_stan.streak_num += 1
# else:
# home_stan.streak_wl = 'w'
# home_stan.streak_num = 1
#
# if away_stan.streak_wl == 'l':
# away_stan.streak_num += 1
# else:
# away_stan.streak_wl = 'l'
# away_stan.streak_num = 1
#
# # - if 1-run, tally accordingly
# if self.homescore == self.awayscore + 1:
# home_stan.one_run_wins += 1
# away_stan.one_run_losses += 1
#
# # Used for one league with 3 divisions
# # - update record v division
# # if away_div.division_abbrev == 'BE':
# # home_stan.div1_wins += 1
# # elif away_div.division_abbrev == 'DO':
# # home_stan.div2_wins += 1
# # else:
# # home_stan.div3_wins += 1
# #
# # if home_div.division_abbrev == 'BE':
# # away_stan.div1_losses += 1
# # elif home_div.division_abbrev == 'DO':
# # away_stan.div2_losses += 1
# # else:
# # away_stan.div3_losses += 1
#
# # Used for two league plus divisions
# if away_div.league_abbrev == 'AL':
# if away_div.division_abbrev == 'E':
# home_stan.div1_wins += 1
# elif away_div.division_abbrev == 'DO':
# else:
# home_stan.div2_wins += 1
# else:
# if away_div.division_abbrev == 'E':
# home_stan.div3_wins += 1
# else:
# home_stan.div4_wins += 1
#
# if home_div.division_abbrev == 'BE':
# if home_div.league_abbrev == 'AL':
# if home_div.division_abbrev == 'E':
# away_stan.div1_losses += 1
# elif home_div.division_abbrev == 'DO':
# else:
# away_stan.div2_losses += 1
# else:
# if home_div.division_abbrev == 'E':
# away_stan.div3_losses += 1
# Used for two league plus divisions
if away_div.league_abbrev == 'AL':
if away_div.division_abbrev == 'E':
home_stan.div1_wins += 1
else:
home_stan.div2_wins += 1
else:
if away_div.division_abbrev == 'E':
home_stan.div3_wins += 1
else:
home_stan.div4_wins += 1
if home_div.league_abbrev == 'AL':
if home_div.division_abbrev == 'E':
away_stan.div1_losses += 1
else:
away_stan.div2_losses += 1
else:
if home_div.division_abbrev == 'E':
away_stan.div3_losses += 1
else:
away_stan.div4_losses += 1
# - adjust run_diff
home_stan.run_diff += self.homescore - self.awayscore
away_stan.run_diff -= self.homescore - self.awayscore
else:
# - generic w/l & home/away w/l
home_stan.losses += 1
home_stan.home_losses += 1
away_stan.wins += 1
away_stan.away_wins += 1
# - update streak wl and num
if home_stan.streak_wl == 'l':
home_stan.streak_num += 1
else:
home_stan.streak_wl = 'l'
home_stan.streak_num = 1
if away_stan.streak_wl == 'w':
away_stan.streak_num += 1
else:
away_stan.streak_wl = 'w'
away_stan.streak_num = 1
# - if 1-run, tally accordingly
if self.awayscore == self.homescore + 1:
home_stan.one_run_losses += 1
away_stan.one_run_wins += 1
# Used for one league with 3 divisions
# - update record v division
# if away_div.division_abbrev == 'BE':
# else:
# away_stan.div4_losses += 1
#
# # - adjust run_diff
# home_stan.run_diff += self.homescore - self.awayscore
# away_stan.run_diff -= self.homescore - self.awayscore
# else:
# # - generic w/l & home/away w/l
# home_stan.losses += 1
# home_stan.home_losses += 1
# away_stan.wins += 1
# away_stan.away_wins += 1
#
# # - update streak wl and num
# if home_stan.streak_wl == 'l':
# home_stan.streak_num += 1
# else:
# home_stan.streak_wl = 'l'
# home_stan.streak_num = 1
#
# if away_stan.streak_wl == 'w':
# away_stan.streak_num += 1
# else:
# away_stan.streak_wl = 'w'
# away_stan.streak_num = 1
#
# # - if 1-run, tally accordingly
# if self.awayscore == self.homescore + 1:
# home_stan.one_run_losses += 1
# away_stan.one_run_wins += 1
#
# # Used for one league with 3 divisions
# # - update record v division
# # if away_div.division_abbrev == 'BE':
# # home_stan.div1_losses += 1
# # elif away_div.division_abbrev == 'DO':
# # home_stan.div2_losses += 1
# # else:
# # home_stan.div3_losses += 1
# #
# # if home_div.division_abbrev == 'BE':
# # away_stan.div1_wins += 1
# # elif home_div.division_abbrev == 'DO':
# # away_stan.div2_wins += 1
# # else:
# # away_stan.div3_wins += 1
#
# # Used for two league plus divisions
# if away_div.league_abbrev == 'AL':
# if away_div.division_abbrev == 'E':
# home_stan.div1_losses += 1
# elif away_div.division_abbrev == 'DO':
# else:
# home_stan.div2_losses += 1
# else:
# if away_div.division_abbrev == 'E':
# home_stan.div3_losses += 1
# else:
# home_stan.div4_losses += 1
#
# if home_div.division_abbrev == 'BE':
# if home_div.league_abbrev == 'AL':
# if home_div.division_abbrev == 'E':
# away_stan.div1_wins += 1
# elif home_div.division_abbrev == 'DO':
# else:
# away_stan.div2_wins += 1
# else:
# if home_div.division_abbrev == 'E':
# away_stan.div3_wins += 1
# Used for two league plus divisions
if away_div.league_abbrev == 'AL':
if away_div.division_abbrev == 'E':
home_stan.div1_losses += 1
else:
home_stan.div2_losses += 1
else:
if away_div.division_abbrev == 'E':
home_stan.div3_losses += 1
else:
home_stan.div4_losses += 1
if home_div.league_abbrev == 'AL':
if home_div.division_abbrev == 'E':
away_stan.div1_wins += 1
else:
away_stan.div2_wins += 1
else:
if home_div.division_abbrev == 'E':
away_stan.div3_wins += 1
else:
away_stan.div4_wins += 1
# - adjust run_diff
home_stan.run_diff -= self.awayscore - self.homescore
away_stan.run_diff += self.awayscore - self.homescore
home_stan.save()
away_stan.save()
# else:
# away_stan.div4_wins += 1
#
# # - adjust run_diff
# home_stan.run_diff -= self.awayscore - self.homescore
# away_stan.run_diff += self.awayscore - self.homescore
#
# home_stan.save()
# away_stan.save()
class Player(BaseModel):
@ -1859,6 +1856,7 @@ class StratGame(BaseModel):
away_div = Division.get_by_id(self.away_team.division.id)
home_div = Division.get_by_id(self.home_team.division.id)
# Home Team Won
if self.home_score > self.away_score:
# - generic w/l & home/away w/l
home_stan.wins += 1
@ -1915,7 +1913,7 @@ class StratGame(BaseModel):
away_stan.div1_losses += 1
elif home_div.division_abbrev == 'NLW':
away_stan.div2_losses += 1
elif away_div.division_abbrev == 'IWGP':
elif home_div.division_abbrev == 'IWGP':
away_stan.div3_losses += 1
else:
away_stan.div4_losses += 1
@ -1946,6 +1944,7 @@ class StratGame(BaseModel):
# - adjust run_diff
home_stan.run_diff += self.home_score - self.away_score
away_stan.run_diff -= self.home_score - self.away_score
# Away Team Won
else:
# - generic w/l & home/away w/l
home_stan.losses += 1
@ -2002,7 +2001,7 @@ class StratGame(BaseModel):
away_stan.div1_wins += 1
elif home_div.division_abbrev == 'NLW':
away_stan.div2_wins += 1
elif away_div.division_abbrev == 'IWGP':
elif home_div.division_abbrev == 'IWGP':
away_stan.div3_wins += 1
else:
away_stan.div4_wins += 1

View File

@ -44,7 +44,7 @@ async def get_games(
team1_id: list = Query(default=None), team2_id: list = Query(default=None), played: Optional[bool] = None,
away_manager_id: list = Query(default=None), home_manager_id: list = Query(default=None),
manager1_id: list = Query(default=None), manager2_id: list = Query(default=None),
division_id: Optional[int] = None, short_output: Optional[bool] = False):
division_id: Optional[int] = None, short_output: Optional[bool] = False, sort: Optional[str] = None):
all_games = StratGame.select()
if season is not None:
@ -93,6 +93,11 @@ async def get_games(
if game_num is not None:
all_games = all_games.where(StratGame.game_num << game_num)
if sort == 'recent-first':
all_games.order_by(-StratGame.season, -StratGame.week, -StratGame.game_num)
else:
all_games.order_by(StratGame.season, StratGame.week, StratGame.game_num)
return_games = {
'count': all_games.count(),
'games': [model_to_dict(x, recurse=not short_output) for x in all_games]