refactor GameStore
add new query methods
This commit is contained in:
parent
c4e399d3d4
commit
126431ccf1
@ -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) {
|
||||
|
||||
@ -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;
|
||||
}
|
||||
@ -3,6 +3,7 @@ import { GameStore } from "./GameStore";
|
||||
|
||||
export class GameStoreImpl implements GameStore {
|
||||
gameStore: Map<string, MancalaGame> = new Map<string, MancalaGame>()
|
||||
userGameMap: Map<string, string> = new Map<string, string>()
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user