Update db_calls_gameplay.py

This commit is contained in:
Cal Corum 2024-05-24 22:03:58 -05:00
parent cf173d42dc
commit 899f214259

View File

@ -397,15 +397,15 @@ class StratManagerAi(pydantic.BaseModel):
logging.info(f'db_calls_gameplay - StratManagerAi - ID: {self.id} - go_to_reliever: False / code 1')
return False
elif run_lead > 5 or (run_lead > 2 and self.ahead_aggression > 5):
if tot_allowed <= lead_target or obc <= 3 or this_play.starting_outs == 2:
if tot_allowed <= lead_target or obc <= 3 or (this_play.starting_outs == 2 and not is_starter):
logging.info(f'db_calls_gameplay - StratManagerAi - ID: {self.id} - go_to_reliever: False / code 2')
return False
elif run_lead > 2 or (run_lead >= 0 and self.ahead_aggression > 5):
if tot_allowed < lead_target or obc <= 1 or this_play.starting_outs == 2:
if tot_allowed < lead_target or obc <= 1 or (this_play.starting_outs == 2 and not is_starter):
logging.info(f'db_calls_gameplay - StratManagerAi - ID: {self.id} - go_to_reliever: False / code 3')
return False
elif run_lead >= 0 or (run_lead >= -2 and self.behind_aggression > 5):
if tot_allowed < 5 or obc <= run_lead or this_play.starting_outs == 2:
if tot_allowed < 5 or obc <= run_lead or (this_play.starting_outs == 2 and not is_starter):
logging.info(f'db_calls_gameplay - StratManagerAi - ID: {self.id} - go_to_reliever: False / code 4')
return False
elif run_lead >= -3 and self.behind_aggression > 5:
@ -1571,7 +1571,7 @@ def complete_play(play_id, batter_to_base: int = None):
new_on_third = None
# score_increment = this_play.homerun
# patch to handle little league home runs TODO: standardize on just _on_final for these
logging.info(f'complete_play - this_play: {this_play}')
logging.debug(f'complete_play - this_play: {this_play}')
if this_play.batter_final == 4 or batter_to_base == 4:
this_play.run = 1
score_increment = 1
@ -1579,7 +1579,7 @@ def complete_play(play_id, batter_to_base: int = None):
this_play.e_run = 1
else:
score_increment = 0
logging.info(f'complete_play - score_increment: {score_increment}')
logging.debug(f'complete_play - score_increment: {score_increment}')
if this_play.on_first_final == 99:
this_play.on_first_final = None
@ -2069,6 +2069,9 @@ def get_fielding_stats(game_id, lineup_id: int = None, team_id: int = None):
def get_pitching_stats(
game_id, lineup_id: int = None, team_id: int = None, in_pow: bool = None, in_innings: list = None):
if in_innings is None:
in_innings = [x for x in range(1, 30)]
logging.info(f'db_calls_gameplay - get_pitching_stats - in_innings: {in_innings}')
pitching_stats = Play.select(
Play.pitcher,
fn.SUM(Play.outs).over(partition_by=[Play.pitcher_id]).alias('pl_outs'),
@ -2081,6 +2084,14 @@ def get_pitching_stats(
Play.on_third_final == 4).over(partition_by=[Play.pitcher_id]).alias('pl_run_third'),
fn.COUNT(Play.batter_final).filter(
Play.batter_final == 4).over(partition_by=[Play.pitcher_id]).alias('pl_run_batter'),
fn.COUNT(Play.on_first_final).filter(
(Play.on_first_final == 4) & (Play.inning_num << in_innings)).over(partition_by=[Play.pitcher_id]).alias('pl_in_run_first'),
fn.COUNT(Play.on_second_final).filter(
(Play.on_second_final == 4) & (Play.inning_num << in_innings)).over(partition_by=[Play.pitcher_id]).alias('pl_in_run_second'),
fn.COUNT(Play.on_third_final).filter(
(Play.on_third_final == 4) & (Play.inning_num << in_innings)).over(partition_by=[Play.pitcher_id]).alias('pl_in_run_third'),
fn.COUNT(Play.batter_final).filter(
(Play.batter_final == 4) & (Play.inning_num << in_innings)).over(partition_by=[Play.pitcher_id]).alias('pl_in_run_batter'),
fn.SUM(Play.so).over(partition_by=[Play.pitcher_id]).alias('pl_so'),
fn.SUM(Play.bb).over(partition_by=[Play.pitcher_id]).alias('pl_bb'),
fn.SUM(Play.hbp).over(partition_by=[Play.pitcher_id]).alias('pl_hbp'),
@ -2104,6 +2115,7 @@ def get_pitching_stats(
fn.SUM(Play.balk).over(partition_by=[Play.pitcher.team_id]).alias('tm_balk'),
fn.SUM(Play.homerun).over(partition_by=[Play.pitcher.team_id]).alias('tm_homerun'),
).join(Lineup, on=Play.pitcher).where(Play.game_id == game_id)
logging.debug(f'db_calls_gameplay - get_pitching_stats - pitching_stats: {pitching_stats}')
# This is counging plays with multiple runs scored on 1 ER and the rest unearned
# earned_runs_pl = Play.select().where(
@ -2136,8 +2148,10 @@ def get_pitching_stats(
if in_pow is not None:
pitching_stats = pitching_stats.where(Play.in_pow == in_pow)
if in_innings is not None:
pitching_stats = pitching_stats.where(Play.inning_num << in_innings)
# if in_innings is not None:
# pitching_stats = pitching_stats.where(Play.inning_num << in_innings)
# logging.info(f'db_calls_gameplay - get_pitching_stats - in_innings: {in_innings} / query: {pitching_stats} / '
# f'stats: {pitching_stats.count()}')
tm_earned_runs = None
if team_id is not None:
@ -2182,6 +2196,7 @@ def get_pitching_stats(
'pl_hit': x.pl_hit,
'pl_eruns': pl_earned_runs,
'pl_runs': x.pl_run_first + x.pl_run_second + x.pl_run_third + x.pl_run_batter,
'pl_in_runs': x.pl_in_run_first + x.pl_in_run_second + x.pl_in_run_third + x.pl_in_run_batter,
'pl_so': x.pl_so,
'pl_bb': x.pl_bb,
'pl_hbp': x.pl_hbp,