Rewrite core game logic to fix 10 critical bugs violating poker rules: - Add betMatched flag to separate chip tracking from bet-matching state - Implement last-aggressor tracking for proper betting round completion - Rewrite all action functions with validation enforcement - Add side pot support for multi-level all-in scenarios - Replace nested setTimeout AI turns with async promise chain - Add aiActing guard to prevent race conditions during AI play - Fix currentTurn advancement to always land on active players
2.5 KiB
2.5 KiB
Why
The current poker game logic contains multiple critical bugs that violate Texas Hold'em rules, causing rounds to skip, bets to calculate incorrectly, and chips to disappear. The game needs a complete rewrite of its core game flow to produce legitimate, playable poker.
What Changes
- Fix
applyCheck: Remove phantom chip assignment that corrupts call calculations and round completion - Fix betting round completion: Exclude all-in players from "all matched" check so active players aren't skipped
- Fix game loop state management: Always apply returned state from
completeBettingRoundinstead of discarding it when rounds don't advance - Implement side pot support: Create and distribute side pots when players go all-in at different levels
- Enforce minimum raise validation: Call
validateActionbefore applying any action, reject illegal raises - Fix fold validation: Allow folding in all situations per Texas Hold'em rules
- Fix pot distribution: Award remainder chips to first winner instead of losing them
- Add input guardrails: Disable player controls during AI turns to prevent race conditions
- Rewrite turn advancement: Properly track last aggressor and end betting rounds when action returns correctly
Capabilities
New Capabilities
side-pots: Handle all-in scenarios by creating main pot and side pots with correct chip distribution among eligible playersbetting-round-flow: Complete rewrite of betting round completion logic with proper last-aggressor tracking, all-in handling, and turn advancementgame-loop-integrity: Unified game state machine that prevents race conditions, always applies state transitions, and disables controls during AI turns
Modified Capabilities
Impact
src/lib/game/actions.ts— Complete rewrite of check, call, raise, fold, all-in functionssrc/lib/game/betting-round.ts— Rewrite completion logic with side pot awareness and last-aggressor trackingsrc/lib/game/state.ts— Add last aggressor tracking, side pot structures to GameStatesrc/lib/types/game-state.ts— Extend withlastAggressor,sidePots, betting round metadatasrc/lib/game/showdown.ts— Fix pot distribution, handle side pots at showdownsrc/lib/game/validation.ts— Fix fold validation, enforce minimum raise rulessrc/routes/+page.svelte— Add input disabling during AI turns, proper state machine flowsrc/lib/game/turn.ts— Rewrite turn advancement logic