migrate to mancala.js

This commit is contained in:
Halit Aksoy 2022-05-04 23:18:29 +03:00
parent c18503a132
commit e34137c68c
3 changed files with 6646 additions and 977 deletions

7510
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -20,6 +20,7 @@
"@types/ws": "^7.4.5",
"cors": "^2.8.5",
"express": "^4.17.1",
"mancala.js": "^0.0.1",
"morgan": "^1.10.0",
"uuid": "^8.3.2",
"ws": "^7.5.0"

View File

@ -1,19 +1,26 @@
import express, { Request, Response } from "express";
import * as http from 'http';
import { decodeText, encodeText } from "./rtmt/byte_util";
import * as http from "http";
import { RTMTWS } from "./rtmt/rtmt_websocket";
import cors from "cors"
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_crashed, channel_on_game_start, channel_on_game_update, channel_on_game_user_leave } from "./channel_names";
import { Bytes } from "./rtmt/rtmt";
import { createGame, Game, GameMove } from "./mancala";
import morgan from 'morgan';
import { CommonMancalaGame, MancalaGame } from "mancala.js";
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";
const app = express();
app.use(cors());
app.use(morgan('dev'));
app.use(morgan("dev"));
const server = http.createServer(app);
@ -25,78 +32,75 @@ app.get("/register/", (req: Request, res: Response) => {
res.send(generateKey());
});
const port = process.env.PORT || 5000
const port = process.env.PORT || 5000;
server.listen(port, () => {
console.log(`Server started on port ${port}`);
})
});
const rtmt = new RTMTWS()
const rtmt = new RTMTWS();
rtmt.initWebSocket(server, (userKey: string) => {
const game = gameStore.get(userKey)
const game = gameStore.get(userKey);
if (game) {
rtmt.sendMessage(userKey, channel_on_game_update, game)
rtmt.sendMessage(userKey, channel_on_game_update, game);
}
})
});
const matchmaker = new MatchMaker()
const matchmaker = new MatchMaker();
rtmt.listenMessage(channel_new_game, (userKey: string, message: Object) => {
matchmaker.find(userKey)
})
matchmaker.find(userKey);
});
const gameStore = new Map<string, Game>()
const gameStore = new Map<string, MancalaGame>();
matchmaker.onPlayersPaired = (userKey1: string, userKey2: string) => {
console.log('onPlayersPaired');
const game = createGame(userKey1, userKey2)
gameStore.set(userKey1, game)
gameStore.set(userKey2, game)
const game = new CommonMancalaGame(userKey1, userKey2);
gameStore.set(userKey1, game);
gameStore.set(userKey2, game);
rtmt.sendMessage(userKey1, channel_on_game_start, game)
rtmt.sendMessage(userKey2, channel_on_game_start, game)
}
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;
console.log(gameMove);
const game = gameStore.get(userKey)
const game = gameStore.get(userKey);
if (game) {
try {
game.moveByIndex(gameMove.index, game.getPlayerNameByKey(userKey))
rtmt.sendMessage(game.player1, channel_on_game_update, game)
rtmt.sendMessage(game.player2, channel_on_game_update, game)
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.player1)
gameStore.delete(game.player2)
gameStore.delete(game.player1Id);
gameStore.delete(game.player2Id);
}
} catch (err : any) {
} catch (err: any) {
console.log(err);
deleteGame(game)
rtmt.sendMessage(game.player1, channel_on_game_crashed, err)
rtmt.sendMessage(game.player2, channel_on_game_crashed, err)
deleteGame(game);
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)
const game = gameStore.get(userKey);
if (game) {
deleteGame(game)
rtmt.sendMessage(game.player1, channel_on_game_user_leave, userKey)
rtmt.sendMessage(game.player2, channel_on_game_user_leave, userKey)
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: Game) => {
const deleteGame = (game: MancalaGame) => {
if (game) {
gameStore.delete(game.player1)
gameStore.delete(game.player2)
gameStore.delete(game.player1Id);
gameStore.delete(game.player2Id);
}
}
};