mirror of
https://github.com/openfrontio/OpenFrontIO.git
synced 2026-07-01 22:43:27 +00:00
0a40bcebf0
## Description: Enable the `sort-imports` eslint rule. Fixes #1784 ## Please complete the following: - [x] I have added screenshots for all UI updates - [x] I process any text displayed to the user through translateText() and I've added it to the en.json file - [x] I have added relevant tests to the test directory - [x] I confirm I have thoroughly tested these changes and take full responsibility for any bugs introduced
75 lines
1.5 KiB
TypeScript
75 lines
1.5 KiB
TypeScript
import {
|
|
GameConfig,
|
|
GameConfigSchema,
|
|
GameID,
|
|
GameRecord,
|
|
GameRecordSchema,
|
|
ID,
|
|
} from "../core/Schemas";
|
|
import { replacer } from "../core/Util";
|
|
import { z } from "zod";
|
|
|
|
const LocalStatsDataSchema = z.record(
|
|
ID,
|
|
z.object({
|
|
lobby: GameConfigSchema.partial(),
|
|
// Only once the game is over
|
|
gameRecord: GameRecordSchema.optional(),
|
|
}),
|
|
);
|
|
type LocalStatsData = z.infer<typeof LocalStatsDataSchema>;
|
|
|
|
let _startTime: number | undefined;
|
|
|
|
function getStats(): LocalStatsData {
|
|
try {
|
|
return LocalStatsDataSchema.parse(
|
|
JSON.parse(localStorage.getItem("game-records") ?? "{}"),
|
|
);
|
|
} catch (e) {
|
|
return {};
|
|
}
|
|
}
|
|
|
|
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: GameRecord) {
|
|
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);
|
|
}
|