# -*- coding: GBK -*-
|
# ÓÃÓÚ¹ÜÀíËùÓлúÆ÷ÈË
|
# author: Alee
|
# Date: 2011.9.1
|
# history: 2011.9.1 Created
|
|
import logging
|
from ProjectBinding.ProjectRobot import ProjectRobot
|
import ConfigurationReader.RobotsConfigReader
|
import ConfigurationReader.ConfigIniReader
|
from threading import Thread
|
import traceback
|
|
import time
|
|
class AIThread(Thread):
|
def __init__(self, robotMgr, index):
|
Thread.__init__(self, None, None, "AIThreadPy_%s"%index)
|
self.robotMgr = robotMgr
|
self.AIList = []
|
config = ConfigurationReader.ConfigIniReader.GetConfig()
|
self.tjgLimitCnt = config.GetTJGLimitCnt() # ÏÞÖÆÍÑ»úÊýÁ¿
|
self.offTime = config.GetPlayerOffTime() # »ñÈ¡ÏÂÏß¶à¾ÃµÄÍæ¼Òʱ¼ä
|
self.lastTime = 0 # ¿ØÖÆÈÕÖ¾Êä³öƵÂÊ
|
self.badRobotCnt = 0 # ÀúÊ·Òì³£ÍÑ»ú¹Ò¸öÊý
|
|
self.ProcessFindTJGTime = 5
|
self.lastLoginTime = 0 # ´¦Àí»úÆ÷È˵ǼµÄƵÂÊ
|
|
def runThread(self):
|
logging.info( "thread %s start" % self.getName() )
|
|
while True:
|
try:
|
|
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() # ÔڵǼµÄ»úÆ÷ÈË
|
|
tokenList = self.robotMgr.dbRobot.GetTokenList()
|
robotList = []
|
for i in xrange(len(tokenList)):
|
tokenInfo = tokenList.pop()
|
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)
|
if i == 10:
|
# Ò»´ÎµÇ¼10¸öÍæ¼Ò
|
break
|
|
for robot in robotList:
|
#logging.debug( "Run AI for Index:%d", index )
|
if not robot:
|
continue
|
|
# Á¬½Ó-µÇ¼-ÔËÐÐAI
|
if not robot.IsConnected() and robot.GetReconnectCount() < 2:
|
robot.ReCreateRobot()
|
|
|
for index in xrange(self.robotMgr.GetRobotCount()):
|
#logging.debug( "Run AI for Index:%d", index )
|
if index >= self.robotMgr.GetRobotCount():
|
# ProcessAI¿ÉÄÜ»áÇå³ýÁËrobot
|
break
|
try:
|
robot = self.robotMgr.GetRobot( index )
|
except:
|
# ÅжÏindex·À·¶²»µ½£¬¿ÉÄܶàÏß³ÌÒýÆð£¬´Ë´¦±¨´í½áÊøÑ»·
|
break
|
if not robot:
|
continue
|
robot.GetAIMgr().ProcessAI()
|
|
if not self.lastLoginTime:
|
self.lastLoginTime = nowTime
|
time.sleep( 0.02 ) # Ãë
|
|
# ¶ÓÁÐÖÐÇåÀíÎÞЧµÄÍÑ»ú¹Ò, Ò»´ÎÖ»ÇåÀíÒ»¸ö
|
for index in xrange(self.robotMgr.GetRobotCount()):
|
#logging.debug( "Run AI for Index:%d", index )
|
try:
|
robot = self.robotMgr.GetRobot( index )
|
except:
|
# ÅжÏindex·À·¶²»µ½£¬¿ÉÄܶàÏß³ÌÒýÆð£¬´Ë´¦±¨´í½áÊøÑ»·
|
break
|
if not robot:
|
print "====ûÓлúÆ÷ÈË"
|
self.robotMgr.Remove(robot)
|
self.badRobotCnt += 1
|
break
|
|
if robot.GetNeedDestroy():
|
print "++++++±»´Ý»Ù"
|
logging.debug( "GetNeedDestroy __peer:0x%08X " % (robot.GetPeerValue()) )
|
self.robotMgr.Remove(robot)
|
self.badRobotCnt += 1
|
break
|
|
# Á¬½Ó-µÇ¼-ÔËÐÐAI
|
nowTime = time.time()
|
if nowTime - self.lastTime > 12:
|
print "ÍÑ»ú¹ÒÍæ¼ÒÊýÁ¿: %s - %s"%(self.robotMgr.GetRobotCount(), self.robotMgr.GetLoginOKRobotCount())
|
self.lastTime = nowTime
|
if self.badRobotCnt:
|
print "²Î¿¼-ÀúÊ·Òì³£ÍÑ»ú¹Ò´ÎÊý£º%s"%self.badRobotCnt
|
|
except Exception, e:
|
logging.error( "AIThread: %s"%str(e) )
|
logging.error( "AIThread: %s"%traceback.print_exc() )
|
|
def run(self):
|
runM = self.runThread
|
|
runM()
|
|
|
|
class RobotMgr():
|
|
def __init__(self, asioMgr, dbRobot):
|
self.__asioMgr = asioMgr
|
self.__robotNum = 0
|
self.__robots = []
|
self.__robotAccIDs = []
|
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 = []
|
for robot in self.__robots:
|
self.__robotAccIDs.append(robot.GetRobotConfig()[1])
|
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 )
|
self.__robots.append( robot)
|
|
return robot
|
|
#»úÆ÷È˵Ä×ÜÊý
|
def GetRobotCount(self):
|
return len(self.__robots)
|
|
#ÒÑÁ¬½ÓÉÏ·þÎñÆ÷µÄ»úÆ÷È˵ÄÊýÁ¿
|
def GetConnectedRobotCount(self):
|
n = 0
|
for robot in self.__robots:
|
if robot.IsConnected():
|
n += 1
|
return n
|
|
#ÒѾµÇ¼³É¹¦µÄ»úÆ÷ÈËÊýÁ¿
|
def GetLoginOKRobotCount(self):
|
n = 0
|
for robot in self.__robots:
|
if robot.GetIsLoginOK():
|
n += 1
|
else:
|
logging.debug( "!!!not login :0x%08X", robot.GetPeerValue())
|
return n
|
|
|
def GetRobot(self, index ):
|
#logging.debug( "GetRobot for %d" % index )
|
|
return self.__robots[index]
|
|
def Remove(self, robot):
|
self.__robots.remove(robot)
|
logging.info( "Remove robot Ê£Óà: %s"%len(self.__robots) )
|
|
|
def DisconnectServer(self, robot ):
|
|
accID = robot.GetPlayerInfo().GetAccID()
|
logging.debug( "DisconnectServer for %s" % accID )
|
|
#¶Ï¿ª·þÎñÆ÷
|
robot.DestroyRobot()
|
if robot in self.__robots:
|
self.Remove(robot)
|
|
def StartAI(self, index ):
|
logging.debug( "StartAI for %d" % index )
|
|
robot = self.GetRobot(index)
|
if not robot:
|
return False
|
|
#robotAIList = robot.GetRobotConfig()
|
#robot.SetAIActive( robotAIList, True )
|
|
|
|
def StopAI(self, index ):
|
logging.debug( "StopAI for %d" % index )
|
|
robot = self.GetRobot(index)
|
if not robot:
|
return False
|
|
#robotAIList = robot.GetRobotConfig()
|
#robot.SetAIActive( robotAIList, False )
|
|
#===============================================================================
|
# def CalcTeam(self):
|
# self.TeamMgr = {}
|
# for robot in self.__robots:
|
# teamID = robot.GetTeamID()
|
# mapID = robot.GetMapID()
|
#
|
# #{mapID:[×é¶ÓÍæ¼ÒÊý£¬ÓÐÍÑ»ú¶Ó³¤µÄÍæ¼ÒÊý£¬Ê£ÓàÍæ¼ÒÊý]}
|
# if mapID not in self.TeamMgr:
|
# self.TeamMgr[mapID] = [{}, {}]
|
#
|
# self.TeamMgr[mapID][0][teamID] = self.TeamMgr[mapID][0].get(teamID, 0) + 1
|
# if robot.GetTeamMemberLV() != 2:
|
# # Ö»¹ÜÀí¶Ó³¤ÊÇÍÑ»ú¹ÒÍæ¼Ò£¬±ÜÃâ±»¾Ü¾ø×é¶ÓµÄÇé¿ö
|
# continue
|
#
|
# self.TeamMgr[mapID][1][teamID] = robot
|
#
|
# logging.debug("CalcTeam----%s"%self.TeamMgr)
|
# # ×é¶ÓÉÏÏÞ4ÈË
|
# maxTeamCnt = 4
|
# spaceCnt = 0 # ×é¶Ó¿Õȱλ
|
# for mapID, teamsInfo in self.TeamMgr:
|
# for teamID in teamsInfo[1]:
|
# spaceCnt += (maxTeamCnt - teamsInfo[0][teamID])
|
#
|
#
|
#
|
# return
|
#===============================================================================
|
|
__gRobotMgr = None
|
|
def CreateRobotMgr( asioMgr, dbRobot ):
|
global __gRobotMgr
|
if not __gRobotMgr:
|
__gRobotMgr = RobotMgr( asioMgr, dbRobot )
|
|
|
def GetRobotMgr():
|
global __gRobotMgr
|
return __gRobotMgr
|