text: /**
   * Created by simon on 9/22/2016.
   */
  const util = require('util');
  const Constants = require('../../constants/constants');
  const matchUtil = require('./matchUtil');
  const randomUtil = require('../../util/randomUtil');
  const matchPoolUtil = require('./matchPoolUtil');
  const dataApi = require('../../dataReader/dataApi');
  const BasePool = require('./basePool');
  const AILib = require('../ai/aiLib');
  const commonLogger = require('../../logger/loggers')(__filename).commonLogger;
  
  const MatchPool = function (app, gameLevel, gameMode) {
    BasePool.call(this, app);
    let matchParams = dataApi.getMajiang2pMatchParams();
    let params = matchParams.split('|');
    let param1 = params[0].split(',');
    let param2 = params[1].split(',');
    this.firstMatchParams = {waitTime: Number(param1[0]), range: param1[1]};
    this.secondMatchParams = {waitTime: Number(param2[0]), range: param2[1]};
    this.gameLevel = gameLevel;
    this.gameMode = gameMode;
    this.eventEmitter.on(Constants.EVENT.MAJIANG_MATCHED, matchPoolUtil.onMatched.bind(this));
  };
  
  util.inherits(MatchPool, BasePool);
  
  MatchPool.prototype.doTick = function () {
    const self = this;
    this.tickDoing = true;
  
    let avgTime = matchPoolUtil.getAverageTime(this.matchTimesArray);
    this.matchTimesArray = [];
    if (avgTime === -1) {
      const addTime = randomUtil.randomBaseNumber(0, 5) * 1000;
      this.averageMatchTime = this.secondMatchParams.waitTime + addTime;
    } else {
      this.averageMatchTime = avgTime;
    }
  
    if (this.getPlayersCount() > 0) {
      commonLogger.debug("[DoTick]queue match once:", this.matchServerId, this.gameLevel, this.gameMode, "-player count:", this.getPlayersCount());
      // this.stopMatch();
      // this.tickDoing = false;
      // return;
    } else {
      commonLogger.trace("[DoTick]queue is null, do not match", this.matchServerId);
    }
  
    matchUtil.doMajiang2pFirstMatch(this.mainNumberIDs, this.mainQueue, this.firstMatchParams.range, this.eventEmitter);
    matchUtil.doMajiang2pSecondMatch(this.mainNumberIDs, this.mainQueue, this.secondMatchParams.range, this.eventEmitter);
    AILib.getAIStatePromise(this.app).then(function (res) {
      if (res === Constants.AI_LIBRARY_STATUS.READY) {
        // Use random wait time, value between firstMatchParams.waitTime and secondMatchParams.waitTime
        let aiTimeWait = self.firstMatchParams.waitTime + 1000 * randomUtil.randomBaseNumber(1, (self.secondMatchParams.waitTime - self.firstMatchParams.waitTime) / 1000);
        matchUtil.doMajiang2pTimeoutMatch(self.mainNumberIDs, self.mainQueue, aiTimeWait, self.eventEmitter);
      }
    });
  
    //add want to enter request.
    for (let p = 0, len = this.wantToAddPlayers.length; p < len; p++) {
      let player = this.wantToAddPlayers[p];
      matchPoolUtil.realAdd.call(this, player);
    }
  
    this.tickDoing = false;
  
    //remove want to remove request.
    for (let j = 0, length = this.wantToRemoveIds.length; j < length; j++) {
      let targetNumberID = this.wantToRemoveIds[j];
      matchPoolUtil.realRemove.call(this, targetNumberID);
    }
  
    this.wantToRemoveIds = [];
    this.wantToAddPlayers = [];
  };
  
  module.exports = MatchPool;