diff --git a/src/game/GameManager.ts b/src/game/GameManager.ts index cc09632..e1f8393 100644 --- a/src/game/GameManager.ts +++ b/src/game/GameManager.ts @@ -50,7 +50,7 @@ export class GameManager { } private onGameMove(userKey: string, gameMove: GameMove) { - const game = this.gameStore.get(userKey); + const game = this.gameStore.getGameByUser(userKey); if (game) { try { game.moveByPlayerPit(userKey, gameMove.index); @@ -76,7 +76,7 @@ export class GameManager { } private onPlayerLeave(userKey: string) { - const game = this.gameStore.get(userKey); + const game = this.gameStore.getGameByUser(userKey); if (game) { this.deleteGame(game); this.rtmt.sendMessage(game.player1Id, channel_on_game_user_leave, userKey); @@ -100,8 +100,9 @@ export class GameManager { const random = Math.random(); game.turnPlayerId = random > 0.5 ? userKey1 : userKey2; - this.gameStore.set(userKey1, game); - this.gameStore.set(userKey2, game); + this.gameStore.set(game.id, game); + this.gameStore.setGameByUser(userKey1, game); + this.gameStore.setGameByUser(userKey2, game); return game; } @@ -122,14 +123,14 @@ export class GameManager { public deleteGame(game: MancalaGame) { if (game) { - this.gameStore.remove(game.player1Id); - this.gameStore.remove(game.player2Id); + this.gameStore.removeGameByPlayer(game.player1Id); + this.gameStore.removeGameByPlayer(game.player2Id); } } private listenUserConnectionChange() { this.rtmt.listenOnClientConnectionChange((clientId: string, isOnline: boolean) => { - const game = this.gameStore.get(clientId); + const game = this.gameStore.getGameByUser(clientId); if (game) { this.sendUserConnectionInfo(game, clientId, isOnline); if (isOnline) { diff --git a/src/game/gamestore/GameStore.ts b/src/game/gamestore/GameStore.ts index 97d3f8e..882ab36 100644 --- a/src/game/gamestore/GameStore.ts +++ b/src/game/gamestore/GameStore.ts @@ -4,4 +4,12 @@ export interface GameStore { get(id: string): MancalaGame | undefined; set(id: string, game: MancalaGame): void; remove(id: string): void; + + setGameIdByUser(userId: string, gameId: string): void; + getGameIdByUser(userId: string): string | undefined; + removeGameIdByPlayer(userId: string): void; + + setGameByUser(userId: string, game: MancalaGame): void; + getGameByUser(userId: string): MancalaGame | undefined; + removeGameByPlayer(userId: string): void; } \ No newline at end of file diff --git a/src/game/gamestore/GameStoreImpl.ts b/src/game/gamestore/GameStoreImpl.ts index 9256485..a5cde9f 100644 --- a/src/game/gamestore/GameStoreImpl.ts +++ b/src/game/gamestore/GameStoreImpl.ts @@ -3,6 +3,7 @@ import { GameStore } from "./GameStore"; export class GameStoreImpl implements GameStore { gameStore: Map = new Map() + userGameMap: Map = new Map() get(id: string): MancalaGame | undefined { return this.gameStore.get(id); @@ -13,4 +14,25 @@ export class GameStoreImpl implements GameStore { 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: MancalaGame): void { + this.setGameIdByUser(userId, game.id); + } + getGameByUser(userId: string): MancalaGame | undefined { + const gameId = this.getGameIdByUser(userId); + return gameId && this.gameStore.get(gameId) || undefined; + } + removeGameByPlayer(userId: string): void { + this.removeGameIdByPlayer(userId); + } } \ No newline at end of file