2022-07-15 21:42:38 +03:00
|
|
|
import { GameStore } from "./GameStore";
|
2024-06-17 15:37:17 +03:00
|
|
|
import { BackendGame } from "../../models/Game";
|
2022-07-15 21:42:38 +03:00
|
|
|
|
|
|
|
|
export class GameStoreImpl implements GameStore {
|
2024-06-17 15:37:17 +03:00
|
|
|
gameStore: Map<string, BackendGame> = new Map<string, BackendGame>()
|
2022-07-31 00:10:34 +03:00
|
|
|
userGameMap: Map<string, string> = new Map<string, string>()
|
2022-07-15 21:42:38 +03:00
|
|
|
|
2024-06-17 15:37:17 +03:00
|
|
|
get(id: string): BackendGame | undefined {
|
2022-07-15 21:42:38 +03:00
|
|
|
return this.gameStore.get(id);
|
|
|
|
|
}
|
2024-06-17 15:37:17 +03:00
|
|
|
set(id: string, game: BackendGame): void {
|
2022-07-15 21:42:38 +03:00
|
|
|
this.gameStore.set(id, game);
|
|
|
|
|
}
|
|
|
|
|
remove(id: string): void {
|
|
|
|
|
this.gameStore.delete(id);
|
|
|
|
|
}
|
2022-07-31 00:10:34 +03:00
|
|
|
|
|
|
|
|
setGameIdByUser(userId: string, gameId: string): void {
|
|
|
|
|
this.userGameMap.set(userId, gameId)
|
|
|
|
|
}
|
|
|
|
|
getGameIdByUser(userId: string): string | undefined {
|
|
|
|
|
return this.userGameMap.get(userId);
|
|
|
|
|
}
|
|
|
|
|
removeGameIdByPlayer(userId: string): void {
|
|
|
|
|
this.userGameMap.delete(userId);
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-17 15:37:17 +03:00
|
|
|
setGameByUser(userId: string, game: BackendGame): void {
|
2022-07-31 00:10:34 +03:00
|
|
|
this.setGameIdByUser(userId, game.id);
|
|
|
|
|
}
|
2024-06-17 15:37:17 +03:00
|
|
|
getGameByUser(userId: string): BackendGame | undefined {
|
2022-07-31 00:10:34 +03:00
|
|
|
const gameId = this.getGameIdByUser(userId);
|
|
|
|
|
return gameId && this.gameStore.get(gameId) || undefined;
|
|
|
|
|
}
|
|
|
|
|
removeGameByPlayer(userId: string): void {
|
|
|
|
|
this.removeGameIdByPlayer(userId);
|
|
|
|
|
}
|
2022-09-02 00:33:31 +03:00
|
|
|
|
|
|
|
|
getSpeactatingGameIdsByUser(userId: string): string[] {
|
|
|
|
|
const speactatingGameIds = [];
|
|
|
|
|
for (const gameId in this.gameStore.keys()) {
|
2024-06-17 15:37:17 +03:00
|
|
|
const game = this.gameStore.get(gameId) as BackendGame;
|
2022-09-02 00:33:31 +03:00
|
|
|
const isSpectator = game.spectatorIds.find((value) => value === userId);
|
|
|
|
|
isSpectator && speactatingGameIds.push(game.id);
|
|
|
|
|
}
|
|
|
|
|
return speactatingGameIds;
|
|
|
|
|
}
|
2022-07-15 21:42:38 +03:00
|
|
|
}
|