Fix per-player timer inflating from client clock skew (#154)
The per-player game clock could show one player using nearly the whole game's elapsed time. Root cause: `move.time` was the acting client's Date.now(), and `updateClocks` banks a stretch by subtracting one player's stamp from another's (`ts - clockStartedAt`, where the two come from different players' moves). Browsers disagree on the wall clock, so a player whose clock ran fast charged that skew to themselves on every turn; over a game's many turns it accumulated toward the total elapsed time (observed in a real game: one player at ~46s of skew reached ~86% of the game's wall-clock duration, while the player who followed them was under-charged, clamped to 0 by the existing Math.max guard). Drive the clocks from a single clock instead. `wrapper.move` — the authoritative move processor, never reached during a deterministic replay — now stamps each turn with the server clock as `serverTime`, and `updateClocks` prefers it over the client `time`. AI turns are already server-stamped, so human turns now match. The client `time` is kept untouched: it is the move's identity for reconciling the turn buffer against the server's echo (viewer turn-buffer helpers), and it still drives the local preview clock on the acting player's own board. Determinism is preserved: the server stamp is written into the log and reused on replay. Added a wrapper test that plays with absurd client stamps and asserts the timers track the server clock, plus a replay check; pinned Date.now across the wrapper suite so buffers replay to byte-identical states. Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
parent
1dcbcfb99c
commit
93d1490a4f
5 changed files with 93 additions and 36 deletions
|
|
@ -1755,7 +1755,7 @@ export function move(G: GameState, move: Move, playerNumber: number): GameState
|
|||
G.cardsLeft = G.powerPlantsDeck.length;
|
||||
G.nextCardWeak = G.options.variant == 'recharged' && G.cardsLeft > 0 && G.powerPlantsDeck[0].number <= 15;
|
||||
|
||||
updateClocks(G, move.time);
|
||||
updateClocks(G, move.serverTime ?? move.time);
|
||||
|
||||
G.currentPlayers.forEach((p) => (G.players[p].availableMoves = availableMoves(G, G.players[p])));
|
||||
|
||||
|
|
@ -1798,9 +1798,13 @@ export function move(G: GameState, move: Move, playerNumber: number): GameState
|
|||
// During simultaneous phases every current player's clock runs independently, so
|
||||
// the sum of players' time can legitimately exceed the wall-clock time elapsed.
|
||||
//
|
||||
// `ts` comes from the move (client-supplied) rather than the system clock, so
|
||||
// replaying a log reproduces identical times. Moves without a timestamp (AI,
|
||||
// dropped players) leave every clock untouched.
|
||||
// `ts` comes from the move (its `serverTime`, else the client `time`) rather than the
|
||||
// system clock, so replaying a log reproduces identical times. It must be a stamp from
|
||||
// a single clock shared by every player: banking a stretch below subtracts one player's
|
||||
// stamp from another's, so mixing skewed per-client clocks would charge the skew to
|
||||
// whoever's clock runs fast. `wrapper.move` supplies `serverTime` for human turns and
|
||||
// `moveAI` stamps the server clock into `time`, so both resolve to the server clock.
|
||||
// Moves without any timestamp (dropped players) leave every clock untouched.
|
||||
function updateClocks(G: GameState, ts?: number) {
|
||||
if (ts == undefined) {
|
||||
return;
|
||||
|
|
|
|||
|
|
@ -72,14 +72,24 @@ export declare namespace Moves {
|
|||
}
|
||||
}
|
||||
|
||||
// Metadata the client may attach to any move. `time` is the wall-clock timestamp
|
||||
// (ms since epoch) at which the move was submitted; it drives the per-player
|
||||
// clocks. It is stored in the log, so replaying a game reproduces the exact same
|
||||
// times — the engine must never read the clock itself or replays would diverge.
|
||||
// Engine-generated moves (AI, dropped players) omit it and simply don't tick the
|
||||
// clocks.
|
||||
// Metadata attached to a move. Both stamps are ms-since-epoch and are stored in the
|
||||
// log, so replaying a game reproduces the exact same times — the engine must never
|
||||
// read the clock itself or replays would diverge. Engine-generated moves (AI, dropped
|
||||
// players) omit them and simply don't tick the clocks.
|
||||
//
|
||||
// `time` is the ACTING CLIENT's wall clock when the move entered the turn buffer. It is
|
||||
// the move's identity for reconciling the buffer against the server's echo (see the
|
||||
// viewer's turn-buffer helpers), so it must survive untouched.
|
||||
//
|
||||
// `serverTime` is stamped once by the authoritative move processor (`wrapper.move`) from
|
||||
// the SERVER clock. The per-player clocks are driven by it in preference to `time`:
|
||||
// banking a clock stretch subtracts one player's stamp from another's, and browser
|
||||
// clocks disagree (skew of a minute has been seen), so a client whose clock ran fast
|
||||
// would otherwise charge that skew to itself on every turn — inflating its timer toward
|
||||
// the whole game's elapsed time. A single server clock removes the skew.
|
||||
export interface MoveMeta {
|
||||
time?: number;
|
||||
serverTime?: number;
|
||||
}
|
||||
|
||||
export type Move = (
|
||||
|
|
|
|||
|
|
@ -11,6 +11,20 @@ const pass: Move = { name: MoveName.Pass, data: true };
|
|||
const json = (value: unknown) => JSON.parse(JSON.stringify(value));
|
||||
|
||||
describe('wrapper (tentative turns)', () => {
|
||||
// `wrapper.move` stamps each turn with the server clock (Date.now) so the per-player
|
||||
// clocks run off one clock instead of the acting client's skewed one. Pin it here so
|
||||
// a buffer replayed twice yields byte-identical states; a test that cares about elapsed
|
||||
// time advances the clock by mutating `now`. Restored after each test.
|
||||
const realNow = Date.now;
|
||||
let now = 1_000_000;
|
||||
beforeEach(() => {
|
||||
now = 1_000_000;
|
||||
Date.now = () => now;
|
||||
});
|
||||
afterEach(() => {
|
||||
Date.now = realNow;
|
||||
});
|
||||
|
||||
/**
|
||||
* Simulates the platform: `saved` only ever advances when `toSave` returns the
|
||||
* state (committed); tentative states are discarded, like the game server does.
|
||||
|
|
@ -368,36 +382,50 @@ describe('wrapper (tentative turns)', () => {
|
|||
expect(platform.saved.players[B].powerPlants, 'the higher bidder wins').to.have.length(1);
|
||||
});
|
||||
|
||||
it('should tick the clocks deterministically across resends of the same buffer', async () => {
|
||||
it('should drive the per-player clocks from the server clock, immune to client skew', async () => {
|
||||
// The absurd client `time`s below stand in for skewed browser clocks — banking a
|
||||
// stretch subtracts one player's stamp from another's, so before turns were
|
||||
// server-stamped a fast client charged its skew to itself every turn, creeping
|
||||
// toward the whole game's elapsed time. None of these client stamps may reach a
|
||||
// timer (the shared `now`, pinned at 1_000_000, is the only clock that counts).
|
||||
const platform = new Platform(2, 'wrapper-test-clocks');
|
||||
const A = platform.saved.currentPlayers[0];
|
||||
const B = 1 - A;
|
||||
|
||||
const t1 = 1_000_000;
|
||||
const t2 = 1_000_000 + 30_000;
|
||||
const skew = (m: Move, t: number): Move => ({ ...m, time: t });
|
||||
|
||||
const choose: Move = { ...cheapestChoosable(platform, A), time: t1 };
|
||||
|
||||
// The tentative call ticks the clocks of its (discarded) result the same way
|
||||
// the final replay will: the stamp travels with the move, not the wall clock.
|
||||
const mid = await platform.send([choose], A);
|
||||
expect(mid.result.players[A].clockStartedAt).to.equal(t1);
|
||||
|
||||
const bid: Move = { ...openingBid(choose), time: t2 };
|
||||
const full = await platform.send([choose, bid], A);
|
||||
expect(full.saved).to.be.true;
|
||||
|
||||
// A's turn banked exactly t2 - t1 (the clock started on their first stamped
|
||||
// move and stopped when the bid handed control to B).
|
||||
expect(platform.saved.players[A].totalTimeUsed).to.equal(t2 - t1);
|
||||
// A opens the auction and commits at server time 1_000_000. Whatever nonsense
|
||||
// client stamps the moves carry, control passing to B starts B's clock on the
|
||||
// SERVER clock, and A's (single-buffer) stretch is banked and cleared.
|
||||
const choose = cheapestChoosable(platform, A);
|
||||
await platform.send([skew(choose, 9_000_000_000), skew(openingBid(choose), 3)], A);
|
||||
expect(platform.saved.players[B].clockStartedAt).to.equal(1_000_000);
|
||||
expect(platform.saved.players[A].clockStartedAt).to.be.undefined;
|
||||
expect(platform.saved.players[B].clockStartedAt).to.equal(t2);
|
||||
|
||||
// Replaying the identical buffer from the same base reproduces identical clocks:
|
||||
// the tentative detour above changed nothing about the committed outcome.
|
||||
const again = await wrapper.move(setup(2, {}, 'wrapper-test-clocks'), [choose, bid], A);
|
||||
expect(json(again)).to.deep.equal(json(full.result));
|
||||
expect(again.players[A].totalTimeUsed).to.equal(t2 - t1);
|
||||
// Finish the auction (still at 1_000_000, so these stretches are ~0) to reach
|
||||
// the sequential Resources phase.
|
||||
await platform.send([skew(pass, 1)], B);
|
||||
await platform.send([skew(cheapestChoosable(platform, B), 2)], B);
|
||||
expect(platform.saved.phase).to.equal(Phase.Resources);
|
||||
|
||||
// The first Resources player is on the clock from the phase start (1_000_000).
|
||||
const R = platform.saved.currentPlayers[0];
|
||||
expect(platform.saved.players[R].clockStartedAt).to.equal(1_000_000);
|
||||
|
||||
// 25 minutes of SERVER time pass; the client stamp is nonsense. The stretch
|
||||
// banked is the server span, not anything derived from the client stamp.
|
||||
now = 1_000_000 + 25 * 60_000;
|
||||
await platform.send([skew(pass, 42)], R);
|
||||
expect(platform.saved.players[R].totalTimeUsed).to.equal(25 * 60_000);
|
||||
expect(platform.saved.players[R].clockStartedAt).to.be.undefined;
|
||||
|
||||
// Replay reads the server stamps stored in the log, never the wall clock, so
|
||||
// it reproduces the identical clocks even though Date.now has moved on.
|
||||
now = 987_654_321;
|
||||
const replayed = wrapper.replay(cloneDeep(platform.saved));
|
||||
expect(replayed.players.map((p) => p.totalTimeUsed)).to.deep.equal(
|
||||
platform.saved.players.map((p) => p.totalTimeUsed)
|
||||
);
|
||||
});
|
||||
|
||||
it('should let two simultaneous Bureaucracy players commit independently (engine-level rebase)', async () => {
|
||||
|
|
|
|||
|
|
@ -46,8 +46,20 @@ export async function move(G: GameState, move: Move | Move[] | null | undefined,
|
|||
return { ...G, newTurn: false };
|
||||
}
|
||||
|
||||
// Drive the per-player clocks from a single clock. Each move's `time` is the acting
|
||||
// client's own Date.now(), and browser clocks disagree — a player whose clock runs
|
||||
// fast would charge that skew to itself on every turn, inflating its timer toward the
|
||||
// whole game's elapsed time. This is the authoritative move processor and never runs
|
||||
// during a deterministic replay (`replay` calls `engine.move` directly), so stamping
|
||||
// the server clock here is safe: the stamp is written into the log and reused on
|
||||
// replay. The client `time` is preserved untouched so it can still reconcile the
|
||||
// turn buffer against this state's echo. One `now` for the whole buffer is enough —
|
||||
// a turn's atomic moves are one player's, so their internal gaps don't matter; the
|
||||
// stretch charged is from the previous turn's stamp to this one.
|
||||
const now = Date.now();
|
||||
|
||||
for (let i = 0; i < moves.length; i++) {
|
||||
G = engine.move(G, moves[i], player);
|
||||
G = engine.move(G, { ...moves[i], serverTime: now }, player);
|
||||
|
||||
// The buffer must describe at most ONE turn: committing is what grants the
|
||||
// mover their per-turn time increment. Without this guard a buffer like
|
||||
|
|
|
|||
|
|
@ -1622,10 +1622,13 @@ export default class Game extends Vue {
|
|||
: undefined;
|
||||
const theServerOfferedThisMove = !!offeredWhenClicked && !!offeredWhenClicked[move.name];
|
||||
|
||||
// Stamp the move ONCE, when it enters the turn buffer, so the engine can
|
||||
// advance the per-player clocks. The engine never reads the system clock
|
||||
// itself — the stamp travels with the move on every resend of the buffer, so
|
||||
// replays (and the eventual committed log) reproduce the same times.
|
||||
// Stamp the move ONCE, when it enters the turn buffer. This client stamp is the
|
||||
// move's identity for reconciling the buffer against the server's echo, and it
|
||||
// drives the clocks in the LOCAL preview below. The engine never reads the system
|
||||
// clock itself, so the stamp travels with the move on every resend of the buffer.
|
||||
// For the authoritative per-player clocks the server re-stamps a single server
|
||||
// clock (`serverTime`) in `wrapper.move`: browser clocks are skewed, and a stamp
|
||||
// shared across players is what keeps a fast clock from inflating its own timer.
|
||||
const stamped = { ...move, time: Date.now() };
|
||||
this.turnMoves.push(stamped);
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue