# -*- 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.lastTime = 0
|
self.badRobotCnt = 0 # ÀúÊ·Òì³£ÍÑ»ú¹Ò¸öÊý
|
|
config = ConfigurationReader.ConfigIniReader.GetConfig()
|
self.ProcessFindTJGTime = config.GetProcessFindTJGTime() #²éѯÊý¾Ý¿â¼ä¸ô
|
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:
|
self.lastLoginTime = nowTime
|
tokenList = self.robotMgr.dbRobot.GetTokenList()
|
robotList = []
|
for i in xrange(len(tokenList)):
|
tokenInfo = tokenList.pop()
|
robot = self.robotMgr.AddRobot(tokenInfo)
|
robotList.append(robot)
|
if i == 20:
|
# Ò»´ÎµÇ¼10¸öÍæ¼Ò
|
break
|
|
for robot in robotList:
|
#logging.debug( "Run AI for Index:%d", index )
|
if not robot:
|
continue
|
|
# Á¬½Ó-µÇ¼-ÔËÐÐAI
|
if not robot.IsConnected():
|
robot.ReCreateRobot()
|
|
if not self.lastLoginTime:
|
self.lastLoginTime = nowTime
|
|
for index in xrange(self.robotMgr.GetRobotCount()):
|
#logging.debug( "Run AI for Index:%d", index )
|
robot = self.robotMgr.GetRobot( index )
|
if not robot:
|
continue
|
robot.GetAIMgr().ProcessAI()
|
|
|
time.sleep( 0.01 ) # Ãë
|
|
# ¶ÓÁÐÖÐÇåÀíÎÞЧµÄÍÑ»ú¹Ò, Ò»´ÎÖ»ÇåÀíÒ»¸ö
|
for index in xrange(self.robotMgr.GetRobotCount()):
|
#logging.debug( "Run AI for Index:%d", index )
|
robot = self.robotMgr.GetRobot( index )
|
if not robot:
|
self.robotMgr.Remove(robot)
|
self.badRobotCnt += 1
|
if robot.GetNeedDestroy():
|
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.dbRobot = dbRobot
|
self.TeamMgr = {}
|
|
#Æô¶¯AIÏß³Ì
|
#config = ConfigurationReader.ConfigIniReader.GetConfig()
|
#AI_THREAD_NUM = config.GetAIThreadNum()
|
|
th = AIThread(self, 0)
|
th.start()
|
|
def AddRobot(self, tokenInfo):
|
|
#´´½¨RobotʵÀý£¬¾ßÌåÏîĿʵÏÖÏàÓ¦µÄRobotBaseµÄ×ÓÀ࣬ÔÚÕâÀï´´½¨×ÓÀàµÄʵÀý
|
#logging.info( "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 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 )
|
|
return self.__robots[index]
|
|
def Remove(self, robot):
|
self.__robots.remove(robot)
|
|
|
def DisconnectServer(self, robot ):
|
|
accID = robot.GetPlayerInfo().GetAccID()
|
logging.debug( "DisconnectServer for %s" % accID )
|
|
#¶Ï¿ª·þÎñÆ÷
|
robot.DestroyRobot()
|
self.__robots.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
|