diff --git a/src/channel_names.ts b/src/channel_names.ts new file mode 100644 index 0000000..2324d47 --- /dev/null +++ b/src/channel_names.ts @@ -0,0 +1,6 @@ +export const channel_new_game = "new_game" +export const channel_on_game_start = "on_game_start" +export const channel_game_move = "game_move" +export const channel_on_game_update = "on_game_update" +export const channel_leave_game = "leave_game" +export const channel_on_game_end = "on_game_end" diff --git a/src/index.ts b/src/index.ts index 65e7dcb..66a2069 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,11 +1,15 @@ import express, { Request, Response } from "express"; import * as http from 'http'; import WebSocket from "ws" -import { encodeText } from "./rtmt/byte_util"; +import { decodeText, encodeText } from "./rtmt/byte_util"; import { encode } from "./rtmt/encode_decode_message"; import { RTMTWS } from "./rtmt/rtmt_websocket"; import cors from "cors" import { generateKey } from "./key_factory"; +import { MatchMaker } from "./matcmaker"; +import { channel_game_move, channel_leave_game, channel_new_game, channel_on_game_start, channel_on_game_update } from "./channel_names"; +import { Bytes } from "./rtmt/rtmt"; +import { createGame, Game, GameMove } from "./mancala"; const app = express(); @@ -18,17 +22,58 @@ app.get("/", (req: Request, res: Response) => { }); app.get("/register/", (req: Request, res: Response) => { - res.send(generateKey()); - }); + res.send(generateKey()); +}); const port = process.env.PORT || 5000 server.listen(port, () => { - console.log(`Server started on port ${port} :)`); + console.log(`Server started on port ${port} :)`); }) const rtmt = new RTMTWS() -rtmt.initWebSocket(server, ()=>{ +rtmt.initWebSocket(server, (userKey : string) => { + const game = gameStore.get(userKey) + if(game){ + 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() + +matchmaker.onPlayersPaired = (userKey1: string, userKey2: string) => { + const game = createGame(userKey1, userKey2) + gameStore.set(userKey1, game) + gameStore.set(userKey2, game) + + rtmt.sendMessage(userKey1, channel_on_game_start, encodeText(JSON.stringify(game))) + rtmt.sendMessage(userKey2, channel_on_game_start, encodeText(JSON.stringify(game))) +} + +rtmt.listenMessage(channel_game_move, (userKey: string, message: Bytes) => { + const gameMove: GameMove = JSON.parse(decodeText(message)) + const game = gameStore.get(userKey) + if (game) { + game.moveByIndex(gameMove.index, game.getPlayerNameByKey(userKey)) + + rtmt.sendMessage(game.player1, channel_on_game_update, encodeText(JSON.stringify(game))) + rtmt.sendMessage(game.player2, channel_on_game_update, encodeText(JSON.stringify(game))) + } else { + console.log("Game not found!"); + } +}) + +rtmt.listenMessage(channel_leave_game, (userKey: string, message: Bytes) => { + const game = gameStore.get(userKey) + if(game){ + gameStore.delete(game.player1) + gameStore.delete(game.player2) + } }) \ No newline at end of file