mirror of
https://github.com/openfrontio/OpenFrontIO.git
synced 2026-06-21 09:20:47 +00:00
62 lines
1.4 KiB
TypeScript
62 lines
1.4 KiB
TypeScript
import { consolex } from "../core/Consolex";
|
|
import { GameConfig, GameID, GameRecord } from "../core/Schemas";
|
|
|
|
export interface LocalStatsData {
|
|
[key: GameID]: {
|
|
lobby: GameConfig;
|
|
// Only once the game is over
|
|
gameRecord?: GameRecord;
|
|
};
|
|
}
|
|
|
|
export namespace LocalPersistantStats {
|
|
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)),
|
|
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: GameConfig) {
|
|
if (typeof 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 (typeof localStorage === "undefined") {
|
|
return;
|
|
}
|
|
|
|
const stats = getStats();
|
|
const gameStat = stats[gameRecord.id];
|
|
|
|
if (!gameStat) {
|
|
consolex.log("LocalPersistantStats: game not found");
|
|
return;
|
|
}
|
|
|
|
gameStat.gameRecord = gameRecord;
|
|
save(stats);
|
|
}
|
|
}
|