lance-pro
Version:
Pro Tools for Lance multiplayer game server
39 lines (32 loc) • 1.45 kB
JavaScript
'use strict';
const Utils = require('../lib/utils.js');
const uuidV4 = require('uuid/v4');
const verbose = true;
class StatsCollector {
constructor(gameEngine) {
this.meta = {
game: process.env.LANCE_GAME,
gamever: process.env.LANCE_GAMEVER,
deploy: process.env.LANCE_DEPLOY
};
// this.statsServer = process.env.LANCE_STATS_SERVER;
this.statsServer = 'https://bjja3lmjme.execute-api.us-west-2.amazonaws.com/prod/';
this.enabled = this.meta.game && this.meta.gamever && this.statsServer;
gameEngine.on('server__start', this.report.bind(this, 'deploy'));
gameEngine.on('gameStart', this.report.bind(this, 'gamerun'));
gameEngine.on('server__gameStats', this.report.bind(this, 'gamerunhour'));
gameEngine.on('server__playerJoined', this.report.bind(this, 'playersession'));
gameEngine.on('server__playerDisconnected', this.report.bind(this, 'playersession'));
}
report(eventName, eventData) {
if (!this.enabled)
return;
Object.assign(eventData, this.meta);
eventData.id = eventData.id || uuidV4();
const serverPath = this.statsServer + eventName;
if (verbose)
console.log(`reporting event to [${serverPath}].\n[${eventName}]:${JSON.stringify(eventData, null, 4)}`);
Utils.post(serverPath, eventData);
}
}
module.exports = StatsCollector;