mirror of
https://github.com/openfrontio/OpenFrontIO.git
synced 2026-06-21 21:04:14 +00:00
fd0fbfab9e
## Description: Describe the PR. ## 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 ## Please put your Discord username so you can be contacted if a bug or regression is found: evan
60 lines
1.3 KiB
TypeScript
60 lines
1.3 KiB
TypeScript
import { GameConfig, GameID, PartialGameRecord } from "../core/Schemas";
|
|
import { replacer } from "../core/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);
|
|
}
|