mancala/src/index.ts

104 lines
3.1 KiB
TypeScript
Raw Normal View History

2021-06-27 01:41:52 +03:00
import express, { Request, Response } from "express";
2021-06-27 02:45:40 +03:00
import * as http from 'http';
2021-06-29 03:26:12 +03:00
import { decodeText, encodeText } from "./rtmt/byte_util";
2021-06-27 19:28:09 +03:00
import { RTMTWS } from "./rtmt/rtmt_websocket";
import cors from "cors"
import { generateKey } from "./key_factory";
2021-06-29 03:26:12 +03:00
import { MatchMaker } from "./matcmaker";
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";
2021-06-29 03:26:12 +03:00
import { Bytes } from "./rtmt/rtmt";
import { createGame, Game, GameMove } from "./mancala";
2022-04-11 22:25:49 +03:00
import morgan from 'morgan';
2021-06-27 01:41:52 +03:00
const app = express();
2022-04-11 22:25:49 +03:00
app.use(cors());
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
2021-06-27 02:45:40 +03:00
const port = process.env.PORT || 5000
server.listen(port, () => {
2022-04-11 22:25:49 +03:00
console.log(`Server started on port ${port}`);
2021-06-27 02:45:40 +03:00
})
2021-06-27 19:28:09 +03:00
const rtmt = new RTMTWS()
2021-06-30 19:27:38 +03:00
rtmt.initWebSocket(server, (userKey: string) => {
2021-06-29 03:26:12 +03:00
const game = gameStore.get(userKey)
2021-06-30 19:27:38 +03:00
if (game) {
2021-06-29 03:26:12 +03:00
rtmt.sendMessage(userKey, channel_on_game_update, encodeText(JSON.stringify(game)))
}
})
const matchmaker = new MatchMaker()
rtmt.listenMessage(channel_new_game, (userKey: string, message: Bytes) => {
matchmaker.find(userKey)
})
const gameStore = new Map<string, Game>()
matchmaker.onPlayersPaired = (userKey1: string, userKey2: string) => {
const game = createGame(userKey1, userKey2)
gameStore.set(userKey1, game)
gameStore.set(userKey2, game)
2021-06-30 19:27:38 +03:00
const data = encodeText(JSON.stringify(game))
rtmt.sendMessage(userKey1, channel_on_game_start, data)
rtmt.sendMessage(userKey2, channel_on_game_start, data)
2021-06-29 03:26:12 +03:00
}
rtmt.listenMessage(channel_game_move, (userKey: string, message: Bytes) => {
const gameMove: GameMove = JSON.parse(decodeText(message))
const game = gameStore.get(userKey)
if (game) {
try {
game.moveByIndex(gameMove.index, game.getPlayerNameByKey(userKey))
const data = encodeText(JSON.stringify(game))
rtmt.sendMessage(game.player1, channel_on_game_update, data)
rtmt.sendMessage(game.player2, channel_on_game_update, data)
if (game.state == "ended") {
gameStore.delete(game.player1)
gameStore.delete(game.player2)
}
2022-04-09 21:11:51 +03:00
} catch (err : any) {
console.log(err);
deleteGame(game)
const data = encodeText(err.toString())
rtmt.sendMessage(game.player1, channel_on_game_crashed, data)
rtmt.sendMessage(game.player2, channel_on_game_crashed, data)
2021-06-30 19:27:38 +03:00
}
2021-06-29 03:26:12 +03:00
} else {
console.log("Game not found!");
}
})
2021-06-27 19:28:09 +03:00
2021-06-29 03:26:12 +03:00
rtmt.listenMessage(channel_leave_game, (userKey: string, message: Bytes) => {
const game = gameStore.get(userKey)
2021-06-30 19:27:38 +03:00
if (game) {
deleteGame(game)
const data = encodeText(userKey)
rtmt.sendMessage(game.player1, channel_on_game_user_leave, data)
rtmt.sendMessage(game.player2, channel_on_game_user_leave, data)
2021-06-29 03:26:12 +03:00
}
2021-06-30 19:27:38 +03:00
})
const deleteGame = (game: Game) => {
if (game) {
gameStore.delete(game.player1)
gameStore.delete(game.player2)
}
}