UNPKG

maniajs

Version:

ManiaPlanet (Dedicated) Server Controller.

146 lines (121 loc) 4.24 kB
/** * Maps Game Flow */ '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 _async = require('async'); var async = _interopRequireWildcard(_async); 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"); } } /** * Maps game flow class. * * @class Maps * @type {Maps} */ var Maps = function () { function Maps(app) { _classCallCheck(this, Maps); this.app = app; /** * Maplist. * * Key: uid * Value: object * * @type {{string: Map}} */ this.list = {}; /** * Current Map. * * @type {Map} */ this.current = null; } _createClass(Maps, [{ key: 'boot', value: function boot() { var _this = this; var Map = this.app.models.Map; // Get all maps on server, sync with database. return new Promise(function (resolve, reject) { _this.app.serverFacade.client.gbx.query('GetMapList', [-1, 0]).then(function (res) { // Clear list first. _this.list = {}; // Loop foreach async (parallel). async.eachSeries(res, function (data, callback) { Map.findOne({ where: { uid: data.UId } }).then(function (map) { if (!map) { // Create Map.create({ uid: data.UId, name: data.Name, author: data.Author, environment: data.Environnement }).then(function (map) { _this.list[data.UId] = map; return callback(); }); } else { // Already in there. // Name update? if (map.name !== data.Name) { map.set('name', data.Name); map.save().then(function (map) { _this.list[data.UId] = map; return callback(); }).catch(function (err) { return callback(err); }); } else { _this.list[data.UId] = map; return callback(); } } }).catch(function (err) { return callback(err); }); }, function (err) { if (err) { return reject(err); } // The sync with db is done, now check the current map and set it in the this.current _this.app.serverFacade.client.gbx.query('GetCurrentMapInfo', []).then(function (res) { if (res) { _this.current = _this.list[res.UId]; } return resolve(); }).catch(function (err) { return reject(err); }); }); }); }); } /** * Begin map call. * * @param uid */ }, { key: 'begin', value: function begin(uid) { if (this.list.hasOwnProperty(uid)) { this.current = this.list[uid]; } // TODO: When not yet in the list. return Promise.resolve(); } // TODO: Adding maps should do something with the maplist. }]); return Maps; }(); exports.default = Maps;