add GameCrashManager
This commit is contained in:
parent
e75eaffd50
commit
2f8a95a971
4
.gitignore
vendored
4
.gitignore
vendored
@ -38,4 +38,6 @@ Thumbs.db
|
||||
dist/**/*
|
||||
|
||||
# ignore yarn.lock
|
||||
yarn.lock
|
||||
yarn.lock
|
||||
|
||||
crashes
|
||||
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,6 +17,7 @@ import {
|
||||
} from "./channel_names";
|
||||
import morgan from "morgan";
|
||||
import { GameMove } from "./models/GameMove";
|
||||
import { GameCrashManager } from "./GameCrashManager";
|
||||
|
||||
const app = express();
|
||||
|
||||
@ -77,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.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);
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user