mirror of
https://github.com/AlecM33/Werewolf.git
synced 2025-12-26 15:57:50 +01:00
some timer logic
This commit is contained in:
@@ -1,90 +1,49 @@
|
||||
const { fork } = require('child_process');
|
||||
const path = require('path');
|
||||
|
||||
const logger = require('./logger')(false);
|
||||
const globals = require('../config/globals');
|
||||
|
||||
class ActiveGameRunner {
|
||||
constructor () {
|
||||
constructor (logger) {
|
||||
this.activeGames = {};
|
||||
this.logger = logger;
|
||||
}
|
||||
|
||||
// runGame = (game, namespace, gameStateFn) => {
|
||||
// logger.debug('running game ' + game.accessCode);
|
||||
// const gameProcess = fork(path.join(__dirname, '/GameProcess.js'));
|
||||
// gameProcess.on('message', (msg) => {
|
||||
// switch (msg.command) {
|
||||
// case serverGlobals.COMMAND.END_COUNTDOWN:
|
||||
// logger.debug('GAME PARENT PROCESS ' + game.accessCode + ': COMMAND: END COUNTDOWN');
|
||||
// namespace.in(game.accessCode).emit(serverGlobals.COMMAND.END_COUNTDOWN);
|
||||
// gameProcess.send({
|
||||
// command: serverGlobals.COMMAND.START_GAME,
|
||||
// cycleNumber: game.words.length - 1,
|
||||
// cycleLength: game.timePerWord * 1000,
|
||||
// accessCode: game.accessCode
|
||||
// });
|
||||
// break;
|
||||
// case serverGlobals.COMMAND.START_GAME:
|
||||
// game.status = serverGlobals.GAME_STATE.STARTED;
|
||||
// game.lastCycleTime = new Date().toJSON();
|
||||
// logger.debug('GAME PARENT PROCESS ' + game.accessCode + ': COMMAND: START GAME');
|
||||
// namespace.in(game.accessCode).emit(serverGlobals.COMMAND.START_GAME, {
|
||||
// firstWord: game.words[0].baseword,
|
||||
// gameLength: game.words.length,
|
||||
// timePerWord: game.timePerWord * 1000
|
||||
// });
|
||||
// break;
|
||||
// case serverGlobals.COMMAND.CYCLE_WORD:
|
||||
// game.currentWordIndex += 1;
|
||||
// game.lastCycleTime = new Date().toJSON();
|
||||
// logger.debug('GAME PARENT PROCESS ' + game.accessCode + ': COMMAND: CYCLE WORD');
|
||||
// if (game.currentWordIndex < game.words.length) {
|
||||
// namespace.in(game.accessCode).emit(serverGlobals.COMMAND.CYCLE_WORD, {
|
||||
// word: game.words[game.currentWordIndex].baseword,
|
||||
// index: game.currentWordIndex + 1,
|
||||
// totalTime: game.timePerWord * 1000,
|
||||
// gameLength: game.words.length
|
||||
// });
|
||||
// }
|
||||
// gameProcess.send({
|
||||
// command: serverGlobals.COMMAND.CYCLE_WORD,
|
||||
// cycleIndex: game.currentWordIndex,
|
||||
// cycleLength: game.timePerWord * 1000,
|
||||
// accessCode: game.accessCode,
|
||||
// gameLength: game.words.length
|
||||
// });
|
||||
// break;
|
||||
// case serverGlobals.COMMAND.END_GAME:
|
||||
// game.status = serverGlobals.GAME_STATE.ENDED;
|
||||
// if (!game.posted) {
|
||||
// logger.debug('GAME PARENT PROCESS: GAME ' + game.accessCode + ' HAS ENDED...BEGINNING POST TO DATABASE');
|
||||
// this.postGameFn(game).then(() => {
|
||||
// game.posted = true;
|
||||
// logger.debug('GAME ' + game.accessCode + ' SUCCESSFULLY POSTED');
|
||||
// namespace.in(game.accessCode).emit(serverGlobals.COMMAND.END_GAME, game.accessCode);
|
||||
// });
|
||||
// }
|
||||
// break;
|
||||
// }
|
||||
// });
|
||||
//
|
||||
// gameProcess.on('exit', () => {
|
||||
// if (this.activeGames[game.accessCode]) {
|
||||
// delete this.activeGames[game.accessCode];
|
||||
// logger.debug('GAME ' + game.accessCode + ' REMOVED FROM ACTIVE GAMES.');
|
||||
// }
|
||||
// });
|
||||
// gameProcess.send({ command: serverGlobals.COMMAND.START_COUNTDOWN, accessCode: game.accessCode });
|
||||
// game.status = serverGlobals.GAME_STATE.STARTING;
|
||||
// game.startCountdownTime = new Date().toJSON();
|
||||
// namespace.in(game.accessCode).emit(serverGlobals.COMMAND.START_COUNTDOWN);
|
||||
// }
|
||||
/* We're only going to fork a child process for games with a timer. They will report back to the parent process whenever
|
||||
the timer is up.
|
||||
*/
|
||||
runGame = (game, namespace) => {
|
||||
this.logger.debug('running game ' + game.accessCode);
|
||||
const gameProcess = fork(path.join(__dirname, '/GameProcess.js'));
|
||||
gameProcess.on('message', (msg) => {
|
||||
switch (msg.command) {
|
||||
case globals.GAME_PROCESS_COMMANDS.END_GAME:
|
||||
game.status = globals.STATUS.ENDED;
|
||||
this.logger.debug('PARENT: END GAME');
|
||||
namespace.in(game.accessCode).emit(globals.GAME_PROCESS_COMMANDS.END_GAME, game.accessCode);
|
||||
break;
|
||||
}
|
||||
});
|
||||
|
||||
gameProcess.on('exit', () => {
|
||||
this.logger.debug('Game ' + game.accessCode + ' has ended. Elapsed: ' + (new Date() - game.startTime) + 'ms');
|
||||
});
|
||||
gameProcess.send({
|
||||
command: globals.GAME_PROCESS_COMMANDS.START_TIMER,
|
||||
accessCode: game.accessCode,
|
||||
logLevel: this.logger.logLevel,
|
||||
hours: game.timerParams.hours,
|
||||
minutes: game.timerParams.minutes
|
||||
});
|
||||
game.startTime = new Date().toJSON();
|
||||
namespace.in(game.accessCode).emit(globals.GAME_PROCESS_COMMANDS.START_TIMER);
|
||||
}
|
||||
}
|
||||
|
||||
class Singleton {
|
||||
constructor () {
|
||||
constructor (logger) {
|
||||
if (!Singleton.instance) {
|
||||
logger.log('CREATING SINGLETON ACTIVE GAME RUNNER');
|
||||
Singleton.instance = new ActiveGameRunner();
|
||||
Singleton.instance = new ActiveGameRunner(logger);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user