Veit F. a07117efaf fix: implement correct Texas Hold'em betting rules and game flow
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
2026-05-17 18:46:08 +02:00

50 lines
2.6 KiB
Markdown

# game-loop-integrity Specification
## Purpose
TBD - created by archiving change fix-texas-holdem-rules. Update Purpose after archive.
## Requirements
### Requirement: Game state transitions are always applied atomically
The system SHALL always apply the complete returned state from action functions and never discard intermediate state updates.
#### Scenario: Betting round completion state is preserved
- **WHEN** `completeBettingRound` returns a new state with advanced `currentTurn` and reset bets
- **THEN** the game applies the entire returned state, including turn advancement and bet resets, even if the betting round stage does not change
#### Scenario: Action function results are never lost
- **WHEN** a player performs any action (check, call, raise, fold, all-in)
- **THEN** the returned state from the action function is applied completely before any subsequent processing occurs
### Requirement: Player controls are disabled during AI turns
The system SHALL disable human player input controls while AI players are taking their turns to prevent race conditions.
#### Scenario: Human clicks during AI turn
- **WHEN** an AI player is processing their turn (400ms delay) and the human player clicks a bet control
- **THEN** the action is rejected, the UI shows disabled controls, and no state mutation occurs
#### Scenario: Controls re-enable after AI turn completes
- **WHEN** an AI player completes their action and the turn advances to the human player
- **THEN** the bet controls become enabled and the human player may act
### Requirement: Validation is enforced before applying any action
The system SHALL validate all actions against game rules before applying state changes, returning unchanged state for invalid actions.
#### Scenario: Invalid raise is rejected
- **WHEN** a player attempts to raise below the minimum raise amount
- **THEN** the action is rejected, the state remains unchanged, and an error reason is returned
#### Scenario: Fold always permitted
- **WHEN** a player attempts to fold during any betting round, including pre-flop
- **THEN** the action is accepted regardless of current bet level
### Requirement: Pot distribution preserves all chips
When awarding pots to winners, the system SHALL distribute all chips without losing remainder amounts.
#### Scenario: Odd pot split between two winners
- **WHEN** the pot contains 105 chips and two players tie for the win
- **THEN** one winner receives 53 chips, the other receives 52 chips, and zero chips are lost
#### Scenario: Single winner receives full pot
- **WHEN** a single player wins the pot of 200 chips
- **THEN** the winner receives exactly 200 chips and the pot is reset to 0