#!/usr/bin/env node // verifyBlackjack.js — headless checks for the Blackjack table geometry and chip maths. // The art/scene code needs a browser; this covers the pure parts it is built on. import { readFileSync } from 'node:fs'; import { VIEW, project, unproject, depthScale, polar, onFelt, seatLayout, cardPose, dealerCardPose, betPose, badgePose, insurancePose, SEAT_ANGLES, PLAY_ORDER, RAIL_OUT, RAIL_FACE_PX, FELT_R, BET_SPOT_R, INSURANCE_LINES, FELT_TEXT, TRAY, SHOE, DISCARD, PLACARD, DOCK, DEALER_V, } from '../src/games/blackjack/BlackjackTable.js'; import { breakdown, DENOMS, CHIP_DEFS } 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++; } } function section(name) { console.log(`\n${name}`); } const CARD_W = 96, CARD_H = 134; const dist = (a, b) => Math.hypot(a.x - b.x, a.y - b.y); const feltR = (pt) => { const { u, v } = unproject(pt.x, pt.y); return Math.hypot(u, v); }; const isOnFelt = (pt) => { const { u, v } = unproject(pt.x, pt.y); return onFelt(u, v); }; // ── Chips ──────────────────────────────────────────────────────────────────── section('Chip breakdown'); for (let amt = 0; amt <= 400; amt++) { const b = breakdown(amt); ok(`breakdown(${amt}) sums`, b.reduce((a, d) => a + d, 0) === amt); ok(`breakdown(${amt}) uses known denoms`, b.every(d => DENOMS.includes(d))); ok(`breakdown(${amt}) is largest-first`, b.every((d, i) => i === 0 || b[i - 1] >= d)); } // Every amount the game can put on the felt: bets 5–100 (AI 10/15 too), // doubles/splits up to 200, 3:2 payouts (floored), insurance (floor bet/2) and its 2:1 pay. const amounts = new Set(); for (let bet = 5; bet <= 100; bet += 5) { amounts.add(bet); amounts.add(bet * 2); amounts.add(Math.floor(bet * 1.5)); amounts.add(Math.floor(bet / 2)); amounts.add(Math.floor(bet / 2) * 2); } for (const a of amounts) ok(`game amount ${a} representable`, breakdown(a).reduce((x, d) => x + d, 0) === a); ok('every denom has a colour definition', DENOMS.every(d => CHIP_DEFS[d]?.base && CHIP_DEFS[d]?.insert)); ok('largest game stack fits the 14-chip cap', Math.max(...[...amounts].map(a => breakdown(a).length)) <= 14, `max ${Math.max(...[...amounts].map(a => breakdown(a).length))}`); // ── Projection ─────────────────────────────────────────────────────────────── section('Projection'); let prev = null; for (let v = -0.05; v <= 1.15; v += 0.01) { const p = project(0, v); if (prev) { ok(`scale grows toward the player (v=${v.toFixed(2)})`, p.s > prev.s); ok(`screen y grows toward the player (v=${v.toFixed(2)})`, p.y > prev.y); } prev = p; } ok('far edge at yFar', Math.abs(project(0, 0).y - VIEW.yFar) < 1e-6); ok('near point at yNear', Math.abs(project(0, 1).y - VIEW.yNear) < 1e-6); ok('scale 1 at the near point', Math.abs(depthScale(1) - 1) < 1e-9); for (const [u, v] of [[0, 0.3], [0.7, 0.2], [-0.5, 0.8], [0.9, 0.05]]) { const p = project(u, v), q = unproject(p.x, p.y); ok(`unproject round-trips (${u},${v})`, Math.abs(q.u - u) < 1e-9 && Math.abs(q.v - v) < 1e-9); } ok('table is symmetric', Math.abs(project(0.6, 0.4).x - VIEW.cx - (VIEW.cx - project(-0.6, 0.4).x)) < 1e-9); // ── Seats ──────────────────────────────────────────────────────────────────── section('Seat layout'); ok('7 seats', SEAT_ANGLES.length === 7); ok('play order covers every seat once', [...PLAY_ORDER].sort().join() === '0,1,2,3,4,5,6'); ok('seat 0 is bottom-centre', SEAT_ANGLES[0] === 0); ok('play order starts at screen-right (first base)', SEAT_ANGLES[PLAY_ORDER[0]] > 0); // Approximate a hand of n cards by a bounding circle. function handRadius(seat, n, split = false) { const { s } = seatLayout(seat).card; const card = 0.5 * Math.hypot(CARD_W, CARD_H * VIEW.squash) * s; if (!split) return card + 0.5 * (n - 1) * 28 * s; return card + 0.74 * CARD_W * s + 0.5 * (n - 1) * 28 * s; } for (let seat = 0; seat < 7; seat++) { const L = seatLayout(seat); ok(`seat ${seat}: card spot on felt`, isOnFelt(L.card)); ok(`seat ${seat}: bet spot on felt`, isOnFelt(L.bet)); ok(`seat ${seat}: badge on felt`, isOnFelt(L.badge)); ok(`seat ${seat}: split bets on felt`, isOnFelt(betPose(seat, 0)) && isOnFelt(betPose(seat, 1))); ok(`seat ${seat}: split badges on felt`, isOnFelt(badgePose(seat, 0)) && isOnFelt(badgePose(seat, 1))); ok(`seat ${seat}: insurance spot on felt`, isOnFelt(insurancePose(seat))); ok(`seat ${seat}: bet circle clear of the rail`, feltR(L.bet) + BET_SPOT_R < FELT_R); ok(`seat ${seat}: bet circle nearer the player than the cards`, L.bet.s > L.card.s); const p5 = cardPose(seat, 4, 5), p0 = cardPose(seat, 0, 5); ok(`seat ${seat}: a 5-card hand stays on the felt`, isOnFelt(p5) && isOnFelt(p0)); for (let other = seat + 1; other < 7; other++) { const O = seatLayout(other); const d = dist(L.card, O.card); ok(`seats ${seat}/${other}: 3-card hands don't overlap`, d > handRadius(seat, 3) + handRadius(other, 3) - 50, `d=${d.toFixed(0)}`); ok(`seats ${seat}/${other}: bet circles apart`, dist(L.bet, O.bet) > 110, dist(L.bet, O.bet).toFixed(0)); } } section('Portraits'); for (let seat = 0; seat < 7; seat++) { const pt = seatLayout(seat).portrait; ok(`seat ${seat}: portrait on screen`, pt.x - pt.r >= 0 && pt.x + pt.r <= 1920 && pt.y - pt.r >= 0 && pt.y + pt.r <= 1080); if (seat === 0) { ok('human portrait sits in the dock', pt.y - pt.r >= DOCK.y - 4); } else { ok(`seat ${seat}: portrait + labels clear of the dock`, pt.y + pt.r + 18 + 24 + 18 < DOCK.y); ok(`seat ${seat}: portrait off the felt`, !isOnFelt({ x: pt.x, y: pt.y }) && feltR(pt) > FELT_R); } for (let other = seat + 1; other < 7; other++) { const q = seatLayout(other).portrait; ok(`portraits ${seat}/${other} don't overlap`, dist(pt, q) > pt.r + q.r + 20); } } // ── Dealer side ────────────────────────────────────────────────────────────── section('Dealer side'); const dealer2 = dealerCardPose(0, 2); const dealerTop = dealer2.y - (CARD_H * dealer2.s * VIEW.squash) / 2; const dealerBottom = dealer2.y + (CARD_H * dealer2.s * VIEW.squash) / 2; ok('dealer cards clear the chip tray', dealerTop > project(0, TRAY.v1).y, `top=${dealerTop.toFixed(0)}`); ok('dealer cards clear the insurance line', dealerBottom < polar(INSURANCE_LINES.r0, 0).y, `bottom=${dealerBottom.toFixed(0)} line=${polar(INSURANCE_LINES.r0, 0).y.toFixed(0)}`); const d6 = dealerCardPose(5, 6); ok('6-card dealer hand stays clear of the shoe', d6.x + (CARD_W * d6.s) / 2 < project(SHOE.u, SHOE.v).x - 70 * d6.s); ok('6-card dealer hand stays clear of the discard', dealerCardPose(0, 6).x - (CARD_W * d6.s) / 2 > project(DISCARD.u, DISCARD.v).x + 60 * d6.s); ok('dealer row is on the felt', onFelt(0, DEALER_V)); ok('shoe / discard / placard on the felt', onFelt(SHOE.u, SHOE.v) && onFelt(DISCARD.u, DISCARD.v) && onFelt(PLACARD.u, PLACARD.v)); ok('placard clear of the tray', project(PLACARD.u, PLACARD.v).x + 52 * 0.72 < project(TRAY.u0, TRAY.v0).x); ok('placard clear of the discard holder', project(PLACARD.u, PLACARD.v).x - 52 * 0.72 > project(DISCARD.u, DISCARD.v).x + 54 * 0.72); const humanCardTop = seatLayout(0).card.y - (CARD_H * seatLayout(0).card.s * VIEW.squash) / 2; const lastText = FELT_TEXT.reduce((a, t) => (t.r > a.r ? t : a)); ok('felt lettering clears the human\'s cards', polar(lastText.r, 0).y + lastText.size * 0.6 < humanCardTop, `text=${polar(lastText.r, 0).y.toFixed(0)} cards=${humanCardTop.toFixed(0)}`); for (const seat of [5, 6]) { const c = seatLayout(seat).card; ok(`seat ${seat} cards clear of the shoe/discard`, Math.abs(c.x - project(seat === 5 ? SHOE.u : DISCARD.u, SHOE.v).x) > 90); } section('Dock'); const railBottom = polar(RAIL_OUT, 0).y + RAIL_FACE_PX * polar(RAIL_OUT, 0).s; ok('rail front face ends above the dock', railBottom < DOCK.y, `rail=${railBottom.toFixed(0)}`); ok('dock reaches the bottom of the canvas', DOCK.y + DOCK.h === 1080); for (let seat = 0; seat < 7; seat++) { const b = betPose(seat); ok(`seat ${seat}: bet stack well above the dock`, b.y + 40 < DOCK.y); } // ── Source lint (container hit-area gotcha) ────────────────────────────────── section('Source lint'); const read = (rel) => readFileSync(new URL(rel, import.meta.url), 'utf8'); for (const rel of ['../src/games/blackjack/BlackjackGame.js', '../src/ui/casino/CasinoDock.js', '../src/ui/casino/CasinoCards.js']) { const src = read(rel); ok(`${rel}: no hand-built Rectangle hit areas`, !/new Phaser\.Geom\.Rectangle\(/.test(src)); ok(`${rel}: no setInteractive on containers`, !/add\.container\([^)]*\)[^;]*setInteractive/.test(src)); } ok('dock chip buttons use a texture-space circle', /new Phaser\.Geom\.Circle\(64, 64, 62\)/.test(read('../src/ui/casino/CasinoDock.js'))); ok('Blackjack uses the shared dock + card skin', /new CasinoDock\(/.test(read('../src/games/blackjack/BlackjackGame.js')) && /new CardSkin\(/.test(read('../src/games/blackjack/BlackjackGame.js'))); console.log(`\n${pass} passed, ${fail} failed`); process.exit(fail ? 1 : 0);