/** * The verifier page (DESIGN §4-2, §16-2): type a seed (32 hex characters) and see the fingerprint, the full 40-card order and who * got what, in Italian and English. It runs the same `core/shuffle.ts` the game runs (imported directly, no copy), so the page cannot * disagree with the app. `verify` is pure (verifica.test.ts checks it against the core for sampled seeds); the DOM part below only * runs in the browser. The page prefills from `#seed=<32 hex>` (plus `&redeal=0` when the match had the three-kings redeal switched * off, REVIEW #6) so the app can open it with the seed of a finished hand and the page deals exactly what the app dealt. * Deal order is the header of core/shuffle.ts: Scopa 0-2 first player, 3-5 dealer, 6-9 table, then 6 per deal (3 + 3); * Briscola 0-2 first player, 3-5 dealer, 6 trump card, 7-39 stock. */ import { RE, rankOf, suitOf, type Card } from './core/cards.ts'; import { dailySeed, deckFromSeed, fingerprint, seedBytes } from './core/shuffle.ts'; import { toHex } from './core/sha256.ts'; export interface Verification { /** The seed as 32 lowercase hex characters. */ seed: string; fingerprint: string; scopa: { /** Redeals before the opening deal (three kings on the table with the standard rule on). */ redeals: number; /** The 40-card order after the redeals, top first. */ deck: Card[]; first: Card[]; dealer: Card[]; table: Card[]; /** Later deals of the round: 3 to the first player, 3 to the dealer, in this order. */ later: Array<{ first: Card[]; dealer: Card[] }>; }; briscola: { deck: Card[]; first: Card[]; dealer: Card[]; trump: Card; stock: Card[] }; } /** Where a card went: seat role and deal. */ export type Holder = 'first' | 'dealer' | 'table'; /** Reads the page's hash: `#seed=<32 hex>[&redeal=0]`. Null when there is no seed in it. */ export function parseHash(hash: string): { seed: string; redeal: boolean } | null { const m = /^#seed=([0-9a-fA-F]{32})(?:&redeal=([01]))?$/.exec(hash); return m ? { seed: m[1]!.toLowerCase(), redeal: m[2] !== '0' } : null; } /** The hash for a seed and the redeal rule (the rule appears only when it is off, the default being on). */ export const hashFor = (seed: string, redeal: boolean): string => `#seed=${seed}${redeal ? '' : '&redeal=0'}`; /** Seed text → everything the page prints. Throws on a seed that is not 32 hex characters. */ export function verify(seedText: string, redealThreeKings = true): Verification { const seed = toHex(seedBytes(seedText)); let redeals = 0; while (redealThreeKings && redeals < 1000 && deckFromSeed(seed, redeals).slice(6, 10).filter((c) => rankOf(c) === RE).length >= 3) redeals++; const deck = deckFromSeed(seed, redeals); const later: Verification['scopa']['later'] = []; for (let i = 10; i + 6 <= 40; i += 6) later.push({ first: deck.slice(i, i + 3), dealer: deck.slice(i + 3, i + 6) }); const b = deckFromSeed(seed, 0); return { seed, fingerprint: fingerprint(seed), scopa: { redeals, deck, first: deck.slice(0, 3), dealer: deck.slice(3, 6), table: deck.slice(6, 10), later }, briscola: { deck: b, first: b.slice(0, 3), dealer: b.slice(3, 6), trump: b[6]!, stock: b.slice(7) }, }; } /** The seed of the daily deal for a local date, so anyone can check it by hand (shuffle.ts `dailySeed`). */ export const dailyFor = (ymd: string, game: 'scopa' | 'briscola'): string => dailySeed(ymd, game); // ---------- names ---------- const RANK_IT = ['', 'Asso', 'Due', 'Tre', 'Quattro', 'Cinque', 'Sei', 'Sette', 'Fante', 'Cavallo', 'Re']; const RANK_EN = ['', 'Ace', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Knave', 'Knight', 'King']; const SUIT_IT = ['denari', 'coppe', 'spade', 'bastoni']; const SUIT_EN = ['coins', 'cups', 'swords', 'clubs']; export type Lang = 'it' | 'en'; export const cardName = (c: Card, lang: Lang): string => lang === 'it' ? `${RANK_IT[rankOf(c)]} di ${SUIT_IT[suitOf(c)]}` : `${RANK_EN[rankOf(c)]} of ${SUIT_EN[suitOf(c)]}`; /** "7D" style code used in logs and fixtures (core/cards.ts cardCode), repeated here so the page needs no other import. */ export const shortCode = (c: Card): string => `${rankOf(c)}${'DCSB'[suitOf(c)]}`; // ---------- the page ---------- const T = { it: { title: 'Verifica il mazzo', lead: 'Inserisci il seme (32 caratteri esadecimali) mostrato dopo una mano. La pagina calcola l\'impronta e l\'ordine completo delle 40 carte con lo stesso codice del gioco.', seed: 'Seme', go: 'Verifica', bad: 'Il seme deve essere di 32 caratteri esadecimali (0-9, a-f).', fp: 'Impronta del mazzo', fpNote: 'I primi 8 caratteri di SHA-256 del seme. Il gioco la mostra prima della prima carta: se coincide con questa, il mazzo è quello.', order: 'Ordine delle 40 carte', orderNote: 'Dalla cima del mazzo (posizione 0, distribuita per prima).', scopa: 'Scopa: chi ha ricevuto cosa', briscola: 'Briscola: chi ha ricevuto cosa', first: 'Primo di mano', dealer: 'Mazziere', table: 'Tavolo (scoperte)', later: 'Distribuzione successiva', redeal: 'Il tavolo aveva tre Re: il mazzo è stato rimescolato {n} volta/e dallo stesso flusso.', redealRule: 'Regola «tre Re in tavola: si rimescola» attiva (come nelle impostazioni della partita)', trump: 'Briscola (carta scoperta)', stock: 'Mazzo da pescare (in ordine)', daily: 'Mano del giorno', dailyNote: 'Il seme del giorno è SHA-256 di "scopa-daily-v1|AAAA-MM-GG" (primi 16 byte); per la Briscola "briscola-daily-v1|".', date: 'Data', useSeed: 'Usa questo seme', source: 'Codice sorgente', sourceNote: 'Il mazzo dipende solo dal seme: nessun livello, punteggio o storico entra nel calcolo.', honest: 'Il gioco si impegna sul seme prima che tu giochi (impronta) e te lo mostra dopo, così puoi controllare la distribuzione. Non diciamo di più di questo.', }, en: { title: 'Check the deck', lead: 'Enter the seed (32 hexadecimal characters) shown after a hand. The page works out the fingerprint and the full order of the 40 cards with the same code the game runs.', seed: 'Seed', go: 'Verify', bad: 'The seed must be 32 hexadecimal characters (0-9, a-f).', fp: 'Deck fingerprint', fpNote: 'The first 8 characters of SHA-256 of the seed. The game shows it before the first card: if it matches, this is the deck.', order: 'Order of the 40 cards', orderNote: 'From the top of the deck (position 0, dealt first).', scopa: 'Scopa: who got what', briscola: 'Briscola: who got what', first: 'First player', dealer: 'Dealer', table: 'Table (face up)', later: 'Later deal', redeal: 'The table had three Kings: the deck was reshuffled {n} time(s) from the same stream.', redealRule: 'Rule "three Kings on the table: reshuffle" on (as in the match settings)', trump: 'Trump (face-up card)', stock: 'Stock to draw (in order)', daily: 'Daily deal', dailyNote: 'The daily seed is SHA-256 of "scopa-daily-v1|YYYY-MM-DD" (first 16 bytes); Briscola uses "briscola-daily-v1|".', date: 'Date', useSeed: 'Use this seed', source: 'Source code', sourceNote: 'The deck depends on the seed alone: no level, score or history goes into it.', honest: 'The game commits to the seed before you play (the fingerprint) and shows it afterwards so you can check the deal. We claim no more than that.', }, } as const; const esc = (s: string): string => s.replace(/[&<>"]/g, (c) => ({ '&': '&', '<': '<', '>': '>', '"': '"' })[c]!); const chips = (cards: readonly Card[], lang: Lang): string => cards.map((c) => `${shortCode(c)}`).join(' '); /** The result as HTML in one language (also what the test renders to check nothing throws). */ export function render(v: Verification, lang: Lang): string { const t = T[lang]; const row = (label: string, cards: readonly Card[]) => `
${esc(t.redeal.replace('{n}', String(s.redeals)))}
` : ''; return `${v.fingerprint.toUpperCase()}
${esc(t.fpNote)}
${esc(t.orderNote)}
${esc(T[lang].bad)}
`; } }; const daily = () => { const ymd = dateEl.value; const box = $('daily-out'); try { const sc = dailyFor(ymd, 'scopa'); const br = dailyFor(ymd, 'briscola'); box.innerHTML = `Scopa ${sc}
Briscola ${br}