From 6e4904282eb7b33eb3d250098f96f0518d7c254d Mon Sep 17 00:00:00 2001 From: Cal Corum Date: Wed, 6 Nov 2024 14:58:24 -0600 Subject: [PATCH] Complete play is complete including re24 and wpa --- command_logic/logic_gameplay.py | 28 ++++++++++----- tests/command_logic/test_logic_gameplay.py | 40 +++++++++++++++++++--- 2 files changed, 56 insertions(+), 12 deletions(-) diff --git a/command_logic/logic_gameplay.py b/command_logic/logic_gameplay.py index 99e6608..5304a56 100644 --- a/command_logic/logic_gameplay.py +++ b/command_logic/logic_gameplay.py @@ -58,24 +58,31 @@ def get_re24(this_play: Play, runs_scored: int, new_obc: int, new_starting_outs: return round(end_re24 - start_re24 + runs_scored, 3) -def get_wpa(old_play: Play, new_play: Play): - new_rd = new_play.home_score - new_play.away_score +def get_wpa(this_play: Play, next_play: Play): + """ + Returns wpa relative to batting team of this_play. Negative value if bad play, positive value if good play. + """ + new_rd = next_play.home_score - next_play.away_score if new_rd > 6: new_rd = 6 elif new_rd < -6: new_rd = -6 - old_rd = old_play.home_score - old_play.away_score + old_rd = this_play.home_score - this_play.away_score if old_rd > 6: old_rd = 6 elif old_rd < -6: old_rd = -6 - new_win_ex = WPA_DF.loc[f'{new_play.inning_half}_{new_play.inning_num}_{new_play.starting_outs}_out_{new_play.on_base_code}_obc_{new_rd}_home_run_diff'].home_win_ex + new_win_ex = WPA_DF.loc[f'{next_play.inning_half}_{next_play.inning_num}_{next_play.starting_outs}_out_{next_play.on_base_code}_obc_{new_rd}_home_run_diff'].home_win_ex - old_win_ex = WPA_DF.loc[f'{old_play.inning_half}_{old_play.inning_num}_{old_play.starting_outs}_out_{old_play.on_base_code}_obc_{old_rd}_home_run_diff'].home_win_ex + old_win_ex = WPA_DF.loc[f'{this_play.inning_half}_{this_play.inning_num}_{this_play.starting_outs}_out_{this_play.on_base_code}_obc_{old_rd}_home_run_diff'].home_win_ex - return round(new_win_ex - old_win_ex, 3) + wpa = round(new_win_ex - old_win_ex, 3) + if this_play.inning_half == 'top': + return wpa * -1 + + return wpa def complete_play(session:Session, this_play: Play): @@ -83,11 +90,11 @@ def complete_play(session:Session, this_play: Play): runs_scored = 0 on_first, on_second, on_third = None, None, None + is_go_ahead = False if nso >= 3: switch_sides = True obc = 0 nso = 0 - is_go_ahead = False nih = 'bot' if this_play.inning_half.lower() == 'top' else 'top' away_score = this_play.away_score home_score = this_play.home_score @@ -173,14 +180,19 @@ def complete_play(session:Session, this_play: Play): on_first=on_first, on_second=on_second, on_third=on_third, - managerai=this_play.managerai + managerai=this_play.managerai, + re24=get_re24(this_play, runs_scored, new_obc=obc, new_starting_outs=nso) ) + this_play.wpa = get_wpa(this_play, new_play) this_play.locked = False this_play.complete = True session.add(this_play) session.add(new_play) session.commit() + session.refresh(new_play) + + return new_play async def get_lineups_from_sheets(session: Session, sheets, this_game: Game, this_team: Team, lineup_num: int, roster_num: int) -> list[Lineup]: diff --git a/tests/command_logic/test_logic_gameplay.py b/tests/command_logic/test_logic_gameplay.py index 0aa3021..2d7d7cf 100644 --- a/tests/command_logic/test_logic_gameplay.py +++ b/tests/command_logic/test_logic_gameplay.py @@ -1,7 +1,7 @@ import pytest from sqlmodel import Session -from command_logic.logic_gameplay import advance_runners, get_obc, get_re24, get_wpa +from command_logic.logic_gameplay import advance_runners, get_obc, get_re24, get_wpa, complete_play from tests.factory import session_fixture, Game @@ -38,10 +38,42 @@ def test_get_re24(session: Session): assert get_re24(this_play, runs_scored=0, new_obc=6, new_starting_outs=2) == 0.217 -def test_get_re24(session: Session): +def test_get_wpa(session: Session): + game_1 = session.get(Game, 1) + next_play = game_1.current_play_or_none(session) # Starting wpa: 0.564 + this_play = game_1.plays[0] # Starting wpa: 0.500 + + assert this_play != next_play + assert get_wpa(this_play, next_play) == -0.064 + + next_play.starting_outs = 2 + next_play.away_score = 3 # Starting wpa: 0.347 + + assert get_wpa(this_play, next_play) == 0.153 + + +def test_complete_play(session: Session): game_1 = session.get(Game, 1) this_play = game_1.current_play_or_none(session) - old_play = game_1.plays[0] - assert old_play.id != this_play + assert this_play.inning_half == 'top' + assert this_play.inning_num == 1 + assert this_play.starting_outs == 1 + + this_play.pa, this_play.ab, this_play.so, this_play.outs = 1, 1, 1, 1 + next_play = complete_play(session, this_play) + + assert next_play.inning_half == 'top' + assert this_play.inning_num == 1 + assert next_play.starting_outs == 2 + assert next_play.is_tied + assert next_play.managerai == this_play.managerai + assert this_play.re24 == -0.154 + + next_play.pa, next_play.ab, next_play.double, next_play.batter_final = 1, 1, 1, 2 + third_play = complete_play(session, next_play) + + assert third_play.on_base_code == 2 + assert next_play.re24 == 0.182 +