mancala/backend/src/game/gamestore/GameStoreImpl.ts

48 lines
1.6 KiB
TypeScript
Raw Normal View History

2022-08-01 21:35:25 +03:00
import { Game } from "../../models/Game";
2022-07-15 21:42:38 +03:00
import { GameStore } from "./GameStore";
export class GameStoreImpl implements GameStore {
2022-08-01 21:35:25 +03:00
gameStore: Map<string, Game> = new Map<string, Game>()
userGameMap: Map<string, string> = new Map<string, string>()
2022-07-15 21:42:38 +03:00
2022-08-01 21:35:25 +03:00
get(id: string): Game | undefined {
2022-07-15 21:42:38 +03:00
return this.gameStore.get(id);
}
2022-08-01 21:35:25 +03:00
set(id: string, game: Game): void {
2022-07-15 21:42:38 +03:00
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);
}
2022-08-01 21:35:25 +03:00
setGameByUser(userId: string, game: Game): void {
this.setGameIdByUser(userId, game.id);
}
2022-08-01 21:35:25 +03:00
getGameByUser(userId: string): Game | undefined {
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()) {
const game = this.gameStore.get(gameId) as Game;
const isSpectator = game.spectatorIds.find((value) => value === userId);
isSpectator && speactatingGameIds.push(game.id);
}
return speactatingGameIds;
}
2022-07-15 21:42:38 +03:00
}