From 94653b74572cbe02f1013cab07ef54f018233fcb Mon Sep 17 00:00:00 2001 From: coyotte508 Date: Sun, 27 Sep 2026 19:30:50 +0200 Subject: [PATCH] Preserve known loaned and radio dice in analysis scenarios --- packages/engine/src/game.spec.ts | 55 ++++++++++++++++++++++++++++++++ packages/engine/src/game.ts | 19 +++++++++++ 2 files changed, 74 insertions(+) diff --git a/packages/engine/src/game.spec.ts b/packages/engine/src/game.spec.ts index 3bbaffb..6835799 100644 --- a/packages/engine/src/game.spec.ts +++ b/packages/engine/src/game.spec.ts @@ -1661,3 +1661,58 @@ test("analysis setup keeps unrolled dice and simulated positions can progress", } assert.ok(s.round > 1 || s.outcome); }); + +test("analysis keeps a lender's known face across sampling without reading the recipient's secret", async () => { + const { createAnalysisScenario } = await import("../wrapper.js"); + for (const kind of ["map", "lighter"]) { + let s = withPhase(); + const die = structuredClone(s.players[0]!.dice[0]!); + if (kind === "map") s = equip(s, 0, "map", { target: 1, ids: [die.id] }); + else { + s = equip(s, 1, "lighter", { target: 0 }); + s = applyMove(s, { action: "respond", ids: [die.id] }, 0); + } + assert.deepEqual(stripSecret(s, 0).log.at(-1)!.dice, [die]); + assert.equal(stripSecret(s, 2).log.at(-1)!.dice, undefined); + const changed = structuredClone(s); + changed.players[1]!.dice.find((d) => d.id === die.id)!.face = (die.face % 6) + 1; + changed.seed = "different secret"; + const a = createAnalysisScenario(s, { player: 0, seed: "fake" }); + assert.deepEqual(a, createAnalysisScenario(changed, { player: 0, seed: "fake" })); + assert.equal(a.players[1]!.dice.find((d) => d.id === die.id)!.face, die.face); + const again = createAnalysisScenario(a, { player: 0, seed: "another fake" }); + assert.equal(again.players[1]!.dice.find((d) => d.id === die.id)!.face, die.face); + for (const equipment of ["water", "shovel"]) { + let turned = equip(s, 1, equipment, { target: 1, ids: [die.id], face: 6 }); + if (equipment === "water") turned = applyMove(turned, { action: "respond", ids: [die.id] }, 1); + const results = new Set( + Array.from( + { length: 20 }, + (_, i) => + createAnalysisScenario(turned, { player: 0, seed: `sample-${i}` }).players[1]!.dice.find( + (d) => d.id === die.id + )!.face + ) + ); + assert.ok(results.size > 1, `${equipment} invalidates the old loan snapshot`); + } + } +}); + +test("analysis remembers publicly revealed Radio dice after the phase ends", async () => { + const { createAnalysisScenario } = await import("../wrapper.js"); + let s = withPhase("planning"); + s = equip(s, 1, "radio"); + const known = structuredClone(s.players[1]!.dice); + for (let seat = 0; seat < s.players.length; seat++) { + const path = Object.values(paths(s, seat)).find( + (path) => !s.players.some((p, j) => j !== seat && p.ready && p.path.at(-1) === path.at(-1)) + )!; + s = applyMove(s, { action: "plan", path }, seat); + s = applyMove(s, { action: "ready" }, seat); + } + assert.equal(s.phase, "reroll"); + assert.equal(s.players[1]!.radio, false); + const scenario = createAnalysisScenario(s, { player: 0, seed: "radio-simulation" }); + assert.deepEqual(scenario.players[1]!.dice, known); +}); diff --git a/packages/engine/src/game.ts b/packages/engine/src/game.ts index e80eddf..2e6b9ac 100644 --- a/packages/engine/src/game.ts +++ b/packages/engine/src/game.ts @@ -155,6 +155,7 @@ function phase(s: State, next: State["phase"]) { s.phase = next; for (const turn of turnProgress(s)) turn.credited = false; for (const p of s.players) { + if (p.radio) diceEvent(s, `${p.name} revealed their dice`, p.dice); p.ready = false; p.radio = false; } @@ -581,6 +582,7 @@ function execute(s: State, m: Move, seat: number) { if (!p) throw Error("Invalid player."); if (s.pending) { const pending = s.pending; + let loan: Die | undefined; const source = pending.equipment ? equipment(pending.equipment).name : "equipment"; let responseText = pending.kind === "lend" @@ -598,6 +600,7 @@ function execute(s: State, m: Move, seat: number) { p.dice = p.dice.filter((x) => x !== d); s.players[pending.receiver!]!.dice.push(d); responseText = `lent 1 die to ${s.players[pending.receiver!]!.name} until the end of the round`; + loan = structuredClone(d); pending.players = []; } else { roll(s, dice); @@ -608,6 +611,10 @@ function execute(s: State, m: Move, seat: number) { } else throw Error("Resolve the equipment effect first."); event(s, `${p.name} ${responseText}.`, "equipment"); if (pending.kind === "reroll") s.log.at(-1)!.sound = "dice"; + if (loan) { + s.log.at(-1)!.dice = [loan]; + s.log.at(-1)!.diceVisibleTo = [seat, pending.receiver!]; + } finishPending(s); return; } @@ -1228,6 +1235,18 @@ export function createAnalysisScenario(data: State, { player, seed }: { player?: EQUIPMENT.map((c) => c.id).filter((id) => !used.has(id)) ).slice(0, deckCount); if (s.phase !== "setup") { + // Logs retain private loan snapshots for their two participants. A later + // hidden roll/turn has no public die IDs, so conservatively forget old faces. + const known = new Map(); + for (const entry of s.log) { + if (entry.round !== s.round) continue; + if (entry.sound === "dice" || (entry.type === "equipment" && / ยท turned \d+ (?:die|dice)\./.test(entry.text))) + known.clear(); + for (const die of [...(entry.dice ?? []), ...(entry.setAside ?? [])]) known.set(die.id, die.face); + } + for (const die of [...s.players.flatMap((p) => p.dice), ...s.ghost]) { + if (die.face === 0 && known.has(die.id)) die.face = known.get(die.id)!; + } for (const p of s.players) roll( s,