UNPKG

@idealic/poker-engine

Version:

Professional poker game engine and hand evaluator with built-in iterator utilities

71 lines 2.96 kB
import { getRemainingPlayers } from '../utils/position'; /** * Determines if the betting round is complete. * Betting is complete if: * - Only one active player remains * - All active players are all-in * - All active players have matched the highest bet or are all-in */ export function completeBetting(game) { const activePlayers = getRemainingPlayers(game); const highestBet = Math.max(...game.players.map((p) => p.totalBet)); const isComplete = activePlayers.length === 1 || activePlayers.every((p) => p.isAllIn) || activePlayers.every((p) => p.totalBet === highestBet || p.isAllIn); game.isBettingComplete = isComplete; return isComplete; } /** * Updates player state when matching a bet */ export function matchBet(game, playerIndex, targetAmount) { setPlayerBet(game, playerIndex, targetAmount); const player = game.players[playerIndex]; // Player has acted if they matched the bet or went all-in player.hasActed = player.currentBet === targetAmount || player.isAllIn; completeBetting(game); } /** * Updates player state when making a new bet */ export function makeBet(game, playerIndex, targetAmount) { setPlayerBet(game, playerIndex, targetAmount); const player = game.players[playerIndex]; // Reset bettingComplete since we have a new bet game.isBettingComplete = false; game.lastBetAction = `p${playerIndex + 1} cbr ${targetAmount}`; player.hasActed = true; // Reset hasActed for all non-all-in players except the bettor // This starts a new betting round, so everyone needs to act again game.players.forEach((p, i) => { // Only reset hasActed for players who: // 1. Are not the bettor // 2. Have not folded // 3. Are not all-in // 4. Have a lower bet than the current bet (need to act on the raise) if (i !== playerIndex && !p.hasFolded && !p.isAllIn && p.currentBet < game.bet) { p.hasActed = false; } }); completeBetting(game); } export function setPlayerAnte(game, playerIndex, amount) { const player = game.players[playerIndex]; player.stack = addWithPrecision(player.stack, -amount); game.pot = addWithPrecision(game.pot, amount); } export function setPlayerBet(game, playerIndex, absoluteAmount) { const player = game.players[playerIndex]; const amount = Math.min(absoluteAmount - player.currentBet, player.stack); player.stack = addWithPrecision(player.stack, -amount); player.currentBet = addWithPrecision(player.currentBet, amount); player.totalBet = addWithPrecision(player.totalBet, amount); player.roundBet = addWithPrecision(player.roundBet, amount); player.isAllIn = player.stack == 0; game.pot = addWithPrecision(game.pot, amount); game.bet = Math.max(game.bet, player.currentBet); } function addWithPrecision(a, b) { return Math.round((a + b) * 10000) / 10000; } //# sourceMappingURL=betting.js.map