diff --git a/backend/app/core/game_engine.py b/backend/app/core/game_engine.py index baf3778..86ba98f 100644 --- a/backend/app/core/game_engine.py +++ b/backend/app/core/game_engine.py @@ -520,6 +520,7 @@ class GameEngine: self._rolls_this_inning[game_id].append(ab_roll) # Capture state before applying result + outs_before = state.outs # Capture BEFORE _apply_play_result modifies it state_before = { "inning": state.inning, "half": state.half, @@ -538,7 +539,7 @@ class GameEngine: db_ops_tx = DatabaseOperations(session) # Save play to DB (uses snapshot from GameState) - await self._save_play_to_db(state, result, db_ops=db_ops_tx) + await self._save_play_to_db(state, result, outs_before=outs_before, db_ops=db_ops_tx) # Update game state in DB only if something changed if ( @@ -1032,7 +1033,11 @@ class GameEngine: raise DatabaseError("save_rolls_batch", e) async def _save_play_to_db( - self, state: GameState, result: PlayResult, db_ops: DatabaseOperations | None = None + self, + state: GameState, + result: PlayResult, + outs_before: int, + db_ops: DatabaseOperations | None = None, ) -> None: """ Save play to database using snapshot from GameState. @@ -1042,7 +1047,8 @@ class GameEngine: Args: state: Current game state result: Play result to save - session: Optional external session for transaction grouping + outs_before: Number of outs BEFORE this play (captured before _apply_play_result) + db_ops: Optional DatabaseOperations for transaction grouping Raises: ValueError: If required player IDs are missing @@ -1091,7 +1097,7 @@ class GameEngine: "play_number": state.play_count, "inning": state.inning, "half": state.half, - "outs_before": state.outs, # Capture current outs BEFORE applying result + "outs_before": outs_before, # Passed from _finalize_play (captured before _apply_play_result) "outs_recorded": result.outs_recorded, "batting_order": state.current_batter.batting_order if state.current_batter else 1, # Player IDs from snapshot