Continuing progress on complete_play plus tests
This commit is contained in:
parent
650ffdb147
commit
d0f635034b
@ -7,27 +7,51 @@ from typing import Literal
|
||||
from exceptions import *
|
||||
from in_game.game_helpers import legal_check
|
||||
from in_game.gameplay_models import Game, Lineup, Team, Play
|
||||
from in_game.gameplay_queries import get_card_or_none, get_channel_game_or_none, get_one_lineup, get_team_or_none, get_players_last_pa
|
||||
from in_game.gameplay_queries import get_card_or_none, get_channel_game_or_none, get_last_team_play, get_one_lineup, get_team_or_none, get_players_last_pa
|
||||
from utilities.buttons import ButtonOptions, Confirm
|
||||
from utilities.embeds import image_embed
|
||||
from utilities.pages import Pagination
|
||||
|
||||
|
||||
def complete_play(session:Session, this_play: Play):
|
||||
nbo = this_play.batting_order + 1 if this_play.pa == 1 else this_play.batting_order
|
||||
if nbo > 9:
|
||||
nbo = 1
|
||||
|
||||
nso = this_play.starting_outs + this_play.outs
|
||||
nih = this_play.inning_half
|
||||
|
||||
if nso >= 3:
|
||||
switch_sides = True
|
||||
nso = 0
|
||||
nih = 'bot' if this_play.inning_half.lower() == 'top' else 'top'
|
||||
try:
|
||||
opponent_play = get_last_team_play(session, this_play.game, this_play.pitcher.team)
|
||||
nbo = opponent_play.batting_order + 1
|
||||
except PlayNotFoundException as e:
|
||||
logging.info(f'logic_gameplay - complete_play - No last play found for {this_play.pitcher.team.sname}, setting upcoming batting order to 1')
|
||||
nbo = 1
|
||||
finally:
|
||||
new_batter_team = this_play.game.away_team if nih == 'top' else this_play.game.home_team
|
||||
new_pitcher_team = this_play.game.away_team if nih == 'bot' else this_play.game.home_team
|
||||
else:
|
||||
switch_sides = False
|
||||
nbo = this_play.batting_order + 1 if this_play.pa == 1 else this_play.batting_order
|
||||
nih = this_play.inning_half
|
||||
new_batter_team = this_play.batter.team
|
||||
new_pitcher_team = this_play.pitcher.team
|
||||
|
||||
if nbo > 9:
|
||||
nbo = 1
|
||||
|
||||
# TODO: Set baserunners
|
||||
|
||||
new_play = Play(
|
||||
game=this_play.game,
|
||||
play_num=this_play.play_num + 1,
|
||||
batting_order=nbo
|
||||
batting_order=nbo,
|
||||
inning_half=nih,
|
||||
starting_outs=nso,
|
||||
batter=get_one_lineup(session, this_play.game, new_batter_team, batting_order=nbo),
|
||||
pitcher=get_one_lineup(session, this_play.game, new_pitcher_team, position='P'),
|
||||
catcher=get_one_lineup(session, this_play.game, new_pitcher_team, position='C'),
|
||||
is_new_inning=switch_sides,
|
||||
managerai=this_play.managerai
|
||||
)
|
||||
|
||||
this_play.locked = False
|
||||
|
||||
@ -256,5 +256,27 @@ def get_players_last_pa(session: Session, lineup_member: Lineup, none_okay: bool
|
||||
else:
|
||||
log_exception(PlayNotFoundException, f'No play found for {lineup_member.player.name_with_desc}\'s last AB')
|
||||
|
||||
def get_one_lineup(session: Session, this_game: Game, this_team: Team, position: str, active: bool = True) -> Lineup:
|
||||
return session.exec(select(Lineup).where(Lineup.game == this_game, Lineup.team == this_team, Lineup.position == position, Lineup.active == active)).one()
|
||||
|
||||
def get_one_lineup(session: Session, this_game: Game, this_team: Team, active: bool = True, position: str = None, batting_order: int = None) -> Lineup:
|
||||
if position is None and batting_order is None:
|
||||
raise KeyError('Position or batting order must be provided for get_one_lineup')
|
||||
|
||||
st = select(Lineup).where(Lineup.game == this_game, Lineup.team == this_team, Lineup.active == active)
|
||||
if position is not None:
|
||||
st = st.where(Lineup.position == position)
|
||||
else:
|
||||
st = st.where(Lineup.batting_order == batting_order)
|
||||
|
||||
return session.exec(st).one()
|
||||
|
||||
|
||||
def get_last_team_play(session: Session, this_game: Game, this_team: Team, none_okay: bool = False):
|
||||
last_play = session.exec(select(Play).join(Lineup, onclause=Lineup.id == Play.batter_id).where(Play.game == this_game, Lineup.team == this_team).order_by(Play.id.desc()).limit(1)).all()
|
||||
|
||||
if len(last_play) == 1:
|
||||
return last_play[0]
|
||||
else:
|
||||
if none_okay:
|
||||
return None
|
||||
else:
|
||||
log_exception(PlayNotFoundException, f'No last play found for the {this_team.sname}')
|
||||
@ -1,7 +1,8 @@
|
||||
import pytest
|
||||
from sqlmodel import Session, select
|
||||
|
||||
from in_game.gameplay_models import Game, Lineup
|
||||
from in_game.gameplay_queries import get_game_lineups
|
||||
from in_game.gameplay_queries import get_game_lineups, get_one_lineup
|
||||
from tests.factory import session_fixture
|
||||
|
||||
|
||||
@ -44,6 +45,21 @@ def test_get_game_lineups(session: Session):
|
||||
assert len(inactive_home_lineups) == 0
|
||||
|
||||
|
||||
def test_get_one_lineup(session: Session):
|
||||
this_game = session.get(Game, 1)
|
||||
|
||||
ss = get_one_lineup(session, this_game, this_game.away_team, position='SS')
|
||||
assert ss.batting_order == 5
|
||||
|
||||
leadoff = get_one_lineup(session, this_game, this_game.away_team, batting_order=1)
|
||||
assert leadoff.position == 'C'
|
||||
|
||||
with pytest.raises(KeyError) as exc_info:
|
||||
get_one_lineup(session, session, this_game, this_game.away_team)
|
||||
|
||||
assert str(exc_info) == "<ExceptionInfo KeyError('Position or batting order must be provided for get_one_lineup') tblen=2>"
|
||||
|
||||
|
||||
# def test_lineup_substitution(session: Session, new_games_with_lineups: list[Game]):
|
||||
# game_1 = new_games_with_lineups[0]
|
||||
# game_2 = new_games_with_lineups[1]
|
||||
|
||||
@ -1,6 +1,7 @@
|
||||
from sqlmodel import Session, select
|
||||
|
||||
from in_game.gameplay_models import Lineup, Play, Game
|
||||
from in_game.gameplay_queries import get_last_team_play
|
||||
from tests.factory import session_fixture
|
||||
|
||||
|
||||
@ -40,4 +41,17 @@ def test_scorebug_ascii(session: Session):
|
||||
assert new_play.scorebug_ascii == '```\nNCB3 0 ○ ▲ 6\n WV4 0 ○ ○ 1 Out\n```'
|
||||
|
||||
|
||||
def test_last_team_play(session: Session):
|
||||
this_game = session.get(Game, 1)
|
||||
this_team = this_game.away_team
|
||||
|
||||
last_play = get_last_team_play(session, this_game, this_team)
|
||||
assert last_play is not None
|
||||
assert last_play.play_num == 2
|
||||
|
||||
this_team = this_game.home_team
|
||||
|
||||
assert get_last_team_play(session, this_game, this_team, none_okay=True) is None
|
||||
|
||||
|
||||
# TODO: test get_ai_note
|
||||
Loading…
Reference in New Issue
Block a user