Reconstruct ongoing analysis from public and own known information
This commit is contained in:
parent
d479076da2
commit
ca641f07b9
2 changed files with 110 additions and 0 deletions
|
|
@ -1498,3 +1498,58 @@ export function replay(data, { to = data.history.length } = {}) {
|
|||
// Analysis may stop within a committed turn. Its prefix is already published.
|
||||
return commitPending(state);
|
||||
}
|
||||
|
||||
/** Reconstruct unknown futures from canonical pools, never the live secret deck. */
|
||||
export function createAnalysisScenario(data, { player, seed }) {
|
||||
const s = stripSecret(data, player);
|
||||
const known = s.foreseenEnvironment;
|
||||
for (const key of ["moves", "actor", "undoPlayer", "foreseenEnvironment"])
|
||||
delete s[key];
|
||||
s._seed = seed;
|
||||
s._counter = 0;
|
||||
s.history = [];
|
||||
s.players.forEach((p) => {
|
||||
p.dropped = false;
|
||||
});
|
||||
const pool = clone(ENVIRONMENTS);
|
||||
const remove = (card) => {
|
||||
const i = pool.findIndex(
|
||||
(c) => c.ozone === card.ozone && c.direction === card.direction,
|
||||
);
|
||||
if (i >= 0) pool.splice(i, 1);
|
||||
};
|
||||
let currentLogged = false;
|
||||
for (const entry of s.log) {
|
||||
const match = /^The current is (.+)\. Ozone layer: (\d+)\.$/.exec(
|
||||
entry.text,
|
||||
);
|
||||
if (match) {
|
||||
const card = {
|
||||
direction: DIRECTIONS.findIndex((d) => d.toLowerCase() === match[1]),
|
||||
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,
|
||||
s.deckRemaining,
|
||||
);
|
||||
if (s.options.newGenes) {
|
||||
s._knowledge =
|
||||
known && player !== undefined ? { [player]: clone(known) } : {};
|
||||
// All previously revealed genes remain in the public market's available list.
|
||||
const canonical = createNewGeneMarket(s.options, (n) => random(s, n));
|
||||
s.newGeneMarket = {
|
||||
available: [...s.newGeneMarket.available],
|
||||
_deck: canonical._deck.filter(
|
||||
(id) => !s.newGeneMarket.available.includes(id),
|
||||
),
|
||||
revealedRound: data.newGeneMarket.revealedRound,
|
||||
};
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
|
|
|||
55
test/analysis-scenario.test.js
Normal file
55
test/analysis-scenario.test.js
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
import test from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import {
|
||||
init,
|
||||
createAnalysisScenario,
|
||||
move,
|
||||
legalMoves,
|
||||
currentPlayer,
|
||||
} 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");
|
||||
const before = structuredClone(source);
|
||||
const changed = structuredClone(source);
|
||||
changed._seed = "different";
|
||||
changed._counter += 100;
|
||||
changed._deck.reverse();
|
||||
changed.newGeneMarket._deck.reverse();
|
||||
changed._undo = [{ player: 0, state: before }];
|
||||
changed._pendingMoves = [{ secret: true }];
|
||||
const a = createAnalysisScenario(source, { player: 0, seed: "fake" });
|
||||
assert.deepEqual(
|
||||
a,
|
||||
createAnalysisScenario(changed, { player: 0, seed: "fake" }),
|
||||
);
|
||||
assert.deepEqual(source, before);
|
||||
assert.equal(a._deck.length, source.deckRemaining);
|
||||
let state = a;
|
||||
for (let i = 0; i < 100 && !state.finished; i++)
|
||||
state = move(state, legalMoves(state)[0], currentPlayer(state));
|
||||
assert.notEqual(state, a);
|
||||
});
|
||||
test("own foresight is preserved; other knowledge and seeds are simulated", () => {
|
||||
const source = init(3, [], { newGenes: true }, "knowledge");
|
||||
source._knowledge = {
|
||||
0: structuredClone(source._deck[0]),
|
||||
1: structuredClone(source._deck[0]),
|
||||
};
|
||||
const a = createAnalysisScenario(source, { player: 0, seed: "sandbox" });
|
||||
assert.deepEqual(a._deck[0], source._knowledge[0]);
|
||||
assert.deepEqual(a._knowledge, { 0: source._knowledge[0] });
|
||||
const changed = structuredClone(source);
|
||||
changed._knowledge[1] = { ozone: 100, direction: 4 };
|
||||
assert.deepEqual(
|
||||
a,
|
||||
createAnalysisScenario(changed, { player: 0, seed: "sandbox" }),
|
||||
);
|
||||
assert.notDeepEqual(
|
||||
a._deck,
|
||||
createAnalysisScenario(source, { player: 0, seed: "another" })._deck,
|
||||
);
|
||||
assert.deepEqual(
|
||||
createAnalysisScenario(source, { seed: "public" }),
|
||||
createAnalysisScenario(changed, { seed: "public" }),
|
||||
);
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue