mirror of
https://github.com/AlecM33/Werewolf.git
synced 2025-12-26 07:47:50 +01:00
add some error handling
This commit is contained in:
@@ -3,7 +3,6 @@ const router = express.Router();
|
||||
const debugMode = Array.from(process.argv.map((arg) => arg.trim().toLowerCase())).includes('debug');
|
||||
const logger = require('../modules/Logger')(debugMode);
|
||||
const eventManager = (require('../modules/singletons/EventManager.js')).instance;
|
||||
const timerManager = (require('../modules/singletons/TimerManager.js')).instance;
|
||||
const globals = require('../config/globals.js');
|
||||
const cors = require('cors');
|
||||
|
||||
@@ -24,7 +23,17 @@ router.get('/games/state', async (req, res) => {
|
||||
const gamesArray = [];
|
||||
const keys = await eventManager.client.keys('*');
|
||||
const values = await eventManager.client.mGet(keys);
|
||||
values.forEach((v) => gamesArray.push(JSON.parse(v)));
|
||||
values.forEach((v) => {
|
||||
let parsedGame;
|
||||
try {
|
||||
parsedGame = JSON.parse(v);
|
||||
} catch (e) {
|
||||
logger.error(e);
|
||||
}
|
||||
if (parsedGame) {
|
||||
gamesArray.push(parsedGame);
|
||||
}
|
||||
});
|
||||
res.status(200).send(gamesArray);
|
||||
});
|
||||
|
||||
|
||||
@@ -26,12 +26,18 @@ class EventManager {
|
||||
|
||||
createRedisPublisher = async () => {
|
||||
this.publisher = redis.createClient();
|
||||
this.publisher.on('error', (e) => {
|
||||
this.logger.error('REDIS PUBLISHER CLIENT ERROR:', e);
|
||||
});
|
||||
await this.publisher.connect();
|
||||
this.logger.info('EVENT MANAGER - CREATED PUBLISHER');
|
||||
}
|
||||
|
||||
createGameSyncSubscriber = async (gameManager, eventManager) => {
|
||||
this.subscriber = this.client.duplicate();
|
||||
this.subscriber.on('error', (e) => {
|
||||
this.logger.error('REDIS SUBSCRIBER CLIENT ERROR:', e);
|
||||
});
|
||||
await this.subscriber.connect();
|
||||
await this.subscriber.subscribe(globals.REDIS_CHANNELS.ACTIVE_GAME_STREAM, async (message) => {
|
||||
this.logger.debug('MESSAGE: ' + message);
|
||||
@@ -46,8 +52,8 @@ class EventManager {
|
||||
message.slice(
|
||||
message.indexOf(messageComponents[messageComponents.length - 1]) + (globals.INSTANCE_ID_LENGTH + 1)
|
||||
)
|
||||
)
|
||||
} catch(e) {
|
||||
);
|
||||
} catch (e) {
|
||||
this.logger.error('MALFORMED MESSAGE RESULTED IN ERROR: ' + e + '; DISREGARDING');
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -28,7 +28,15 @@ class GameManager {
|
||||
}
|
||||
delete this.timerManager.timerThreads[accessCode];
|
||||
}
|
||||
return r === null ? r : JSON.parse(r);
|
||||
let activeGame;
|
||||
if (r !== null) {
|
||||
try {
|
||||
activeGame = JSON.parse(r);
|
||||
} catch (e) {
|
||||
this.logger.error('ERROR PARSING ACTIVE GAME: ' + e);
|
||||
}
|
||||
}
|
||||
return r === null ? r : activeGame;
|
||||
}
|
||||
|
||||
setGameSocketNamespace = (namespace) => {
|
||||
@@ -62,8 +70,8 @@ class GameManager {
|
||||
moderator.assigned = true;
|
||||
if (req.timerParams !== null) {
|
||||
req.timerParams.paused = true;
|
||||
req.timerParams.timeRemaining = convertFromHoursToMilliseconds(req.timerParams.hours)
|
||||
+ convertFromMinutesToMilliseconds(req.timerParams.minutes);
|
||||
req.timerParams.timeRemaining = convertFromHoursToMilliseconds(req.timerParams.hours) +
|
||||
convertFromMinutesToMilliseconds(req.timerParams.minutes);
|
||||
}
|
||||
const newGame = new Game(
|
||||
newAccessCode,
|
||||
@@ -251,8 +259,8 @@ class GameManager {
|
||||
game.status = globals.STATUS.IN_PROGRESS;
|
||||
if (game.hasTimer) {
|
||||
game.timerParams.paused = true;
|
||||
game.timerParams.timeRemaining = convertFromHoursToMilliseconds(game.timerParams.hours)
|
||||
+ convertFromMinutesToMilliseconds(game.timerParams.minutes);
|
||||
game.timerParams.timeRemaining = convertFromHoursToMilliseconds(game.timerParams.hours) +
|
||||
convertFromMinutesToMilliseconds(game.timerParams.minutes);
|
||||
await this.timerManager.runTimer(game, namespace, this.eventManager, this);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user