lance-pro
Version:
Pro Tools for Lance multiplayer game server
47 lines (37 loc) • 1.43 kB
JavaScript
;
const process = require('process');
const GAMESTATUS_PATH = '/gameStatus';
const DEFAULT_POLL_PERIOD = 1000;
class MatchMakerTarget {
/**
* Constructor of the MatchMaker singleton.
* @param {Object} expressApp - Reference to the express app
* @param {ServerEngine} serverEngine - Reference to the game's ServerEngine instance
* @param {Object} options - matchmaker options
* @param {Number} options.pollPeriod - gameserver polling interval in milliseconds. Default 10000
*/
constructor(expressApp, serverEngine, options) {
this.shutDownStarted = false;
this.serverEngine = serverEngine;
expressApp.get(GAMESTATUS_PATH, this.gameStatus.bind(this));
this.options = Object.assign({
pollPeriod: DEFAULT_POLL_PERIOD
}, options);
// delay shutdown gracefully
process.on('SIGINT', () => {
this.shutDownStarted = true;
console.log('\n\n\n\nSHUTTING DOWN - PLEASE WAIT\n\n\n\n');
setTimeout(() => {
console.log('\n\n\n\nSERVER SHUTDOWN COMPLETE');
process.exit();
}, this.options.pollPeriod * 1.5);
});
}
gameStatus(req, res) {
if (this.shutDownStarted)
res.sendStatus(404);
else
res.send(this.serverEngine.gameStatus(req.query));
}
}
module.exports = MatchMakerTarget;