From b48aa0847e41e326631a039d740bd2c1b65df3ff Mon Sep 17 00:00:00 2001 From: Halit Aksoy Date: Mon, 1 Aug 2022 21:34:42 +0300 Subject: [PATCH 1/3] add game model --- src/models/Game.ts | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 src/models/Game.ts diff --git a/src/models/Game.ts b/src/models/Game.ts new file mode 100644 index 0000000..ec37311 --- /dev/null +++ b/src/models/Game.ts @@ -0,0 +1,13 @@ +import { MancalaGame } from "mancala.js"; +import { UserConnectionInfo } from "./UserConnectionInfo"; + +export interface Game { + id: string; + mancalaGame: MancalaGame; + gameUsersConnectionInfo: GameUsersConnectionInfo; +} + +export interface GameUsersConnectionInfo { + user1ConnectionInfo: UserConnectionInfo; + user2ConnectionInfo: UserConnectionInfo; +} \ No newline at end of file From 24560c0b1cb03376d77ffdb6994385aca1039b16 Mon Sep 17 00:00:00 2001 From: Halit Aksoy Date: Mon, 1 Aug 2022 21:35:25 +0300 Subject: [PATCH 2/3] replace MancalaGame to Game --- src/game/GameCrashManager.ts | 4 ++-- src/game/gamestore/GameStore.ts | 10 +++++----- src/game/gamestore/GameStoreImpl.ts | 12 ++++++------ 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/game/GameCrashManager.ts b/src/game/GameCrashManager.ts index c513228..45d8f19 100644 --- a/src/game/GameCrashManager.ts +++ b/src/game/GameCrashManager.ts @@ -1,5 +1,5 @@ -import { MancalaGame } from "mancala.js"; import fs from "fs"; +import { Game } from "../models/Game"; const crashFolder = "./crashes"; export class GameCrashManager { constructor() {} @@ -22,7 +22,7 @@ export class GameCrashManager { return `${year}-${month}-${date}-${hours}:${minutes}:${seconds}:${miliSeconds}`; } - public static logGameCrash(error: Error, game: MancalaGame): string { + public static logGameCrash(error: Error, game: Game): string { this.createCrashFolderIfNotExist(); const crash = { message: error.message, diff --git a/src/game/gamestore/GameStore.ts b/src/game/gamestore/GameStore.ts index 882ab36..8813b29 100644 --- a/src/game/gamestore/GameStore.ts +++ b/src/game/gamestore/GameStore.ts @@ -1,15 +1,15 @@ -import { MancalaGame } from "mancala.js"; +import { Game } from "../../models/Game"; export interface GameStore { - get(id: string): MancalaGame | undefined; - set(id: string, game: MancalaGame): void; + get(id: string): Game | undefined; + set(id: string, game: Game): 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; + setGameByUser(userId: string, game: Game): void; + getGameByUser(userId: string): Game | 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 a5cde9f..e720cb4 100644 --- a/src/game/gamestore/GameStoreImpl.ts +++ b/src/game/gamestore/GameStoreImpl.ts @@ -1,14 +1,14 @@ -import { MancalaGame } from "mancala.js"; +import { Game } from "../../models/Game"; import { GameStore } from "./GameStore"; export class GameStoreImpl implements GameStore { - gameStore: Map = new Map() + gameStore: Map = new Map() userGameMap: Map = new Map() - get(id: string): MancalaGame | undefined { + get(id: string): Game | undefined { return this.gameStore.get(id); } - set(id: string, game: MancalaGame): void { + set(id: string, game: Game): void { this.gameStore.set(id, game); } remove(id: string): void { @@ -25,10 +25,10 @@ export class GameStoreImpl implements GameStore { this.userGameMap.delete(userId); } - setGameByUser(userId: string, game: MancalaGame): void { + setGameByUser(userId: string, game: Game): void { this.setGameIdByUser(userId, game.id); } - getGameByUser(userId: string): MancalaGame | undefined { + getGameByUser(userId: string): Game | undefined { const gameId = this.getGameIdByUser(userId); return gameId && this.gameStore.get(gameId) || undefined; } From 2ae9a579e2bb254e4723fed012d095066f1123ba Mon Sep 17 00:00:00 2001 From: Halit Aksoy Date: Mon, 1 Aug 2022 21:36:47 +0300 Subject: [PATCH 3/3] refactor GameManager for Game model and fix user connection issue --- src/game/GameManager.ts | 86 ++++++++++++++++++++++++----------------- 1 file changed, 50 insertions(+), 36 deletions(-) diff --git a/src/game/GameManager.ts b/src/game/GameManager.ts index e1f8393..7cd5786 100644 --- a/src/game/GameManager.ts +++ b/src/game/GameManager.ts @@ -15,7 +15,7 @@ import { import { GameMove } from "../models/GameMove"; import { GameCrashManager } from "./GameCrashManager"; import { MatchMaker } from "../matchmaker/MatchMaker"; -import { UserConnectionInfo } from "../models/UserConnectionInfo"; +import { Game, GameUsersConnectionInfo } from "../models/Game"; export class GameManager { gameStore: GameStore; @@ -53,10 +53,10 @@ export class GameManager { const game = this.gameStore.getGameByUser(userKey); if (game) { try { - game.moveByPlayerPit(userKey, gameMove.index); - this.rtmt.sendMessage(game.player1Id, channel_on_game_update, game); - this.rtmt.sendMessage(game.player2Id, channel_on_game_update, game); - if (game.state == "ended") { + game.mancalaGame.moveByPlayerPit(userKey, gameMove.index); + this.rtmt.sendMessage(game.mancalaGame.player1Id, channel_on_game_update, game); + this.rtmt.sendMessage(game.mancalaGame.player2Id, channel_on_game_update, game); + if (game.mancalaGame.state == "ended") { this.deleteGame(game); } } catch (err: any) { @@ -67,20 +67,20 @@ export class GameManager { } } - private onGameError(game: MancalaGame, error: any) { + private onGameError(game: Game, error: any) { console.error(error); const crashFileName = GameCrashManager.logGameCrash(error, game); console.info(`Game crash saved to file : ${crashFileName}`); - this.rtmt.sendMessage(game.player1Id, channel_on_game_crashed, error); - this.rtmt.sendMessage(game.player2Id, channel_on_game_crashed, error); + this.rtmt.sendMessage(game.mancalaGame.player1Id, channel_on_game_crashed, error); + this.rtmt.sendMessage(game.mancalaGame.player2Id, channel_on_game_crashed, error); } private onPlayerLeave(userKey: string) { const game = this.gameStore.getGameByUser(userKey); if (game) { this.deleteGame(game); - this.rtmt.sendMessage(game.player1Id, channel_on_game_user_leave, userKey); - this.rtmt.sendMessage(game.player2Id, channel_on_game_user_leave, userKey); + this.rtmt.sendMessage(game.mancalaGame.player1Id, channel_on_game_user_leave, userKey); + this.rtmt.sendMessage(game.mancalaGame.player2Id, channel_on_game_user_leave, userKey); } } @@ -91,14 +91,18 @@ export class GameManager { }); } - public fireOnPlayerConnected(playerId: string) { - - } + public fireOnPlayerConnected(playerId: string) {} - public createMancalaGame(userKey1: string, userKey2: string) { - const game = new CommonMancalaGame(generateKey(), userKey1, userKey2); + public createMancalaGame(userKey1: string, userKey2: string): Game { + const mancalaGame = new CommonMancalaGame(generateKey(), userKey1, userKey2); const random = Math.random(); - game.turnPlayerId = random > 0.5 ? userKey1 : userKey2; + mancalaGame.turnPlayerId = random > 0.5 ? userKey1 : userKey2; + + const game: Game = { + id: mancalaGame.id, + mancalaGame, + gameUsersConnectionInfo: this.getGameUsersConnectionInfoFromUsers(mancalaGame.player1Id, mancalaGame.player2Id), + }; this.gameStore.set(game.id, game); this.gameStore.setGameByUser(userKey1, game); @@ -106,38 +110,48 @@ export class GameManager { return game; } - public startGame(game: MancalaGame) { - this.rtmt.sendMessage(game.player1Id, channel_on_game_start, game); - this.rtmt.sendMessage(game.player2Id, channel_on_game_start, game); - this.sendUserConnectionInfo(game, game.player1Id); - this.sendUserConnectionInfo(game, game.player2Id); + public getGameUsersConnectionInfo(game: Game): GameUsersConnectionInfo { + return this.getGameUsersConnectionInfoFromUsers(game.mancalaGame.player1Id, game.mancalaGame.player2Id); } - public sendUserConnectionInfo(game: MancalaGame, playerId: string, isOnline?: boolean) { + public getGameUsersConnectionInfoFromUsers(user1Id: string, user2Id: string): GameUsersConnectionInfo { + const isPlayer1Online = this.rtmt.isClientOnline(user1Id); + const isPlayer2Online = this.rtmt.isClientOnline(user2Id); + return { + user1ConnectionInfo: { userId: user1Id, isOnline: isPlayer1Online }, + user2ConnectionInfo: { userId: user2Id, isOnline: isPlayer2Online } + }; + } + + public startGame(game: Game) { + const mancalaGame = game.mancalaGame; + this.rtmt.sendMessage(mancalaGame.player1Id, channel_on_game_start, game); + this.rtmt.sendMessage(mancalaGame.player2Id, channel_on_game_start, game); + this.sendUserConnectionInfo(game); + this.sendUserConnectionInfo(game); + } + + public sendUserConnectionInfo(game: Game) { + const user1 = game.mancalaGame.player1Id; + const user2 = game.mancalaGame.player2Id; + const gameUsersConnectionInfo = this.getGameUsersConnectionInfo(game); + //todo: reimplement when watch game feature added - const _isOnline = isOnline === undefined ? this.rtmt.isClientOnline(playerId) : isOnline; - const otherUser = game.player1Id === playerId ? game.player2Id : game.player1Id; - const userConnectionInfo: UserConnectionInfo = { userId: playerId, isOnline: _isOnline }; - this.rtmt.sendMessage(otherUser, channel_on_user_connection_change, userConnectionInfo); + this.rtmt.sendMessage(user1, channel_on_user_connection_change, gameUsersConnectionInfo); + this.rtmt.sendMessage(user2, channel_on_user_connection_change, gameUsersConnectionInfo); } - public deleteGame(game: MancalaGame) { + public deleteGame(game: Game) { if (game) { - this.gameStore.removeGameByPlayer(game.player1Id); - this.gameStore.removeGameByPlayer(game.player2Id); + this.gameStore.removeGameByPlayer(game.mancalaGame.player1Id); + this.gameStore.removeGameByPlayer(game.mancalaGame.player2Id); } } private listenUserConnectionChange() { this.rtmt.listenOnClientConnectionChange((clientId: string, isOnline: boolean) => { const game = this.gameStore.getGameByUser(clientId); - if (game) { - this.sendUserConnectionInfo(game, clientId, isOnline); - if (isOnline) { - const otherUser = game.player1Id === clientId ? game.player2Id : game.player1Id; - this.sendUserConnectionInfo(game, otherUser); - } - } + if (game) this.sendUserConnectionInfo(game); }); } } \ No newline at end of file