fertig-classic-games/src/games/farkel/FarkelEndScreen.js

124 lines
6.7 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// FarkelEndScreen.js — the after-game card over the dice tray: final standings,
// per-player stats, a score-over-turns chart and the human's personal bests.
import { Button } from '../../ui/Button.js';
import { fmtPts } from './FarkelLogic.js';
import { INK, INK_HEX, DISPLAY_FONT, PRINT_FONT, SERIF, HAND_FONT } from './FarkelArt.js';
import { frame } from './FarkelDialogs.js';
const BEST_KEY = 'farkel-best';
// Records the human's best final score and biggest single turn.
export function recordPersonalBest(score, bestTurn) {
let prev = null;
try { prev = JSON.parse(localStorage.getItem(BEST_KEY) || 'null'); } catch (_) { prev = null; }
const today = new Date().toISOString().slice(0, 10);
const next = {
score: Math.max(prev?.score ?? 0, score),
scoreDate: score > (prev?.score ?? -1) ? today : prev?.scoreDate ?? today,
turn: Math.max(prev?.turn ?? 0, bestTurn),
turnDate: bestTurn > (prev?.turn ?? -1) ? today : prev?.turnDate ?? today,
};
try { localStorage.setItem(BEST_KEY, JSON.stringify(next)); } catch (_) { /* private mode */ }
return { prev, next, newScore: !prev || score > prev.score, newTurn: !prev || bestTurn > (prev.turn ?? 0) };
}
export function openEndScreen(scene, { gs, ranks, depth, best, onPlayAgain, onMenu }) {
const objs = [];
const n = gs.players.length;
const w = 1060, rowsH = n * 58;
const chartH = 190;
const h = 150 + rowsH + chartH + 150;
const x = 60 + (1100 - w) / 2, y = Math.max(16, 540 - h / 2);
const shade = scene.add.rectangle(590, 540, 1180, 1080, 0x000000, 0.5).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 the lodge!' : `${gs.players[winner].name} wins`;
objs.push(scene.add.text(x + w / 2, y + 42, head, { fontFamily: DISPLAY_FONT, fontSize: '34px', color: '#8e1a1a' }).setOrigin(0.5).setDepth(depth + 1));
const cols = [
{ k: 'rank', x: x + 50, label: '', o: 0.5 },
{ k: 'name', x: x + 96, label: 'PLAYER', o: 0 },
{ k: 'total', x: x + 420, label: 'FINAL', o: 0.5 },
{ k: 'best', x: x + 560, label: 'BEST TURN', o: 0.5 },
{ k: 'avg', x: x + 690, label: 'AVG BANK', o: 0.5 },
{ k: 'hot', x: x + 820, label: 'HOT DICE', o: 0.5 },
{ k: 'far', x: x + 950, label: 'FARKLES', o: 0.5 },
];
const hy = y + 104;
cols.forEach((c) => {
if (!c.label) return;
objs.push(scene.add.text(c.x, hy, c.label, { fontFamily: PRINT_FONT, fontSize: '13px', color: '#6f5d45' }).setOrigin(c.o, 0.5).setDepth(depth + 1));
});
order.forEach((p, row) => {
const pl = gs.players[p];
const st = pl.stats;
const ry = hy + 40 + row * 58;
const ink = INK[p];
const txt = (c, s, opts = {}) => {
const o = scene.add.text(c.x, ry, s, { fontFamily: HAND_FONT, fontSize: '27px', color: ink, ...opts }).setOrigin(c.o, 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 - 29, w - 60, 1);
objs.push(g);
}
txt(cols[0], ['1st', '2nd', '3rd', '4th'][ranks[p] - 1], { color: ranks[p] === 1 ? '#8e1a1a' : '#6f5d45', fontSize: '23px' });
txt(cols[1], pl.name.length > 16 ? pl.name.slice(0, 15) + '…' : pl.name);
txt(cols[2], fmtPts(pl.score), { fontSize: '34px' });
txt(cols[3], st.bestTurn ? fmtPts(st.bestTurn) : '—');
txt(cols[4], st.banks ? fmtPts(Math.round(st.bankTotal / st.banks / 10) * 10) : '—');
txt(cols[5], String(st.hotDice));
txt(cols[6], `${st.farkles}${st.penalties ? ` (−${st.penalties})` : ''}`);
});
// score-over-turns chart
const cx0 = x + 90, cy0 = hy + 40 + rowsH, cw = w - 160, ch = chartH - 30;
const g = scene.add.graphics().setDepth(depth + 1);
objs.push(g);
const maxTurns = Math.max(2, ...gs.players.map((p) => p.stats.history.length));
const maxScore = Math.max(gs.rules.target, ...gs.players.map((p) => p.score)) * 1.05;
const X = (k) => cx0 + (k / (maxTurns - 1)) * cw;
const Y = (v) => cy0 + ch - (Math.max(0, v) / maxScore) * ch;
g.lineStyle(1, 0x6f5d45, 0.5).strokeRect(cx0, cy0, cw, ch);
// target line (dashed)
const ty = Y(gs.rules.target);
g.lineStyle(2, 0x8e1a1a, 0.6);
for (let xx = cx0; xx < cx0 + cw; xx += 18) g.lineBetween(xx, ty, Math.min(cx0 + cw, xx + 10), ty);
objs.push(scene.add.text(cx0 - 8, ty, fmtPts(gs.rules.target), { fontFamily: PRINT_FONT, fontSize: '12px', color: '#8e1a1a' }).setOrigin(1, 0.5).setDepth(depth + 1));
objs.push(scene.add.text(cx0 - 8, cy0 + ch, '0', { fontFamily: PRINT_FONT, fontSize: '12px', color: '#6f5d45' }).setOrigin(1, 0.5).setDepth(depth + 1));
objs.push(scene.add.text(cx0 + cw, cy0 + ch + 14, `turn ${maxTurns - 1}`, { fontFamily: PRINT_FONT, fontSize: '12px', color: '#6f5d45' }).setOrigin(1, 0.5).setDepth(depth + 1));
gs.players.forEach((pl, p) => {
const hist = pl.stats.history;
g.lineStyle(3, INK_HEX[p], 0.9);
g.beginPath();
hist.forEach((v, k) => { if (k === 0) g.moveTo(X(k), Y(v)); else g.lineTo(X(k), Y(v)); });
g.strokePath();
g.fillStyle(INK_HEX[p], 1).fillCircle(X(hist.length - 1), Y(hist[hist.length - 1]), 4);
});
const fy = y + h - 104;
const me = gs.players[0];
const bits = [];
if (best.newScore && best.prev) bits.push(`New best score! ${fmtPts(me.score)} beats ${fmtPts(best.prev.score)}.`);
else bits.push(`Best score: ${fmtPts(best.next.score)}.`);
if (best.newTurn && best.prev && me.stats.bestTurn) bits.push(`New biggest turn: ${fmtPts(me.stats.bestTurn)}!`);
else bits.push(`Biggest turn ever: ${fmtPts(best.next.turn)}.`);
objs.push(scene.add.text(x + w / 2, fy, bits.join(' · '), {
fontFamily: SERIF, fontSize: '18px', fontStyle: 'italic', color: (best.newScore || best.newTurn) && best.prev ? '#8e1a1a' : '#5a4428',
}).setOrigin(0.5).setDepth(depth + 1));
objs.push(new Button(scene, x + w / 2 - 140, y + h - 52, 'Rematch', 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));
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()) };
}