| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- 'use strict';
- var quick = require('quick-pomelo');
- var P = quick.Promise;
- var _ = require('lodash');
- var C = require('../../share/constant');
- var logger = quick.logger.getLogger('increase', __filename);
- // 构造方法
- var Controller = function (app) {
- this.app = app;
- this.userId = 100000;
- this.app.event.on('start_server', () => this.initAsync());
- };
- // 导出方法
- module.exports = function (app) {
- if (app.getServerType() == 'increaser') return new Controller(app);
- };
- // 原型对象
- var proto = Controller.prototype;
- // 初始计数
- proto.initAsync = function () {
- var goose = this.app.memdb.goose;
- return goose.transaction(function () {
- return goose.autoconn.collection('__increases__').findByIdReadOnly('player', 'userId')
- }, this.app.getServerId())
- .then((doc) => {
- this.userId = doc ? doc.userId : 100000;
- });
- };
- // 获取计数
- proto.newUserIdAsync = function* () {
- //this.userId += 1;
- var record = null
- for (;;){
- let records = yield this.app.models.UserIds.findMongoAsync({},{},{limit: 1});
- let players = yield this.app.models.UserIds.findMongoAsync({userId:records[0].userId});
- if (players.length == 0) {
- record = records[0].userId
- break
- }else {
- goose.transaction(function () {
- record[0].removeAsync()
- }, this.app.getServerId())
- .then(() => newId);
- }
- }
- this.userId = record.userId
- var newId = this.userId;
- var self = this;
- var goose = this.app.memdb.goose;
- return goose.transaction(function () {
- return goose.autoconn.collection('__increases__').update('player', { $set: { userId: self.userId } }, { upsert: true });
- }, this.app.getServerId())
- .then(() => newId);
- };
|