import { Game } from "../../models/Game"; import { GameStore } from "./GameStore"; export class GameStoreImpl implements GameStore { gameStore: Map = new Map() userGameMap: Map = new Map() get(id: string): Game | undefined { return this.gameStore.get(id); } set(id: string, game: Game): void { this.gameStore.set(id, game); } remove(id: string): void { this.gameStore.delete(id); } 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); } setGameByUser(userId: string, game: Game): void { this.setGameIdByUser(userId, game.id); } getGameByUser(userId: string): Game | undefined { const gameId = this.getGameIdByUser(userId); return gameId && this.gameStore.get(gameId) || undefined; } removeGameByPlayer(userId: string): void { this.removeGameIdByPlayer(userId); } getSpeactatingGameIdsByUser(userId: string): string[] { const speactatingGameIds = []; for (const gameId in this.gameStore.keys()) { const game = this.gameStore.get(gameId) as Game; const isSpectator = game.spectatorIds.find((value) => value === userId); isSpectator && speactatingGameIds.push(game.id); } return speactatingGameIds; } }