| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- // 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);
- };
|