## 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 `completeBettingRound` instead 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 `validateAction` before 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 players - `betting-round-flow`: Complete rewrite of betting round completion logic with proper last-aggressor tracking, all-in handling, and turn advancement - `game-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 functions - `src/lib/game/betting-round.ts` — Rewrite completion logic with side pot awareness and last-aggressor tracking - `src/lib/game/state.ts` — Add last aggressor tracking, side pot structures to GameState - `src/lib/types/game-state.ts` — Extend with `lastAggressor`, `sidePots`, betting round metadata - `src/lib/game/showdown.ts` — Fix pot distribution, handle side pots at showdown - `src/lib/game/validation.ts` — Fix fold validation, enforce minimum raise rules - `src/routes/+page.svelte` — Add input disabling during AI turns, proper state machine flow - `src/lib/game/turn.ts` — Rewrite turn advancement logic