From ceaf70ef050fe9842e15b960835d84a1710ddec6 Mon Sep 17 00:00:00 2001 From: Halit Aksoy Date: Sat, 30 Jul 2022 16:14:08 +0300 Subject: [PATCH 1/3] do not send on game update when user connected --- src/game/GameManager.ts | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/game/GameManager.ts b/src/game/GameManager.ts index f3bc1a8..cc09632 100644 --- a/src/game/GameManager.ts +++ b/src/game/GameManager.ts @@ -92,10 +92,7 @@ export class GameManager { } public fireOnPlayerConnected(playerId: string) { - const game = this.gameStore.get(playerId); - if (game) { - this.rtmt.sendMessage(playerId, channel_on_game_update, game); - } + } public createMancalaGame(userKey1: string, userKey2: string) { From c4e399d3d483252dd232e6ed2e102f6cfc5a7c82 Mon Sep 17 00:00:00 2001 From: Halit Aksoy Date: Sat, 30 Jul 2022 16:14:37 +0300 Subject: [PATCH 2/3] add game id route --- src/index.ts | 2 +- src/server/ExpressApp.ts | 16 +++++++++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/index.ts b/src/index.ts index eb86d54..e5d51e7 100644 --- a/src/index.ts +++ b/src/index.ts @@ -11,7 +11,7 @@ const gameStore = new GameStoreImpl(); const matchMaker = new MatchMakerImpl({ rtmt }); const gameManager = new GameManager({ rtmt, gameStore, matchMaker }) -const expressApp = new ExpressApp(); +const expressApp = new ExpressApp({ gameStore }); const server = new WebServer({ expressApp: expressApp.app }); rtmt.initWebSocket(server.server, (userKey: string) => gameManager.fireOnPlayerConnected(userKey)); diff --git a/src/server/ExpressApp.ts b/src/server/ExpressApp.ts index 8638fea..29b92c5 100644 --- a/src/server/ExpressApp.ts +++ b/src/server/ExpressApp.ts @@ -4,10 +4,13 @@ import { Request, Response } from "express"; import morgan from "morgan"; import fs from "fs"; import { generateKey } from "../util/key_factory"; +import { GameStore } from "../game/gamestore/GameStore"; export class ExpressApp { app: Express.Application; - constructor() { + gameStore: GameStore; + constructor(params: { gameStore: GameStore }) { + this.gameStore = params.gameStore; this.app = this.createExpressApp(); } private createAccessLogMiddleware() { @@ -26,6 +29,17 @@ export class ExpressApp { app.get("/register/", (req: Request, res: Response) => { res.send(generateKey()); }); + + app.get("/game/:id", (req: Request, res: Response) => { + const gameId = req.params.id; + const game = this.gameStore.get(gameId); + if(game) { + res.send(game) + } else { + res.sendStatus(404) + } + }); + return app; } } From 126431ccf1287f73ccb19200b933c3880f549c91 Mon Sep 17 00:00:00 2001 From: Halit Aksoy Date: Sun, 31 Jul 2022 00:10:34 +0300 Subject: [PATCH 3/3] refactor GameStore add new query methods --- src/game/GameManager.ts | 15 ++++++++------- src/game/gamestore/GameStore.ts | 8 ++++++++ src/game/gamestore/GameStoreImpl.ts | 22 ++++++++++++++++++++++ 3 files changed, 38 insertions(+), 7 deletions(-) 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