mirror of
https://github.com/openfrontio/OpenFrontIO.git
synced 2026-06-22 10:48:10 +00:00
52f64db6d3
## Description: Fixes a number of ESLint violations. Although I have tested these changes through the local dev server, I don't have a high confidence that the testing is sufficient, as I am new to this codebase. This change would benefit from heightened scrutiny. ## Please complete the following: - [x] I have added screenshots for all UI updates - [x] I confirm I have thoroughly tested these changes and take full responsibility for any bugs introduced - [x] I understand that submitting code with bugs that could have been caught through manual testing blocks releases and new features for all contributors ## Please put your Discord username so you can be contacted if a bug or regression is found: fake.neo --------- Co-authored-by: Scott Anderson <662325+scottanderson@users.noreply.github.com>
60 lines
1.3 KiB
TypeScript
60 lines
1.3 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;
|
|
};
|
|
}
|
|
|
|
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);
|
|
}
|