|  |  |  | 
|---|
|  |  |  | Thread.__init__(self, None, None, "AIThreadPy_%s"%index) | 
|---|
|  |  |  | self.robotMgr = robotMgr | 
|---|
|  |  |  | self.AIList = [] | 
|---|
|  |  |  | #config =  ConfigurationReader.ConfigIniReader.GetConfig() | 
|---|
|  |  |  |  | 
|---|
|  |  |  | self.lastTime = 0 | 
|---|
|  |  |  | config =  ConfigurationReader.ConfigIniReader.GetConfig() | 
|---|
|  |  |  | self.tjgLimitCnt = config.GetTJGLimitCnt()  # 限制脱机数量 | 
|---|
|  |  |  | self.offTime = config.GetPlayerOffTime()    # 获取下线多久的玩家时间 | 
|---|
|  |  |  | self.lastTime = 0   # 控制日志输出频率 | 
|---|
|  |  |  | self.badRobotCnt = 0 # 历史异常脱机挂个数 | 
|---|
|  |  |  |  | 
|---|
|  |  |  | self.ProcessFindTJGTime = 5 | 
|---|
|  |  |  | self.lastLoginTime = 0 | 
|---|
|  |  |  | self.lastLoginTime = 0  # 处理机器人登录的频率 | 
|---|
|  |  |  |  | 
|---|
|  |  |  | def runThread(self): | 
|---|
|  |  |  | logging.info( "thread %s start" % self.getName() ) | 
|---|
|  |  |  | 
|---|
|  |  |  | nowTime = time.time() | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if self.lastLoginTime and nowTime - self.lastLoginTime > self.ProcessFindTJGTime: | 
|---|
|  |  |  | logging.debug( "self.tjgLimitCnt %s " % self.tjgLimitCnt ) | 
|---|
|  |  |  | # 限制脱机挂登录数量, 没有实时的玩家上线后会补收益 | 
|---|
|  |  |  | if self.robotMgr.GetRobotCount() > self.tjgLimitCnt: | 
|---|
|  |  |  | self.robotMgr.dbRobot.ClearToken() | 
|---|
|  |  |  |  | 
|---|
|  |  |  | self.lastLoginTime = nowTime | 
|---|
|  |  |  | logoningList = self.robotMgr.GetRobotAccIDList()    # 在登录的机器人 | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  | robotList = [] | 
|---|
|  |  |  | for i in xrange(len(tokenList)): | 
|---|
|  |  |  | tokenInfo = tokenList.pop() | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if tokenInfo[1] in logoningList: | 
|---|
|  |  |  | print "正在登录中------", tokenInfo[1] | 
|---|
|  |  |  | accID = tokenInfo[1] | 
|---|
|  |  |  | if accID in logoningList: | 
|---|
|  |  |  | print "正在登录中------", accID | 
|---|
|  |  |  | continue | 
|---|
|  |  |  |  | 
|---|
|  |  |  | lastTime = self.robotMgr.GetNoteLastTimeByAccID(accID)   # 上一次加入队列的时间 | 
|---|
|  |  |  | if lastTime != 0 and nowTime - lastTime < self.offTime: | 
|---|
|  |  |  | # 登录有问题则进入排队 | 
|---|
|  |  |  | continue | 
|---|
|  |  |  | robot = self.robotMgr.AddRobot(tokenInfo) | 
|---|
|  |  |  | robotList.append(robot) | 
|---|
|  |  |  | 
|---|
|  |  |  | continue | 
|---|
|  |  |  |  | 
|---|
|  |  |  | # 连接-登录-运行AI | 
|---|
|  |  |  | if not robot.IsConnected(): | 
|---|
|  |  |  | if not robot.IsConnected() and robot.GetReconnectCount() < 2: | 
|---|
|  |  |  | robot.ReCreateRobot() | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  | if index >= self.robotMgr.GetRobotCount(): | 
|---|
|  |  |  | # ProcessAI可能会清除了robot | 
|---|
|  |  |  | break | 
|---|
|  |  |  | robot = self.robotMgr.GetRobot( index ) | 
|---|
|  |  |  | try: | 
|---|
|  |  |  | robot = self.robotMgr.GetRobot( index ) | 
|---|
|  |  |  | except: | 
|---|
|  |  |  | # 判断index防范不到,可能多线程引起,此处报错结束循环 | 
|---|
|  |  |  | break | 
|---|
|  |  |  | if not robot: | 
|---|
|  |  |  | continue | 
|---|
|  |  |  | robot.GetAIMgr().ProcessAI() | 
|---|
|  |  |  | 
|---|
|  |  |  | # 队列中清理无效的脱机挂, 一次只清理一个 | 
|---|
|  |  |  | for index in xrange(self.robotMgr.GetRobotCount()): | 
|---|
|  |  |  | #logging.debug( "Run AI for Index:%d", index ) | 
|---|
|  |  |  | robot = self.robotMgr.GetRobot( index ) | 
|---|
|  |  |  | try: | 
|---|
|  |  |  | robot = self.robotMgr.GetRobot( index ) | 
|---|
|  |  |  | except: | 
|---|
|  |  |  | # 判断index防范不到,可能多线程引起,此处报错结束循环 | 
|---|
|  |  |  | break | 
|---|
|  |  |  | if not robot: | 
|---|
|  |  |  | print "====没有机器人" | 
|---|
|  |  |  | self.robotMgr.Remove(robot) | 
|---|
|  |  |  | 
|---|
|  |  |  | self.dbRobot = dbRobot | 
|---|
|  |  |  | self.TeamMgr = {} | 
|---|
|  |  |  |  | 
|---|
|  |  |  | self.LoginHistory = {}  # 记录历史添加时间 {账号:时间} 登录失败需3分钟后方可尝试 | 
|---|
|  |  |  |  | 
|---|
|  |  |  | #启动AI线程 | 
|---|
|  |  |  | #config =  ConfigurationReader.ConfigIniReader.GetConfig() | 
|---|
|  |  |  | #AI_THREAD_NUM = config.GetAIThreadNum() | 
|---|
|  |  |  |  | 
|---|
|  |  |  | th = AIThread(self, 0) | 
|---|
|  |  |  | th.start() | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | def NoteHistory(self, accID): | 
|---|
|  |  |  | self.LoginHistory[accID] = time.time() | 
|---|
|  |  |  |  | 
|---|
|  |  |  | def GetNoteLastTimeByAccID(self, accID): | 
|---|
|  |  |  | return self.LoginHistory.get(accID, 0) | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | def GetRobotAccIDList(self): | 
|---|
|  |  |  | self.__robotAccIDs = [] | 
|---|
|  |  |  | 
|---|
|  |  |  | return self.__robotAccIDs | 
|---|
|  |  |  |  | 
|---|
|  |  |  | def AddRobot(self, tokenInfo): | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | self.LoginHistory[tokenInfo[1]] = time.time()   # 账号记录 | 
|---|
|  |  |  |  | 
|---|
|  |  |  | #创建Robot实例,具体项目实现相应的RobotBase的子类,在这里创建子类的实例 | 
|---|
|  |  |  | logging.debug( "Create Robot index:%s....." % (tokenInfo,)  ) | 
|---|
|  |  |  | robot = ProjectRobot( self.__asioMgr, tokenInfo ) | 
|---|
|  |  |  | 
|---|
|  |  |  | return n | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | #=============================================================================== | 
|---|
|  |  |  | #    def ConnectServer(self, index ): | 
|---|
|  |  |  | # | 
|---|
|  |  |  | #        robot = self.GetRobot(index) | 
|---|
|  |  |  | #        if not robot: | 
|---|
|  |  |  | #            return False | 
|---|
|  |  |  | # | 
|---|
|  |  |  | #        logging.debug( "ConnectServer for %d" % index ) | 
|---|
|  |  |  | #        #创建了实例了,这时只需要再次连接即可 | 
|---|
|  |  |  | #        robot.ReCreateRobot() | 
|---|
|  |  |  | #        return True | 
|---|
|  |  |  | #=============================================================================== | 
|---|
|  |  |  |  | 
|---|
|  |  |  | def GetRobot(self, index ): | 
|---|
|  |  |  | #logging.debug( "GetRobot for %d" % index ) | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | def Remove(self, robot): | 
|---|
|  |  |  | self.__robots.remove(robot) | 
|---|
|  |  |  | logging.info( "Remove robot 剩余: %s"%len(self.__robots) ) | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | def DisconnectServer(self, robot ): | 
|---|