def processCmd(self): while not self.conn.recvQueue.empty(): try: cmdDict = self.conn.recvQueue.get_nowait() if cmdDict is None: break except Queue.Empty: break cmdDict = fromGzJson(cmdDict) cmd = cmdDict.get("cmd") if cmd == "gameState": putParams2Queue(self.conn.sendQueue, cmd="reqState") self.applyState(cmdDict) if self.myteam is not None: self.makeClientAIAction() elif cmd == "actACK": pass elif cmd == "teamInfo": teamname = cmdDict.get("teamname") teamid = cmdDict.get("teamid") self.myteam = {"teamname": teamname, "teamid": teamid, "teamStartTime": self.thistick} Log.info("joined %s %s", teamname, teamid) Log.info("%s", self.myteam) putParams2Queue(self.conn.sendQueue, cmd="reqState") else: Log.warn("unknown cmd %s", cmdDict)
def makeTeam(self): teamname = 'AI_%08X' % random.getrandbits(32) teamcolor = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)) Log.info('makeTeam %s %s', teamname, teamcolor) putParams2Queue( self.sendQueue, cmd='makeTeam', teamname=teamname, teamcolor=teamcolor )
def makeClientAIAction(self): # make AI action if self.myteam is None: return aa = self.getTeamByID(self.myteam["teamid"]) if aa is None: return targets = [tt for tt in self.dispgroup["objplayers"] if tt.teamname != aa.teamname] aa.prepareActions(targets, self.frameinfo.lastFPS, self.thistick) actions = aa.SelectAction(targets, aa[0]) actionjson = self.serializeActions(actions) putParams2Queue(self.conn.sendQueue, cmd="act", teamid=self.myteam["teamid"], actions=actionjson)
def __init__(self, connectTo, teamname): if connectTo[0] is None: connectTo = ("localhost", connectTo[1]) sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.connect(connectTo) protocol = I32sendrecv(sock) self.conn = Storage( {"protocol": protocol, "recvQueue": protocol.recvQueue, "sendQueue": protocol.sendQueue, "quit": False} ) Log.info("%s", self) if teamname: teamcolor = random.choice(wx.lib.colourdb.getColourInfoList()) Log.info("makeTeam %s %s", teamname, teamcolor) putParams2Queue(self.conn.sendQueue, cmd="makeTeam", teamname=teamname, teamcolor=teamcolor[1:]) else: # observer mode Log.info("observer mode") putParams2Queue(self.conn.sendQueue, cmd="reqState")
def makeClientAIAction(self, client): # make AI action if client.teaminfo is None: return False aa = self.getTeamByID(client.teaminfo['teamid']) if aa is None: return False targets = [tt for tt in self.dispgroup[ 'objplayers'] if tt.teamname != aa.teamname] aa.prepareActions( targets, self.frameinfo.lastFPS, self.thistick ) actions = aa.SelectAction(targets, aa[0]) actionjson = self.serializeActions(actions) putParams2Queue( client.sendQueue, cmd='act', teamid=client.teaminfo['teamid'], actions=actionjson, ) return True
def reqState(self): putParams2Queue( self.sendQueue, cmd='reqState', )