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
4.0 KiB
4.0 KiB
1. Type system updates
- 1.1 Add
betMatched: booleanfield toPlayerSeatinterface insrc/lib/types/player.ts - 1.2 Add
lastAggressorIndex: numberfield toGameStateinterface insrc/lib/types/game-state.ts - 1.3 Define
SidePotinterface withamountandeligiblePlayerIdsfields - 1.4 Add
sidePots: SidePot[]field toGameStateinterface
2. State initialization & reset
- 2.1 Update
createInitialStateto initialize new fields (betMatched,lastAggressorIndex,sidePots) - 2.2 Update
startNewHandto reset all per-round fields including new fields - 2.3 Update
postBlindsto setbetMatchedcorrectly for SB and BB players
3. Action function rewrites
- 3.1 Rewrite
applyCheckto setbetMatched = truewithout modifyingcurrentBetor chips - 3.2 Rewrite
applyCallto usestate.currentBet - player.currentBetfor call amount with correct chip deduction - 3.3 Rewrite
applyRaiseto updatelastAggressorIndex, reset other players'betMatched, and enforce minimum raise - 3.4 Rewrite
applyFoldto correctly mark player as folded and advance turn - 3.5 Rewrite
applyAllInto handle partial all-in raises and updatelastAggressorIndexif applicable - 3.6 Add
validateActioncall at the start of each action function with early return on invalid actions
4. Validation fixes
- 4.1 Fix fold validation to always permit folding (remove pre-flop restriction)
- 4.2 Fix raise validation to correctly calculate minimum raise based on
lastRaiseAmount - 4.3 Add validation for all-in actions that don't constitute a full raise
5. Betting round completion rewrite
- 5.1 Implement
isRoundCompletefunction with last-aggressor tracking logic - 5.2 Rewrite
completeBettingRoundto usebetMatchedflag and exclude all-in players from match check - 5.3 Implement BB option: allow BB to check pre-flop when no raises occurred
- 5.4 Ensure
currentTurnadvances correctly after round completion
6. Side pot implementation
- 6.1 Implement
calculatePotsfunction to compute main pot and side pots from player bets - 6.2 Integrate side pot calculation into all-in action handling
- 6.3 Update
awardPotto distribute main pot and all side pots correctly - 6.4 Fix remainder chip distribution (award leftover chips to first winner)
- 6.5 Update showdown logic to determine winners for each pot based on eligibility
7. Turn advancement fixes
- 7.1 Rewrite
advanceTurnto skip folded and all-in players correctly - 7.2 Ensure turn advancement respects dealer position and betting round start position
- 7.3 Handle edge case where no active players remain for turn advancement
8. Game loop rewrite (+page.svelte)
- 8.1 Add
aiActing: booleanstate flag to disable controls during AI turns - 8.2 Replace
processGamewith unifiedprocessTurnstate machine function - 8.3 Ensure all state transitions apply returned state atomically (no discarding)
- 8.4 Convert AI turn timing from nested setTimeout to sequential promise chain
- 8.5 Add guard clause in
handleActionto reject actions whenaiActingis true - 8.6 Update BetControls component to accept and respond to
disabledprop
9. Bug fixes discovered during playtesting
- 9.1 Fix deadlock:
completeBettingRoundsetscurrentTurnto first active player instead of hardcodeddealerPosition + 1 - 9.2 Simplify
processTurnto use correctedcurrentTurndirectly after round completion
10. Testing & verification
- 10.1 Test pre-flop betting round with all action combinations (check, call, raise, fold)
- 10.2 Test single all-in scenario verifies correct main pot and side pot creation
- 10.3 Test multiple all-ins at different levels verifies correct multi-side pot distribution
- 10.4 Test BB option pre-flop (BB checks when no raises)
- 10.5 Test full hand flow from deal through showdown with no state corruption
- 10.6 Test race condition prevention (rapid human clicks during AI turn are rejected)