Esempio n. 1
0
 def __init__(self, address, port):
     """ Initialize this! """
     self.socket = SocketHandler(None)
     self.socket.openSocket(address, port)
     self.completeHandshake()
Esempio n. 2
0
class BZRFClient:
    def __init__(self, address, port):
        """ Initialize this! """
        self.socket = SocketHandler(None)
        self.socket.openSocket(address, port)
        self.completeHandshake()
    
    def completeHandshake(self):
        """ Just completes the hand shake with the server """
        response = self.socket.readLine()
        self.socket.sendLine('agent 1')
       
    def shoot(self, index):
        """ Tell the tank to shoot, then return whether it happened as a boolean """
        if self.sendAndCheckLine('shoot ' + str(index)):
            return self.parseBooleanResponse()
        return False
       
    def setSpeed(self, index, speed):
        """ Set the tanks speed, then return whether it happened as a boolean """
        if self.sendAndCheckArgs('speed', str(index), str(speed)):
            return self.parseBooleanResponse()
        return False
       
    def setAngvel(self, index, angvel):
        """ Set the tanks turn rate, then return whether it happened as a boolean """
        if self.sendAndCheckArgs('angvel', str(index), str(angvel)):
            return self.parseBooleanResponse()
        return False
        
    def getTeams(self):
        ''' Return a list of team objects '''
        ## create the list
        teamList = []
        if self.sendAndCheckLine('teams'):
            ## Now retrieve, parse, and return the list of teams
            self.makeObjectsFromList(teamList, 'team', Team)
        return teamList
    
    def getObstacles(self):
        ''' Return a list of obstacle objects '''
        ## create the list
        obstacleList = []
        if self.sendAndCheckLine('obstacles'):
            ## Now retrieve, parse, and return the list of obstacles
            self.makeObjectsFromList(obstacleList, 'obstacle', Obstacle)
        return obstacleList
    
    def getBases(self):
        ''' Return a list of bases '''
        ## create the list
        baseList = []
        if self.sendAndCheckLine('bases'):
            ## Now retrieve, parse, and return the list of bases
            self.makeObjectsFromList(baseList, 'base', Base)
        return baseList
    
    def getFlags(self):
        ''' Return a list of Flags '''
        ## create the list
        flagList = []
        if self.sendAndCheckLine('flags'):
            ## Now retrieve, parse, and return the list of bases
            self.makeObjectsFromList(flagList, 'flag', Flag)
        return flagList
    
    def getShots(self):
        ''' Return a list of Bullets flying around '''
        ## create the list
        shotList = []
        if self.sendAndCheckLine('shots'):
            ## Now retrieve, parse, and return the list of bases
            self.makeObjectsFromList(shotList, 'shot', Shot)
        return shotList
    
    def getMyTanks(self):
        ''' Return a list of tanks on my team '''
        ## create the list
        tankList = []
        if self.sendAndCheckLine('mytanks'):
            ## Now retrieve, parse, and return the list of bases
            self.makeObjectsFromList(tankList, 'mytank', MyTank)
        return tankList

    def getTank(self, index):
        allTanks= self.getMyTanks()
        for tank in allTanks:
            if tank.index == index:
                return tank
        return None
    
    def getOtherTanks(self):
        ''' Return a list of enemy tanks '''
        ## create the list
        tankList = []
        if self.sendAndCheckLine('othertanks'):
            ## Now retrieve, parse, and return the list of bases
            self.makeObjectsFromList(tankList, 'othertank', OtherTank)
        return tankList

    def getConstants(self):
        """ Get the constants from the server """
        constants = {}
        if self.sendAndCheckLine('constants'):
            for line in self.parseListResponse():
                tokens = line.strip().split()
                if tokens[0] == 'constant':
                    ## Create and add the new object
                    constants[tokens[1]] = tokens[2]
        return constants
## Meh?
#    def getOccGrid(self, index):
#        pass
##
    
    def sendAndCheckLine(self, command):
        ## Send the command
        self.socket.sendLine(command)
        ## Check the ack
        return self.parseAckResponse()
    
    def sendAndCheckArgs(self, *args):
        ## Send the command
        self.socket.sendArgsAsLine(*args)
        ## Check the ack
        return self.parseAckResponse()

    def parseAckResponse(self):
        """ This is mostly for convenience since all acks will be treated the same """
        ## Retrieve the 'ack'
        response = self.socket.readLine()
        ## Return true if we got an ack
        return response and 'ack' in response
        
    def parseBooleanResponse(self):
        """ Parse the fail/success, and return the proper boolean value """
        response = self.socket.readLine()
        return response and 'ok' in response
    
    def parseListResponse(self):
        """ Parses a list response from the socket, and passes back the list of lines """
        ## Set up our return value
        stringList = []
        ## parse the begin statement
        response = self.socket.readLine().strip()
        if response == 'begin':
            response = self.socket.readLine().strip()   ## grab the first list item
            while response != 'end':
                stringList.append(response)
                response = self.socket.readLine().strip()  ## grab the next line
        return stringList
    
    def makeObjectsFromList(self, listOut, objectName, Object):
        for line in self.parseListResponse():
            tokens = line.strip().split()
            if tokens[0] == objectName:
                ## Create and add the new object
                listOut.append(Object(tokens[1:]))