UNPKG

maniajs

Version:

ManiaPlanet (Dedicated) Server Controller.

159 lines (125 loc) 5.44 kB
/** * Callback Manager for the GBX Client. * * Abstract away the ManiaPlanet callbacks. */ 'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); var _util = require('util'); var util = _interopRequireWildcard(_util); var _maniaplanetCallbacks = require('./callbacks/maniaplanet-callbacks'); var _maniaplanetCallbacks2 = _interopRequireDefault(_maniaplanetCallbacks); var _trackmaniaCallbacks = require('./callbacks/trackmania-callbacks'); var _trackmaniaCallbacks2 = _interopRequireDefault(_trackmaniaCallbacks); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } /** * CallbackManager * @class CallbackManager * @type {CallbackManager} */ var CallbackManager = function () { /** * @param {App} app * @param {ServerClient} client */ function CallbackManager(app, client) { _classCallCheck(this, CallbackManager); this.app = app; this.client = client; } /** * Register a GBX callback, make it a normal event. * * @param {object} options Provide gbx and event details. * @param {string} options.callback Callback name that will be fired by the GBX Client. * @param {string} options.event Converting into event name. * @param {string} options.type Type of callback, could be 'native' or 'scripted'. Native is default. * @param {object} options.parameters Parameters mapping. * @param {function} options.parse Custom parse mapping function. * @param {function} options.pass Custom pass function. Optional! Give false as return to ignore the callback! * @param {object} options.game Optional game strings. Provide the titleid of the parent title (the game title). * * @param {function} options.flow Optional promise returning funciton for controlling the game flow. Calls with first parameter app and second params. */ _createClass(CallbackManager, [{ key: 'register', value: function register(options) { var _this = this; var callbackName = options.callback; var eventName = options.event; var parameters = options.parameters || {}; var parse = options.parse; var pass = options.pass; var flow = options.flow; var type = options.type || 'native'; var game = options.game || []; // Default all games // Register callback, make it an event. this.client.gbx.on(callbackName, function (rawParams) { // Output var var params = {}; // Check if we can continue first. if (typeof pass === 'function') { if (pass(rawParams) === false) { // Skip return; } } // If we have custom parser, then call it, if not we go with the mapping. if (typeof parse === 'function') { params = parse(rawParams); } else { params = util._extend({}, parameters); // (clone it). var paramKeys = Object.keys(parameters); for (var i = 0; i < paramKeys.length; i++) { var key = paramKeys[i]; var num = parameters[paramKeys[i]]; // Do we have that num in our rawParams? if (num < rawParams.length) { // Allrighty! // Map it. params[key] = rawParams[num]; } } } // Maybe we want to pass it to the game flow manager before we want to give // the event to the plugins and core (for player connect for example). if (typeof flow !== 'function') { flow = function flow() { return Promise.resolve(); }; } // Promise the flow. flow(_this.app, params).then(function () { // Trigger the event on our client _this.client.emit(eventName, params); }).catch(function (err) { _this.app.log.warn(err.stack); }); }); } /** * Load Set from specific prefixed sets. * * @param {string} name For example: 'maniaplanet' */ }, { key: 'loadSet', value: function loadSet(name) { switch (name) { case 'maniaplanet': (0, _maniaplanetCallbacks2.default)(this);break; case 'trackmania': (0, _trackmaniaCallbacks2.default)(this);break; default: return; } } }]); return CallbackManager; }(); exports.default = CallbackManager;