area.js 4.1 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('area', __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 area = new this.app.models.Area(opts);
  26. if(!area._id){
  27. area._id = uuid.v4();
  28. }
  29. var areaId = area._id;
  30. return P.bind(this)
  31. .then(function(){
  32. return area.saveAsync();
  33. })
  34. .then(function(){
  35. logger.info('create %j => %j', opts, areaId);
  36. return areaId;
  37. });
  38. };
  39. proto.removeAsync = function(areaId){
  40. return P.bind(this)
  41. .then(function(){
  42. return this.app.models.Area.findByIdAsync(areaId);
  43. })
  44. .then(function(area){
  45. if(!area){
  46. throw new Error('area ' + areaId + ' not exist');
  47. }
  48. return P.bind(this)
  49. .then(function(){
  50. return this.getPlayersAsync(areaId);
  51. })
  52. .then(function(players){
  53. if(players.length > 0){
  54. throw new Error('area is not empty');
  55. }
  56. return area.removeAsync();
  57. });
  58. })
  59. .then(function(){
  60. logger.info('remove %s', areaId);
  61. });
  62. };
  63. proto.getPlayersAsync = function(areaId){
  64. return this.app.models.Player.findAsync({areaId : areaId});
  65. };
  66. proto.joinAsync = function(areaId, playerId){
  67. var player = null;
  68. return P.bind(this)
  69. .then(function(){
  70. return this.app.models.Area.findByIdAsync(areaId);
  71. })
  72. .then(function(area){
  73. if(!area){
  74. throw new Error('area ' + areaId + ' not exist');
  75. }
  76. return this.app.models.Player.findByIdAsync(playerId);
  77. })
  78. .then(function(ret){
  79. player = ret;
  80. if(!player){
  81. throw new Error('player ' + playerId + ' not exist');
  82. }
  83. player.areaId = areaId;
  84. return player.saveAsync();
  85. })
  86. .then(function(){
  87. var channelId = 'a:' + areaId;
  88. return this.app.controllers.push.joinAsync(channelId, playerId, player.connectorId);
  89. })
  90. .then(function(){
  91. logger.info('join %s %s', areaId, playerId);
  92. });
  93. };
  94. proto.quitAsync = function(areaId, playerId){
  95. var player = null;
  96. return P.bind(this)
  97. .then(function(){
  98. return this.app.models.Player.findByIdAsync(playerId);
  99. })
  100. .then(function(ret){
  101. player = ret;
  102. return this.app.models.Area.findByIdAsync(areaId);
  103. })
  104. .then(function(){
  105. if(!player){
  106. throw new Error('player ' + playerId + ' not exist');
  107. }
  108. if(player.areaId !== areaId){
  109. throw new Error('player ' + playerId + ' not in area ' + areaId);
  110. }
  111. player.areaId = '';
  112. return player.saveAsync();
  113. })
  114. .then(function(){
  115. var channelId = 'a:' + areaId;
  116. return this.app.controllers.push.quitAsync(channelId, playerId);
  117. })
  118. .then(function(){
  119. logger.info('quit %s %s', areaId, playerId);
  120. });
  121. };
  122. /**
  123. * playerIds - [playerId], set null to push all
  124. */
  125. proto.pushAsync = function(areaId, playerIds, route, msg, persistent){
  126. var channelId = 'a:' + areaId;
  127. return this.app.controllers.push.pushAsync(channelId, playerIds, route, msg, persistent);
  128. };
  129. proto.getMsgsAsync = function(areaId, seq, count){
  130. var channelId = 'a:' + areaId;
  131. return this.app.controllers.push.getMsgsAsync(channelId, seq, count);
  132. };
  133. module.exports = function(app){
  134. return new Controller(app);
  135. };