commit
a9a9bb8b17
@ -50,7 +50,7 @@ export class GameManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private onGameMove(userKey: string, gameMove: GameMove) {
|
private onGameMove(userKey: string, gameMove: GameMove) {
|
||||||
const game = this.gameStore.get(userKey);
|
const game = this.gameStore.getGameByUser(userKey);
|
||||||
if (game) {
|
if (game) {
|
||||||
try {
|
try {
|
||||||
game.moveByPlayerPit(userKey, gameMove.index);
|
game.moveByPlayerPit(userKey, gameMove.index);
|
||||||
@ -76,7 +76,7 @@ export class GameManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private onPlayerLeave(userKey: string) {
|
private onPlayerLeave(userKey: string) {
|
||||||
const game = this.gameStore.get(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.player1Id, channel_on_game_user_leave, userKey);
|
||||||
@ -92,10 +92,7 @@ export class GameManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public fireOnPlayerConnected(playerId: string) {
|
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) {
|
public createMancalaGame(userKey1: string, userKey2: string) {
|
||||||
@ -103,8 +100,9 @@ export class GameManager {
|
|||||||
const random = Math.random();
|
const random = Math.random();
|
||||||
game.turnPlayerId = random > 0.5 ? userKey1 : userKey2;
|
game.turnPlayerId = random > 0.5 ? userKey1 : userKey2;
|
||||||
|
|
||||||
this.gameStore.set(userKey1, game);
|
this.gameStore.set(game.id, game);
|
||||||
this.gameStore.set(userKey2, game);
|
this.gameStore.setGameByUser(userKey1, game);
|
||||||
|
this.gameStore.setGameByUser(userKey2, game);
|
||||||
return game;
|
return game;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -125,14 +123,14 @@ export class GameManager {
|
|||||||
|
|
||||||
public deleteGame(game: MancalaGame) {
|
public deleteGame(game: MancalaGame) {
|
||||||
if (game) {
|
if (game) {
|
||||||
this.gameStore.remove(game.player1Id);
|
this.gameStore.removeGameByPlayer(game.player1Id);
|
||||||
this.gameStore.remove(game.player2Id);
|
this.gameStore.removeGameByPlayer(game.player2Id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private listenUserConnectionChange() {
|
private listenUserConnectionChange() {
|
||||||
this.rtmt.listenOnClientConnectionChange((clientId: string, isOnline: boolean) => {
|
this.rtmt.listenOnClientConnectionChange((clientId: string, isOnline: boolean) => {
|
||||||
const game = this.gameStore.get(clientId);
|
const game = this.gameStore.getGameByUser(clientId);
|
||||||
if (game) {
|
if (game) {
|
||||||
this.sendUserConnectionInfo(game, clientId, isOnline);
|
this.sendUserConnectionInfo(game, clientId, isOnline);
|
||||||
if (isOnline) {
|
if (isOnline) {
|
||||||
|
|||||||
@ -4,4 +4,12 @@ export interface GameStore {
|
|||||||
get(id: string): MancalaGame | undefined;
|
get(id: string): MancalaGame | undefined;
|
||||||
set(id: string, game: MancalaGame): void;
|
set(id: string, game: MancalaGame): void;
|
||||||
remove(id: string): 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 {
|
export class GameStoreImpl implements GameStore {
|
||||||
gameStore: Map<string, MancalaGame> = new Map<string, MancalaGame>()
|
gameStore: Map<string, MancalaGame> = new Map<string, MancalaGame>()
|
||||||
|
userGameMap: Map<string, string> = new Map<string, string>()
|
||||||
|
|
||||||
get(id: string): MancalaGame | undefined {
|
get(id: string): MancalaGame | undefined {
|
||||||
return this.gameStore.get(id);
|
return this.gameStore.get(id);
|
||||||
@ -13,4 +14,25 @@ export class GameStoreImpl implements GameStore {
|
|||||||
remove(id: string): void {
|
remove(id: string): void {
|
||||||
this.gameStore.delete(id);
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@ -11,7 +11,7 @@ const gameStore = new GameStoreImpl();
|
|||||||
const matchMaker = new MatchMakerImpl({ rtmt });
|
const matchMaker = new MatchMakerImpl({ rtmt });
|
||||||
const gameManager = new GameManager({ rtmt, gameStore, matchMaker })
|
const gameManager = new GameManager({ rtmt, gameStore, matchMaker })
|
||||||
|
|
||||||
const expressApp = new ExpressApp();
|
const expressApp = new ExpressApp({ gameStore });
|
||||||
const server = new WebServer({ expressApp: expressApp.app });
|
const server = new WebServer({ expressApp: expressApp.app });
|
||||||
|
|
||||||
rtmt.initWebSocket(server.server, (userKey: string) => gameManager.fireOnPlayerConnected(userKey));
|
rtmt.initWebSocket(server.server, (userKey: string) => gameManager.fireOnPlayerConnected(userKey));
|
||||||
|
|||||||
@ -4,10 +4,13 @@ import { Request, Response } from "express";
|
|||||||
import morgan from "morgan";
|
import morgan from "morgan";
|
||||||
import fs from "fs";
|
import fs from "fs";
|
||||||
import { generateKey } from "../util/key_factory";
|
import { generateKey } from "../util/key_factory";
|
||||||
|
import { GameStore } from "../game/gamestore/GameStore";
|
||||||
|
|
||||||
export class ExpressApp {
|
export class ExpressApp {
|
||||||
app: Express.Application;
|
app: Express.Application;
|
||||||
constructor() {
|
gameStore: GameStore;
|
||||||
|
constructor(params: { gameStore: GameStore }) {
|
||||||
|
this.gameStore = params.gameStore;
|
||||||
this.app = this.createExpressApp();
|
this.app = this.createExpressApp();
|
||||||
}
|
}
|
||||||
private createAccessLogMiddleware() {
|
private createAccessLogMiddleware() {
|
||||||
@ -26,6 +29,17 @@ export class ExpressApp {
|
|||||||
app.get("/register/", (req: Request, res: Response) => {
|
app.get("/register/", (req: Request, res: Response) => {
|
||||||
res.send(generateKey());
|
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;
|
return app;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user