playerHandler.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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.player.createAsync(opts)
  24. .nodeify(next);
  25. };
  26. proto.remove = function(msg, session, next){
  27. var playerId = session.uid;
  28. if(!playerId){
  29. return next(new Error('player is not logged in'));
  30. }
  31. P.bind(this)
  32. .then(function(){
  33. return this.app.controllers.player.removeAsync(playerId);
  34. })
  35. .then(function(){
  36. return P.promisify(session.unbind, session)(playerId);
  37. })
  38. .nodeify(next);
  39. };
  40. module.exports = function(app){
  41. return new Handler(app);
  42. };