refactor GameManager for Game model and fix user connection issue

This commit is contained in:
Halit Aksoy 2022-08-01 21:36:47 +03:00
parent 24560c0b1c
commit 2ae9a579e2

View File

@ -15,7 +15,7 @@ import {
import { GameMove } from "../models/GameMove"; import { GameMove } from "../models/GameMove";
import { GameCrashManager } from "./GameCrashManager"; import { GameCrashManager } from "./GameCrashManager";
import { MatchMaker } from "../matchmaker/MatchMaker"; import { MatchMaker } from "../matchmaker/MatchMaker";
import { UserConnectionInfo } from "../models/UserConnectionInfo"; import { Game, GameUsersConnectionInfo } from "../models/Game";
export class GameManager { export class GameManager {
gameStore: GameStore; gameStore: GameStore;
@ -53,10 +53,10 @@ export class GameManager {
const game = this.gameStore.getGameByUser(userKey); const game = this.gameStore.getGameByUser(userKey);
if (game) { if (game) {
try { try {
game.moveByPlayerPit(userKey, gameMove.index); game.mancalaGame.moveByPlayerPit(userKey, gameMove.index);
this.rtmt.sendMessage(game.player1Id, channel_on_game_update, game); this.rtmt.sendMessage(game.mancalaGame.player1Id, channel_on_game_update, game);
this.rtmt.sendMessage(game.player2Id, channel_on_game_update, game); this.rtmt.sendMessage(game.mancalaGame.player2Id, channel_on_game_update, game);
if (game.state == "ended") { if (game.mancalaGame.state == "ended") {
this.deleteGame(game); this.deleteGame(game);
} }
} catch (err: any) { } 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); console.error(error);
const crashFileName = GameCrashManager.logGameCrash(error, game); const crashFileName = GameCrashManager.logGameCrash(error, game);
console.info(`Game crash saved to file : ${crashFileName}`); console.info(`Game crash saved to file : ${crashFileName}`);
this.rtmt.sendMessage(game.player1Id, channel_on_game_crashed, error); this.rtmt.sendMessage(game.mancalaGame.player1Id, channel_on_game_crashed, error);
this.rtmt.sendMessage(game.player2Id, channel_on_game_crashed, error); this.rtmt.sendMessage(game.mancalaGame.player2Id, channel_on_game_crashed, error);
} }
private onPlayerLeave(userKey: string) { private onPlayerLeave(userKey: string) {
const game = this.gameStore.getGameByUser(userKey); const game = this.gameStore.getGameByUser(userKey);
if (game) { if (game) {
this.deleteGame(game); this.deleteGame(game);
this.rtmt.sendMessage(game.player1Id, channel_on_game_user_leave, userKey); this.rtmt.sendMessage(game.mancalaGame.player1Id, channel_on_game_user_leave, userKey);
this.rtmt.sendMessage(game.player2Id, 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) { public createMancalaGame(userKey1: string, userKey2: string): Game {
const game = new CommonMancalaGame(generateKey(), userKey1, userKey2); const mancalaGame = new CommonMancalaGame(generateKey(), userKey1, userKey2);
const random = Math.random(); 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.set(game.id, game);
this.gameStore.setGameByUser(userKey1, game); this.gameStore.setGameByUser(userKey1, game);
@ -106,38 +110,48 @@ export class GameManager {
return game; return game;
} }
public startGame(game: MancalaGame) { public getGameUsersConnectionInfo(game: Game): GameUsersConnectionInfo {
this.rtmt.sendMessage(game.player1Id, channel_on_game_start, game); return this.getGameUsersConnectionInfoFromUsers(game.mancalaGame.player1Id, game.mancalaGame.player2Id);
this.rtmt.sendMessage(game.player2Id, channel_on_game_start, game);
this.sendUserConnectionInfo(game, game.player1Id);
this.sendUserConnectionInfo(game, game.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 //todo: reimplement when watch game feature added
const _isOnline = isOnline === undefined ? this.rtmt.isClientOnline(playerId) : isOnline; this.rtmt.sendMessage(user1, channel_on_user_connection_change, gameUsersConnectionInfo);
const otherUser = game.player1Id === playerId ? game.player2Id : game.player1Id; this.rtmt.sendMessage(user2, channel_on_user_connection_change, gameUsersConnectionInfo);
const userConnectionInfo: UserConnectionInfo = { userId: playerId, isOnline: _isOnline };
this.rtmt.sendMessage(otherUser, channel_on_user_connection_change, userConnectionInfo);
} }
public deleteGame(game: MancalaGame) { public deleteGame(game: Game) {
if (game) { if (game) {
this.gameStore.removeGameByPlayer(game.player1Id); this.gameStore.removeGameByPlayer(game.mancalaGame.player1Id);
this.gameStore.removeGameByPlayer(game.player2Id); this.gameStore.removeGameByPlayer(game.mancalaGame.player2Id);
} }
} }
private listenUserConnectionChange() { private listenUserConnectionChange() {
this.rtmt.listenOnClientConnectionChange((clientId: string, isOnline: boolean) => { this.rtmt.listenOnClientConnectionChange((clientId: string, isOnline: boolean) => {
const game = this.gameStore.getGameByUser(clientId); const game = this.gameStore.getGameByUser(clientId);
if (game) { if (game) this.sendUserConnectionInfo(game);
this.sendUserConnectionInfo(game, clientId, isOnline);
if (isOnline) {
const otherUser = game.player1Id === clientId ? game.player2Id : game.player1Id;
this.sendUserConnectionInfo(game, otherUser);
}
}
}); });
} }
} }