engine: replay hidden-log moves in wrapper.replay
A state saved mid-bid-phase keeps its newest moves (bids, bid-phase loans) in G.hiddenLog — they are only flushed to G.log when the accept/decline phase starts. replay() only walked G.log, so an admin full-replay of such a save silently lost the bids and diverged. Whenever hiddenLog is non-empty it holds exactly the moves made after the last visible log entry, in order (the visible log freezes during the bid phase), so replaying [...log, ...hiddenLog] reconstructs the state faithfully. logSlice's truncated-log replay now explicitly drops the hidden log, since it reconstructs a historical state predating it.
This commit is contained in:
parent
7c94bc75b7
commit
501a33103c
2 changed files with 29 additions and 2 deletions
|
|
@ -408,6 +408,24 @@ describe('wrapper (tentative turns)', () => {
|
|||
expect(platform.saved.currentPlayers).to.deep.equal([B]);
|
||||
});
|
||||
|
||||
it('should replay a state saved mid-bid-phase without dropping hidden log moves', async () => {
|
||||
const platform = new Platform(3, 'wrapper-test-replay');
|
||||
const { B, C } = await playToBidPhase(platform);
|
||||
|
||||
// One bid placed, plus a bid-phase loan: both live in the hidden log of the
|
||||
// saved state (they are only flushed once the accept/decline phase starts)
|
||||
await platform.send([{ name: MoveName.Bid, data: true, extraData: { price: 3 } }], B);
|
||||
await platform.send([getLoan], C);
|
||||
expect(platform.saved.hiddenLog).to.have.length(2);
|
||||
expect(platform.saved.phase).to.equal(Phase.Bid);
|
||||
|
||||
// An admin full-replay of that save must reproduce it exactly, hidden bids included
|
||||
const replayed = wrapper.replay(cloneDeep(platform.saved));
|
||||
expect(JSON.parse(JSON.stringify(replayed))).to.deep.equal(JSON.parse(JSON.stringify(platform.saved)));
|
||||
expect(replayed.players[B].bid).to.equal(3);
|
||||
expect(replayed.players[C].loans).to.have.length(1);
|
||||
});
|
||||
|
||||
it('should always produce committed states from moveAI', async () => {
|
||||
let G = setup(2, {}, 'wrapper-test-ai');
|
||||
G.players.forEach((player, i) => {
|
||||
|
|
|
|||
|
|
@ -122,7 +122,13 @@ export function replay(G: GameState) {
|
|||
G.players[i].name = oldPlayers[i].name;
|
||||
}
|
||||
|
||||
for (const move of oldG.log.filter((event) => event.type === 'move')) {
|
||||
// The visible log freezes during the bid phase: bids and bid-phase loans go to
|
||||
// `G.hiddenLog` and are only flushed to `G.log` when the accept/decline phase
|
||||
// starts. A state saved mid-bid therefore keeps its newest moves in `hiddenLog`,
|
||||
// and whenever `hiddenLog` is non-empty it holds exactly the moves made after the
|
||||
// last visible entry, in order — so replaying the visible log followed by the
|
||||
// hidden log reproduces the state faithfully.
|
||||
for (const move of [...oldG.log, ...(oldG.hiddenLog ?? [])].filter((event) => event.type === 'move')) {
|
||||
asserts<LogMove>(move);
|
||||
|
||||
G = engine.move(G, move.move, move.player);
|
||||
|
|
@ -174,7 +180,10 @@ export function logSlice(G: GameState, options?: { player?: number; start?: numb
|
|||
options?.end === undefined
|
||||
? stripped.players.map((pl) => pl.availableMoves)
|
||||
: engine
|
||||
.stripSecret(replay({ ...G, log: G.log.slice(0, options!.end) }), options!.player)
|
||||
// Replaying a *truncated* log reconstructs a historical state, so
|
||||
// the hidden log (whose moves all come after the full visible log)
|
||||
// must not be appended to it.
|
||||
.stripSecret(replay({ ...G, log: G.log.slice(0, options!.end), hiddenLog: [] }), options!.player)
|
||||
.players.map((pl) => pl.availableMoves),
|
||||
};
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue