#!/usr/bin/env node // verifyBaccarat.js — headless checks for the Baccarat table layout, felt // hit-testing, payouts as chip stacks, and the scoreboard roads. import { readFileSync } from 'node:fs'; import { BET, betPayout } from '../src/games/baccarat/BaccaratLogic.js'; import { VIEW, unproject, onFelt, SEAT_ANGLES, DISPLAY_ORDER, BANDS, BET_TYPES, BOX_HALF_DEG, boxCenter, boxOutline, hitBox, seatPortrait, handCenter, handCardPose, handBoxRect, SHOE, DISCARD, PLACARD, TRAY, DOCK, READOUT, BOARD, boardSize, FELT_TEXT, polar, project, FELT_R, CARD_W, CARD_H, TOTAL_BADGE_DY, } from '../src/games/baccarat/BaccaratTable.js'; import { counts, beadPlate, bigRoad, bigRoadWindow } from '../src/games/baccarat/BaccaratRoads.js'; import { breakdown } from '../src/ui/CasinoChips.js'; let pass = 0, fail = 0; function ok(label, cond, detail = '') { if (cond) pass++; else { console.error(` ✗ ${label}${detail ? ` — ${detail}` : ''}`); fail++; } } const section = (n) => console.log(`\n${n}`); const onFeltPt = (p) => { const q = unproject(p.x, p.y); return onFelt(q.u, q.v); }; const sum = (a) => a.reduce((x, y) => x + y, 0); const inRect = (p, r) => p.x > r.x - r.w / 2 && p.x < r.x + r.w / 2 && p.y > r.y - r.h / 2 && p.y < r.y + r.h / 2; // ── Payouts → chips ────────────────────────────────────────────────────────── section('Payouts'); for (let bet = 5; bet <= 500; bet += 5) { for (const outcome of ['player', 'banker', 'tie']) { for (const type of BET_TYPES) { const net = betPayout(type, bet, outcome); if (net > 0) { ok(`$${bet} ${type} on ${outcome}: payout stack sums`, sum(breakdown(net)) === net); if (type === BET.BANKER) { const commission = bet - net; ok(`$${bet} banker: commission is 5% floored`, commission === Math.floor(bet * 0.05)); ok(`$${bet} banker: gross stack = bet`, sum(breakdown(net + commission)) === bet); } if (type === BET.TIE) ok(`$${bet} tie pays 8:1`, net === bet * 8); } } } } ok('player/banker push on a tie', betPayout(BET.PLAYER, 25, 'tie') === 0 && betPayout(BET.BANKER, 25, 'tie') === 0); // ── Betting boxes ──────────────────────────────────────────────────────────── section('Betting boxes'); ok('7 seats', SEAT_ANGLES.length === 7); ok('display order covers every seat once', [...DISPLAY_ORDER].sort().join() === '0,1,2,3,4,5,6'); const sorted = [...SEAT_ANGLES].sort((a, b) => a - b); ok('neighbouring seats\' boxes don\'t overlap', sorted.every((a, i) => i === 0 || a - sorted[i - 1] > 2 * BOX_HALF_DEG), sorted.join(',')); ok('bands are disjoint, PLAYER outermost', BANDS.player.r0 > BANDS.banker.r1 && BANDS.banker.r0 > BANDS.tie.r1); ok('PLAYER band inside the felt', BANDS.player.r1 < FELT_R); for (let seat = 0; seat < 7; seat++) { for (const type of BET_TYPES) { const c = boxCenter(seat, type); const hit = hitBox(c.x, c.y); ok(`seat ${seat} ${type}: centre hit-tests to itself`, hit && hit.seat === seat && hit.type === type, JSON.stringify(hit)); ok(`seat ${seat} ${type}: outline on the felt`, boxOutline(seat, type).every(onFeltPt)); const inner = boxOutline(seat, type, 0.01); ok(`seat ${seat} ${type}: inset outline hit-tests to itself`, inner.every(p => { const h = hitBox(p.x, p.y); return h && h.seat === seat && h.type === type; })); ok(`seat ${seat} ${type}: stack well above the dock`, c.y + 40 < DOCK.y); } } ok('gap between bands hits nothing', hitBox(polar((BANDS.banker.r1 + BANDS.player.r0) / 2, 0).x, polar((BANDS.banker.r1 + BANDS.player.r0) / 2, 0).y) === null); ok('between seats hits nothing', hitBox(polar(0.9, 12.5).x, polar(0.9, 12.5).y) === null); ok('the dealer row hits nothing', hitBox(handCenter('player').x, handCenter('player').y) === null); ok('off the felt hits nothing', hitBox(960, 1050) === null && hitBox(960, 100) === null); // ── Dealer side ────────────────────────────────────────────────────────────── section('Dealer side'); const halfCard = (p, sideways) => sideways ? { w: (CARD_H / 2) * p.s, h: (CARD_W / 2) * p.s * VIEW.squash } : { w: (CARD_W / 2) * p.s, h: (CARD_H / 2) * p.s * VIEW.squash }; for (const side of ['player', 'banker']) { for (let i = 0; i < 3; i++) ok(`${side} card ${i} on the felt`, onFeltPt(handCardPose(side, i))); const third = handCardPose(side, 2), hc = halfCard(third, true); const shoe = project(SHOE.u, SHOE.v), disc = project(DISCARD.u, DISCARD.v); const furn = side === 'player' ? disc : shoe; ok(`${side} third card clear of the ${side === 'player' ? 'discard' : 'shoe'}`, third.y - hc.h > furn.y + 40 * furn.s, `card top ${(third.y - hc.h).toFixed(0)} furniture bottom ${(furn.y + 40 * furn.s).toFixed(0)}`); const first = handCardPose(side, 0), h1 = halfCard(first, false); ok(`${side} cards clear of the chip tray`, first.y - h1.h > project(0, TRAY.v1).y); const box = handBoxRect(side); ok(`${side} box doesn't overlap the readout`, box.y + box.h / 2 < READOUT.y - READOUT.h / 2 + 4); const badgeY = handCenter(side).y + TOTAL_BADGE_DY * handCenter(side).s; ok(`${side} total badge between box and readout`, badgeY > box.y + box.h / 2 && badgeY + 17 < READOUT.y - READOUT.h / 2 + 18, `badge ${badgeY.toFixed(0)}`); for (let seat = 0; seat < 7; seat++) { ok(`${side} third card clear of seat ${seat}'s boxes`, BET_TYPES.every(t => boxOutline(seat, t).every(p => Math.abs(p.x - third.x) > hc.w || Math.abs(p.y - third.y) > hc.h))); } } ok('the hands sit either side of centre', handCenter('player').x < 960 && handCenter('banker').x > 960); for (let seat = 0; seat < 7; seat++) { ok(`readout clear of seat ${seat}'s boxes`, BET_TYPES.every(t => !boxOutline(seat, t).some(p => inRect(p, READOUT)))); } ok('felt lettering clear of every box', FELT_TEXT.every(t => t.r < BANDS.tie.r0 - 0.03)); ok('placard / shoe / discard on the felt', [SHOE, DISCARD, PLACARD].every(p => onFelt(p.u, p.v))); // ── Portraits / scoreboard ─────────────────────────────────────────────────── section('Portraits + scoreboard'); for (let seat = 1; seat < 7; seat++) { const pt = seatPortrait(seat); ok(`seat ${seat}: portrait + labels clear of the dock`, pt.y + pt.r + 60 < DOCK.y); const q = unproject(pt.x, pt.y); ok(`seat ${seat}: portrait off the felt`, Math.hypot(q.u, q.v) > FELT_R); } const { w: bw, h: bh } = boardSize(); ok('scoreboard on screen', BOARD.x + bw <= 1910 && BOARD.y >= 0); ok('scoreboard clear of the title', BOARD.x > 960 + 160); ok('scoreboard above the shoe and the dealer edge', BOARD.y + bh < project(SHOE.u, SHOE.v).y - 20 && BOARD.y + bh < VIEW.yFar + 8); ok('scoreboard clear of the top-right portrait', BOARD.y + bh < seatPortrait(5).y - seatPortrait(5).r); // ── Roads ──────────────────────────────────────────────────────────────────── section('Roads'); const P = 'player', B = 'banker', T = 'tie'; ok('counts', JSON.stringify(counts([P, B, B, T])) === JSON.stringify({ player: 1, banker: 2, tie: 1 })); { const bp = beadPlate(Array(7).fill(P)); ok('bead plate fills columns top-down', bp[5].col === 0 && bp[5].row === 5 && bp[6].col === 1 && bp[6].row === 0); const long = beadPlate(Array.from({ length: 75 }, (_, i) => (i % 2 ? P : B)), 6, 10); ok('bead plate keeps the last whole columns', long.length <= 60 && long.every(c => c.col < 10) && long[long.length - 1].outcome === B); ok('bead plate drops whole columns', long[0].row === 0); } { const r = bigRoad([B, B, B, P, P, B]); ok('big road: streak goes down', r[0].col === 0 && r[2].row === 2 && r[2].col === 0); ok('big road: new winner → next column, top row', r[3].col === 1 && r[3].row === 0 && r[4].row === 1); ok('big road: third streak starts in column 2', r[5].col === 2 && r[5].row === 0); } { const r = bigRoad([P, T, T, B]); ok('big road: ties mark the last cell instead of taking one', r.length === 2 && r[0].ties === 2 && r[1].ties === 0); const lead = bigRoad([T, P]); ok('big road: leading ties attach to the first cell', lead.length === 1 && lead[0].ties === 1); } { const r = bigRoad(Array(9).fill(B)); ok('big road: dragon tail turns right at the bottom row', r[5].row === 5 && r[6].col === 1 && r[6].row === 5 && r[8].col === 3); const after = bigRoad([...Array(9).fill(B), P]); ok('big road: next streak starts after the tail\'s first column', after[9].col === 1 && after[9].row === 0); const blocked = bigRoad([...Array(8).fill(B), ...Array(7).fill(P)]); const pCells = blocked.filter(c => c.outcome === P); ok('big road: a streak blocked by an earlier tail turns right early', pCells[0].col === 1 && pCells[4].row === 4 && pCells[5].row === 4 && pCells[5].col === 2); const keys = new Set(blocked.map(c => `${c.col},${c.row}`)); ok('big road: no two cells share a spot', keys.size === blocked.length); } { const hist = Array.from({ length: 80 }, (_, i) => (i % 3 === 0 ? P : B)); const win = bigRoadWindow(hist, 6, 18); ok('big road window fits 18 columns', win.every(c => c.col >= 0 && c.col < 18)); ok('big road window ends on the latest coup', win[win.length - 1].outcome === hist[hist.length - 1]); } // ── Source lint ────────────────────────────────────────────────────────────── section('Source lint'); const src = readFileSync(new URL('../src/games/baccarat/BaccaratGame.js', import.meta.url), 'utf8'); ok('no hand-built Rectangle hit areas', !/new Phaser\.Geom\.Rectangle\(/.test(src)); ok('no setInteractive on containers', !/add\.container\([^)]*\)[^;]*setInteractive/.test(src)); ok('felt clicks go through hitBox', /hitBox\(p\.worldX, p\.worldY\)/.test(src)); ok('uses the shared dock + card skin', /new CasinoDock\(/.test(src) && /new CardSkin\(/.test(src)); console.log(`\n${pass} passed, ${fail} failed`); process.exit(fail ? 1 : 0);