feat: v0.9.2 — power-of-two time scale: slider steps through the 1/2/4/8/16/32/64 ladder, POST /api/sim clamp widened to [1,64]

This commit is contained in:
2026-09-17 19:15:15 +00:00
parent 62f9929b51
commit 03653fc50e
12 changed files with 80 additions and 51 deletions
+1 -1
View File
@@ -122,7 +122,7 @@ describe('OpenAPI spec', () => {
expect(Object.keys(controls).sort()).toEqual(['paused', 'timeScale']);
expect(controls.timeScale.type).toBe('number');
expect(controls.timeScale.minimum).toBe(1);
expect(controls.timeScale.maximum).toBe(60);
expect(controls.timeScale.maximum).toBe(64);
expect(controls.paused.type).toBe('boolean');
const error = openApiSpec.components.schemas.Error.properties;
+3 -3
View File
@@ -336,7 +336,7 @@ export const openApiSpec = {
type: 'object',
properties: {
clockMs: { type: 'integer', description: 'Sim-clock epoch ms (game time)' },
timeScale: { type: 'number', minimum: 1, maximum: 60 },
timeScale: { type: 'number', minimum: 1, maximum: 64 },
paused: { type: 'boolean' },
},
required: ['clockMs', 'timeScale', 'paused'],
@@ -348,8 +348,8 @@ export const openApiSpec = {
timeScale: {
type: 'number',
minimum: 1,
maximum: 60,
description: 'Game-time multiplier; values outside [1, 60] are clamped.',
maximum: 64,
description: 'Game-time multiplier; values outside [1, 64] are clamped. The UI offers the power-of-two ladder 1/2/4/8/16/32/64.',
},
paused: { type: 'boolean' },
},
+3 -3
View File
@@ -22,12 +22,12 @@ import { arrivalTimeFor, fuelAt } from './movement';
// Clamp constant: max real-time gap counted by a single tick. Chosen at 2000 ms —
// comfortably above a normal client poll interval (~1 s), so active play advances
// ~real time × scale; small enough that one stale read after an absence moves game
// time by at most 2 s × scale (≤ 2 min even at max scale 60). The sim therefore
// time by at most 2 s × scale (≈ 2 min even at max scale 64). The sim therefore
// only meaningfully progresses while actively polled.
export const MAX_TICK_ELAPSED_MS = 2_000;
export const MIN_TIME_SCALE = 1;
export const MAX_TIME_SCALE = 60;
export const MAX_TIME_SCALE = 64; // top of the power-of-two ladder (v0.9.2)
// Reputation gain per completed contract: BASE + PER_KM × great-circle km, rounded
// to 2 decimals (reputation is REAL). A short hop (~300 km) earns ~1.15 rep, a
@@ -252,7 +252,7 @@ export function runAction<T>(fn: (inst: GameDb) => Promise<T>): Promise<T> {
}
// Tick first so the returned state reflects current time, then apply the new
// controls. timeScale is clamped to [1, 60]; validation of raw input happens in
// controls. timeScale is clamped to [1, 64]; validation of raw input happens in
// the route handler (400 on non-number / non-boolean).
export function applyTimeControls(input: SimControlsInput): Promise<schema.SimStateRow> {
return withLock(async () => {
+30 -15
View File
@@ -1,30 +1,34 @@
import { describe, expect, it } from 'vitest';
import { MAX_SCALE, MIN_SCALE, scaleToSlider, sliderToScale } from './timeScale';
import { MAX_SCALE, MIN_SCALE, TIME_SCALES, scaleToSlider, sliderToScale } from './timeScale';
const STEPS = TIME_SCALES.length - 1; // 6 intervals across the 7 ladder positions
describe('sliderToScale', () => {
it('is exact at the endpoints (0 → 1×, 1 → 60×)', () => {
it('is exact at the endpoints (0 → 1×, 1 → 64×)', () => {
expect(sliderToScale(0)).toBe(MIN_SCALE);
expect(sliderToScale(1)).toBe(MAX_SCALE);
});
it('maps mid-slider to ~8× (√60 rounds to 8)', () => {
expect(sliderToScale(0.5)).toBe(8);
it('maps each of the 7 positions to the power-of-two ladder', () => {
const expected = [1, 2, 4, 8, 16, 32, 64];
for (let i = 0; i < TIME_SCALES.length; i++) {
expect(sliderToScale(i / STEPS)).toBe(expected[i]);
}
});
it('is monotonic non-decreasing across the whole range', () => {
let prev = sliderToScale(0);
for (let i = 1; i <= 2000; i++) {
const t = i / 2000;
const s = sliderToScale(t);
const s = sliderToScale(i / 2000);
expect(s).toBeGreaterThanOrEqual(prev);
prev = s;
}
});
it('covers every integer scale 1..60 somewhere on the slider', () => {
const seen = new Set<number>();
for (let i = 0; i <= 2000; i++) seen.add(sliderToScale(i / 2000));
for (let s = MIN_SCALE; s <= MAX_SCALE; s++) expect(seen.has(s)).toBe(true);
it('only ever returns a ladder value', () => {
for (let i = 0; i <= 2000; i++) {
expect(TIME_SCALES).toContain(sliderToScale(i / 2000));
}
});
it('clamps out-of-range and non-finite input', () => {
@@ -35,9 +39,15 @@ describe('sliderToScale', () => {
});
describe('scaleToSlider', () => {
it('is exact at the endpoints (1× → 0, 60× → 1)', () => {
it('is exact at the endpoints (1× → 0, 64× → 1)', () => {
expect(scaleToSlider(1)).toBe(0);
expect(scaleToSlider(60)).toBe(1);
expect(scaleToSlider(64)).toBe(1);
});
it('places each ladder value on its own position', () => {
for (let i = 0; i < TIME_SCALES.length; i++) {
expect(scaleToSlider(TIME_SCALES[i])).toBeCloseTo(i / STEPS, 12);
}
});
it('stays within [0, 1] for in-range scales', () => {
@@ -51,15 +61,20 @@ describe('scaleToSlider', () => {
it('clamps out-of-range and non-finite input', () => {
expect(scaleToSlider(0)).toBe(0);
expect(scaleToSlider(-3)).toBe(0);
expect(scaleToSlider(61)).toBe(1);
expect(scaleToSlider(65)).toBe(1);
expect(scaleToSlider(NaN)).toBe(0);
});
});
describe('round-trip', () => {
it('sliderToScale(scaleToSlider(s)) === s for every integer scale 1..60', () => {
for (let s = MIN_SCALE; s <= MAX_SCALE; s++) {
it('sliderToScale(scaleToSlider(s)) === s for every ladder value', () => {
for (const s of TIME_SCALES) {
expect(sliderToScale(scaleToSlider(s))).toBe(s);
}
});
it('maps off-ladder scales to the nearest power of two', () => {
expect(sliderToScale(scaleToSlider(3))).toBe(4); // log2(3) ≈ 1.58 → step 2 (4×)
expect(sliderToScale(scaleToSlider(60))).toBe(64); // legacy max from the v0.9.x era
});
});
+13 -11
View File
@@ -1,22 +1,24 @@
// Pure log-scale mapping helpers for the time-scale slider (v0.9.1) — no game logic,
// unit-testable in node. Slider position t ∈ [0,1] ↔ scale = round(60^t), so equal
// distances on the slider correspond to equal multiplicative steps: 1× at t=0, ~8×
// mid-slider, 60× at t=1 (matching POST /api/sim's clamp range).
// Pure mapping helpers for the time-scale slider (v0.9.2) — no game logic, unit-testable
// in node. The slider has 7 discrete positions on a power-of-two ladder:
// 1× → 2× → 4× → 8× → 16× → 32× → 64×, so each step doubles the speed (matching
// POST /api/sim's clamp range [1, 64]).
export const MIN_SCALE = 1;
export const MAX_SCALE = 60;
export const TIME_SCALES = [1, 2, 4, 8, 16, 32, 64] as const;
export const MIN_SCALE: number = TIME_SCALES[0];
export const MAX_SCALE: number = TIME_SCALES[TIME_SCALES.length - 1];
const clamp = (v: number, lo: number, hi: number) => Math.min(hi, Math.max(lo, v));
// t → scale. Exact at the endpoints: sliderToScale(0) === 1, sliderToScale(1) === 60.
// t → scale: the position maps to the nearest ladder step. Exact at the endpoints:
// sliderToScale(0) === 1, sliderToScale(1) === 64.
export function sliderToScale(t: number): number {
const tt = clamp(Number.isFinite(t) ? t : 0, 0, 1);
return Math.round(clamp(Math.pow(MAX_SCALE, tt), MIN_SCALE, MAX_SCALE));
return TIME_SCALES[clamp(Math.round(tt * (TIME_SCALES.length - 1)), 0, TIME_SCALES.length - 1)];
}
// scale → t (inverse of the pre-rounding mapping). Exact at the endpoints:
// scaleToSlider(1) === 0, scaleToSlider(60) === 1. Round-trips every integer 1..60.
// scale → t: the nearest ladder position by log2 distance. Exact at the endpoints:
// scaleToSlider(1) === 0, scaleToSlider(64) === 1; round-trips every value in TIME_SCALES.
export function scaleToSlider(scale: number): number {
const s = clamp(Number.isFinite(scale) ? scale : MIN_SCALE, MIN_SCALE, MAX_SCALE);
return Math.log(s) / Math.log(MAX_SCALE);
return Math.log2(s) / Math.log2(MAX_SCALE);
}