mirror of
https://github.com/openfrontio/OpenFrontIO.git
synced 2026-06-22 09:18:11 +00:00
70745faac4
## Description: Improve type safety and runtime correctness by: 1. Enabling TypeScript's [strictNullChecks](https://www.typescriptlang.org/tsconfig/#strictNullChecks) compiler option. 2. Replacing all loose equality operators (`==` and `!=`) with strict equality operators (`===` and `!==`). 3. Cleaning up of type declarations, null handling logic, and equality expressions throughout the project. Currently, the code allows implicit assumptions that `null` and `undefined` are interchangeable, and relies on type-coercing equality checks that can introduce subtle bugs. These practices make it difficult to reason about when values may be absent and hinder the effectiveness of static analysis. Migrating to strict null checks and enforcing strict equality comparisons will clarify intent, reduce bugs, and make the codebase safer and easier to maintain. Fixes #466 ## 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 --------- Co-authored-by: Scott Anderson <662325+scottanderson@users.noreply.github.com> Co-authored-by: evanpelle <openfrontio@gmail.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: Partial<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: 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.id];
|
|
|
|
if (!gameStat) {
|
|
consolex.log("LocalPersistantStats: game not found");
|
|
return;
|
|
}
|
|
|
|
gameStat.gameRecord = gameRecord;
|
|
save(stats);
|
|
}
|