Complete play is complete including re24 and wpa

This commit is contained in:
Cal Corum 2024-11-06 14:58:24 -06:00
parent e399fec853
commit 6e4904282e
2 changed files with 56 additions and 12 deletions

View File

@ -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) return round(end_re24 - start_re24 + runs_scored, 3)
def get_wpa(old_play: Play, new_play: Play): def get_wpa(this_play: Play, next_play: Play):
new_rd = new_play.home_score - new_play.away_score """
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: if new_rd > 6:
new_rd = 6 new_rd = 6
elif new_rd < -6: elif new_rd < -6:
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: if old_rd > 6:
old_rd = 6 old_rd = 6
elif old_rd < -6: elif old_rd < -6:
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): def complete_play(session:Session, this_play: Play):
@ -83,11 +90,11 @@ def complete_play(session:Session, this_play: Play):
runs_scored = 0 runs_scored = 0
on_first, on_second, on_third = None, None, None on_first, on_second, on_third = None, None, None
is_go_ahead = False
if nso >= 3: if nso >= 3:
switch_sides = True switch_sides = True
obc = 0 obc = 0
nso = 0 nso = 0
is_go_ahead = False
nih = 'bot' if this_play.inning_half.lower() == 'top' else 'top' nih = 'bot' if this_play.inning_half.lower() == 'top' else 'top'
away_score = this_play.away_score away_score = this_play.away_score
home_score = this_play.home_score home_score = this_play.home_score
@ -173,14 +180,19 @@ def complete_play(session:Session, this_play: Play):
on_first=on_first, on_first=on_first,
on_second=on_second, on_second=on_second,
on_third=on_third, 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.locked = False
this_play.complete = True this_play.complete = True
session.add(this_play) session.add(this_play)
session.add(new_play) session.add(new_play)
session.commit() 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]: async def get_lineups_from_sheets(session: Session, sheets, this_game: Game, this_team: Team, lineup_num: int, roster_num: int) -> list[Lineup]:

View File

@ -1,7 +1,7 @@
import pytest import pytest
from sqlmodel import Session 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 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 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) game_1 = session.get(Game, 1)
this_play = game_1.current_play_or_none(session) 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