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
34 lines
829 B
TypeScript
34 lines
829 B
TypeScript
import type { GameState } from '$lib/types/game-state';
|
|
|
|
export function getNextActivePlayer(state: GameState): number | null {
|
|
const numPlayers = state.players.length;
|
|
let next = (state.currentTurn + 1) % numPlayers;
|
|
let checked = 0;
|
|
|
|
while (checked < numPlayers) {
|
|
const player = state.players[next];
|
|
if (player.status === 'active') {
|
|
return next;
|
|
}
|
|
next = (next + 1) % numPlayers;
|
|
checked++;
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
export function getFirstActivePlayerLeftOfDealer(state: GameState): number | null {
|
|
const numPlayers = state.players.length;
|
|
let pos = (state.dealerPosition + 1) % numPlayers;
|
|
let checked = 0;
|
|
|
|
while (checked < numPlayers) {
|
|
if (state.players[pos].status === 'active') {
|
|
return pos;
|
|
}
|
|
pos = (pos + 1) % numPlayers;
|
|
checked++;
|
|
}
|
|
|
|
return null;
|
|
} |