2025-01-19 05:24:56 +02:00
|
|
|
import type { Player } from '$lib/types';
|
|
|
|
|
|
|
|
export class PlayerState {
|
2025-01-21 11:42:52 +02:00
|
|
|
players = $state<Player[]>([]);
|
2025-01-19 05:24:56 +02:00
|
|
|
|
2025-01-21 11:42:52 +02:00
|
|
|
newPlayer(id: string) {
|
|
|
|
this.players.push({ id: id, stage: 0, highscore: 0, playing: true });
|
|
|
|
}
|
2025-01-19 05:24:56 +02:00
|
|
|
|
2025-01-21 11:42:52 +02:00
|
|
|
getStage(id: string) {
|
|
|
|
const player = this.players.find((player) => player.id === id);
|
2025-01-19 05:24:56 +02:00
|
|
|
|
2025-01-21 11:42:52 +02:00
|
|
|
if (!player) {
|
|
|
|
return undefined;
|
|
|
|
}
|
2025-01-19 05:24:56 +02:00
|
|
|
|
2025-01-21 11:42:52 +02:00
|
|
|
return player.stage;
|
|
|
|
}
|
2025-01-19 05:24:56 +02:00
|
|
|
|
2025-01-21 11:42:52 +02:00
|
|
|
nextStage(id: string) {
|
|
|
|
const player = this.players.find((player) => player.id === id);
|
2025-01-19 05:24:56 +02:00
|
|
|
|
2025-01-21 11:42:52 +02:00
|
|
|
if (!player) {
|
|
|
|
return;
|
|
|
|
}
|
2025-01-19 05:24:56 +02:00
|
|
|
|
2025-01-21 11:42:52 +02:00
|
|
|
player.stage += 1;
|
|
|
|
}
|
2025-01-19 05:24:56 +02:00
|
|
|
|
2025-01-21 11:42:52 +02:00
|
|
|
restart(id: string) {
|
|
|
|
const player = this.players.find((player) => player.id === id);
|
2025-01-19 05:24:56 +02:00
|
|
|
|
2025-01-21 11:42:52 +02:00
|
|
|
if (!player) {
|
|
|
|
return;
|
|
|
|
}
|
2025-01-19 05:24:56 +02:00
|
|
|
|
2025-01-21 11:42:52 +02:00
|
|
|
player.stage = 0;
|
|
|
|
player.playing = true;
|
|
|
|
}
|
2025-01-19 05:24:56 +02:00
|
|
|
|
2025-01-21 11:42:52 +02:00
|
|
|
score(id: string, won: boolean) {
|
|
|
|
const player = this.players.find((player) => player.id === id);
|
2025-01-19 05:24:56 +02:00
|
|
|
|
2025-01-21 11:42:52 +02:00
|
|
|
if (!player) {
|
|
|
|
return;
|
|
|
|
}
|
2025-01-19 05:24:56 +02:00
|
|
|
|
2025-01-21 11:42:52 +02:00
|
|
|
if (won) {
|
|
|
|
player.stage += 1;
|
|
|
|
return;
|
|
|
|
}
|
2025-01-19 05:24:56 +02:00
|
|
|
|
2025-01-21 11:42:52 +02:00
|
|
|
player.playing = false;
|
|
|
|
|
|
|
|
if (player.stage > player.highscore) {
|
|
|
|
player.highscore = player.stage;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
getHighscore(id: string) {
|
|
|
|
const player = this.players.find((player) => player.id === id);
|
|
|
|
|
|
|
|
if (!player) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
return player.highscore;
|
|
|
|
}
|
|
|
|
|
|
|
|
getPlaying(id: string) {
|
|
|
|
const player = this.players.find((player) => player.id === id);
|
|
|
|
|
|
|
|
if (!player) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
return player.playing;
|
|
|
|
}
|
|
|
|
|
|
|
|
setPlaying(id: string, playing: boolean) {
|
|
|
|
const player = this.players.find((player) => player.id === id);
|
|
|
|
|
|
|
|
if (!player) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
player.playing = playing;
|
|
|
|
}
|
2025-01-19 05:24:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export const playerState = new PlayerState();
|