Merge pull request #2 from jhalitaksoy/feature/mancala-js-0.0.2-beta.0
Feature/mancala js 0.0.2 beta.0
This commit is contained in:
commit
19ca9f74b8
4
.gitignore
vendored
4
.gitignore
vendored
@ -38,4 +38,6 @@ Thumbs.db
|
||||
dist/**/*
|
||||
|
||||
# ignore yarn.lock
|
||||
yarn.lock
|
||||
yarn.lock
|
||||
|
||||
crashes
|
||||
@ -20,7 +20,7 @@
|
||||
"@types/ws": "^7.4.5",
|
||||
"cors": "^2.8.5",
|
||||
"express": "^4.17.1",
|
||||
"mancala.js": "^0.0.1",
|
||||
"mancala.js": "^0.0.2-beta.0",
|
||||
"morgan": "^1.10.0",
|
||||
"uuid": "^8.3.2",
|
||||
"ws": "^7.5.0"
|
||||
|
||||
37
src/GameCrashManager.ts
Normal file
37
src/GameCrashManager.ts
Normal file
@ -0,0 +1,37 @@
|
||||
import { MancalaGame } from "mancala.js";
|
||||
import fs from "fs";
|
||||
const crashFolder = "./crashes";
|
||||
export class GameCrashManager {
|
||||
constructor() {}
|
||||
|
||||
static createCrashFolderIfNotExist(): void {
|
||||
if (!fs.existsSync(crashFolder)) {
|
||||
fs.mkdirSync(crashFolder);
|
||||
}
|
||||
}
|
||||
|
||||
static getTime(): string {
|
||||
let date_ob = new Date();
|
||||
let date = ("0" + date_ob.getDate()).slice(-2);
|
||||
let month = ("0" + (date_ob.getMonth() + 1)).slice(-2);
|
||||
let year = date_ob.getFullYear();
|
||||
let hours = date_ob.getHours();
|
||||
let minutes = date_ob.getMinutes();
|
||||
let seconds = date_ob.getSeconds();
|
||||
let miliSeconds = date_ob.getMilliseconds();
|
||||
return `${year}-${month}-${date}-${hours}:${minutes}:${seconds}:${miliSeconds}`;
|
||||
}
|
||||
|
||||
public static logGameCrash(error: Error, game: MancalaGame): string {
|
||||
this.createCrashFolderIfNotExist();
|
||||
const crash = {
|
||||
message: error.message,
|
||||
stack: error.stack,
|
||||
game,
|
||||
};
|
||||
const crashStr = JSON.stringify(crash);
|
||||
const crashFileName = `${game.id}::${this.getTime()}`;
|
||||
fs.writeFile(`${crashFolder}/${crashFileName}`, crashStr, () => {});
|
||||
return crashFileName;
|
||||
}
|
||||
}
|
||||
17
src/index.ts
17
src/index.ts
@ -17,6 +17,7 @@ import {
|
||||
} from "./channel_names";
|
||||
import morgan from "morgan";
|
||||
import { GameMove } from "./models/GameMove";
|
||||
import { GameCrashManager } from "./GameCrashManager";
|
||||
|
||||
const app = express();
|
||||
|
||||
@ -62,9 +63,7 @@ rtmt.listenMessage(channel_new_game, (userKey: string, message: Object) => {
|
||||
const gameStore = new Map<string, MancalaGame>();
|
||||
|
||||
matchmaker.onPlayersPaired = (userKey1: string, userKey2: string) => {
|
||||
const game = new CommonMancalaGame(userKey1, userKey2);
|
||||
gameStore.set(userKey1, game);
|
||||
gameStore.set(userKey2, game);
|
||||
const game = createMancalaGame(userKey1, userKey2);
|
||||
|
||||
rtmt.sendMessage(userKey1, channel_on_game_start, game);
|
||||
rtmt.sendMessage(userKey2, channel_on_game_start, game);
|
||||
@ -79,13 +78,14 @@ rtmt.listenMessage(channel_game_move, (userKey: string, message: Object) => {
|
||||
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.log(err);
|
||||
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);
|
||||
}
|
||||
@ -109,3 +109,10 @@ const deleteGame = (game: MancalaGame) => {
|
||||
gameStore.delete(game.player2Id);
|
||||
}
|
||||
};
|
||||
|
||||
function createMancalaGame(userKey1: string, userKey2: string) {
|
||||
const game = new CommonMancalaGame(generateKey(), userKey1, userKey2);
|
||||
gameStore.set(userKey1, game);
|
||||
gameStore.set(userKey2, game);
|
||||
return game;
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user