721 lines
28 KiB
JavaScript
721 lines
28 KiB
JavaScript
// Pure Craps rules. No Phaser dependency (verify scripts import this under Node).
|
||
//
|
||
// Bets:
|
||
// Line Pass / Don't Pass (bar 12), Come / Don't Come, with free odds
|
||
// behind them (3-4-5× on the right side, 6× flat-equivalent laid)
|
||
// Numbers Place (4·5·6·8·9·10), Buy (true odds, 5% vig on wins),
|
||
// Lay (true odds against, 5% of the win), Big 6 / Big 8
|
||
// Hardways Hard 4·6·8·10 (off on the come-out unless called on)
|
||
// One-roll Field (2 double, 12 triple), Any 7, Any Craps, Yo-11, Aces,
|
||
// Ace-Deuce, Boxcars, C&E, Horn
|
||
//
|
||
// A "round" is one shooter sequence: a come-out roll, then (if a point is set)
|
||
// rolls until the point repeats (pass wins) or a 7 shows (seven-out → the dice
|
||
// move to the next player).
|
||
//
|
||
// State is treated as immutable: every mutating helper returns a fresh object.
|
||
|
||
export const POINT_NUMBERS = [4, 5, 6, 8, 9, 10];
|
||
export const HARD_NUMBERS = [4, 6, 8, 10];
|
||
|
||
export const BET = {
|
||
PASS: 'pass',
|
||
DONT_PASS: 'dontpass',
|
||
COME: 'come',
|
||
DONT_COME: 'dontcome',
|
||
FIELD: 'field',
|
||
PLACE: 'place',
|
||
BUY: 'buy',
|
||
LAY: 'lay',
|
||
BIG: 'big', // number 6 or 8
|
||
HARD: 'hard', // number 4, 6, 8 or 10
|
||
ANY7: 'any7',
|
||
ANY_CRAPS: 'anycraps',
|
||
YO: 'yo',
|
||
ACES: 'aces',
|
||
ACE_DEUCE: 'acedeuce',
|
||
BOXCARS: 'boxcars',
|
||
CE: 'ce',
|
||
HORN: 'horn',
|
||
};
|
||
|
||
// Bets that carry a number (and which numbers are valid).
|
||
export const NUMBERED = {
|
||
[BET.PLACE]: POINT_NUMBERS,
|
||
[BET.BUY]: POINT_NUMBERS,
|
||
[BET.LAY]: POINT_NUMBERS,
|
||
[BET.BIG]: [6, 8],
|
||
[BET.HARD]: HARD_NUMBERS,
|
||
};
|
||
|
||
export const ONE_ROLL = new Set([
|
||
BET.FIELD, BET.ANY7, BET.ANY_CRAPS, BET.YO, BET.ACES, BET.ACE_DEUCE, BET.BOXCARS, BET.CE, BET.HORN,
|
||
]);
|
||
|
||
// Bets that are "off" on the come-out roll unless the player calls them on.
|
||
const OFF_ON_COMEOUT = new Set([BET.PLACE, BET.BUY, BET.HARD]);
|
||
|
||
// Chip denominations offered in the UI.
|
||
export const CHIP_AMOUNTS = [1, 5, 25, 50, 100];
|
||
|
||
export const VIG = 0.05;
|
||
|
||
// ─── Setup ───────────────────────────────────────────────────────────────────
|
||
|
||
export function createInitialState(opponents = [], humanChips = 2000, humanName = 'You') {
|
||
const players = [
|
||
{ name: humanName, isAI: false, avatar: null, chips: humanChips, bets: [], lastDelta: 0 },
|
||
...opponents.map((o) => ({
|
||
name: o.name ?? o.id ?? 'Bot',
|
||
isAI: true,
|
||
avatar: o,
|
||
chips: 2000,
|
||
bets: [],
|
||
lastDelta: 0,
|
||
})),
|
||
];
|
||
return {
|
||
players,
|
||
shooterIndex: 0,
|
||
phase: 'comeout', // comeout | point | gameover
|
||
point: null,
|
||
dice: [1, 1],
|
||
rollCount: 0,
|
||
shooterRolls: 0, // rolls by the current shooter (resets on seven-out)
|
||
_nextBetId: 1,
|
||
};
|
||
}
|
||
|
||
export function cloneState(state) {
|
||
return JSON.parse(JSON.stringify(state));
|
||
}
|
||
|
||
// ─── Queries ─────────────────────────────────────────────────────────────────
|
||
|
||
// Which bet types may be opened right now.
|
||
export function legalBetTypes(state) {
|
||
const out = new Set(Object.values(BET));
|
||
if (state.point === null) {
|
||
out.delete(BET.COME);
|
||
out.delete(BET.DONT_COME);
|
||
} else {
|
||
out.delete(BET.PASS);
|
||
out.delete(BET.DONT_PASS);
|
||
}
|
||
return out;
|
||
}
|
||
|
||
export function isLegalBet(state, type, number = null) {
|
||
if (!legalBetTypes(state).has(type)) return false;
|
||
if (NUMBERED[type]) return NUMBERED[type].includes(number);
|
||
return number == null;
|
||
}
|
||
|
||
// The number a line/come bet's odds are tied to (null until established).
|
||
export function oddsNumber(bet, state) {
|
||
if (bet.type === BET.PASS || bet.type === BET.DONT_PASS) return state.point;
|
||
if (bet.type === BET.COME || bet.type === BET.DONT_COME) return bet.comePoint ?? null;
|
||
return null;
|
||
}
|
||
|
||
// A line/come bet that can still receive odds (point/come-point established).
|
||
export function oddsEligibleBets(player, state) {
|
||
return player.bets.filter((b) => oddsNumber(b, state) != null);
|
||
}
|
||
|
||
// 3-4-5× odds behind the line; laid odds capped so the win is at most 6× the flat.
|
||
export function oddsMultiple(number) {
|
||
if (number === 4 || number === 10) return 3;
|
||
if (number === 5 || number === 9) return 4;
|
||
if (number === 6 || number === 8) return 5;
|
||
return 0;
|
||
}
|
||
|
||
export function maxOddsFor(bet, state) {
|
||
const n = oddsNumber(bet, state);
|
||
if (n == null) return 0;
|
||
const dont = bet.type === BET.DONT_PASS || bet.type === BET.DONT_COME;
|
||
return dont ? bet.amount * 6 : bet.amount * oddsMultiple(n);
|
||
}
|
||
|
||
// Is this bet live for the next roll?
|
||
export function isWorking(bet, state) {
|
||
if (state.point === null && OFF_ON_COMEOUT.has(bet.type)) return !!bet.onComeOut;
|
||
return true;
|
||
}
|
||
|
||
// Contract bets may not be taken down: Pass once a point is set, and a Come bet
|
||
// once it has travelled to its number.
|
||
export function isContract(bet, state) {
|
||
if (bet.type === BET.PASS) return state.point !== null;
|
||
if (bet.type === BET.COME) return bet.comePoint != null;
|
||
return false;
|
||
}
|
||
|
||
export function canToggleWorking(bet, state) {
|
||
return state.point === null && OFF_ON_COMEOUT.has(bet.type);
|
||
}
|
||
|
||
// ─── Bet placement ───────────────────────────────────────────────────────────
|
||
|
||
function findMatch(bets, type, number) {
|
||
return bets.find((b) => b.type === type && (b.number ?? null) === (number ?? null) && b.comePoint == null);
|
||
}
|
||
|
||
// Place (or add to) a flat bet. The stake leaves the bankroll immediately.
|
||
// Returns the same state object when the placement is rejected.
|
||
export function placeBet(state, playerIndex, { type, number = null, amount }) {
|
||
const player = state.players[playerIndex];
|
||
amount = Math.floor(amount);
|
||
if (!(amount > 0) || amount > player.chips) return state;
|
||
if (!NUMBERED[type]) number = null;
|
||
if (!isLegalBet(state, type, number)) return state;
|
||
|
||
const s = cloneState(state);
|
||
const p = s.players[playerIndex];
|
||
const existing = findMatch(p.bets, type, number);
|
||
if (existing) {
|
||
existing.amount += amount;
|
||
} else {
|
||
p.bets.push({
|
||
id: s._nextBetId++,
|
||
type,
|
||
number,
|
||
comePoint: null,
|
||
amount,
|
||
oddsAmount: 0,
|
||
onComeOut: false,
|
||
});
|
||
}
|
||
p.chips -= amount;
|
||
return s;
|
||
}
|
||
|
||
// Add odds behind an eligible line/come bet (clamped to the cap and bankroll).
|
||
export function addOdds(state, playerIndex, betId, amount) {
|
||
const bet = state.players[playerIndex].bets.find((b) => b.id === betId);
|
||
if (!bet) return state;
|
||
const cap = maxOddsFor(bet, state) - bet.oddsAmount;
|
||
const add = Math.min(Math.floor(amount), cap, state.players[playerIndex].chips);
|
||
if (add <= 0) return state;
|
||
const s = cloneState(state);
|
||
const p = s.players[playerIndex];
|
||
const b = p.bets.find((x) => x.id === betId);
|
||
b.oddsAmount += add;
|
||
p.chips -= add;
|
||
return s;
|
||
}
|
||
|
||
// Odds that would be added by "max odds" for this bet.
|
||
export function oddsRoom(state, playerIndex, betId) {
|
||
const p = state.players[playerIndex];
|
||
const bet = p.bets.find((b) => b.id === betId);
|
||
if (!bet) return 0;
|
||
return Math.max(0, Math.min(maxOddsFor(bet, state) - bet.oddsAmount, p.chips));
|
||
}
|
||
|
||
// Take `amount` (default: all) off a bet's flat stake or odds. Contract flats
|
||
// can't come down. Removing the whole flat also returns its odds.
|
||
export function reduceBet(state, playerIndex, betId, { part = 'flat', amount = Infinity } = {}) {
|
||
const bet = state.players[playerIndex].bets.find((b) => b.id === betId);
|
||
if (!bet) return state;
|
||
if (part === 'odds') {
|
||
const take = Math.min(bet.oddsAmount, Math.floor(amount));
|
||
if (take <= 0) return state;
|
||
const s = cloneState(state);
|
||
const p = s.players[playerIndex];
|
||
p.bets.find((b) => b.id === betId).oddsAmount -= take;
|
||
p.chips += take;
|
||
return s;
|
||
}
|
||
if (isContract(bet, state)) return state;
|
||
const take = Math.min(bet.amount, Math.floor(amount));
|
||
if (take <= 0) return state;
|
||
const s = cloneState(state);
|
||
const p = s.players[playerIndex];
|
||
const b = p.bets.find((x) => x.id === betId);
|
||
if (take >= b.amount) {
|
||
p.chips += b.amount + b.oddsAmount;
|
||
p.bets = p.bets.filter((x) => x.id !== betId);
|
||
} else {
|
||
b.amount -= take;
|
||
p.chips += take;
|
||
const excess = b.oddsAmount - maxOddsFor(b, s);
|
||
if (excess > 0) { b.oddsAmount -= excess; p.chips += excess; }
|
||
}
|
||
return s;
|
||
}
|
||
|
||
export function removeBet(state, playerIndex, betId) {
|
||
return reduceBet(state, playerIndex, betId, { part: 'flat' });
|
||
}
|
||
|
||
// Call a Place / Buy / Hardway bet on (or off) for the come-out roll.
|
||
export function setWorking(state, playerIndex, betId, on) {
|
||
const bet = state.players[playerIndex].bets.find((b) => b.id === betId);
|
||
if (!bet || !canToggleWorking(bet, state)) return state;
|
||
const s = cloneState(state);
|
||
s.players[playerIndex].bets.find((b) => b.id === betId).onComeOut = !!on;
|
||
return s;
|
||
}
|
||
|
||
// Return every removable stake to the bankroll (Clear, and leaving the table).
|
||
// Contract bets stay unless `force` (leaving refunds everything, since only
|
||
// resolved deltas are ever posted to the server).
|
||
export function refundBets(state, playerIndex, { force = false, predicate = () => true } = {}) {
|
||
const s = cloneState(state);
|
||
const p = s.players[playerIndex];
|
||
const kept = [];
|
||
for (const b of p.bets) {
|
||
if (predicate(b) && (force || !isContract(b, state))) p.chips += b.amount + b.oddsAmount;
|
||
else kept.push(b);
|
||
}
|
||
p.bets = kept;
|
||
return s;
|
||
}
|
||
|
||
// ─── Dice ────────────────────────────────────────────────────────────────────
|
||
|
||
export function rollDice(state, rng = Math.random) {
|
||
const s = cloneState(state);
|
||
s.dice = [1 + Math.floor(rng() * 6), 1 + Math.floor(rng() * 6)];
|
||
s.rollCount += 1;
|
||
return s;
|
||
}
|
||
|
||
// Force specific dice (testing / scripted demos).
|
||
export function setDice(state, dice) {
|
||
const s = cloneState(state);
|
||
s.dice = [...dice];
|
||
return s;
|
||
}
|
||
|
||
// ─── Payout tables (winnings only, not the returned stake) ───────────────────
|
||
|
||
const floor = Math.floor;
|
||
|
||
// True odds for the right side (point → pays X to Y).
|
||
export function trueOddsWin(amount, number) {
|
||
if (number === 4 || number === 10) return amount * 2; // 2:1
|
||
if (number === 5 || number === 9) return floor((amount * 3) / 2); // 3:2
|
||
if (number === 6 || number === 8) return floor((amount * 6) / 5); // 6:5
|
||
return 0;
|
||
}
|
||
|
||
// True odds laid against a number.
|
||
export function layOddsWin(amount, number) {
|
||
if (number === 4 || number === 10) return floor(amount / 2); // 1:2
|
||
if (number === 5 || number === 9) return floor((amount * 2) / 3); // 2:3
|
||
if (number === 6 || number === 8) return floor((amount * 5) / 6); // 5:6
|
||
return 0;
|
||
}
|
||
|
||
export function placeWin(amount, number) {
|
||
if (number === 4 || number === 10) return floor((amount * 9) / 5);
|
||
if (number === 5 || number === 9) return floor((amount * 7) / 5);
|
||
if (number === 6 || number === 8) return floor((amount * 7) / 6);
|
||
return 0;
|
||
}
|
||
|
||
export function vig(amount) {
|
||
return Math.max(1, floor(amount * VIG));
|
||
}
|
||
|
||
// Payout ratios shown on the layout / tooltips.
|
||
export const PAYS = {
|
||
[BET.PASS]: '1 to 1', [BET.DONT_PASS]: '1 to 1', [BET.COME]: '1 to 1', [BET.DONT_COME]: '1 to 1',
|
||
[BET.FIELD]: '1 to 1 · 2 pays 2× · 12 pays 3×',
|
||
[BET.BIG]: '1 to 1',
|
||
[BET.ANY7]: '4 to 1', [BET.ANY_CRAPS]: '7 to 1', [BET.YO]: '15 to 1',
|
||
[BET.ACES]: '30 to 1', [BET.ACE_DEUCE]: '15 to 1', [BET.BOXCARS]: '30 to 1',
|
||
[BET.CE]: 'Craps 3 to 1 · 11 pays 7 to 1', [BET.HORN]: '2/12 pay 27 to 4 · 3/11 pay 3 to 1',
|
||
};
|
||
|
||
export function paysLabel(type, number) {
|
||
if (type === BET.PLACE) return number === 6 || number === 8 ? '7 to 6' : number === 5 || number === 9 ? '7 to 5' : '9 to 5';
|
||
if (type === BET.BUY) return `${number === 6 || number === 8 ? '6 to 5' : number === 5 || number === 9 ? '3 to 2' : '2 to 1'} · 5% vig on wins`;
|
||
if (type === BET.LAY) return `${number === 6 || number === 8 ? '5 to 6' : number === 5 || number === 9 ? '2 to 3' : '1 to 2'} · 5% vig on wins`;
|
||
if (type === BET.HARD) return number === 4 || number === 10 ? '7 to 1' : '9 to 1';
|
||
return PAYS[type] ?? '';
|
||
}
|
||
|
||
// House edge (per bet, as a %) for tooltips — verified exactly in tools/verifyCraps.js.
|
||
export function houseEdge(type, number) {
|
||
switch (type) {
|
||
case BET.PASS: case BET.COME: return 1.41;
|
||
case BET.DONT_PASS: case BET.DONT_COME: return 1.4; // per decision (a barred 12 is no action)
|
||
case BET.FIELD: return 2.78;
|
||
case BET.PLACE: return number === 6 || number === 8 ? 1.52 : number === 5 || number === 9 ? 4.0 : 6.67;
|
||
// Buy / Lay quoted at their natural sizes ($20 buys, lays that win $20).
|
||
case BET.BUY: return number === 6 || number === 8 ? 2.27 : number === 5 || number === 9 ? 2.0 : 1.67;
|
||
case BET.LAY: return number === 6 || number === 8 ? 1.82 : number === 5 || number === 9 ? 2.0 : 1.67;
|
||
case BET.BIG: return 9.09;
|
||
case BET.HARD: return number === 6 || number === 8 ? 9.09 : 11.11;
|
||
case BET.ANY7: return 16.67;
|
||
case BET.ANY_CRAPS: case BET.YO: case BET.ACE_DEUCE: case BET.CE: return 11.11;
|
||
case BET.ACES: case BET.BOXCARS: return 13.89;
|
||
case BET.HORN: return 12.5;
|
||
default: return null;
|
||
}
|
||
}
|
||
|
||
// ─── Resolution ──────────────────────────────────────────────────────────────
|
||
|
||
// Resolve every bet against the dice already set on `state`.
|
||
// Returns { state, total, hard, payouts, moves, pointEstablished, pointMade, sevenOut, newComeOut }.
|
||
//
|
||
// payouts entry: { playerIndex, betId, betType, number, result, amount, winnings,
|
||
// commission, refund, kept, delta }
|
||
// result ∈ 'win' | 'lose' | 'push'; amount = stake at risk; delta = net bankroll change.
|
||
// kept = the bet stays on the felt after a win (Place, Buy, Lay, Big, Hard).
|
||
// moves entry: { playerIndex, betId, type, to } — a Come / Don't Come bet
|
||
// travelling to its number.
|
||
export function resolveRoll(state) {
|
||
const s = cloneState(state);
|
||
const [d1, d2] = s.dice;
|
||
const total = d1 + d2;
|
||
const hard = d1 === d2;
|
||
const point = s.point;
|
||
const isComeOut = point === null;
|
||
const payouts = [];
|
||
const moves = [];
|
||
const ctx = { total, hard, point, isComeOut, state };
|
||
|
||
let pointEstablished = null;
|
||
let pointMade = false;
|
||
let sevenOut = false;
|
||
|
||
for (let pi = 0; pi < s.players.length; pi++) {
|
||
const p = s.players[pi];
|
||
const kept = [];
|
||
|
||
for (const bet of p.bets) {
|
||
const r = resolveBet(bet, ctx);
|
||
if (r.result === 'open') {
|
||
const nb = r.bet ?? bet;
|
||
// A Come / Don't Come bet travelling to its number. (An older come bet on
|
||
// that number always resolves on this same roll — "off and on" — and
|
||
// placeBet keeps one unmoved come bet per player, so no twins can exist.)
|
||
if (nb.comePoint != null && bet.comePoint == null) {
|
||
moves.push({ playerIndex: pi, betId: bet.id, type: bet.type, to: nb.comePoint });
|
||
}
|
||
kept.push(nb);
|
||
continue;
|
||
}
|
||
const stake = bet.amount + bet.oddsAmount;
|
||
const winnings = r.winnings ?? 0;
|
||
const commission = r.commission ?? 0;
|
||
let credited = 0;
|
||
if (r.result === 'win') credited = (r.keep ? 0 : stake) + winnings - commission;
|
||
else if (r.result === 'push') credited = stake;
|
||
else credited = r.refund ?? 0;
|
||
p.chips += credited;
|
||
const delta = r.keep ? winnings - commission : credited - stake;
|
||
p.lastDelta += delta;
|
||
payouts.push({
|
||
playerIndex: pi,
|
||
betId: bet.id,
|
||
betType: bet.type,
|
||
number: bet.number ?? bet.comePoint ?? null,
|
||
result: r.result,
|
||
amount: stake,
|
||
winnings,
|
||
commission,
|
||
refund: r.result === 'lose' ? (r.refund ?? 0) : 0,
|
||
kept: !!r.keep,
|
||
delta,
|
||
});
|
||
if (r.keep) kept.push(bet);
|
||
}
|
||
p.bets = kept;
|
||
}
|
||
|
||
// Phase / point transitions driven by the line outcome.
|
||
if (isComeOut) {
|
||
if (POINT_NUMBERS.includes(total)) {
|
||
pointEstablished = total;
|
||
s.point = total;
|
||
s.phase = 'point';
|
||
} else {
|
||
s.phase = 'comeout';
|
||
}
|
||
} else if (total === point) {
|
||
pointMade = true;
|
||
s.point = null;
|
||
s.phase = 'comeout';
|
||
} else if (total === 7) {
|
||
sevenOut = true;
|
||
s.point = null;
|
||
s.phase = 'comeout';
|
||
}
|
||
// Called-on flags only last for a come-out.
|
||
if (s.point !== null) for (const p of s.players) for (const b of p.bets) b.onComeOut = false;
|
||
|
||
s.shooterRolls += 1;
|
||
const newComeOut = pointMade || sevenOut;
|
||
if (sevenOut) {
|
||
s.shooterIndex = (s.shooterIndex + 1) % s.players.length;
|
||
s.shooterRolls = 0;
|
||
}
|
||
|
||
return { state: s, total, hard, payouts, moves, pointEstablished, pointMade, sevenOut, newComeOut };
|
||
}
|
||
|
||
// Resolve a single bet. Returns one of:
|
||
// { result:'open', bet? } unresolved (bet may have mutated)
|
||
// { result:'win', winnings, commission?, keep? } paid; keep → stays on the felt
|
||
// { result:'lose', refund? } refund = odds that were off
|
||
// { result:'push' }
|
||
function resolveBet(bet, { total, hard, point, isComeOut, state }) {
|
||
const n = bet.number;
|
||
if (!isWorking(bet, state)) return { result: 'open' };
|
||
|
||
switch (bet.type) {
|
||
case BET.FIELD: {
|
||
if (total === 2) return { result: 'win', winnings: bet.amount * 2 };
|
||
if (total === 12) return { result: 'win', winnings: bet.amount * 3 };
|
||
if ([3, 4, 9, 10, 11].includes(total)) return { result: 'win', winnings: bet.amount };
|
||
return { result: 'lose' };
|
||
}
|
||
|
||
case BET.PASS: {
|
||
if (isComeOut) {
|
||
if (total === 7 || total === 11) return { result: 'win', winnings: bet.amount };
|
||
if (total === 2 || total === 3 || total === 12) return { result: 'lose' };
|
||
return { result: 'open' };
|
||
}
|
||
if (total === point) return { result: 'win', winnings: bet.amount + trueOddsWin(bet.oddsAmount, point) };
|
||
if (total === 7) return { result: 'lose' };
|
||
return { result: 'open' };
|
||
}
|
||
|
||
case BET.DONT_PASS: {
|
||
if (isComeOut) {
|
||
if (total === 2 || total === 3) return { result: 'win', winnings: bet.amount };
|
||
if (total === 12) return { result: 'open' }; // barred: no action, the bet stands
|
||
if (total === 7 || total === 11) return { result: 'lose' };
|
||
return { result: 'open' };
|
||
}
|
||
if (total === 7) return { result: 'win', winnings: bet.amount + layOddsWin(bet.oddsAmount, point) };
|
||
if (total === point) return { result: 'lose' };
|
||
return { result: 'open' };
|
||
}
|
||
|
||
case BET.COME:
|
||
case BET.DONT_COME: {
|
||
const dont = bet.type === BET.DONT_COME;
|
||
if (bet.comePoint == null) {
|
||
if (total === 7 || total === 11) return dont ? { result: 'lose' } : { result: 'win', winnings: bet.amount };
|
||
if (total === 2 || total === 3) return dont ? { result: 'win', winnings: bet.amount } : { result: 'lose' };
|
||
if (total === 12) return dont ? { result: 'open' } : { result: 'lose' }; // barred 12 stands
|
||
return { result: 'open', bet: { ...bet, comePoint: total } };
|
||
}
|
||
const cp = bet.comePoint;
|
||
if (!dont) {
|
||
// Come odds are off on the come-out roll: returned, not paid or lost.
|
||
const oddsOn = !isComeOut;
|
||
if (total === cp) {
|
||
return { result: 'win', winnings: bet.amount + (oddsOn ? trueOddsWin(bet.oddsAmount, cp) : 0) };
|
||
}
|
||
if (total === 7) return { result: 'lose', refund: oddsOn ? 0 : bet.oddsAmount };
|
||
return { result: 'open' };
|
||
}
|
||
if (total === 7) return { result: 'win', winnings: bet.amount + layOddsWin(bet.oddsAmount, cp) };
|
||
if (total === cp) return { result: 'lose' };
|
||
return { result: 'open' };
|
||
}
|
||
|
||
case BET.PLACE: {
|
||
if (total === n) return { result: 'win', winnings: placeWin(bet.amount, n), keep: true };
|
||
if (total === 7) return { result: 'lose' };
|
||
return { result: 'open' };
|
||
}
|
||
|
||
case BET.BUY: {
|
||
if (total === n) return { result: 'win', winnings: trueOddsWin(bet.amount, n), commission: vig(bet.amount), keep: true };
|
||
if (total === 7) return { result: 'lose' };
|
||
return { result: 'open' };
|
||
}
|
||
|
||
case BET.LAY: {
|
||
if (total === 7) {
|
||
const w = layOddsWin(bet.amount, n);
|
||
return { result: 'win', winnings: w, commission: Math.min(w, vig(w)), keep: true };
|
||
}
|
||
if (total === n) return { result: 'lose' };
|
||
return { result: 'open' };
|
||
}
|
||
|
||
case BET.BIG: {
|
||
if (total === n) return { result: 'win', winnings: bet.amount, keep: true };
|
||
if (total === 7) return { result: 'lose' };
|
||
return { result: 'open' };
|
||
}
|
||
|
||
case BET.HARD: {
|
||
if (total === n && hard) {
|
||
return { result: 'win', winnings: bet.amount * (n === 4 || n === 10 ? 7 : 9), keep: true };
|
||
}
|
||
if (total === 7 || total === n) return { result: 'lose' };
|
||
return { result: 'open' };
|
||
}
|
||
|
||
case BET.ANY7: return total === 7 ? { result: 'win', winnings: bet.amount * 4 } : { result: 'lose' };
|
||
case BET.ANY_CRAPS:
|
||
return [2, 3, 12].includes(total) ? { result: 'win', winnings: bet.amount * 7 } : { result: 'lose' };
|
||
case BET.YO: return total === 11 ? { result: 'win', winnings: bet.amount * 15 } : { result: 'lose' };
|
||
case BET.ACES: return total === 2 ? { result: 'win', winnings: bet.amount * 30 } : { result: 'lose' };
|
||
case BET.ACE_DEUCE: return total === 3 ? { result: 'win', winnings: bet.amount * 15 } : { result: 'lose' };
|
||
case BET.BOXCARS: return total === 12 ? { result: 'win', winnings: bet.amount * 30 } : { result: 'lose' };
|
||
case BET.CE: {
|
||
if ([2, 3, 12].includes(total)) return { result: 'win', winnings: bet.amount * 3 };
|
||
if (total === 11) return { result: 'win', winnings: bet.amount * 7 };
|
||
return { result: 'lose' };
|
||
}
|
||
case BET.HORN: {
|
||
if (total === 2 || total === 12) return { result: 'win', winnings: floor((bet.amount * 27) / 4) };
|
||
if (total === 3 || total === 11) return { result: 'win', winnings: bet.amount * 3 };
|
||
return { result: 'lose' };
|
||
}
|
||
|
||
default:
|
||
return { result: 'open' };
|
||
}
|
||
}
|
||
|
||
// ─── Round / table helpers ───────────────────────────────────────────────────
|
||
|
||
export function clearLastDeltas(state) {
|
||
const s = cloneState(state);
|
||
for (const p of s.players) p.lastDelta = 0;
|
||
return s;
|
||
}
|
||
|
||
export function totalAtRisk(player) {
|
||
return player.bets.reduce((sum, b) => sum + b.amount + b.oddsAmount, 0);
|
||
}
|
||
|
||
export function hasActiveBets(player) {
|
||
return player.bets.length > 0;
|
||
}
|
||
|
||
// Human net result vs. the chips they walked in with, for match history.
|
||
export function getNetResult(player, startingChips) {
|
||
const equity = player.chips + totalAtRisk(player);
|
||
if (equity > startingChips) return 'win';
|
||
if (equity < startingChips) return 'loss';
|
||
return 'draw';
|
||
}
|
||
|
||
// Stickman's call for a roll (flavour text).
|
||
export function stickCall(res, prevPoint) {
|
||
const { total, hard } = res;
|
||
const easyHard = POINT_NUMBERS.includes(total) && total !== 5 && total !== 9
|
||
? (hard ? `Hard ${total}!` : `Easy ${total}`) : null;
|
||
if (res.sevenOut) return 'Seven out — line away, pay the don\'ts!';
|
||
if (res.pointMade) return `${hard && HARD_NUMBERS.includes(total) ? `Hard ${total}` : total}, winner! Pay the line`;
|
||
if (res.pointEstablished) {
|
||
const base = total === 6 ? 'Six' : total === 8 ? 'Eight' : total === 9 ? 'Nine' : total === 10 ? 'Ten' : total === 4 ? 'Four' : 'Five';
|
||
return `${easyHard ?? base} — the point is ${total}, mark it!`;
|
||
}
|
||
if (prevPoint === null) {
|
||
if (total === 7) return 'Seven, winner on the pass line!';
|
||
if (total === 11) return 'Yo-leven! Front line winner!';
|
||
if (total === 2) return 'Aces — craps! Line away';
|
||
if (total === 3) return 'Ace-deuce, craps three';
|
||
if (total === 12) return 'Boxcars! Craps — bar the don\'ts';
|
||
}
|
||
if (total === 11) return 'Yo-leven!';
|
||
if (total === 2) return 'Aces, snake eyes!';
|
||
if (total === 3) return 'Three craps three';
|
||
if (total === 12) return 'Midnight — boxcars!';
|
||
if (easyHard) return easyHard;
|
||
if (total === 5) return 'Five, no field five';
|
||
if (total === 9) return 'Nine, centre field';
|
||
return `${total}`;
|
||
}
|
||
|
||
// ─── Plain-English results ───────────────────────────────────────────────────
|
||
|
||
const PROP_NAME = {
|
||
[BET.FIELD]: 'Field', [BET.ANY7]: 'Any Seven', [BET.ANY_CRAPS]: 'Any Craps', [BET.YO]: 'Yo-Eleven',
|
||
[BET.ACES]: 'Aces', [BET.ACE_DEUCE]: 'Ace-Deuce', [BET.BOXCARS]: 'Boxcars', [BET.CE]: 'C & E', [BET.HORN]: 'Horn',
|
||
};
|
||
|
||
// Display name for a resolved bet (payout entry) or a live bet.
|
||
export function betLabel(type, number = null, comePoint = null) {
|
||
switch (type) {
|
||
case BET.PASS: return 'Pass Line';
|
||
case BET.DONT_PASS: return "Don't Pass";
|
||
case BET.COME: return comePoint ?? number ? `Come on ${comePoint ?? number}` : 'Come';
|
||
case BET.DONT_COME: return comePoint ?? number ? `Don't Come on ${comePoint ?? number}` : "Don't Come";
|
||
case BET.PLACE: return `Place ${number}`;
|
||
case BET.BUY: return `Buy ${number}`;
|
||
case BET.LAY: return `Lay ${number}`;
|
||
case BET.BIG: return `Big ${number}`;
|
||
case BET.HARD: return `Hard ${number}`;
|
||
default: return PROP_NAME[type] ?? type;
|
||
}
|
||
}
|
||
|
||
// Why a bet won or lost on this roll. `ctx` = { dice: [a, b], prevPoint, unmoved }
|
||
// where unmoved = it was a Come / Don't Come bet still in its own come-out.
|
||
export function explainPayout(p, { dice, prevPoint, unmoved = false }) {
|
||
const t = dice[0] + dice[1];
|
||
const n = p.number;
|
||
const win = p.result === 'win';
|
||
const sevenOut = prevPoint !== null && t === 7;
|
||
const seven = sevenOut ? 'Seven-out' : 'A 7 rolled';
|
||
let why;
|
||
if (p.result === 'push') why = 'Push — stake returned';
|
||
else switch (p.betType) {
|
||
case BET.FIELD:
|
||
why = !win ? `${t} isn't a Field number` : t === 2 ? '2 in the Field pays double' : t === 12 ? '12 in the Field pays triple' : `${t} is a Field number`;
|
||
break;
|
||
case BET.PASS:
|
||
if (prevPoint === null) why = win ? `Natural ${t} on the come-out` : `Craps ${t} on the come-out`;
|
||
else why = win ? `Point ${prevPoint} made` : 'Seven-out before the point';
|
||
break;
|
||
case BET.DONT_PASS:
|
||
if (prevPoint === null) why = win ? `Craps ${t} — the Don't wins` : `Natural ${t} beats the Don't`;
|
||
else why = win ? 'Seven-out — the Don\'t wins' : `Point ${prevPoint} made`;
|
||
break;
|
||
case BET.COME:
|
||
if (unmoved) why = win ? `Natural ${t} on the Come` : `Craps ${t} on the Come`;
|
||
else why = win ? `${n} rolled again` : `${seven} before the ${n}`;
|
||
break;
|
||
case BET.DONT_COME:
|
||
if (unmoved) why = win ? `Craps ${t} — the Don't Come wins` : `Natural ${t} beats the Don't Come`;
|
||
else why = win ? `7 before the ${n}` : `The ${n} rolled`;
|
||
break;
|
||
case BET.PLACE: case BET.BUY: case BET.BIG:
|
||
why = win ? `The ${n} rolled` : `${seven} before the ${n}`;
|
||
break;
|
||
case BET.LAY:
|
||
why = win ? `7 before the ${n}` : `The ${n} rolled`;
|
||
break;
|
||
case BET.HARD:
|
||
if (win) why = `Hard ${n} — ${n / 2} and ${n / 2}`;
|
||
else why = t === 7 ? seven : `${n} came easy (${dice[0]}-${dice[1]})`;
|
||
break;
|
||
case BET.ANY7: why = win ? 'Seven!' : `${t} — no seven`; break;
|
||
case BET.ANY_CRAPS: why = win ? `Craps ${t}` : `${t} — no 2, 3 or 12`; break;
|
||
case BET.YO: why = win ? 'Yo-eleven!' : `${t} — no 11`; break;
|
||
case BET.ACES: why = win ? 'Snake eyes!' : `${t} — no 2`; break;
|
||
case BET.ACE_DEUCE: why = win ? 'Ace-deuce!' : `${t} — no 3`; break;
|
||
case BET.BOXCARS: why = win ? 'Boxcars!' : `${t} — no 12`; break;
|
||
case BET.CE: why = !win ? `${t} — no craps or 11` : t === 11 ? '11 pays 7 to 1' : `Craps ${t} pays 3 to 1`; break;
|
||
case BET.HORN: why = win ? `${t} on the Horn` : `${t} — no 2, 3, 11 or 12`; break;
|
||
default: why = win ? 'Winner' : 'Loser';
|
||
}
|
||
const extra = [];
|
||
if (win && p.kept) extra.push('stays up');
|
||
if (p.commission > 0) extra.push(`$${p.commission} vig`);
|
||
if (p.refund > 0) extra.push('odds returned (off on the come-out)');
|
||
return extra.length ? `${why} · ${extra.join(' · ')}` : why;
|
||
}
|
||
|
||
export function explainMove(m) {
|
||
return m.type === BET.DONT_COME ? `Moves behind the ${m.to}` : `Moves to the ${m.to}`;
|
||
}
|