Merge pull request #12 from jhalitaksoy/feature/game-id

Feature/game
This commit is contained in:
Halit Aksoy 2022-07-31 01:05:28 +03:00 committed by GitHub
commit a9a9bb8b17
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 55 additions and 13 deletions

View File

@ -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);
@ -92,10 +92,7 @@ export class GameManager {
}
public fireOnPlayerConnected(playerId: string) {
const game = this.gameStore.get(playerId);
if (game) {
this.rtmt.sendMessage(playerId, channel_on_game_update, game);
}
}
public createMancalaGame(userKey1: string, userKey2: string) {
@ -103,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;
}
@ -125,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) {

View File

@ -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;
}

View File

@ -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);
}
}

View File

@ -11,7 +11,7 @@ const gameStore = new GameStoreImpl();
const matchMaker = new MatchMakerImpl({ rtmt });
const gameManager = new GameManager({ rtmt, gameStore, matchMaker })
const expressApp = new ExpressApp();
const expressApp = new ExpressApp({ gameStore });
const server = new WebServer({ expressApp: expressApp.app });
rtmt.initWebSocket(server.server, (userKey: string) => gameManager.fireOnPlayerConnected(userKey));

View File

@ -4,10 +4,13 @@ import { Request, Response } from "express";
import morgan from "morgan";
import fs from "fs";
import { generateKey } from "../util/key_factory";
import { GameStore } from "../game/gamestore/GameStore";
export class ExpressApp {
app: Express.Application;
constructor() {
gameStore: GameStore;
constructor(params: { gameStore: GameStore }) {
this.gameStore = params.gameStore;
this.app = this.createExpressApp();
}
private createAccessLogMiddleware() {
@ -26,6 +29,17 @@ export class ExpressApp {
app.get("/register/", (req: Request, res: Response) => {
res.send(generateKey());
});
app.get("/game/:id", (req: Request, res: Response) => {
const gameId = req.params.id;
const game = this.gameStore.get(gameId);
if(game) {
res.send(game)
} else {
res.sendStatus(404)
}
});
return app;
}
}