From 49bc017fd384f4d4cadfaca8f2172692cdc223bb Mon Sep 17 00:00:00 2001 From: Cal Corum Date: Sun, 22 Oct 2023 16:42:38 -0500 Subject: [PATCH] Update db_calls_gameplay.py Update run and earned run calculation --- db_calls_gameplay.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/db_calls_gameplay.py b/db_calls_gameplay.py index a4c7506..5622b19 100644 --- a/db_calls_gameplay.py +++ b/db_calls_gameplay.py @@ -1206,11 +1206,24 @@ def get_last_inning_end_play(game_id, inning_half, inning_num): return return_play +def add_run_last_player_ab(lineup: Lineup, is_erun: bool = True): + try: + last_ab = Play.select().where(Play.batter == lineup).order_by(-Play.id).get() + except DoesNotExist as e: + logging.error(f'Unable to apply run to Lineup {lineup}') + else: + last_ab.run = 1 + last_ab.e_run = 1 if is_erun else 0 + last_ab.save() + + def get_dbready_plays(game_id: int, db_game_id: int): all_plays = Play.select().where(Play.game_id == game_id) prep_plays = [model_to_dict(x) for x in all_plays] db.close() + obc_list = ['000', '001', '010', '100', '011', '101', '110', '111'] + for x in prep_plays: x['pitcher_id'] = x['pitcher']['player_id'] x['batter_id'] = x['batter']['player_id'] @@ -1226,6 +1239,7 @@ def get_dbready_plays(game_id: int, db_game_id: int): x['runner_id'] = x['runner']['player_id'] x['runner_team_id'] = x['runner']['team_id'] x['game_id'] = db_game_id + x['on_base_code'] = obc_list[x['on_base_code']] logging.debug(f'all_plays:\n\n{prep_plays}\n') return prep_plays @@ -1449,7 +1463,10 @@ def complete_play(play_id, batter_to_base: int = None): # patch to handle little league home runs TODO: standardize on just _on_final for these logging.info(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 + if not this_play.error: + this_play.e_run = 1 else: score_increment = 0 logging.info(f'complete_play - score_increment: {score_increment}') @@ -1458,16 +1475,19 @@ def complete_play(play_id, batter_to_base: int = None): this_play.on_first_final = None elif this_play.on_first_final == 4: score_increment += 1 + add_run_last_player_ab(this_play.on_first, this_play.error == 0) if this_play.on_second_final == 99: this_play.on_second_final = None elif this_play.on_second_final == 4: score_increment += 1 + add_run_last_player_ab(this_play.on_second, this_play.error == 0) if this_play.on_third_final == 99: this_play.on_third_final = None elif this_play.on_third_final == 4: score_increment += 1 + add_run_last_player_ab(this_play.on_third, this_play.error == 0) this_play.save() # for runner in [this_play.on_first_final, this_play.on_second_final, this_play.on_third_final]: