// Copyright 2015 rain1017. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or // implied. See the License for the specific language governing // permissions and limitations under the License. See the AUTHORS file // for names of contributors. 'use strict'; var quick = require('quick-pomelo'); var P = quick.Promise; var logger = quick.logger.getLogger('player', __filename); var uuid = require('node-uuid'); var Controller = function(app){ this.app = app; }; var proto = Controller.prototype; proto.createAsync = function(opts){ var player = new this.app.models.Player(opts); if(!player._id){ player._id = uuid.v4(); } var playerId = player._id; return P.bind(this) .then(function(){ return player.saveAsync(); }) .then(function(){ var channelId = 'p:' + playerId; return this.app.controllers.push.joinAsync(channelId, playerId); }) .then(function(){ logger.info('create %j => %s', opts, playerId); return playerId; }); }; proto.removeAsync = function(playerId){ return P.bind(this) .then(function(){ return this.app.models.Player.findByIdAsync(playerId); }) .then(function(player){ if(!player){ throw new Error('player ' + playerId + ' not exist'); } return P.bind(this) .then(function(){ if(!!player.areaId){ return this.app.controllers.area.quitAsync(player.areaId, playerId); } }) .then(function(){ if(!!player.teamId){ return this.app.controllers.team.quitAsync(player.teamId, playerId); } }) .then(function(){ var channelId = 'p:' + playerId; return this.app.controllers.push.quitAsync(channelId, playerId); }) .then(function(){ return player.removeAsync(); }); }) .then(function(){ if(this.app.reqIdFilter){ return this.app.reqIdFilter.removeReqId(playerId); } }) .then(function(){ logger.info('remove %s', playerId); }); }; proto.connectAsync = function(playerId, connectorId){ var player = null; var oldConnectorId = null; return P.bind(this) .then(function(){ return this.app.models.Player.findByIdAsync(playerId); }) .then(function(ret){ player = ret; if(!player){ throw new Error('player ' + playerId + ' not exist'); } oldConnectorId = player.connectorId; player.connectorId = connectorId; return player.saveAsync(); }) .then(function(){ return this.app.controllers.push.connectAsync(playerId, connectorId); }) .then(function(){ logger.info('connect %s %s => %s', playerId, connectorId, oldConnectorId); return {oldConnectorId : oldConnectorId, player : player}; }); }; proto.disconnectAsync = function(playerId){ var player = null; return P.bind(this) .then(function(){ return this.app.models.Player.findByIdAsync(playerId); }) .then(function(ret){ player = ret; if(!player){ throw new Error('player ' + playerId + ' not exist'); } player.connectorId = ''; return player.saveAsync(); }) .then(function(){ return this.app.controllers.push.disconnectAsync(playerId); }) .then(function(){ logger.info('disconnect %s', playerId); }); }; proto.pushAsync = function(playerId, route, msg, persistent){ var channelId = 'p:' + playerId; return this.app.controllers.push.pushAsync(channelId, null, route, msg, persistent); }; proto.getMsgsAsync = function(playerId, seq, count){ var channelId = 'p:' + playerId; return this.app.controllers.push.getMsgsAsync(channelId, seq, count); }; module.exports = function(app){ return new Controller(app); };