refactor index.ts
This commit is contained in:
parent
540c2a25d6
commit
fde8a83331
128
src/index.ts
128
src/index.ts
@ -1,121 +1,17 @@
|
|||||||
import express, { Request, Response } from "express";
|
|
||||||
import * as http from "http";
|
|
||||||
import { RTMTWS } from "./rtmt/rtmt_websocket";
|
import { RTMTWS } from "./rtmt/rtmt_websocket";
|
||||||
import cors from "cors";
|
import { GameManager } from "./game/GameManager";
|
||||||
import { generateKey } from "./key_factory";
|
import { GameStoreImpl } from "./game/gamestore/GameStoreImpl";
|
||||||
import { MatchMaker } from "./matcmaker";
|
import { MatchMakerImpl } from "./matchmaker/MatchMakerImpl";
|
||||||
import { CommonMancalaGame, MancalaGame } from "mancala.js";
|
import { ExpressApp } from "./server/ExpressApp";
|
||||||
import fs from "fs";
|
import { WebServer } from "./server/WebServer";
|
||||||
import {
|
|
||||||
channel_game_move,
|
|
||||||
channel_leave_game,
|
|
||||||
channel_new_game,
|
|
||||||
channel_on_game_crashed,
|
|
||||||
channel_on_game_start,
|
|
||||||
channel_on_game_update,
|
|
||||||
channel_on_game_user_leave,
|
|
||||||
} from "./consts/channel_names";
|
|
||||||
import morgan from "morgan";
|
|
||||||
import { GameMove } from "./models/GameMove";
|
|
||||||
import { GameCrashManager } from "./GameCrashManager";
|
|
||||||
|
|
||||||
const app = express();
|
|
||||||
|
|
||||||
app.use(cors());
|
|
||||||
app.use(
|
|
||||||
morgan("common", {
|
|
||||||
stream: fs.createWriteStream("./access.log", { flags: "a" }),
|
|
||||||
})
|
|
||||||
);
|
|
||||||
app.use(morgan("dev"));
|
|
||||||
|
|
||||||
const server = http.createServer(app);
|
|
||||||
|
|
||||||
app.get("/", (req: Request, res: Response) => {
|
|
||||||
res.send("Server up and running!");
|
|
||||||
});
|
|
||||||
|
|
||||||
app.get("/register/", (req: Request, res: Response) => {
|
|
||||||
res.send(generateKey());
|
|
||||||
});
|
|
||||||
|
|
||||||
const port = process.env.PORT || 5000;
|
|
||||||
|
|
||||||
server.listen(port, () => {
|
|
||||||
console.log(`Server started on port ${port}`);
|
|
||||||
});
|
|
||||||
|
|
||||||
const rtmt = new RTMTWS();
|
const rtmt = new RTMTWS();
|
||||||
|
const gameStore = new GameStoreImpl();
|
||||||
|
const matchMaker = new MatchMakerImpl();
|
||||||
|
const gameManager = new GameManager({ rtmt, gameStore, matchMaker })
|
||||||
|
|
||||||
rtmt.initWebSocket(server, (userKey: string) => {
|
const expressApp = new ExpressApp();
|
||||||
const game = gameStore.get(userKey);
|
const server = new WebServer({expressApp});
|
||||||
if (game) {
|
|
||||||
rtmt.sendMessage(userKey, channel_on_game_update, game);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
const matchmaker = new MatchMaker();
|
rtmt.initWebSocket(server.server, (userKey: string) => gameManager.fireOnPlayerConnected(userKey));
|
||||||
|
|
||||||
rtmt.listenMessage(channel_new_game, (userKey: string, message: Object) => {
|
|
||||||
matchmaker.find(userKey);
|
|
||||||
});
|
|
||||||
|
|
||||||
const gameStore = new Map<string, MancalaGame>();
|
|
||||||
|
|
||||||
matchmaker.onPlayersPaired = (userKey1: string, userKey2: string) => {
|
|
||||||
const game = createMancalaGame(userKey1, userKey2);
|
|
||||||
|
|
||||||
rtmt.sendMessage(userKey1, channel_on_game_start, game);
|
|
||||||
rtmt.sendMessage(userKey2, channel_on_game_start, game);
|
|
||||||
};
|
|
||||||
|
|
||||||
rtmt.listenMessage(channel_game_move, (userKey: string, message: Object) => {
|
|
||||||
const gameMove: GameMove = message as GameMove;
|
|
||||||
|
|
||||||
const game = gameStore.get(userKey);
|
|
||||||
if (game) {
|
|
||||||
try {
|
|
||||||
game.moveByPlayerPit(userKey, gameMove.index);
|
|
||||||
rtmt.sendMessage(game.player1Id, channel_on_game_update, game);
|
|
||||||
rtmt.sendMessage(game.player2Id, channel_on_game_update, game);
|
|
||||||
if (game.state == "ended") {
|
|
||||||
gameStore.delete(game.player1Id);
|
|
||||||
gameStore.delete(game.player2Id);
|
|
||||||
}
|
|
||||||
} catch (err: any) {
|
|
||||||
console.error(err);
|
|
||||||
const crashFileName = GameCrashManager.logGameCrash(err, game);
|
|
||||||
console.info(`Game crash saved to file : ${crashFileName}`);
|
|
||||||
rtmt.sendMessage(game.player1Id, channel_on_game_crashed, err);
|
|
||||||
rtmt.sendMessage(game.player2Id, channel_on_game_crashed, err);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
console.log("Game not found!");
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
rtmt.listenMessage(channel_leave_game, (userKey: string, message: Object) => {
|
|
||||||
const game = gameStore.get(userKey);
|
|
||||||
if (game) {
|
|
||||||
deleteGame(game);
|
|
||||||
rtmt.sendMessage(game.player1Id, channel_on_game_user_leave, userKey);
|
|
||||||
rtmt.sendMessage(game.player2Id, channel_on_game_user_leave, userKey);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
const deleteGame = (game: MancalaGame) => {
|
|
||||||
if (game) {
|
|
||||||
gameStore.delete(game.player1Id);
|
|
||||||
gameStore.delete(game.player2Id);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
function createMancalaGame(userKey1: string, userKey2: string) {
|
|
||||||
const game = new CommonMancalaGame(generateKey(), userKey1, userKey2);
|
|
||||||
const random = Math.random();
|
|
||||||
game.turnPlayerId = random > 0.5 ? userKey1 : userKey2;
|
|
||||||
|
|
||||||
gameStore.set(userKey1, game);
|
|
||||||
gameStore.set(userKey2, game);
|
|
||||||
return game;
|
|
||||||
}
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user