Ejemplo n.º 1
0
    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)
Ejemplo n.º 2
0
 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
Ejemplo n.º 3
0
 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
Ejemplo n.º 4
0
 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
     ))
Ejemplo n.º 5
0
    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)
Ejemplo n.º 6
0
 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
          )))
Ejemplo n.º 7
0
    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()
Ejemplo n.º 8
0
    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 = []
Ejemplo n.º 9
0
 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,
     ))
Ejemplo n.º 10
0
 def reqState(self, conn):
     conn.sendString(toGzJsonParams(
         cmd='reqState',
     ))
Ejemplo n.º 11
0
 def reqState(self):
     self.toGameCh.sendQueue.put(
         ((self.serverType, 0),
          toGzJsonParams(cmd='reqState')
          ))
Ejemplo n.º 12
0
 def reqState(self):
     self.sendString(toGzJsonParams(cmd="reqState"))
Ejemplo n.º 13
0
 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))