def do1ClientCmd(self, ch, clientid, cmdDict): teaminfo = self.clients.get(clientid) if teaminfo is None: self.clients[clientid] = {} teaminfo = self.clients[clientid] cmd = cmdDict.get('cmd') if cmd == 'makeTeam': tn = cmdDict.get('teamname') o = self.make1TeamCustom( teamname=tn, aiclass=GameObjectGroup, teamcolor=cmdDict.get('teamcolor'), servermove=False, spriteClass=SpriteObj ) self.dispgroup['objplayers'].append(o) teaminfo['teamid'] = o.ID teaminfo['teamname'] = tn ch.sendQueue.put((clientid, toGzJsonParams( cmd='teamInfo', teamname=tn, teamid=o.ID))) Log.debug('Join team %s %s', clientid, teaminfo) elif cmd == 'del': Log.debug('Leave team %s', teaminfo) try: self.delTeamByID(teaminfo['teamid']) except KeyError: pass del self.clients[clientid] elif cmd == 'reqState': ch.sendQueue.put((clientid, self.gameState)) elif cmd == 'act': ch.sendQueue.put((clientid, toGzJsonParams(cmd='actACK'))) actions = cmdDict.get('actions') tid = cmdDict['teamid'] thisTeam = self.getTeamByID(tid) if thisTeam.servermove: Log.error('invalid client team %s', thisTeam) return actionjson = cmdDict['actions'] actions = self.deserializeActions(actionjson) enemyTeamList = self.getEnemyTeamList(thisTeam) thisTeam.prepareActions( enemyTeamList, self.frameinfo.lastFPS, self.thistick ) thisTeam.applyActions(actions) else: Log.warn('unknown cmd %s', cmdDict)
def makeClientAIAction(self, client): # make AI action if client.teaminfo is None: return False aa = self.getTeamByID(client.teaminfo["teamid"]) if aa is None: client.sendString(toGzJsonParams(cmd="act", teamid=client.teaminfo["teamid"], actions=[])) 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) client.sendString(toGzJsonParams(cmd="act", teamid=client.teaminfo["teamid"], actions=actionjson)) return True
def makeClientAIAction(self, idno): # make AI action if idno not in self.clientDict: return False client = self.clientDict[idno] aa = self.getTeamByID(client.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) self.toGameCh.sendQueue.put( (idno, toGzJsonParams( cmd='act', teamid=client.teamid, actions=actionjson, ))) return True
def makeTeam(self, conn): teamname = 'AI_%08X' % random.getrandbits(32) teamcolor = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)) log.msg('makeTeam %s %s', teamname, teamcolor) conn.sendString(toGzJsonParams( cmd='makeTeam', teamname=teamname, teamcolor=teamcolor ))
def do1ClientCmd(self, clinentprotocol, cmdDict): cmd = cmdDict.get("cmd") if cmd == "makeTeam": tn = cmdDict.get("teamname") o = self.make1TeamCustom( teamname=tn, aiclass=GameObjectGroup, teamcolor=cmdDict.get("teamcolor"), servermove=False, spriteClass=SpriteObj, ) self.dispgroup["objplayers"].append(o) clinentprotocol.sendString(toGzJsonParams(cmd="teamInfo", teamname=tn, teamid=o.ID)) elif cmd == "del": try: self.delTeamByID(cmdDict["teamid"]) except KeyError: pass elif cmd == "reqState": clinentprotocol.sendString(self.gameState) elif cmd == "act": clinentprotocol.sendString(toGzJsonParams(cmd="actACK")) actions = cmdDict.get("actions") tid = cmdDict["teamid"] thisTeam = self.getTeamByID(tid) if thisTeam.servermove: log.msg("invalid client team ", thisTeam) return actionjson = cmdDict["actions"] actions = self.deserializeActions(actionjson) enemyTeamList = self.getEnemyTeamList(thisTeam) thisTeam.prepareActions(enemyTeamList, self.frameinfo.lastFPS, self.thistick) thisTeam.applyActions(actions) else: log.msg("unknown cmd ", cmdDict)
def makeTeam(self): idno = (self.serverType, getSerial()) self.clientDict[idno] = Storage( teamname=None, teamid=None, teamStartTime=None, ) teamname = 'AI_%08X' % random.getrandbits(32) teamcolor = [random.randint(0, 255) for i in [0, 1, 2]] Log.debug('makeTeam %s %s', teamname, teamcolor) self.toGameCh.sendQueue.put( (idno, toGzJsonParams( cmd='makeTeam', teamname=teamname, teamcolor=teamcolor )))
def closeClient(self, p): Log.info('client disconnected %s', p) try: self.recvList.remove(p) except ValueError: pass try: self.sendList.remove(p) except ValueError: pass self.mainChannel.writeTo( ((self.serverType, p.safefileno), toGzJsonParams(cmd='del')) ) try: del self.clientDict[(self.serverType, p.safefileno)] except KeyError: pass p.sock.close()
def beforeLoop(self): self.allInited = False self.thistick = getFrameTime() self.clientDict = {} # wait for game is ready while not self.mainChannel.canReadFrom(): time.sleep(0) self.mainChannel.readFrom() # recv start packet for i in range(self.aicount): self.mainChannel.sendQueue.put( ((self.serverType, getSerial()), toGzJsonParams( cmd='sayHello', teamname='AI_%08X' % random.getrandbits(32), teamcolor=[random.randint(0, 255) for i in [0, 1, 2]] ))) self.recvList = [self.mainChannel.reader] self.sendList = []
def makeClientAIAction(self, conn): # make AI action if self.teaminfo is None: return aa = self.getTeamByID(self.teaminfo['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) conn.sendString(toGzJsonParams( cmd='act', teamid=self.teaminfo['teamid'], actions=actionjson, ))
def reqState(self, conn): conn.sendString(toGzJsonParams( cmd='reqState', ))
def reqState(self): self.toGameCh.sendQueue.put( ((self.serverType, 0), toGzJsonParams(cmd='reqState') ))
def reqState(self): self.sendString(toGzJsonParams(cmd="reqState"))
def makeTeam(self): teamname = "AI_%08X" % random.getrandbits(32) teamcolor = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)) log.msg("makeTeam %s %s", teamname, teamcolor) self.sendString(toGzJsonParams(cmd="makeTeam", teamname=teamname, teamcolor=teamcolor))