Added Savegame

This commit is contained in:
2025-09-12 14:43:07 +02:00
parent fb9c17fd69
commit e6f16ca8cd

View File

@@ -25,6 +25,13 @@
import AudioMultipleChoiceQuestionComponent from "$lib/AudioMultipleChoiceQuestionComponent.svelte"; import AudioMultipleChoiceQuestionComponent from "$lib/AudioMultipleChoiceQuestionComponent.svelte";
import ImageMultipleChoiceQuestionComponent from "$lib/ImageMultipleChoiceQuestionComponent.svelte"; import ImageMultipleChoiceQuestionComponent from "$lib/ImageMultipleChoiceQuestionComponent.svelte";
interface SaveData {
players: Player[];
currentPlayer: number;
currentWall: number;
visitedQuestions: VisitedQuestions;
}
let startDisabled = $state(true); let startDisabled = $state(true);
function handleDisplayConnected() { function handleDisplayConnected() {
@@ -80,6 +87,31 @@
this.game = game; 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 { startGame(): void {
this.currentPlayer = Math.floor(Math.random() * this.players.length); this.currentPlayer = Math.floor(Math.random() * this.players.length);
this.state = GameState.CHOOSING_QUESTION; this.state = GameState.CHOOSING_QUESTION;
@@ -250,12 +282,13 @@
this.sendWall(); this.sendWall();
this.state = GameState.CHOOSING_QUESTION; this.state = GameState.CHOOSING_QUESTION;
} }
this.save();
} }
wallIsDone(): boolean { wallIsDone(): boolean {
let visitedNum = 0; let visitedNum = 0;
for (const questions of this.visitedQuestions) { for (const questions of this.visitedQuestions) {
if (questions !== undefined) { if (questions != undefined) {
visitedNum += questions.length; visitedNum += questions.length;
} }
} }
@@ -316,6 +349,7 @@
<div class="p-4"> <div class="p-4">
<div class="flex items-center"> <div class="flex items-center">
<h2 class="grow pb-4 text-5xl">Spieler</h2> <h2 class="grow pb-4 text-5xl">Spieler</h2>
<button class="btn me-4" onclick={() => gameManager.load()}>Load SaveGame</button>
<button <button
class="btn" class="btn"
disabled={!startDisabled} disabled={!startDisabled}