add GameCrashManager

This commit is contained in:
Halit Aksoy 2022-05-06 00:46:32 +03:00
parent e75eaffd50
commit 2f8a95a971
3 changed files with 43 additions and 2 deletions

2
.gitignore vendored
View File

@ -39,3 +39,5 @@ dist/**/*
# ignore yarn.lock # ignore yarn.lock
yarn.lock yarn.lock
crashes

37
src/GameCrashManager.ts Normal file
View 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;
}
}

View File

@ -17,6 +17,7 @@ import {
} from "./channel_names"; } from "./channel_names";
import morgan from "morgan"; import morgan from "morgan";
import { GameMove } from "./models/GameMove"; import { GameMove } from "./models/GameMove";
import { GameCrashManager } from "./GameCrashManager";
const app = express(); const app = express();
@ -77,13 +78,14 @@ rtmt.listenMessage(channel_game_move, (userKey: string, message: Object) => {
game.moveByPlayerPit(userKey, gameMove.index); game.moveByPlayerPit(userKey, gameMove.index);
rtmt.sendMessage(game.player1Id, channel_on_game_update, game); rtmt.sendMessage(game.player1Id, channel_on_game_update, game);
rtmt.sendMessage(game.player2Id, channel_on_game_update, game); rtmt.sendMessage(game.player2Id, channel_on_game_update, game);
if (game.state == "ended") { if (game.state == "ended") {
gameStore.delete(game.player1Id); gameStore.delete(game.player1Id);
gameStore.delete(game.player2Id); gameStore.delete(game.player2Id);
} }
} catch (err: any) { } catch (err: any) {
console.error(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.player1Id, channel_on_game_crashed, err);
rtmt.sendMessage(game.player2Id, channel_on_game_crashed, err); rtmt.sendMessage(game.player2Id, channel_on_game_crashed, err);
} }