testsAndMisc/poker-stakes/src/engine/session.ts
Krzysztof kuhy Rudnicki 72a5e28456 Track poker-stakes so the file-length cap can reach it
The directory has existed on disk with 58 untracked files, four of them over
the 250-line cap. An untracked file is invisible to every pre-commit hook, so
those violations could not be fixed or gated while it stayed that way -- and
its CI workflow was untracked too, so nothing ran against it.

Landed with its already-written evidence artifact and a contract, since this
crosses the 4-file threshold.

Verified before staging: 58 plain text source/config files, no binaries, no
node_modules, no build output. npm run check passes -- typecheck, eslint
--max-warnings 0, and 220 tests at 100% statements/branches/functions/lines.

Splitting its four oversized files is deliberately a separate commit.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011w2Pj4Ek3ZNMQVYcFHgXDY
2026-08-16 15:19:07 +02:00

92 lines
3.3 KiB
TypeScript

import type { HandFlowCallbacks, HandResult, HeadsUpStacks } from './handFlow'
import { playHand } from './handFlow'
import type { Rng } from './rng'
import type { Seat } from './types'
export interface SessionConfig {
buyInChips: number
}
export type SessionEndReason = 'cashOut' | 'bust'
export interface SessionOutcome {
reason: SessionEndReason
finalChips: number // the player's stack at the moment the session ended
}
export interface SessionState {
stacks: HeadsUpStacks
buttonSeat: Seat // rotates each hand
handsPlayed: number
outcome: SessionOutcome | null // null while the session is still live
}
/** Big blind derived from the buy-in; small blind is always ≥1 and strictly less than BB. */
export const deriveBlindSize = (buyInChips: number): { bigBlind: number; smallBlind: number } => {
const bigBlind = Math.max(2, Math.floor(buyInChips / 100))
const smallBlind = Math.floor(bigBlind / 2)
return { bigBlind, smallBlind }
}
export const startSession = (config: SessionConfig): SessionState => ({
stacks: { player: config.buyInChips, opponent: config.buyInChips },
buttonSeat: 'player',
handsPlayed: 0,
outcome: null,
})
const other = (seat: Seat): Seat => (seat === 'player' ? 'opponent' : 'player')
/**
* Folds a completed hand's result into the session: updates stacks, rotates the button, and
* detects bust/cash-out. Pure — shared by `playSessionHand` (the PC-only synchronous path) and
* any interactive driver that runs a hand via `playHandSteps` instead.
*/
export const applyHandResult = (session: SessionState, handResult: HandResult): SessionState => {
const nextStacks = handResult.finalStacks
// Player busting is a real debt event. Opponent busting means the player holds every chip in
// play, which settles to owing nothing under the settlement formula — model it the same as a
// cash-out (at the max chip count) rather than a special case, since no further hand is possible.
let outcome: SessionOutcome | null = null
if (nextStacks.player === 0) {
outcome = { reason: 'bust', finalChips: 0 }
} else if (nextStacks.opponent === 0) {
outcome = { reason: 'cashOut', finalChips: nextStacks.player }
}
return {
stacks: nextStacks,
buttonSeat: other(session.buttonSeat),
handsPlayed: session.handsPlayed + 1,
outcome,
}
}
/** Plays one hand, updates stacks, rotates the button, and detects bust. No-op if already ended. */
export const playSessionHand = (
session: SessionState,
rng: Rng,
buyInChips: number,
callbacks: HandFlowCallbacks,
): { session: SessionState; handResult: HandResult } => {
if (session.outcome !== null) {
throw new Error('playSessionHand: session has already ended')
}
const { bigBlind, smallBlind } = deriveBlindSize(buyInChips)
const handResult = playHand(
rng,
{ buttonSeat: session.buttonSeat, bigBlind, smallBlind, stacks: session.stacks },
callbacks,
)
return { session: applyHandResult(session, handResult), handResult }
}
/** Ends a live session by cash-out, recording the player's current chip count. */
export const cashOutSession = (session: SessionState): SessionState => {
if (session.outcome !== null) {
throw new Error('cashOutSession: session has already ended')
}
return { ...session, outcome: { reason: 'cashOut', finalChips: session.stacks.player } }
}