@idealic/poker-engine
Version:
Professional poker game engine and hand evaluator with built-in iterator utilities
71 lines • 2.27 kB
JavaScript
import { finalizeStacks } from '../game/stacks';
import { calculateHandStrength } from '../utils/hand-strength';
import { isMultiWayAllIn } from '../utils/validation';
import { completeBetting } from './betting';
/**
* Compares two poker hands and returns:
* -1 if hand1 is worse than hand2
* 0 if hands are equal
* 1 if hand1 is better than hand2
*/
export function compareHands(hand1, hand2) {
// calculateHandStrength returns lower numbers for better hands
// so we need to reverse the comparison
const strength1 = calculateHandStrength(hand1);
const strength2 = calculateHandStrength(hand2);
if (strength1 < strength2)
return 1; // hand1 is better
if (strength1 > strength2)
return -1; // hand2 is better
return 0; // equal hands
}
/**
* Helper: completeHand
* Handles hand completion by determining winners, distributing pot and resetting state
*/
export function completeHand(table) {
if (table.isHandComplete)
return;
completeBetting(table);
const finishingStacks = finalizeStacks(table);
table.players.forEach((p, i) => {
p.stack = finishingStacks[i];
});
// --- 5) Final cleanup ---
table.pot = 0; // All pots distributed
table.isHandComplete = true; // This will set isBettingComplete
resetBettingState(table);
}
/**
* Helper: resetBettingState
* Resets all betting-related state when a hand is complete
*/
function resetBettingState(table) {
table.bet = 0;
table.players.forEach((p) => {
p.currentBet = 0;
p.roundBet = 0;
});
}
/**
* Resets player states for a new street
*/
export function resetForNewStreet(table) {
completeBetting(table);
const isTableMultiWayAllIn = isMultiWayAllIn(table);
table.bet = 0;
table.lastBetAction = undefined;
table.lastPlayerAction = undefined;
// Only reset state for players who are still in the hand and not all-in
// Skip resetting hasActed in multi-way all-in situations
table.players.forEach((p) => {
if (!p.hasFolded && !p.isAllIn) {
if (!isTableMultiWayAllIn) {
p.hasActed = false;
}
p.currentBet = 0;
p.roundBet = 0;
}
});
}
//# sourceMappingURL=showdown.js.map