player.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. // Copyright 2015 rain1017.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
  12. // implied. See the License for the specific language governing
  13. // permissions and limitations under the License. See the AUTHORS file
  14. // for names of contributors.
  15. 'use strict';
  16. var quick = require('quick-pomelo');
  17. var P = quick.Promise;
  18. var logger = quick.logger.getLogger('player', __filename);
  19. var uuid = require('node-uuid');
  20. var Controller = function(app){
  21. this.app = app;
  22. };
  23. var proto = Controller.prototype;
  24. proto.createAsync = function(opts){
  25. var player = new this.app.models.Player(opts);
  26. if(!player._id){
  27. player._id = uuid.v4();
  28. }
  29. var playerId = player._id;
  30. return P.bind(this)
  31. .then(function(){
  32. return player.saveAsync();
  33. })
  34. .then(function(){
  35. var channelId = 'p:' + playerId;
  36. return this.app.controllers.push.joinAsync(channelId, playerId);
  37. })
  38. .then(function(){
  39. logger.info('create %j => %s', opts, playerId);
  40. return playerId;
  41. });
  42. };
  43. proto.removeAsync = function(playerId){
  44. return P.bind(this)
  45. .then(function(){
  46. return this.app.models.Player.findByIdAsync(playerId);
  47. })
  48. .then(function(player){
  49. if(!player){
  50. throw new Error('player ' + playerId + ' not exist');
  51. }
  52. return P.bind(this)
  53. .then(function(){
  54. if(!!player.areaId){
  55. return this.app.controllers.area.quitAsync(player.areaId, playerId);
  56. }
  57. })
  58. .then(function(){
  59. if(!!player.teamId){
  60. return this.app.controllers.team.quitAsync(player.teamId, playerId);
  61. }
  62. })
  63. .then(function(){
  64. var channelId = 'p:' + playerId;
  65. return this.app.controllers.push.quitAsync(channelId, playerId);
  66. })
  67. .then(function(){
  68. return player.removeAsync();
  69. });
  70. })
  71. .then(function(){
  72. if(this.app.reqIdFilter){
  73. return this.app.reqIdFilter.removeReqId(playerId);
  74. }
  75. })
  76. .then(function(){
  77. logger.info('remove %s', playerId);
  78. });
  79. };
  80. proto.connectAsync = function(playerId, connectorId){
  81. var player = null;
  82. var oldConnectorId = null;
  83. return P.bind(this)
  84. .then(function(){
  85. return this.app.models.Player.findByIdAsync(playerId);
  86. })
  87. .then(function(ret){
  88. player = ret;
  89. if(!player){
  90. throw new Error('player ' + playerId + ' not exist');
  91. }
  92. oldConnectorId = player.connectorId;
  93. player.connectorId = connectorId;
  94. return player.saveAsync();
  95. })
  96. .then(function(){
  97. return this.app.controllers.push.connectAsync(playerId, connectorId);
  98. })
  99. .then(function(){
  100. logger.info('connect %s %s => %s', playerId, connectorId, oldConnectorId);
  101. return {oldConnectorId : oldConnectorId, player : player};
  102. });
  103. };
  104. proto.disconnectAsync = function(playerId){
  105. var player = null;
  106. return P.bind(this)
  107. .then(function(){
  108. return this.app.models.Player.findByIdAsync(playerId);
  109. })
  110. .then(function(ret){
  111. player = ret;
  112. if(!player){
  113. throw new Error('player ' + playerId + ' not exist');
  114. }
  115. player.connectorId = '';
  116. return player.saveAsync();
  117. })
  118. .then(function(){
  119. return this.app.controllers.push.disconnectAsync(playerId);
  120. })
  121. .then(function(){
  122. logger.info('disconnect %s', playerId);
  123. });
  124. };
  125. proto.pushAsync = function(playerId, route, msg, persistent){
  126. var channelId = 'p:' + playerId;
  127. return this.app.controllers.push.pushAsync(channelId, null, route, msg, persistent);
  128. };
  129. proto.getMsgsAsync = function(playerId, seq, count){
  130. var channelId = 'p:' + playerId;
  131. return this.app.controllers.push.getMsgsAsync(channelId, seq, count);
  132. };
  133. module.exports = function(app){
  134. return new Controller(app);
  135. };