diff --git a/src/routes/connected/games/[game]/+page.svelte b/src/routes/connected/games/[game]/+page.svelte index 123e22d..9a78244 100644 --- a/src/routes/connected/games/[game]/+page.svelte +++ b/src/routes/connected/games/[game]/+page.svelte @@ -25,6 +25,13 @@ import AudioMultipleChoiceQuestionComponent from "$lib/AudioMultipleChoiceQuestionComponent.svelte"; import ImageMultipleChoiceQuestionComponent from "$lib/ImageMultipleChoiceQuestionComponent.svelte"; + interface SaveData { + players: Player[]; + currentPlayer: number; + currentWall: number; + visitedQuestions: VisitedQuestions; + } + let startDisabled = $state(true); function handleDisplayConnected() { @@ -80,6 +87,31 @@ this.game = game; } + save(): void { + const saveData = { + players: this.players, + currentPlayer: this.currentPlayer, + currentWall: this.currentWall, + visitedQuestions: this.visitedQuestions + }; + localStorage.setItem(`saveGame-${this.game.name}`, JSON.stringify(saveData)); + } + + load(): void { + const saveDataString = localStorage.getItem(`saveGame-${this.game.name}`); + if (saveDataString === null) return; + try { + const saveData: SaveData = JSON.parse(saveDataString); + this.players = saveData.players; + this.currentPlayer = saveData.currentPlayer; + this.currentWall = saveData.currentWall; + this.visitedQuestions = saveData.visitedQuestions; + console.log(saveData); + } catch (e) { + // + } + } + startGame(): void { this.currentPlayer = Math.floor(Math.random() * this.players.length); this.state = GameState.CHOOSING_QUESTION; @@ -250,12 +282,13 @@ this.sendWall(); this.state = GameState.CHOOSING_QUESTION; } + this.save(); } wallIsDone(): boolean { let visitedNum = 0; for (const questions of this.visitedQuestions) { - if (questions !== undefined) { + if (questions != undefined) { visitedNum += questions.length; } } @@ -316,6 +349,7 @@