maniajs
Version:
ManiaPlanet (Dedicated) Server Controller.
148 lines (122 loc) • 4.23 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj; };
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; }; }();
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
/**
* Send anything to the MP Server.
* Sending query builder.
*/
/**
* Global Send Queue. When using delayed sending.
* @type {Array}
* @todo Implement global queue, and runner.
*/
var globalQueue = exports.globalQueue = [];
/**
* Send
* @class Send
* @type {Send}
*/
var Send = function () {
/**
* Construct sending query instance.
*
* @param {App} app
* @param {ServerClient} client
*/
function Send(app, client) {
_classCallCheck(this, Send);
this.app = app;
this.client = client;
// Contains the sending queues. (raw).
this.queue = [];
}
/**
* Send chat message.
*
* @param {string} text
* @param {object} options Optional options.
* @param {string} options.source Source of message, could be 'player', 'server' or 'global' (default)
* @param {string} options.destination Destination, false for all, string for login (default false).
*
* @return {Send}
*/
_createClass(Send, [{
key: 'chat',
value: function chat(text, options) {
options = options || {};
var source = options.source || 'global';
var destination = options.destination || false;
var query = {};
if (source === 'global') {
if (!destination) {
query = {
query: 'ChatSendServerMessage',
params: ['»» ' + text]
};
} else {
query = {
query: 'ChatSendServerMessageToLogin',
params: ['» ' + text, destination]
};
}
this.queue.push(query);
}
// TODO: Manipulate 'source'.
return this;
}
/**
* Custom Query (maniaplanet query).
* @param {string} query query string.
* @param {object} params array of parameters.
* @return {Send}
*/
}, {
key: 'custom',
value: function custom(query, params) {
params = params || [];
this.queue.push({
query: query,
params: params
});
return this;
}
/**
* Execute builded query.
*
* @return {Promise|boolean}
*/
}, {
key: 'exec',
value: function exec() {
var _this = this;
if (!this.queue.length) {
return false;
}
if (this.queue.length === 1) {
return this.client.gbx.query(this.queue[0].query, this.queue[0].params);
} else {
var _ret = function () {
// TODO: Calculate size of queue. Limit is 2MB (XML!!)
// TODO: Add support for multicall (todo in gbxremote!).
var params = [];
_this.queue.forEach(function (query) {
params.push({ methodName: query.query, params: query.params });
});
var inspect = require('util').inspect;
console.log(inspect(params, { colors: true }));
return {
v: Promise.reject('No support for multicalls yet!')
};
//return this.client.gbx.query('system.multicall', params);
}();
if ((typeof _ret === 'undefined' ? 'undefined' : _typeof(_ret)) === "object") return _ret.v;
}
}
}]);
return Send;
}();
exports.default = Send;