feat: v0.9.1 — left-pane time controls refined: log-scale slider (t→round(60^t), 250 ms debounced commit with release flush + in-flight queue) replaces the scale select; game clock now shows seconds (HH:MM:SS UTC)

This commit is contained in:
2026-09-17 16:13:28 +00:00
parent 9502cd1975
commit 62f9929b51
9 changed files with 232 additions and 51 deletions
+4 -3
View File
@@ -22,8 +22,9 @@ describe('formatFuel', () => {
});
describe('formatSimClock', () => {
it('formats epoch ms as a UTC wall-clock string (deterministic across timezones)', () => {
expect(formatSimClock(Date.UTC(2026, 8, 17, 14, 5))).toBe('2026-09-17 14:05 UTC');
expect(formatSimClock(Date.UTC(2026, 0, 1, 0, 0))).toBe('2026-01-01 00:00 UTC');
it('formats epoch ms as a UTC wall-clock string with seconds (deterministic across timezones)', () => {
expect(formatSimClock(Date.UTC(2026, 8, 17, 14, 5))).toBe('2026-09-17 14:05:00 UTC');
expect(formatSimClock(Date.UTC(2026, 0, 1, 0, 0))).toBe('2026-01-01 00:00:00 UTC');
expect(formatSimClock(Date.UTC(2026, 8, 17, 14, 5, 9))).toBe('2026-09-17 14:05:09 UTC');
});
});
+3 -3
View File
@@ -11,10 +11,10 @@ export function formatFuel(fuel: number, maxFuel: number): string {
return `${Math.round(fuel)} / ${maxFuel} L`;
}
// The sim clock is epoch ms (seeded from real time) — shown as a UTC wall-clock so the
// display is deterministic regardless of the viewer's timezone.
// The sim clock is epoch ms (seeded from real time) — shown as a UTC wall-clock with
// seconds so the display is deterministic regardless of the viewer's timezone.
export function formatSimClock(clockMs: number): string {
const d = new Date(clockMs);
const pad = (n: number) => String(n).padStart(2, '0');
return `${d.getUTCFullYear()}-${pad(d.getUTCMonth() + 1)}-${pad(d.getUTCDate())} ${pad(d.getUTCHours())}:${pad(d.getUTCMinutes())} UTC`;
return `${d.getUTCFullYear()}-${pad(d.getUTCMonth() + 1)}-${pad(d.getUTCDate())} ${pad(d.getUTCHours())}:${pad(d.getUTCMinutes())}:${pad(d.getUTCSeconds())} UTC`;
}
+65
View File
@@ -0,0 +1,65 @@
import { describe, expect, it } from 'vitest';
import { MAX_SCALE, MIN_SCALE, scaleToSlider, sliderToScale } from './timeScale';
describe('sliderToScale', () => {
it('is exact at the endpoints (0 → 1×, 1 → 60×)', () => {
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('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);
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('clamps out-of-range and non-finite input', () => {
expect(sliderToScale(-0.5)).toBe(MIN_SCALE);
expect(sliderToScale(1.5)).toBe(MAX_SCALE);
expect(sliderToScale(NaN)).toBe(MIN_SCALE);
});
});
describe('scaleToSlider', () => {
it('is exact at the endpoints (1× → 0, 60× → 1)', () => {
expect(scaleToSlider(1)).toBe(0);
expect(scaleToSlider(60)).toBe(1);
});
it('stays within [0, 1] for in-range scales', () => {
for (let s = MIN_SCALE; s <= MAX_SCALE; s++) {
const t = scaleToSlider(s);
expect(t).toBeGreaterThanOrEqual(0);
expect(t).toBeLessThanOrEqual(1);
}
});
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(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++) {
expect(sliderToScale(scaleToSlider(s))).toBe(s);
}
});
});
+22
View File
@@ -0,0 +1,22 @@
// 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).
export const MIN_SCALE = 1;
export const MAX_SCALE = 60;
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.
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));
}
// scale → t (inverse of the pre-rounding mapping). Exact at the endpoints:
// scaleToSlider(1) === 0, scaleToSlider(60) === 1. Round-trips every integer 1..60.
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);
}