CLAUDE: Fix batting order recovery after inning changes
When recovering game state after reconnect/refresh, the old code: 1. Only looked at the last play overall (regardless of team) 2. Used that play's batting order for the currently batting team 3. Reset the non-batting team's index to 0 This caused the wrong batter to appear after inning changes - e.g., batter #1 showing instead of #5 in top of 2nd after bottom of 1st ended. Fix: Now recovers each team's batter index separately by filtering plays by half ("top" = away team, "bottom" = home team) and finding each team's last at-bat independently. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
parent
4d2c905a1c
commit
dc9f98ad88
@ -435,26 +435,48 @@ class StateManager:
|
|||||||
else:
|
else:
|
||||||
state.outs = outs_after
|
state.outs = outs_after
|
||||||
|
|
||||||
# Recover batter indices - find which batter is next based on last play
|
# Recover batter indices - find last play for EACH team separately
|
||||||
last_batter_order = last_play.get("batting_order")
|
# This is critical after inning changes - we need both teams' positions
|
||||||
logger.info(f"Recovery: last_batter_order from play #{last_play['play_number']} = {last_batter_order}")
|
away_plays = [p for p in completed_plays if p.get("half") == "top"]
|
||||||
|
home_plays = [p for p in completed_plays if p.get("half") == "bottom"]
|
||||||
|
|
||||||
if last_batter_order:
|
# Away team's last at-bat
|
||||||
|
if away_plays:
|
||||||
|
last_away_play = max(away_plays, key=lambda p: p["play_number"])
|
||||||
|
away_last_order = last_away_play.get("batting_order")
|
||||||
|
if away_last_order:
|
||||||
# Next batter is last_batter + 1, wrapping at 9
|
# Next batter is last_batter + 1, wrapping at 9
|
||||||
next_batter_order = (last_batter_order % 9) + 1
|
state.away_team_batter_idx = away_last_order % 9 # Already 0-indexed after mod
|
||||||
|
logger.info(f"Recovery: Away team last batter was #{away_last_order}, next is #{state.away_team_batter_idx + 1}")
|
||||||
|
else:
|
||||||
|
state.away_team_batter_idx = 0
|
||||||
|
else:
|
||||||
|
# Away team hasn't batted yet (game just started or first pitch)
|
||||||
|
state.away_team_batter_idx = 0
|
||||||
|
logger.info("Recovery: Away team has no plays yet, starting at batter #1")
|
||||||
|
|
||||||
# Set the correct index for the batting team
|
# Home team's last at-bat
|
||||||
# Index is order - 1 (0-indexed)
|
if home_plays:
|
||||||
next_batter_idx = next_batter_order - 1
|
last_home_play = max(home_plays, key=lambda p: p["play_number"])
|
||||||
|
home_last_order = last_home_play.get("batting_order")
|
||||||
if batting_team_id == away_team_id:
|
if home_last_order:
|
||||||
state.away_team_batter_idx = next_batter_idx
|
# Next batter is last_batter + 1, wrapping at 9
|
||||||
|
state.home_team_batter_idx = home_last_order % 9 # Already 0-indexed after mod
|
||||||
|
logger.info(f"Recovery: Home team last batter was #{home_last_order}, next is #{state.home_team_batter_idx + 1}")
|
||||||
|
else:
|
||||||
state.home_team_batter_idx = 0
|
state.home_team_batter_idx = 0
|
||||||
else:
|
else:
|
||||||
state.home_team_batter_idx = next_batter_idx
|
# Home team hasn't batted yet
|
||||||
state.away_team_batter_idx = 0
|
state.home_team_batter_idx = 0
|
||||||
|
logger.info("Recovery: Home team has no plays yet, starting at batter #1")
|
||||||
|
|
||||||
logger.info(f"Recovery: Set batter indices - next_order={next_batter_order}, next_idx={next_batter_idx}, batting_team={batting_team_id}")
|
# Determine which index to use for current_batter
|
||||||
|
if batting_team_id == away_team_id:
|
||||||
|
next_batter_idx = state.away_team_batter_idx
|
||||||
|
else:
|
||||||
|
next_batter_idx = state.home_team_batter_idx
|
||||||
|
|
||||||
|
logger.info(f"Recovery: Set batter indices - away={state.away_team_batter_idx}, home={state.home_team_batter_idx}, current batting team uses idx={next_batter_idx}")
|
||||||
|
|
||||||
# Update current_batter to match the recovered batter index
|
# Update current_batter to match the recovered batter index
|
||||||
# Get batting lineup sorted by batting_order
|
# Get batting lineup sorted by batting_order
|
||||||
@ -480,7 +502,7 @@ class StateManager:
|
|||||||
if next_batter_idx < len(batting_lineup_sorted):
|
if next_batter_idx < len(batting_lineup_sorted):
|
||||||
state.current_batter = batting_lineup_sorted[next_batter_idx]
|
state.current_batter = batting_lineup_sorted[next_batter_idx]
|
||||||
logger.info(
|
logger.info(
|
||||||
f"Recovery: ✓ Set current_batter to order={next_batter_order}, idx={next_batter_idx}, "
|
f"Recovery: ✓ Set current_batter to idx={next_batter_idx}, "
|
||||||
f"card_id={state.current_batter.card_id}, batting_order={state.current_batter.batting_order}"
|
f"card_id={state.current_batter.card_id}, batting_order={state.current_batter.batting_order}"
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
@ -488,12 +510,6 @@ class StateManager:
|
|||||||
f"Recovery: ✗ Batter index {next_batter_idx} out of range for batting order "
|
f"Recovery: ✗ Batter index {next_batter_idx} out of range for batting order "
|
||||||
f"(lineup size: {len(batting_lineup_sorted)})"
|
f"(lineup size: {len(batting_lineup_sorted)})"
|
||||||
)
|
)
|
||||||
logger.info(f"Recovery: Calculated next_order={next_batter_order}, next_idx={next_batter_idx}")
|
|
||||||
else:
|
|
||||||
logger.info("Recovery: No last_batter_order found - using defaults")
|
|
||||||
# Fallback if no batting order in last play
|
|
||||||
state.away_team_batter_idx = 0
|
|
||||||
state.home_team_batter_idx = 0
|
|
||||||
|
|
||||||
# Always start at awaiting_defensive on recovery
|
# Always start at awaiting_defensive on recovery
|
||||||
# (Users can re-submit decisions if they refreshed mid-workflow)
|
# (Users can re-submit decisions if they refreshed mid-workflow)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user