2 Commits

Author SHA1 Message Date
bb78d7f5af Hotfix null 2025-09-12 14:48:42 +02:00
e6f16ca8cd Added Savegame 2025-09-12 14:43:07 +02:00

View File

@@ -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;
@@ -235,7 +267,7 @@
}
finishQuestion(): void {
if (this.visitedQuestions[this.currentCategory] === undefined) {
if (this.visitedQuestions[this.currentCategory] == undefined) {
this.visitedQuestions[this.currentCategory] = [this.currentQuestion];
} else if (
!this.visitedQuestions[this.currentCategory].includes(this.currentQuestion)
@@ -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 @@
<div class="p-4">
<div class="flex items-center">
<h2 class="grow pb-4 text-5xl">Spieler</h2>
<button class="btn me-4" onclick={() => gameManager.load()}>Load SaveGame</button>
<button
class="btn"
disabled={!startDisabled}