From 46cf1cc02b949eaf4f2490574cd51bddfcb97084 Mon Sep 17 00:00:00 2001 From: Cal Corum Date: Sat, 29 Nov 2025 09:46:46 -0600 Subject: [PATCH] CLAUDE: Fix outs_before capturing AFTER value instead of BEFORE MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The outs_before field in play records was incorrectly storing the outs count AFTER _apply_play_result() modified state.outs, rather than the value before the play resolved. Fix: Capture state.outs before calling _apply_play_result() and pass it explicitly to _save_play_to_db() as a required parameter. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude --- backend/app/core/game_engine.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) 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