idle fleet slows world clock to real time instead of freezing (ISS-128)
CI / build + unit + e2e (pull_request) Successful in 10m32s

This commit is contained in:
2026-09-22 18:37:06 +00:00
parent 5268a39fe9
commit 0a40143497
12 changed files with 142 additions and 72 deletions
+10 -9
View File
@@ -2,15 +2,16 @@
import { useEffect, useRef, useState } from 'react';
import type { StateSnapshot } from './game/state';
import { WORLD_TIME_SCALE } from './worldClock';
import { IDLE_WORLD_TIME_SCALE, WORLD_TIME_SCALE } from './worldClock';
// ISS-007 (v0.16.1): the sim clock ticks smoothly on the CLIENT between polls instead of
// jumping once per snapshot. The server's authoritative rate is exactly real-time × scale
// (tickOnce in lib/sim/engine.ts: advance = min(elapsed, MAX_TICK_ELAPSED_MS) × timeScale), so
// client-side extrapolation mirrors the server's own model. Every new snapshot re-anchors to
// server truth, bounding any drift by network latency only. ISS-117 (v0.52.0): the scale is the
// fixed WORLD_TIME_SCALE and the clock freezes while nothing is happening — mirroring the
// server's idle-pause instead of a player-controlled pause.
// fixed WORLD_TIME_SCALE; ISS-128 (v0.56.0): while nothing is happening it slows to
// IDLE_WORLD_TIME_SCALE (real time) instead of freezing — mirroring the server's idle slowdown,
// so polls re-anchor without a visible jump.
export interface SimClockAnchor {
// Server truth at anchor time — the snapshot's simState.clockMs.
@@ -22,11 +23,11 @@ export interface SimClockAnchor {
}
// Pure core of the extrapolation — mirrors tickOnce: while advancing, game time advances by
// real-time × WORLD_TIME_SCALE; while idle it is frozen (advance 0). nowMs before `at` clamps to
// the anchor value instead of going backwards. Unit-testable in node.
// real-time × WORLD_TIME_SCALE; while idle it slows to real-time × IDLE_WORLD_TIME_SCALE (ISS-128).
// nowMs before `at` clamps to the anchor value instead of going backwards. Unit-testable in node.
export function advanceSimClock(anchor: SimClockAnchor, nowMs: number): number {
if (!anchor.advancing) return anchor.clockMs;
return anchor.clockMs + Math.max(0, nowMs - anchor.at) * WORLD_TIME_SCALE;
const scale = anchor.advancing ? WORLD_TIME_SCALE : IDLE_WORLD_TIME_SCALE;
return anchor.clockMs + Math.max(0, nowMs - anchor.at) * scale;
}
// ~10 fps display refresh — smooth for a seconds-precision wall clock without busy re-renders.
@@ -41,8 +42,8 @@ export function useSimClock(snapshot: StateSnapshot | null): number | null {
// (useGameState), so this runs once per poll cycle as well.
useEffect(() => {
if (!snapshot) return;
// Mirror of the server's shouldAdvanceTime (lib/sim/engine.ts): frozen while no plane is
// flying or in repair. One line on purpose — keep it in sync with that function.
// Mirror of the server's shouldAdvanceTime (lib/sim/engine.ts): full rate only while a plane
// is flying or in repair, idle real-time otherwise. One line on purpose — keep it in sync.
const advancing = snapshot.planes.some((p) => p.status === 'flying' || p.status === 'repairing');
anchorRef.current = { clockMs: snapshot.simState.clockMs, at: Date.now(), advancing };
setDisplayMs(advanceSimClock(anchorRef.current, Date.now()));