Update db_calls_gameplay.py

Update run and earned run calculation
This commit is contained in:
Cal Corum 2023-10-22 16:42:38 -05:00
parent 30ea1f300a
commit 49bc017fd3

View File

@ -1206,11 +1206,24 @@ def get_last_inning_end_play(game_id, inning_half, inning_num):
return return_play 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): def get_dbready_plays(game_id: int, db_game_id: int):
all_plays = Play.select().where(Play.game_id == game_id) all_plays = Play.select().where(Play.game_id == game_id)
prep_plays = [model_to_dict(x) for x in all_plays] prep_plays = [model_to_dict(x) for x in all_plays]
db.close() db.close()
obc_list = ['000', '001', '010', '100', '011', '101', '110', '111']
for x in prep_plays: for x in prep_plays:
x['pitcher_id'] = x['pitcher']['player_id'] x['pitcher_id'] = x['pitcher']['player_id']
x['batter_id'] = x['batter']['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_id'] = x['runner']['player_id']
x['runner_team_id'] = x['runner']['team_id'] x['runner_team_id'] = x['runner']['team_id']
x['game_id'] = db_game_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') logging.debug(f'all_plays:\n\n{prep_plays}\n')
return prep_plays 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 # 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.info(f'complete_play - this_play: {this_play}')
if this_play.batter_final == 4 or batter_to_base == 4: if this_play.batter_final == 4 or batter_to_base == 4:
this_play.run = 1
score_increment = 1 score_increment = 1
if not this_play.error:
this_play.e_run = 1
else: else:
score_increment = 0 score_increment = 0
logging.info(f'complete_play - score_increment: {score_increment}') 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 this_play.on_first_final = None
elif this_play.on_first_final == 4: elif this_play.on_first_final == 4:
score_increment += 1 score_increment += 1
add_run_last_player_ab(this_play.on_first, this_play.error == 0)
if this_play.on_second_final == 99: if this_play.on_second_final == 99:
this_play.on_second_final = None this_play.on_second_final = None
elif this_play.on_second_final == 4: elif this_play.on_second_final == 4:
score_increment += 1 score_increment += 1
add_run_last_player_ab(this_play.on_second, this_play.error == 0)
if this_play.on_third_final == 99: if this_play.on_third_final == 99:
this_play.on_third_final = None this_play.on_third_final = None
elif this_play.on_third_final == 4: elif this_play.on_third_final == 4:
score_increment += 1 score_increment += 1
add_run_last_player_ab(this_play.on_third, this_play.error == 0)
this_play.save() this_play.save()
# for runner in [this_play.on_first_final, this_play.on_second_final, this_play.on_third_final]: # for runner in [this_play.on_first_final, this_play.on_second_final, this_play.on_third_final]: