diff --git a/command_logic/logic_gameplay.py b/command_logic/logic_gameplay.py index 35b8ef9..3795c00 100644 --- a/command_logic/logic_gameplay.py +++ b/command_logic/logic_gameplay.py @@ -2879,7 +2879,7 @@ async def xchecks(session: Session, interaction: discord.Interaction, this_play: def activate_last_play(session: Session, this_game: Game) -> Play: logger.info(f'Pulling last play to complete and advance') - p_query = session.exec(select(Play).where(Play.game == this_game).order_by(Play.id.desc()).limit(1)).all() + p_query = session.exec(select(Play).where(Play.game == this_game).order_by(Play.play_num.desc()).limit(1)).all() logger.info(f'last play: {p_query[0].id}') this_play = complete_play(session, p_query[0]) @@ -2891,7 +2891,7 @@ def undo_play(session: Session, this_play: Play): this_game = this_play.game after_play_min = max(1, this_play.play_num - 2) - last_two_plays = session.exec(select(Play).where(Play.game == this_game).order_by(Play.id.desc()).limit(2)).all() + last_two_plays = session.exec(select(Play).where(Play.game == this_game).order_by(Play.play_num.desc()).limit(2)).all() for play in last_two_plays: for runner, to_base in [(play.on_first, play.on_first_final), (play.on_second, play.on_second_final), (play.on_third, play.on_third_final)]: diff --git a/in_game/gameplay_models.py b/in_game/gameplay_models.py index 23f164d..d209843 100644 --- a/in_game/gameplay_models.py +++ b/in_game/gameplay_models.py @@ -161,7 +161,7 @@ class Game(SQLModel, table=True): return f'{pri_cardsets}{back_cardsets}' def current_play_or_none(self, session: Session): - this_play = session.exec(select(Play).where(Play.game == self, Play.complete == False).order_by(Play.id.desc()).limit(1)).all() + this_play = session.exec(select(Play).where(Play.game == self, Play.complete == False).order_by(Play.play_num.desc()).limit(1)).all() if len(this_play) == 1: return this_play[0] else: diff --git a/in_game/gameplay_queries.py b/in_game/gameplay_queries.py index cf1d233..8edefe1 100644 --- a/in_game/gameplay_queries.py +++ b/in_game/gameplay_queries.py @@ -574,7 +574,7 @@ def get_game_lineups(session: Session, this_game: Game, specific_team: Team = No def get_players_last_pa(session: Session, lineup_member: Lineup, none_okay: bool = False): logger.info(f'Getting last AB for {lineup_member.player.name_with_desc} on the {lineup_member.team.lname}') - last_pa = session.exec(select(Play).where(Play.game == lineup_member.game, Play.batter == lineup_member).order_by(Play.id.desc()).limit(1)).all() + last_pa = session.exec(select(Play).where(Play.game == lineup_member.game, Play.batter == lineup_member).order_by(Play.play_num.desc()).limit(1)).all() if len(last_pa) == 1: return last_pa[0] else: @@ -600,7 +600,7 @@ def get_one_lineup(session: Session, this_game: Game, this_team: Team, active: b def get_last_team_play(session: Session, this_game: Game, this_team: Team, none_okay: bool = False): logger.info(f'Getting last play for the {this_team.lname} in game {this_game.id}') - 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() + 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.play_num.desc()).limit(1)).all() if len(last_play) == 1: return last_play[0] @@ -623,7 +623,7 @@ def get_sorted_lineups(session: Session, this_game: Game, this_team: Team) -> li def get_db_ready_plays(session: Session, this_game: Game, db_game_id: int): logger.info(f'Getting db ready plays for game {this_game.id}') - all_plays = session.exec(select(Play).where(Play.game == this_game)).all() + all_plays = session.exec(select(Play).where(Play.game == this_game).order_by(Play.play_num.desc())).all() obc_list = ['000', '001', '010', '100', '011', '101', '110', '111'] @@ -652,13 +652,13 @@ def get_db_ready_plays(session: Session, this_game: Game, db_game_id: int): def get_db_ready_decisions(session: Session, this_game: Game, db_game_id: int) -> list[DecisionModel]: logger.info(f'Game {this_game.id} | Getting db ready decisions for game {this_game.id}') - winner = None - loser = None save = None away_starter = None home_starter = None away_finisher = None home_finisher = None + winner = None + loser = None b_save = [] holds = [] @@ -673,8 +673,11 @@ def get_db_ready_decisions(session: Session, this_game: Game, db_game_id: int) - away_starter = session.exec(select(Lineup).where(Lineup.game == this_game, Lineup.team == this_game.away_team, Lineup.position == 'P', Lineup.after_play == 0)).one() away_pitcher = away_starter + last_winner = None + last_loser = None + # Get starting pitchers and update this as a pointer for the play crawl - for play in session.exec(select(Play).where(Play.game == this_game)).all(): + for play in session.exec(select(Play).where(Play.game == this_game).order_by(Play.play_num)).all(): logger.info(f'Game {this_game.id} | Crawling play #{play.play_num}') runs_scored = 0 if play.inning_half == 'top': @@ -769,6 +772,8 @@ def get_db_ready_decisions(session: Session, this_game: Game, db_game_id: int) - if play.is_tied and runs_scored == 0: logger.info(f'Game {this_game.id} | Clearing winner and loser on play #{play.play_num}') + last_winner = winner + last_loser = loser winner, loser = None, None if save is not None: @@ -776,7 +781,7 @@ def get_db_ready_decisions(session: Session, this_game: Game, db_game_id: int) - b_save.append(save) save = None - if play.pitcher_id not in decisions: + if play.pitcher.player_id not in decisions: logger.info(f'Game {this_game.id} | Adding {play.pitcher.player.name} to decisions dict on play #{play.play_num}') decisions[play.pitcher.player_id] = DecisionModel( game_id=db_game_id, @@ -785,6 +790,13 @@ def get_db_ready_decisions(session: Session, this_game: Game, db_game_id: int) - pitcher_id=play.pitcher.player_id, pitcher_team_id=play.pitcher.team_id ) + + if winner is None: + logger.info(f'Setting winner to last_winner: {last_winner}') + winner = last_winner + if loser is None: + logger.info(f'Setting loser to last_loser: {last_loser}') + loser = last_loser logger.info(f'winner: {winner} / loser: {loser}') decisions[winner.player_id].win = 1 @@ -1056,7 +1068,7 @@ def get_plays_by_pitcher(session: Session, this_game: Game, this_lineup: Lineup, logger.info(f'Getting all pitching plays for {this_lineup.card.player.name_with_desc}') statement = select(Play).where(Play.game == this_game, Play.pitcher == this_lineup) if reversed: - statement = statement.order_by(Play.id.desc()) + statement = statement.order_by(Play.play_num.desc()) all_plays = session.exec(statement).all() logger.info(f'all_plays: {all_plays}')