Merge pull request #13 from jhalitaksoy/feature/game-model
Feature/game model
This commit is contained in:
commit
88727c86d0
@ -1,5 +1,5 @@
|
|||||||
import { MancalaGame } from "mancala.js";
|
|
||||||
import fs from "fs";
|
import fs from "fs";
|
||||||
|
import { Game } from "../models/Game";
|
||||||
const crashFolder = "./crashes";
|
const crashFolder = "./crashes";
|
||||||
export class GameCrashManager {
|
export class GameCrashManager {
|
||||||
constructor() {}
|
constructor() {}
|
||||||
@ -22,7 +22,7 @@ export class GameCrashManager {
|
|||||||
return `${year}-${month}-${date}-${hours}:${minutes}:${seconds}:${miliSeconds}`;
|
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();
|
this.createCrashFolderIfNotExist();
|
||||||
const crash = {
|
const crash = {
|
||||||
message: error.message,
|
message: error.message,
|
||||||
|
|||||||
@ -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): Game {
|
||||||
|
const mancalaGame = new CommonMancalaGame(generateKey(), userKey1, userKey2);
|
||||||
public createMancalaGame(userKey1: string, userKey2: string) {
|
|
||||||
const game = 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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1,15 +1,15 @@
|
|||||||
import { MancalaGame } from "mancala.js";
|
import { Game } from "../../models/Game";
|
||||||
|
|
||||||
export interface GameStore {
|
export interface GameStore {
|
||||||
get(id: string): MancalaGame | undefined;
|
get(id: string): Game | undefined;
|
||||||
set(id: string, game: MancalaGame): void;
|
set(id: string, game: Game): void;
|
||||||
remove(id: string): void;
|
remove(id: string): void;
|
||||||
|
|
||||||
setGameIdByUser(userId: string, gameId: string): void;
|
setGameIdByUser(userId: string, gameId: string): void;
|
||||||
getGameIdByUser(userId: string): string | undefined;
|
getGameIdByUser(userId: string): string | undefined;
|
||||||
removeGameIdByPlayer(userId: string): void;
|
removeGameIdByPlayer(userId: string): void;
|
||||||
|
|
||||||
setGameByUser(userId: string, game: MancalaGame): void;
|
setGameByUser(userId: string, game: Game): void;
|
||||||
getGameByUser(userId: string): MancalaGame | undefined;
|
getGameByUser(userId: string): Game | undefined;
|
||||||
removeGameByPlayer(userId: string): void;
|
removeGameByPlayer(userId: string): void;
|
||||||
}
|
}
|
||||||
@ -1,14 +1,14 @@
|
|||||||
import { MancalaGame } from "mancala.js";
|
import { Game } from "../../models/Game";
|
||||||
import { GameStore } from "./GameStore";
|
import { GameStore } from "./GameStore";
|
||||||
|
|
||||||
export class GameStoreImpl implements GameStore {
|
export class GameStoreImpl implements GameStore {
|
||||||
gameStore: Map<string, MancalaGame> = new Map<string, MancalaGame>()
|
gameStore: Map<string, Game> = new Map<string, Game>()
|
||||||
userGameMap: Map<string, string> = new Map<string, string>()
|
userGameMap: Map<string, string> = new Map<string, string>()
|
||||||
|
|
||||||
get(id: string): MancalaGame | undefined {
|
get(id: string): Game | undefined {
|
||||||
return this.gameStore.get(id);
|
return this.gameStore.get(id);
|
||||||
}
|
}
|
||||||
set(id: string, game: MancalaGame): void {
|
set(id: string, game: Game): void {
|
||||||
this.gameStore.set(id, game);
|
this.gameStore.set(id, game);
|
||||||
}
|
}
|
||||||
remove(id: string): void {
|
remove(id: string): void {
|
||||||
@ -25,10 +25,10 @@ export class GameStoreImpl implements GameStore {
|
|||||||
this.userGameMap.delete(userId);
|
this.userGameMap.delete(userId);
|
||||||
}
|
}
|
||||||
|
|
||||||
setGameByUser(userId: string, game: MancalaGame): void {
|
setGameByUser(userId: string, game: Game): void {
|
||||||
this.setGameIdByUser(userId, game.id);
|
this.setGameIdByUser(userId, game.id);
|
||||||
}
|
}
|
||||||
getGameByUser(userId: string): MancalaGame | undefined {
|
getGameByUser(userId: string): Game | undefined {
|
||||||
const gameId = this.getGameIdByUser(userId);
|
const gameId = this.getGameIdByUser(userId);
|
||||||
return gameId && this.gameStore.get(gameId) || undefined;
|
return gameId && this.gameStore.get(gameId) || undefined;
|
||||||
}
|
}
|
||||||
|
|||||||
13
src/models/Game.ts
Normal file
13
src/models/Game.ts
Normal file
@ -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;
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user