Files
OpenFrontIO/packages/client/src/LocalPersistantStats.ts
T
Evan Pelle 43d07ca85f refactor: rename core-public -> engine-public; fold helpers in; drop shared
Tighten the package model around the determinism invariant: everything the
engine imports must itself be deterministic.

- Rename core-public -> engine-public (package, dir, `engine-public/*` alias,
  all 118 import sites). It is the deterministic public surface the engine
  depends on and that client/server also use.
- Move the pure formatting helpers (renderNumber/renderTroops) from shared
  into engine-public/format — the engine uses them and they're deterministic.
- Drop the now-empty shared package (and its alias). `shared` was intended for
  client/server-shared, engine-forbidden code; there is none yet, so it is
  removed and can be re-added when such code appears.

Final graph: engine-public (leaf) <- engine <- {client, server};
client/server also -> engine-public.

Verified: tsc clean; 1364 + 65 tests pass; prod build succeeds; engine-public
is a leaf; engine has no client/server imports. Lockfile regenerated.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-10 20:32:32 +00:00

60 lines
1.3 KiB
TypeScript

import { GameConfig, GameID, PartialGameRecord } from "engine-public/Schemas";
import { replacer } from "engine/Util";
export interface LocalStatsData {
[key: GameID]: {
lobby: Partial<GameConfig>;
// Only once the game is over
gameRecord?: PartialGameRecord;
};
}
let _startTime: number;
function getStats(): LocalStatsData {
const statsStr = localStorage.getItem("game-records");
return statsStr ? JSON.parse(statsStr) : {};
}
function save(stats: LocalStatsData) {
// To execute asynchronously
setTimeout(
() => localStorage.setItem("game-records", JSON.stringify(stats, replacer)),
0,
);
}
// The user can quit the game anytime so better save the lobby as soon as the
// game starts.
export function startGame(id: GameID, lobby: Partial<GameConfig>) {
if (localStorage === undefined) {
return;
}
_startTime = Date.now();
const stats = getStats();
stats[id] = { lobby };
save(stats);
}
export function startTime() {
return _startTime;
}
export function endGame(gameRecord: PartialGameRecord) {
if (localStorage === undefined) {
return;
}
const stats = getStats();
const gameStat = stats[gameRecord.info.gameID];
if (!gameStat) {
console.log("LocalPersistantStats: game not found");
return;
}
gameStat.gameRecord = gameRecord;
save(stats);
}