Fix finished game loading and reveal final score breakdowns

This commit is contained in:
coyotte508 2026-10-13 22:02:48 +02:00
commit b86ec19341
5 changed files with 33 additions and 7 deletions

View file

@ -778,7 +778,11 @@ try {
),
);
// Journal and final scoring remain scrollable even on the smallest viewport.
await page.evaluate((s) => host.emit("state", s), E.stripSecret(full, 0));
await page.evaluate((s) => host.emit("state", s), full);
for (let i = 0; i < full.players.length; i++) {
const counter = page.locator(`.player-strip [data-score="${i}"]`);
assert.ok((await counter.getAttribute("aria-label")).endsWith(`: ${E.scoreBreakdown(full, i).total}`));
}
await page.locator('[data-panel="journal"]').click();
const log = page.locator(".pr-modal .modal-body");
await log.evaluate((el) => (el.scrollTop = 0));
@ -790,7 +794,7 @@ try {
await page.locator("[data-scores]").click();
assert.equal(await overflow(), false);
assert.ok((await page.locator(".scores article").count()) === 3);
await page.locator('.scores [data-score="1"]').click();
await page.locator('.scores [data-score="1"]').last().click();
assert.match(await page.locator(".score-total").innerText(), /Total final/);
await page.keyboard.press("Escape");
// Scroll starts on disabled controls without interception.

View file

@ -122,3 +122,19 @@ test("another player placement does not acknowledge our pending submission", asy
assert.equal(done, true);
viewer.destroy();
});
test("finished BGS saves are converted to a complete public viewer state", async () => {
const E = await import("../engine/index.js");
const raw = E.init(3, [], {}, "finished-view");
raw.finished = true;
raw.players[1].vp = 17;
const { calls, viewer } = fixture();
viewer.onPlayer({ index: 0 });
viewer.onState(raw);
const state = calls.findLast(([kind]) => kind === "render")[1];
assert.deepEqual(state.legal, []);
assert.equal(state.players[1].vp, 17);
assert.equal(state.results[1].total, E.scoreBreakdown(raw, 1).total);
assert.equal(state.historyLength, E.logLength(raw));
assert.equal(state.history, undefined);
});

View file

@ -1,3 +1,4 @@
import { stripSecret } from "../engine/index.js";
/** Host preferences arrive separately from theme changes. Preserve seat appearance. */
export function mergePreferences(previous, update) {
return { ...previous, ...update };
@ -53,6 +54,8 @@ export function viewerLifecycle(ctx, ui, chat) {
},
chat,
onState(s) {
// BGS returns the raw saved engine state after the game ends.
if (s.finished && Array.isArray(s.history)) s = stripSecret(s, playerIndex);
live = s;
const awaitingPlacement =
pending &&

View file

@ -82,7 +82,7 @@ export function scoreContent(state, player, t) {
.join("");
return `<div class="score-detail">
<section><header><h3>${t("scoreEarned")}</h3>${points(score.tokens)}</header>
<p class="score-note">${t("scoreCounterHint")}</p>
${state.finished ? "" : `<p class="score-note">${t("scoreCounterHint")}</p>`}
${sources.length ? `<ul class="score-lines">${sources.map(({ id, points: n }) => row(label(id), n, id === "scoreShippingBonus" ? t("scoreShippingBonusHint") : id === "hiddenMarket" ? t("scoreSpent") : "")).join("")}</ul>` : `<p class="score-note">${t("scoreNoTokens")}</p>`}
${history.length ? `<details class="score-history"><summary>${t("scoreHistory")}</summary>${journalContent({ ...state, events: history }, t)}</details>` : ""}
</section>

View file

@ -16,6 +16,7 @@ import {
cost,
citySize,
production,
scoreBreakdown,
} from "../engine/index.js";
import { translator } from "./i18n.js";
import { icon, scoreToken } from "./icons.js";
@ -156,7 +157,7 @@ export function mountGame(
? `<span class="building-type-icon" role="img" title="${label}" aria-label="${label}">${drawing}</span>`
: drawing;
};
const scoreButton = (i, n = state.players[i].vp) =>
const scoreButton = (i, n = state.finished ? state.results?.[i]?.total : state.players[i].vp) =>
Number.isFinite(n)
? `<button type="button" class="score-button" data-score="${i}" title="${esc(t("scoreDetails"))}" aria-label="${esc(`${t("scoreDetails")} · ${state.players[i].name} : ${n}`)}">${scoreToken(n)}</button>`
: `<span title="${esc(t("scoreHidden"))}">${metric("vp", "?")}</span>`;
@ -866,7 +867,9 @@ export function mountGame(
}
function render(s, awaitingMove = false) {
if (s) {
state = s;
state = s.finished
? { ...s, results: s.results ?? s.players.map((_, i) => scoreBreakdown(s, i)) }
: s;
pending = awaitingMove;
}
if (!state) return;
@ -1072,11 +1075,11 @@ export function mountGame(
`<div class="scores">${(state.results ?? [])
.map(
(r, i) =>
`<article><header><h3>${esc(state.players[i].name)}</h3>${scoreButton(i, r.total)}</header><p>${t("tokens")} <b>${r.tokens}</b></p><p>${t("market")} <b>${r.buildings}</b></p><p>${t("citizens")} <b>${r.citizens}</b></p>${Object.entries(
`<article><header><h3>${esc(state.players[i].name)}</h3>${scoreButton(i, r.total)}</header><p>${t("tokens")} <b>${r.tokens}</b></p><p>${t("scoreBuildings")} <b>${r.buildings}</b></p><p>${t("citizens")} <b>${r.citizens}</b></p>${Object.entries(
r.bonus,
)
.map(([id, n]) => `<p>${t(id)} <b>${n}</b></p>`)
.join("")}<p>${t("tie")} <b>${r.tie}</b></p></article>`,
.join("")}<p>${t("tie")} <b>${r.tie}</b></p><button type="button" data-score="${i}">${t("scoreDetails")}</button></article>`,
)
.join("")}</div>`,
);