2021-06-27 01:41:52 +03:00
|
|
|
import express, { Request, Response } from "express";
|
2022-05-04 23:18:29 +03:00
|
|
|
import * as http from "http";
|
2021-06-27 19:28:09 +03:00
|
|
|
import { RTMTWS } from "./rtmt/rtmt_websocket";
|
2022-05-04 23:18:29 +03:00
|
|
|
import cors from "cors";
|
2021-06-27 19:28:09 +03:00
|
|
|
import { generateKey } from "./key_factory";
|
2021-06-29 03:26:12 +03:00
|
|
|
import { MatchMaker } from "./matcmaker";
|
2022-05-04 23:18:29 +03:00
|
|
|
import { CommonMancalaGame, MancalaGame } from "mancala.js";
|
2022-05-05 23:56:02 +03:00
|
|
|
import fs from "fs";
|
2022-05-04 23:18:29 +03:00
|
|
|
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 "./channel_names";
|
|
|
|
|
import morgan from "morgan";
|
|
|
|
|
import { GameMove } from "./models/GameMove";
|
2021-06-27 01:41:52 +03:00
|
|
|
|
|
|
|
|
const app = express();
|
|
|
|
|
|
2022-04-11 22:25:49 +03:00
|
|
|
app.use(cors());
|
2022-05-05 23:56:02 +03:00
|
|
|
app.use(
|
|
|
|
|
morgan("common", {
|
|
|
|
|
stream: fs.createWriteStream("./access.log", { flags: "a" }),
|
|
|
|
|
})
|
|
|
|
|
);
|
2022-05-04 23:18:29 +03:00
|
|
|
app.use(morgan("dev"));
|
2021-06-27 02:45:40 +03:00
|
|
|
|
2021-06-27 19:28:09 +03:00
|
|
|
const server = http.createServer(app);
|
2021-06-27 02:45:40 +03:00
|
|
|
|
2021-06-27 01:41:52 +03:00
|
|
|
app.get("/", (req: Request, res: Response) => {
|
2022-04-11 22:25:49 +03:00
|
|
|
res.send("Server up and running!");
|
2021-06-27 01:41:52 +03:00
|
|
|
});
|
|
|
|
|
|
2021-06-27 19:28:09 +03:00
|
|
|
app.get("/register/", (req: Request, res: Response) => {
|
2021-06-29 03:26:12 +03:00
|
|
|
res.send(generateKey());
|
|
|
|
|
});
|
2021-06-27 19:28:09 +03:00
|
|
|
|
2022-05-04 23:18:29 +03:00
|
|
|
const port = process.env.PORT || 5000;
|
2021-06-27 02:45:40 +03:00
|
|
|
|
|
|
|
|
server.listen(port, () => {
|
2022-04-11 22:25:49 +03:00
|
|
|
console.log(`Server started on port ${port}`);
|
2022-05-04 23:18:29 +03:00
|
|
|
});
|
2021-06-27 19:28:09 +03:00
|
|
|
|
2022-05-04 23:18:29 +03:00
|
|
|
const rtmt = new RTMTWS();
|
2021-06-27 19:28:09 +03:00
|
|
|
|
2021-06-30 19:27:38 +03:00
|
|
|
rtmt.initWebSocket(server, (userKey: string) => {
|
2022-05-04 23:18:29 +03:00
|
|
|
const game = gameStore.get(userKey);
|
2021-06-30 19:27:38 +03:00
|
|
|
if (game) {
|
2022-05-04 23:18:29 +03:00
|
|
|
rtmt.sendMessage(userKey, channel_on_game_update, game);
|
2021-06-29 03:26:12 +03:00
|
|
|
}
|
2022-05-04 23:18:29 +03:00
|
|
|
});
|
2021-06-29 03:26:12 +03:00
|
|
|
|
2022-05-04 23:18:29 +03:00
|
|
|
const matchmaker = new MatchMaker();
|
2021-06-29 03:26:12 +03:00
|
|
|
|
2022-04-23 12:54:56 +03:00
|
|
|
rtmt.listenMessage(channel_new_game, (userKey: string, message: Object) => {
|
2022-05-04 23:18:29 +03:00
|
|
|
matchmaker.find(userKey);
|
|
|
|
|
});
|
2021-06-29 03:26:12 +03:00
|
|
|
|
2022-05-04 23:18:29 +03:00
|
|
|
const gameStore = new Map<string, MancalaGame>();
|
2021-06-29 03:26:12 +03:00
|
|
|
|
|
|
|
|
matchmaker.onPlayersPaired = (userKey1: string, userKey2: string) => {
|
2022-05-06 00:07:41 +03:00
|
|
|
const game = createMancalaGame(userKey1, userKey2);
|
2021-06-29 03:26:12 +03:00
|
|
|
|
2022-05-04 23:18:29 +03:00
|
|
|
rtmt.sendMessage(userKey1, channel_on_game_start, game);
|
|
|
|
|
rtmt.sendMessage(userKey2, channel_on_game_start, game);
|
|
|
|
|
};
|
2021-06-29 03:26:12 +03:00
|
|
|
|
2022-04-23 12:54:56 +03:00
|
|
|
rtmt.listenMessage(channel_game_move, (userKey: string, message: Object) => {
|
|
|
|
|
const gameMove: GameMove = message as GameMove;
|
2022-05-04 23:18:29 +03:00
|
|
|
|
|
|
|
|
const game = gameStore.get(userKey);
|
2021-06-29 03:26:12 +03:00
|
|
|
if (game) {
|
2021-07-04 00:44:15 +03:00
|
|
|
try {
|
2022-05-04 23:18:29 +03:00
|
|
|
game.moveByPlayerPit(userKey, gameMove.index);
|
|
|
|
|
rtmt.sendMessage(game.player1Id, channel_on_game_update, game);
|
|
|
|
|
rtmt.sendMessage(game.player2Id, channel_on_game_update, game);
|
2021-07-04 00:44:15 +03:00
|
|
|
|
|
|
|
|
if (game.state == "ended") {
|
2022-05-04 23:18:29 +03:00
|
|
|
gameStore.delete(game.player1Id);
|
|
|
|
|
gameStore.delete(game.player2Id);
|
2021-07-04 00:44:15 +03:00
|
|
|
}
|
2022-05-04 23:18:29 +03:00
|
|
|
} catch (err: any) {
|
2022-05-06 00:07:41 +03:00
|
|
|
console.error(err);
|
2022-05-04 23:18:29 +03:00
|
|
|
rtmt.sendMessage(game.player1Id, channel_on_game_crashed, err);
|
|
|
|
|
rtmt.sendMessage(game.player2Id, channel_on_game_crashed, err);
|
2021-06-30 19:27:38 +03:00
|
|
|
}
|
2021-06-29 03:26:12 +03:00
|
|
|
} else {
|
|
|
|
|
console.log("Game not found!");
|
|
|
|
|
}
|
2022-05-04 23:18:29 +03:00
|
|
|
});
|
2021-06-27 19:28:09 +03:00
|
|
|
|
2022-04-23 12:54:56 +03:00
|
|
|
rtmt.listenMessage(channel_leave_game, (userKey: string, message: Object) => {
|
2022-05-04 23:18:29 +03:00
|
|
|
const game = gameStore.get(userKey);
|
2021-06-30 19:27:38 +03:00
|
|
|
if (game) {
|
2022-05-04 23:18:29 +03:00
|
|
|
deleteGame(game);
|
|
|
|
|
rtmt.sendMessage(game.player1Id, channel_on_game_user_leave, userKey);
|
|
|
|
|
rtmt.sendMessage(game.player2Id, channel_on_game_user_leave, userKey);
|
2021-06-29 03:26:12 +03:00
|
|
|
}
|
2022-05-04 23:18:29 +03:00
|
|
|
});
|
2021-07-04 00:44:15 +03:00
|
|
|
|
2022-05-04 23:18:29 +03:00
|
|
|
const deleteGame = (game: MancalaGame) => {
|
2021-07-04 00:44:15 +03:00
|
|
|
if (game) {
|
2022-05-04 23:18:29 +03:00
|
|
|
gameStore.delete(game.player1Id);
|
|
|
|
|
gameStore.delete(game.player2Id);
|
2021-07-04 00:44:15 +03:00
|
|
|
}
|
2022-05-04 23:18:29 +03:00
|
|
|
};
|
2022-05-06 00:07:41 +03:00
|
|
|
|
|
|
|
|
function createMancalaGame(userKey1: string, userKey2: string) {
|
|
|
|
|
const game = new CommonMancalaGame(generateKey(), userKey1, userKey2);
|
|
|
|
|
gameStore.set(userKey1, game);
|
|
|
|
|
gameStore.set(userKey2, game);
|
|
|
|
|
return game;
|
|
|
|
|
}
|