areaHandler.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 P = require('quick-pomelo').Promise;
  17. var Handler = function(app){
  18. this.app = app;
  19. };
  20. var proto = Handler.prototype;
  21. proto.create = function(msg, session, next){
  22. var opts = msg.opts;
  23. return this.app.controllers.area.createAsync(opts)
  24. .nodeify(next);
  25. };
  26. proto.remove = function(msg, session, next){
  27. var areaId = msg.areaId || session.uid;
  28. if(!areaId){
  29. return next(new Error('areaId is missing'));
  30. }
  31. return this.app.controllers.area.removeAsync(areaId)
  32. .nodeify(next);
  33. };
  34. proto.join = function(msg, session, next){
  35. var playerId = session.uid;
  36. var areaId = msg.areaId;
  37. if(!playerId || !areaId){
  38. return next(new Error('playerId or areaId is missing'));
  39. }
  40. return this.app.controllers.area.joinAsync(areaId, playerId)
  41. .nodeify(next);
  42. };
  43. proto.quit = function(msg, session, next){
  44. var playerId = session.uid;
  45. var areaId = msg.areaId;
  46. if(!playerId || !areaId){
  47. return next(new Error('playerId or areaId is missing'));
  48. }
  49. return this.app.controllers.area.quitAsync(areaId, playerId)
  50. .nodeify(next);
  51. };
  52. proto.push = function(msg, session, next){
  53. var areaId = msg.areaId;
  54. if(!areaId){
  55. return next(new Error('areaId is missing'));
  56. }
  57. return this.app.controllers.area.pushAsync(areaId, msg.playerIds, msg.route, msg.msg, msg.persistent)
  58. .nodeify(next);
  59. };
  60. proto.getMsgs = function(msg, session, next){
  61. var areaId = msg.areaId;
  62. if(!areaId){
  63. return next(new Error('areaId is missing'));
  64. }
  65. return this.app.controllers.area.getMsgsAsync(areaId, msg.seq, msg.count)
  66. .nodeify(next);
  67. };
  68. module.exports = function(app){
  69. return new Handler(app);
  70. };