From 0b31b782dbe1660aff096f2b2f47e7495795d3a7 Mon Sep 17 00:00:00 2001 From: coyotte508 Date: Sun, 27 Sep 2026 19:27:22 +0200 Subject: [PATCH] Preserve initial environment knowledge in analysis scenarios --- ANALYSIS.md | 12 ++++++++++++ engine/index.js | 19 ++++++++++++++++--- test/analysis-scenario.test.js | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 62 insertions(+), 3 deletions(-) create mode 100644 ANALYSIS.md diff --git a/ANALYSIS.md b/ANALYSIS.md new file mode 100644 index 0000000..f17cf80 --- /dev/null +++ b/ANALYSIS.md @@ -0,0 +1,12 @@ +# Ongoing analysis + +`createAnalysisScenario` retains public state and the requesting player's foresight, +reconstructs future environment and gene draws independently, and removes live undo, +private history and randomness. The platform supplies the authenticated perspective +and a fresh seed. Re-sampling uses only the sanitized starting snapshot. + +New games record `initialEnvironment`, which is public from setup, so that card stays +excluded from future draws after subsequent environments are revealed. Legacy games +without this record support ongoing analysis only before the first environment +change; later positions fail closed rather than reconstructing the missing public +history from the real secret seed. Finished-game analysis remains available. diff --git a/engine/index.js b/engine/index.js index a9fb15a..e663815 100644 --- a/engine/index.js +++ b/engine/index.js @@ -148,6 +148,7 @@ export function init(players, expansions = [], options = {}, seed = "soup") { }; s._deck = shuffle(s, ENVIRONMENTS); s.environment = s._deck.shift(); + s.initialEnvironment = clone(s.environment); if (options.newGenes) { s.toxins = Array(25).fill(0); s.alarmPlayers = []; @@ -1499,8 +1500,21 @@ export function replay(data, { to = data.history.length } = {}) { return commitPending(state); } +export function canLaunchAnalysisMode(data) { + return ( + !!data.initialEnvironment || + !data.log.some((entry) => + /^The current is (.+)\. Ozone layer: (\d+)\.$/.test(entry.text), + ) + ); +} + /** Reconstruct unknown futures from canonical pools, never the live secret deck. */ export function createAnalysisScenario(data, { player, seed }) { + requireThat( + canLaunchAnalysisMode(data), + "This legacy position lacks its public initial environment record.", + ); const s = stripSecret(data, player); const known = s.foreseenEnvironment; for (const key of ["moves", "actor", "undoPlayer", "foreseenEnvironment"]) @@ -1518,7 +1532,8 @@ export function createAnalysisScenario(data, { player, seed }) { ); if (i >= 0) pool.splice(i, 1); }; - let currentLogged = false; + s.initialEnvironment ??= clone(s.environment); + remove(s.initialEnvironment); for (const entry of s.log) { const match = /^The current is (.+)\. Ozone layer: (\d+)\.$/.exec( entry.text, @@ -1529,10 +1544,8 @@ export function createAnalysisScenario(data, { player, seed }) { ozone: Number(match[2]), }; remove(card); - currentLogged = true; } } - if (!currentLogged) remove(s.environment); if (known) remove(known); s._deck = [...(known ? [clone(known)] : []), ...shuffle(s, pool)].slice( 0, diff --git a/test/analysis-scenario.test.js b/test/analysis-scenario.test.js index e3303cc..1c1e25b 100644 --- a/test/analysis-scenario.test.js +++ b/test/analysis-scenario.test.js @@ -6,6 +6,7 @@ import { move, legalMoves, currentPlayer, + canLaunchAnalysisMode, } from "../engine/index.js"; test("scenario does not depend on hidden decks, RNG or undo and stays playable", () => { const source = init(3, [], { newGenes: true }, "secret"); @@ -53,3 +54,36 @@ test("own foresight is preserved; other knowledge and seeds are simulated", () = createAnalysisScenario(changed, { seed: "public" }), ); }); + +test("initial public environment stays excluded after later reveals and resampling", () => { + const source = init(3, [], {}, "initial-environment"); + const initial = structuredClone(source.environment); + source.environment = source._deck.shift(); + source.deckRemaining = source._deck.length; + const directions = ["west", "north", "east", "south", "still"]; + source.log.push({ + text: `The current is ${directions[source.environment.direction]}. Ozone layer: ${source.environment.ozone}.`, + }); + for (const seed of ["a", "b", "c"]) { + const scenario = createAnalysisScenario(source, { seed }); + assert.equal(scenario._deck.length, source.deckRemaining); + assert.ok( + !scenario._deck.some( + (c) => c.direction === initial.direction && c.ozone === initial.ozone, + ), + ); + assert.deepEqual(scenario.initialEnvironment, initial); + const again = createAnalysisScenario(scenario, { seed: "resample" }); + assert.ok( + !again._deck.some( + (c) => c.direction === initial.direction && c.ozone === initial.ozone, + ), + ); + } + delete source.initialEnvironment; + assert.equal(canLaunchAnalysisMode(source), false); + assert.throws( + () => createAnalysisScenario(source, { seed: "safe" }), + /initial environment/, + ); +});