refactor GameManager for Game model and fix user connection issue
This commit is contained in:
parent
24560c0b1c
commit
2ae9a579e2
@ -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);
|
||||
});
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user