increase.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. 'use strict';
  2. var quick = require('quick-pomelo');
  3. var P = quick.Promise;
  4. var _ = require('lodash');
  5. var C = require('../../share/constant');
  6. var logger = quick.logger.getLogger('increase', __filename);
  7. // 构造方法
  8. var Controller = function (app) {
  9. this.app = app;
  10. this.userId = 100000;
  11. this.app.event.on('start_server', () => this.initAsync());
  12. };
  13. // 导出方法
  14. module.exports = function (app) {
  15. if (app.getServerType() == 'increaser') return new Controller(app);
  16. };
  17. // 原型对象
  18. var proto = Controller.prototype;
  19. // 初始计数
  20. proto.initAsync = function () {
  21. var goose = this.app.memdb.goose;
  22. return goose.transaction(function () {
  23. return goose.autoconn.collection('__increases__').findByIdReadOnly('player', 'userId')
  24. }, this.app.getServerId())
  25. .then((doc) => {
  26. this.userId = doc ? doc.userId : 100000;
  27. });
  28. };
  29. // 获取计数
  30. proto.newUserIdAsync = function () {
  31. this.userId += 1;
  32. var newId = this.userId;
  33. var self = this;
  34. var goose = this.app.memdb.goose;
  35. return goose.transaction(function () {
  36. return goose.autoconn.collection('__increases__').update('player', { $set: { userId: self.userId } }, { upsert: true });
  37. }, this.app.getServerId())
  38. .then(() => newId);
  39. };