// YatziEndScreen.js — the after-game card over the dice tray: final standings, // a few stats per player, and the human's personal best (localStorage). import { Button } from '../../ui/Button.js'; import { CATEGORY_LABELS, computeTotals } from './YatziLogic.js'; import { TRAY } from './YatziLayout.js'; import { INK, DISPLAY_FONT, PRINT_FONT, SERIF, HAND_FONT } from './YatziArt.js'; import { frame } from './YatziDialogs.js'; const BEST_KEY = 'yatzi-best'; export function recordPersonalBest(score) { let prev = null; try { prev = JSON.parse(localStorage.getItem(BEST_KEY) || 'null'); } catch (_) { prev = null; } const isNew = !prev || score > prev.score; if (isNew) { try { localStorage.setItem(BEST_KEY, JSON.stringify({ score, date: new Date().toISOString().slice(0, 10) })); } catch (_) { /* private mode */ } } return { prev, isNew }; } export function openEndScreen(scene, { gs, ranks, depth, best, onPlayAgain, onMenu }) { const objs = []; const n = gs.players.length; const w = 980, h = 190 + n * 74 + 70; const x = TRAY.x + (TRAY.w - w) / 2, y = Math.max(20, TRAY.y + (TRAY.h - h) / 2 + 40); const shade = scene.add.rectangle(590, 540, 1180, 1080, 0x000000, 0.45).setDepth(depth - 1).setInteractive(); objs.push(shade, frame(scene, x, y, w, h, depth)); const order = gs.players.map((_, i) => i).sort((a, b) => ranks[a] - ranks[b]); const winner = order[0]; const human = ranks[0] === 1; const tie = ranks.filter((r) => r === 1).length > 1; const head = tie ? (human ? 'A tie at the top!' : 'A shared win') : human ? 'You win!' : `${gs.players[winner].name} wins`; objs.push(scene.add.text(x + w / 2, y + 42, head, { fontFamily: DISPLAY_FONT, fontSize: '34px', color: '#b3261e' }).setOrigin(0.5).setDepth(depth + 1)); const cols = [ { k: 'rank', x: x + 46, label: '' }, { k: 'name', x: x + 96, label: 'PLAYER' }, { k: 'total', x: x + 380, label: 'TOTAL' }, { k: 'bonus', x: x + 490, label: 'UPPER BONUS' }, { k: 'z', x: x + 630, label: 'ZAHTZEES' }, { k: 'best', x: x + 740, label: 'BEST TURN' }, { k: 'scr', x: x + w - 50, label: 'SCRATCHES' }, ]; const hy = y + 100; cols.forEach((c) => { if (!c.label) return; const o = c.k === 'name' ? 0 : c.k === 'scr' ? 1 : 0; objs.push(scene.add.text(c.x, hy, c.label, { fontFamily: PRINT_FONT, fontSize: '13px', color: '#6f5d45' }).setOrigin(o, 0.5).setDepth(depth + 1)); }); order.forEach((p, row) => { const pl = gs.players[p]; const t = computeTotals(pl); const ry = hy + 44 + row * 74; const ink = INK[p]; const txt = (cx, s, opts = {}, ox = 0) => { const o = scene.add.text(cx, ry, s, { fontFamily: HAND_FONT, fontSize: '28px', color: ink, ...opts }).setOrigin(ox, 0.5).setDepth(depth + 1); objs.push(o); return o; }; if (row > 0) { const g = scene.add.graphics().setDepth(depth + 1); g.fillStyle(0x465f96, 0.25).fillRect(x + 30, ry - 37, w - 60, 1); objs.push(g); } txt(cols[0].x, ['1st', '2nd', '3rd', '4th'][ranks[p] - 1], { color: ranks[p] === 1 ? '#b3261e' : '#6f5d45', fontSize: '24px' }, 0.5); txt(cols[1].x, pl.name.length > 14 ? pl.name.slice(0, 13) + '…' : pl.name); txt(cols[2].x, String(t.grand), { fontSize: '36px' }, 0.5); txt(cols[3].x, t.upperBonus ? '✓ +35' : '—', {}, 0); txt(cols[4].x, String(pl.stats.yahtzees), {}, 0.5); const b = pl.stats.bestTurn; txt(cols[5].x, b ? `${b.value} · ${CATEGORY_LABELS[b.category]}` : '—', { fontSize: '22px' }, 0); txt(cols[6].x, String(pl.stats.scratches), {}, 1); }); const fy = y + h - 104; const human0 = computeTotals(gs.players[0]).grand; const bestLine = best.isNew ? (best.prev ? `New personal best! ${human0} beats your old ${best.prev.score}.` : `Personal best set: ${human0}.`) : `Your personal best: ${best.prev.score} (${best.prev.date}).`; objs.push(scene.add.text(x + w / 2, fy, bestLine, { fontFamily: SERIF, fontSize: '18px', fontStyle: 'italic', color: best.isNew ? '#b3261e' : '#5a4428', }).setOrigin(0.5).setDepth(depth + 1)); objs.push(new Button(scene, x + w / 2 - 140, y + h - 52, 'Play Again', onPlayAgain, { width: 240, height: 50, fontSize: 20, bg: 0x22304f }).setDepth(depth + 2)); objs.push(new Button(scene, x + w / 2 + 140, y + h - 52, 'Back to Menu', onMenu, { width: 240, height: 50, fontSize: 20, bg: 0x5a3820 }).setDepth(depth + 2)); // drop in objs.forEach((o) => { if (o !== shade && o.y !== undefined) o.y -= 30; if (o.setAlpha) o.setAlpha(0); }); scene.tweens.add({ targets: objs.filter((o) => o !== shade), y: '+=30', alpha: 1, duration: 380, ease: 'Back.Out' }); shade.setAlpha(0); scene.tweens.add({ targets: shade, alpha: 1, duration: 300 }); return { close: () => objs.forEach((o) => o.destroy()) }; }