feat: save stats to local storage

Victory lose, player stats, lobby config are stored to local storage
(for later use)
This commit is contained in:
ilan schemoul
2025-03-06 01:00:19 +01:00
parent d2208755c4
commit 398d354702
10 changed files with 130 additions and 14 deletions
+9 -5
View File
@@ -22,6 +22,7 @@ import { getConfig, getServerConfig } from "../core/configuration/Config";
import { GameView, PlayerView } from "../core/game/GameView";
import { GameUpdateViewData } from "../core/game/GameUpdates";
import { UserSettings } from "../core/game/UserSettings";
import { LocalPersistantStats } from "./LocalPersistantStats";
export interface LobbyConfig {
flag: () => string;
@@ -120,6 +121,7 @@ export async function createClientGame(
config,
gameMap.gameMap,
lobbyConfig.clientID,
lobbyConfig.gameID,
);
consolex.log("going to init path finder");
@@ -137,7 +139,7 @@ export async function createClientGame(
);
return new ClientGameRunner(
lobbyConfig.clientID,
lobbyConfig,
eventBus,
gameRenderer,
new InputHandler(canvas, eventBus),
@@ -148,6 +150,7 @@ export async function createClientGame(
}
export class ClientGameRunner {
private localPersistantsStats = new LocalPersistantStats();
private myPlayer: PlayerView;
private isActive = false;
@@ -155,7 +158,7 @@ export class ClientGameRunner {
private hasJoined = false;
constructor(
private clientID: ClientID,
private lobby: LobbyConfig,
private eventBus: EventBus,
private renderer: GameRenderer,
private input: InputHandler,
@@ -165,6 +168,7 @@ export class ClientGameRunner {
) {}
public start() {
this.localPersistantsStats.startGame(this.lobby);
consolex.log("starting client game");
this.isActive = true;
this.eventBus.on(MouseUpEvent, (e) => this.inputEvent(e));
@@ -173,7 +177,7 @@ export class ClientGameRunner {
this.input.initialize();
this.worker.start((gu: GameUpdateViewData | ErrorUpdate) => {
if ("errMsg" in gu) {
showErrorModal(gu.errMsg, gu.stack, this.clientID);
showErrorModal(gu.errMsg, gu.stack, this.lobby.clientID);
return;
}
gu.updates[GameUpdateType.Hash].forEach((hu: HashUpdate) => {
@@ -217,7 +221,7 @@ export class ClientGameRunner {
showErrorModal(
`desync from server: ${JSON.stringify(message)}`,
"",
this.clientID,
this.lobby.clientID,
);
}
if (message.type == "turn") {
@@ -269,7 +273,7 @@ export class ClientGameRunner {
return;
}
if (this.myPlayer == null) {
this.myPlayer = this.gameView.playerByClientID(this.clientID);
this.myPlayer = this.gameView.playerByClientID(this.lobby.clientID);
if (this.myPlayer == null) {
return;
}