示例#1
0
    def parseHMGameStateXML(self, nickname, gameStateXML):
        gameStateInfo = GameStateInfo()
        if gameStateXML.tag != "HM_GameState":
            print "ERROR can't find HM_GameState:" + gameStateXML.tag
            return [False, gameStateInfo]

        success = gameStateInfo.parse(nickname, gameStateXML)
        return [success, gameStateInfo]
示例#2
0
 def __init__(self):
     self.serverXML = None
     self.clientXMLs = []
     self.serverGameStateInfo = GameStateInfo()
     self.clientGameStateInfos = []
示例#3
0
class HMParse:
    def __init__(self):
        self.serverXML = None
        self.clientXMLs = []
        self.serverGameStateInfo = GameStateInfo()
        self.clientGameStateInfos = []

    def clearClientXMLs(self):
        self.clientGameStateInfos = []

    def loadClientXMLs(self, clientFileNames):
        self.clientGameStateInfos = []
        for clientFileName in clientFileNames:
            self.loadClientXML(clientFileName)

    def loadClientXML(self, clientFileName):
        print "loadClientXML:", clientFileName
        if os.path.isfile(clientFileName) == False:
            print "file does't exist:", clientFileName
            return
        clientXML = xml.etree.ElementTree.parse(clientFileName).getroot()
        # "GameState_2011_07_29_15_41_zorro_2_10.16.5.161_31544.xml"
        tokens = clientFileName.split("_")
        nickname = "Client " + "_".join(tokens[6:-2])
        print "Tokens:", tokens
        print "File:", clientFileName, "nickname:", nickname
        clientXML = xml.etree.ElementTree.parse(clientFileName).getroot()
        clientXMLstring = xml.etree.ElementTree.tostring(clientXML)
        self.addClientXML(nickname, clientXMLstring)

    def addClientXML(self, nickname, clientXMLstring):
        print "addClientXML"
        clientXML = xml.etree.ElementTree.XML(clientXMLstring)
        self.parseClientXML(nickname, clientXML)

    def parseClientXML(self, nickname, clientXML):
        result = self.parseHMGameStateXML(nickname, clientXML)
        if result[0] == False:
            print "parseServerXML failed"
            return

        print "parseClientXML succeeded"
        self.clientXMLs.append(clientXML)
        clientGameStateInfo = result[1]
        self.clientGameStateInfos.append(clientGameStateInfo)

        print clientGameStateInfo.output()

    def loadServerXML(self, serverFileName):
        print "loadServerXML:", serverFileName
        if os.path.isfile(serverFileName) == False:
            print "file does't exist:", serverFileName
            return
        serverXML = xml.etree.ElementTree.parse(serverFileName).getroot()
        serverXMLstring = xml.etree.ElementTree.tostring(serverXML)
        self.setServerXML(serverXMLstring)

    def setServerXML(self, serverXMLstring):
        print "serServerXML"
        self.serverXML = xml.etree.ElementTree.XML(serverXMLstring)
        self.parseServerXML()

    def parseServerXML(self):
        result = self.parseHMGameStateXML("Server", self.serverXML)
        if result[0] == False:
            print "parseServerXML failed"
            self.serverXML = None
            return
        print "parseServerXML succeeded"
        self.serverGameStateInfo = result[1]
        print self.serverGameStateInfo.output()

    def parseHMGameStateXML(self, nickname, gameStateXML):
        gameStateInfo = GameStateInfo()
        if gameStateXML.tag != "HM_GameState":
            print "ERROR can't find HM_GameState:" + gameStateXML.tag
            return [False, gameStateInfo]

        success = gameStateInfo.parse(nickname, gameStateXML)
        return [success, gameStateInfo]

    def compareServerToAllClients(self):
        clientsDifferent = []
        for clientGameStateInfo in self.clientGameStateInfos:
            if self.compareServerToClient(clientGameStateInfo) == False:
                clientsDifferent.append(clientGameStateInfo.nickname)

        if len(clientsDifferent) > 0:
            return [False, clientsDifferent]

        return [True, clientsDifferent]

    def compareServerToClient(self, clientGameStateInfo):
        res = self.serverGameStateInfo.compare(clientGameStateInfo)
        print res[1]
        return res[0]

    def compareServerToClientByNickname(self, nickname):
        for clientGameStateInfo in self.clientGameStateInfos:
            if clientGameStateInfo.nickname == nickname:
                res = self.serverGameStateInfo.compare(clientGameStateInfo)
                return res

        print "ERRRO: Client nickname:", nickname, " not found"
        return [False, ""]