Accelerate long replay log slices without losing move history
This commit is contained in:
parent
46cf889856
commit
f7270b91d3
5 changed files with 76 additions and 18 deletions
|
|
@ -68,7 +68,7 @@ Enable “Show everyone’s VP” at game creation to reveal accumulated VP toke
|
|||
|
||||
## Replay, journal and platform compatibility
|
||||
|
||||
The journal retains the latest 200 events. Older score contributions are accumulated by player and source, so the score breakdown remains accurate. Replay keeps the actual accepted move sequence. Undo restores its previous branch instead of appending undo events; regression tests repeatedly undo and replay the same action to check that state size does not accumulate. Replay frames are private to the requesting seat, and postgame analysis disables forced-move automation so each branch can be explored explicitly. Analysis is unavailable during live games to protect hidden objectives and future draws.
|
||||
The journal retains the latest 200 events. Older score contributions are accumulated by player and source, so the score breakdown remains accurate. The accepted move history remains complete for every replay boundary and analysis branch; unlike the journal, its storage grows with the number of moves. Undo restores its previous branch instead of appending undo events; regression tests repeatedly undo and replay the same action to check that abandoned branches do not accumulate. Log slices reuse the current position and short-lived role checkpoints rather than repeatedly replaying the entire game; raw checkpoints are never saved or sent to viewers. Replay frames are private to the requesting seat, and postgame analysis disables forced-move automation so each branch can be explored explicitly. Analysis is unavailable during live games to protect hidden objectives and future draws.
|
||||
|
||||
Chat opens from the button next to the journal. On a host/SDK exposing `openChat` and advertising `platformChat`, it opens the native BGS chat. Protocol 0.9 hosts retain the functional SDK chat dialog. Native chat requires the corresponding platform PR and a protocol package upgrade; no unsupported messages are sent to older hosts.
|
||||
|
||||
|
|
|
|||
|
|
@ -1411,7 +1411,7 @@ function placementUndoState(s, p) {
|
|||
return null;
|
||||
return candidate;
|
||||
}
|
||||
function productionEditState(s, p) {
|
||||
function productionEditState(s, p, checkpoint) {
|
||||
if (s.tutorial || s.finished || s.puertoma || s.tasks[0]?.kind !== "produce")
|
||||
return null;
|
||||
const index = s.history.findLastIndex((h) => h.p === p);
|
||||
|
|
@ -1425,7 +1425,7 @@ function productionEditState(s, p) {
|
|||
.some((h) => !["produce", "produceBonus", "pass"].includes(h.move.type))
|
||||
)
|
||||
return null;
|
||||
const before = replay(s, index + 1);
|
||||
const before = replayFromCheckpoint(s, index + 1, checkpoint);
|
||||
const task = playerTask(before, p);
|
||||
if (
|
||||
!["produce", "produceBonus"].includes(task?.kind) ||
|
||||
|
|
@ -1441,10 +1441,11 @@ function productionEditState(s, p) {
|
|||
return null;
|
||||
let candidate;
|
||||
try {
|
||||
candidate = replay({
|
||||
...s,
|
||||
history: s.history.filter((_, i) => i !== index),
|
||||
});
|
||||
candidate = replayFromCheckpoint(
|
||||
{ ...s, history: s.history.filter((_, i) => i !== index) },
|
||||
s.history.length,
|
||||
checkpoint,
|
||||
);
|
||||
} catch {
|
||||
return null;
|
||||
} // A later choice depends on this one: keep it committed.
|
||||
|
|
@ -1466,18 +1467,21 @@ function productionEditState(s, p) {
|
|||
]);
|
||||
return reveals(candidate) === reveals(s) ? candidate : null;
|
||||
}
|
||||
export function canEditProduction(s, p) {
|
||||
return !!productionEditState(s, p);
|
||||
export function canEditProduction(s, p, checkpoint) {
|
||||
return !!productionEditState(s, p, checkpoint);
|
||||
}
|
||||
export function canEditPlacement(s, p) {
|
||||
return !!placementUndoState(s, p);
|
||||
}
|
||||
export function canUndo(s, p) {
|
||||
return canUndoFromPrevious(s, p);
|
||||
}
|
||||
function canUndoFromPrevious(s, p, previous) {
|
||||
if (s.tutorial) return false;
|
||||
const last = s.history.at(-1);
|
||||
if (!last || last.p !== p || s.finished || !currentPlayers(s).includes(p))
|
||||
return false;
|
||||
const before = replay(s, logLength(s) - 1);
|
||||
const before = previous ?? replay(s, logLength(s) - 1);
|
||||
return (
|
||||
JSON.stringify(currentPlayers(before).slice().sort()) ===
|
||||
JSON.stringify(currentPlayers(s).slice().sort()) &&
|
||||
|
|
@ -2155,11 +2159,13 @@ export function rankings(s) {
|
|||
).length,
|
||||
);
|
||||
}
|
||||
export function stripSecret(state, p) {
|
||||
const s = clone(state);
|
||||
export function stripSecret(state, p, previous, checkpoint) {
|
||||
// The authoritative move history is needed for replay, but never belongs in a
|
||||
// viewer frame. Exclude it before cloning every frame of a long log slice.
|
||||
const { history: _history, ...visible } = state;
|
||||
const s = clone(visible);
|
||||
delete s.random;
|
||||
delete s.config;
|
||||
delete s.history;
|
||||
delete s.pendingMessages;
|
||||
delete s.liveUndo;
|
||||
delete s.placementUndoSeats;
|
||||
|
|
@ -2192,9 +2198,10 @@ export function stripSecret(state, p) {
|
|||
);
|
||||
}
|
||||
// Logs contain only public moves; historical earned points are public even though totals are concealed.
|
||||
s.canUndo = Number.isInteger(p) && canUndo(state, p);
|
||||
s.canUndo = Number.isInteger(p) && canUndoFromPrevious(state, p, previous);
|
||||
s.canEditPlacement = Number.isInteger(p) && canEditPlacement(state, p);
|
||||
s.canEditProduction = Number.isInteger(p) && canEditProduction(state, p);
|
||||
s.canEditProduction =
|
||||
Number.isInteger(p) && canEditProduction(state, p, checkpoint);
|
||||
s.currentPlayers = currentPlayers(state);
|
||||
const ownTask = Number.isInteger(p) ? playerTask(state, p) : null;
|
||||
if (ownTask && ownTask.p === p && ownTask !== state.tasks[0]) {
|
||||
|
|
@ -2225,6 +2232,21 @@ export function replay(s, options = {}) {
|
|||
});
|
||||
return r;
|
||||
}
|
||||
// Only the current logSlice call supplies a checkpoint from its own prefix.
|
||||
// No cached raw state is stored in game data or exposed to the viewer.
|
||||
function replayFromCheckpoint(s, to, checkpoint) {
|
||||
if (!checkpoint || checkpoint.history.length > to - 1) return replay(s, to);
|
||||
let r = checkpoint;
|
||||
for (let i = checkpoint.history.length; i < to - 1; i++)
|
||||
r = applyMove(r, s.history[i].move, s.history[i].p, true);
|
||||
if (r === checkpoint) r = clone(r);
|
||||
r.players.forEach((p, i) => {
|
||||
p.name = s.players[i].name;
|
||||
p.bot = s.players[i].bot;
|
||||
if (s.players[i].settings) p.settings = clone(s.players[i].settings);
|
||||
});
|
||||
return r;
|
||||
}
|
||||
export function logSlice(
|
||||
s,
|
||||
{ player, start = 0, end = logLength(s) - 1 } = {},
|
||||
|
|
@ -2232,11 +2254,27 @@ export function logSlice(
|
|||
assert(integer(start) && integer(end), "Invalid replay range");
|
||||
const frames = [];
|
||||
if (start > Math.min(end, logLength(s) - 1)) return { frames };
|
||||
// The common move response and last-position seek need no reconstruction.
|
||||
if (start === logLength(s) - 1) {
|
||||
frames.push(stripSecret(s, player));
|
||||
return { frames };
|
||||
}
|
||||
let position = replay(s, Math.max(1, start + 1));
|
||||
let previous;
|
||||
let checkpoint;
|
||||
for (let i = Math.max(0, start); i <= Math.min(end, logLength(s) - 1); i++) {
|
||||
frames.push(stripSecret(position, player));
|
||||
// Keep just the start of this role to accelerate production-edit checks.
|
||||
// The checkpoint stays inside this call; branches use the ordinary replay.
|
||||
if (position.history.length === position.roleHistoryStart + 1)
|
||||
checkpoint = position;
|
||||
// A preceding frame is already available after the first step. Reuse it
|
||||
// when computing undo availability rather than replaying from move zero.
|
||||
frames.push(stripSecret(position, player, previous, checkpoint));
|
||||
const h = s.history[i];
|
||||
if (h && i < end) position = applyMove(position, h.move, h.p, true);
|
||||
if (h && i < end) {
|
||||
previous = position;
|
||||
position = applyMove(position, h.move, h.p, true);
|
||||
}
|
||||
}
|
||||
return { frames };
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "@boardgamers/puerto-rico",
|
||||
"version": "0.3.13",
|
||||
"version": "0.3.14",
|
||||
"private": true,
|
||||
"type": "module",
|
||||
"description": "Puerto Rico 1897 Special Edition for Boardgamers Space",
|
||||
|
|
|
|||
|
|
@ -27,6 +27,10 @@ test("early production can be edited without retracting another producer's choic
|
|||
s = produce(s, 0);
|
||||
assert.equal(E.canUndo(s, 1), false);
|
||||
assert.equal(E.canEditProduction(s, 1), true);
|
||||
const frames = E.logSlice(s, { player: 1 }).frames;
|
||||
assert.equal(frames.at(-1).canEditProduction, true);
|
||||
for (let i = 0; i < frames.length; i++)
|
||||
assert.deepEqual(frames[i], E.stripSecret(E.replay(s, { to: i + 1 }), 1));
|
||||
const edited = E.move(s, { type: "editProduction" }, 1);
|
||||
assert.deepEqual(edited.players[0], s.players[0]);
|
||||
assert.deepEqual(edited.players[1], start.players[1]);
|
||||
|
|
|
|||
|
|
@ -117,6 +117,22 @@ test("long pass sequences keep the journal bounded without losing deterministic
|
|||
assert.equal(s.events.length, E.JOURNAL_LIMIT);
|
||||
assert.deepEqual(E.replay(s), s);
|
||||
assert.deepEqual(E.replay(s, { to: 1 }), initial);
|
||||
const before = structuredClone(s);
|
||||
// Long BGS log requests must retain all historical frames even though the
|
||||
// visible journal is capped. Compare the reused predecessor against replay.
|
||||
const frames = E.logSlice(s, { player: 0 }).frames;
|
||||
assert.equal(frames.length, 451);
|
||||
for (const i of [0, 1, 50, 199, 200, 300, 400, 449, 450])
|
||||
assert.deepEqual(
|
||||
frames[i],
|
||||
E.stripSecret(E.replay(s, { to: i + 1 }), 0),
|
||||
`frame ${i}`,
|
||||
);
|
||||
assert.deepEqual(E.logSlice(s, { player: 0, start: 450, end: 450 }).frames, [
|
||||
frames[450],
|
||||
]);
|
||||
assert.deepEqual(E.logSlice(s, { start: 450 }).frames, [E.stripSecret(s)]);
|
||||
assert.deepEqual(s, before);
|
||||
});
|
||||
|
||||
test("untrusted oversized moves and invalid seats cannot be retained in the game log", () => {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue