Preserve initial environment knowledge in analysis scenarios

This commit is contained in:
coyotte508 2026-09-27 19:27:22 +02:00
commit 0b31b782db
3 changed files with 62 additions and 3 deletions

12
ANALYSIS.md Normal file
View file

@ -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.

View file

@ -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,

View file

@ -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/,
);
});