Speed up replay generation and release engine 0.2.23
This commit is contained in:
parent
fe01b40fa3
commit
01d369e27e
4 changed files with 87 additions and 27 deletions
|
|
@ -1139,12 +1139,14 @@ function settle(s) {
|
|||
const same = (a, b) =>
|
||||
JSON.stringify(Object.entries(a).sort()) ===
|
||||
JSON.stringify(Object.entries(b).sort());
|
||||
function applyMove(data, action, player) {
|
||||
function applyMove(data, action, player, replayOnly = false) {
|
||||
requireThat(!data.finished, "The game has ended.");
|
||||
requireThat(player === currentPlayer(data), "It is another colony's turn.");
|
||||
const legal = legalMoves(data).find((m) => same(m, action));
|
||||
requireThat(legal, "That action is not available.");
|
||||
const s = clone(data),
|
||||
// Replay frames own a scratch state. Keep validation and rule resolution,
|
||||
// but don't clone its growing journal or build unused history/undo snapshots.
|
||||
const s = replayOnly ? data : clone(data),
|
||||
m = legal,
|
||||
t = s.tasks.shift(),
|
||||
p = taskPlayer(s, t),
|
||||
|
|
@ -1492,7 +1494,9 @@ function applyMove(data, action, player) {
|
|||
throw new Error("Unknown move.");
|
||||
}
|
||||
settle(s);
|
||||
return recordMove(data, s, action, player, currentPlayer(s) === player);
|
||||
return replayOnly
|
||||
? s
|
||||
: recordMove(data, s, action, player, currentPlayer(s) === player);
|
||||
}
|
||||
export function move(data, action, player) {
|
||||
if (action?.type === "undo") return undoMove(data, action, player);
|
||||
|
|
@ -1522,25 +1526,27 @@ export function setPlayerMetaData(s, p, meta) {
|
|||
if (typeof meta.name === "string") s.players[p].name = meta.name;
|
||||
return s;
|
||||
}
|
||||
function publicPosition(data, logStart = 0) {
|
||||
// Filter before cloning: neither public views nor replay packets need copies
|
||||
// of private decks, the move history or nested undo snapshots.
|
||||
const fields = Object.fromEntries(
|
||||
Object.entries(data).filter(
|
||||
([key]) =>
|
||||
!key.startsWith("_") &&
|
||||
!["history", "log", "newGeneMarket"].includes(key),
|
||||
),
|
||||
);
|
||||
fields.log = data.log.slice(logStart);
|
||||
if (data.newGeneMarket)
|
||||
fields.newGeneMarket = publicNewGeneMarket(data.newGeneMarket);
|
||||
return clone(fields);
|
||||
}
|
||||
export function stripSecret(data, player) {
|
||||
const s = clone(data);
|
||||
delete s._seed;
|
||||
delete s._counter;
|
||||
delete s._deck;
|
||||
delete s.history;
|
||||
delete s._knowledge;
|
||||
delete s._undo;
|
||||
delete s._pendingMoves;
|
||||
delete s._moveDepth;
|
||||
delete s._maxMoveDepth;
|
||||
delete s._timeIncrements;
|
||||
delete s._liveUpdate;
|
||||
const s = publicPosition(data);
|
||||
const undoPlayer = data._undo?.at(-1)?.player;
|
||||
if (canUndo(data, undoPlayer)) s.undoPlayer = undoPlayer;
|
||||
if (data._knowledge?.[player])
|
||||
s.foreseenEnvironment = clone(data._knowledge[player]);
|
||||
if (s.newGeneMarket)
|
||||
s.newGeneMarket = publicNewGeneMarket(data.newGeneMarket);
|
||||
s.moves = legalMoves(data);
|
||||
s.actor = currentPlayer(data);
|
||||
s.historyLength = data.history.length;
|
||||
|
|
@ -1695,23 +1701,31 @@ export function replayFrames(
|
|||
end <= data.history.length,
|
||||
"Invalid replay range.",
|
||||
);
|
||||
const view = (state, position) => {
|
||||
const view = (state, position, logStart = 0) => {
|
||||
// Spectator projection: even a player's past Foresight is not included.
|
||||
const frame = stripSecret(state);
|
||||
delete frame.undoPlayer;
|
||||
const frame = publicPosition(state, logStart);
|
||||
frame.actor = currentPlayer(state);
|
||||
frame.moves = [];
|
||||
frame.historyLength = position;
|
||||
return frame;
|
||||
};
|
||||
let state = replay(data, { to: start });
|
||||
const state = init(data.players.length, [], data.options ?? {}, data._seed);
|
||||
state.players.forEach((player, seat) => {
|
||||
player.name = data.players[seat].name;
|
||||
});
|
||||
for (let index = 0; index < start; index++) {
|
||||
const entry = data.history[index];
|
||||
applyMove(state, entry.move, entry.player, true);
|
||||
}
|
||||
const initial = view(state, start),
|
||||
steps = [];
|
||||
let previous = initial;
|
||||
for (let index = start; index < end; index++) {
|
||||
const entry = data.history[index];
|
||||
state = applyMove(state, entry.move, entry.player);
|
||||
const next = view(state, index + 1);
|
||||
steps.push(replayChange(previous, next));
|
||||
const logStart = state.log.length;
|
||||
applyMove(state, entry.move, entry.player, true);
|
||||
const next = view(state, index + 1, logStart);
|
||||
steps.push(replayChange(previous, next, next.log));
|
||||
previous = next;
|
||||
}
|
||||
return { start, state: initial, steps };
|
||||
|
|
|
|||
|
|
@ -1,6 +1,10 @@
|
|||
// Replay packets contain public board changes, never seeds or future draws.
|
||||
// Journal entries are appended separately so a full replay stays linear in size.
|
||||
export function replayChange(before, after) {
|
||||
export function replayChange(
|
||||
before,
|
||||
after,
|
||||
log = after.log.slice(before.log.length),
|
||||
) {
|
||||
const set = {};
|
||||
for (const [key, value] of Object.entries(after)) {
|
||||
if (key !== "log" && JSON.stringify(value) !== JSON.stringify(before[key]))
|
||||
|
|
@ -9,7 +13,7 @@ export function replayChange(before, after) {
|
|||
return {
|
||||
set,
|
||||
remove: Object.keys(before).filter((key) => !(key in after)),
|
||||
log: after.log.slice(before.log.length),
|
||||
log,
|
||||
};
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "@boardgamers/primordial-soup",
|
||||
"version": "0.2.22",
|
||||
"version": "0.2.23",
|
||||
"private": true,
|
||||
"type": "module",
|
||||
"main": "dist/engine.js",
|
||||
|
|
|
|||
|
|
@ -98,6 +98,48 @@ test("bounded log slices use inclusive endpoints and expose only public replay d
|
|||
assert.throws(() => E.replayFrames(state, { start: -1 }), /range/);
|
||||
});
|
||||
|
||||
test("replay generation copies journal entries once without cloning history or undo stacks", (t) => {
|
||||
const state = game(6, true);
|
||||
const original = structuredClone(state);
|
||||
const clone = globalThis.structuredClone;
|
||||
let journalEntries = 0;
|
||||
const spy = t.mock.method(globalThis, "structuredClone", (value, ...args) => {
|
||||
if (value && typeof value === "object" && !Array.isArray(value)) {
|
||||
assert.ok(
|
||||
!("history" in value),
|
||||
"Replay copied the growing move history",
|
||||
);
|
||||
assert.ok(!("_undo" in value), "Replay built and copied an undo stack");
|
||||
journalEntries += value.log?.length ?? 0;
|
||||
}
|
||||
return clone(value, ...args);
|
||||
});
|
||||
const packet = E.replayFrames(state);
|
||||
spy.mock.restore();
|
||||
assert.equal(journalEntries, state.log.length);
|
||||
assert.deepEqual(at(packet, state.history.length), publicFrame(state));
|
||||
assert.deepEqual(state, original);
|
||||
});
|
||||
|
||||
test("fast replay matches every intermediate position and still rejects illegal history", () => {
|
||||
let state = E.init(3, [], { newGenes: true }, "replay-intermediate");
|
||||
const snapshots = [publicFrame(state, 0)];
|
||||
for (let i = 0; i < 180 && !state.finished; i++) {
|
||||
state = E.move(state, E.chooseAI(state), E.currentPlayer(state));
|
||||
snapshots.push(publicFrame(state, i + 1));
|
||||
}
|
||||
const packet = E.replayFrames(state);
|
||||
let position = packet.state;
|
||||
assert.deepEqual(position, snapshots[0]);
|
||||
packet.steps.forEach((step, i) => {
|
||||
position = applyReplayChange(position, step);
|
||||
assert.deepEqual(position, snapshots[i + 1], `Position ${i + 1}`);
|
||||
});
|
||||
const bad = structuredClone(state);
|
||||
bad.history[0].move = { type: "score", score: 10000 };
|
||||
assert.throws(() => E.replayFrames(bad), /not available/);
|
||||
});
|
||||
|
||||
test("replay excludes undoable actions and records only the replacement once committed", () => {
|
||||
let state = E.init(3, [], {}, "pending-replay");
|
||||
while (state.round === 0)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue