Exemplo n.º 1
0
    def getTradeData(self, tradesection):
        """Extract the trade data from the selected save file"""

        logging.info("Parsing %i chars" % len(tradesection))
        t0 = time.time()

        logging.debug("Parsing trade section...")
        # pydev thinks tradeSection.parseString is an undefined import, but it's not
        result = tradeSection.parseString(tradesection) # @UndefinedVariable
        d = result.asDict()
        r = {}

        logging.info("Finished parsing save in %.3f seconds" % (time.time() - t0))

        self.maxIncoming = 0
        self.maxCurrent = 0
        self.maxLocal = 0

        logging.debug("Processing parsed results")

        for n in d["Nodes"]:
            d = n.asDict()
            node = {}
            for k in d:
                if k not in ["quotedName" , "incomingFromNode", "incomingValue"]:
                    node[k] = d[k]
                elif k in ["incomingFromNode", "incomingValue"]:
                    node[k] = d[k].asList()

                if k == "currentValue":
                    self.maxCurrent = max(self.maxCurrent, d[k])
                if k == "localValue":
                    self.maxLocal = max(self.maxLocal, d[k])
                if k == "incomingValue":
                    self.maxIncoming = max(self.maxIncoming, *d[k].asList())

            r[d["quotedName"][0]] = node

        try:
            logging.debug("Seville:\n\t%s" % r["sevilla"])
            logging.debug("max current value: %s" % self.maxCurrent)
            logging.debug("max incoming value: %s" % self.maxIncoming)
        except KeyError:
            logging.warn("Trade node Seville not found! Save file is either from a modded game or malformed!")

        self.nodeData = r
Exemplo n.º 2
0
    def getTradeData(self, savepath):
        """Extract the trade data from the selected save file"""

        logging.debug("Getting trade data")

        self.canvas.create_text((self.mapThumbSize[0] / 2, self.mapThumbSize[1] / 2),
                                text="Please wait... Save file is being processed...", fill="white")
        self.root.update()
        logging.debug("Reading save file %s" % os.path.basename(savepath))
        with open(savepath) as f:
            txt = f.read()
            txt = txt.split("trade=")
            self.preTradeSectionLines = txt[0].count("\n")

            tradesection = txt[1]                                       # drop part before trade section starts
            tradesection = tradesection.split("production_leader")[0]   # drop the part after the end

        logging.info("Parsing %i chars" % len(tradesection))
        t0 = time.time()

        logging.debug("Parsing trade section...")
        result = tradeSection.parseString(tradesection)
        d = result.asDict()
        r = {}

        with open(savepath) as f:
            for line in f:
                if "=" in line:
                    key, val = line.split("=")
                    if key == "date":
                        self.date = val.strip('" \n')
                    elif key == "player":
                        self.player = val.strip('" \n')
                    elif key == "speed":
                        break

        logging.info("Finished parsing save in %.3f seconds" % (time.time() - t0))

        self.maxIncoming = 0
        self.maxCurrent = 0
        self.maxLocal = 0

        logging.debug("Processing parsed results")

        for n in d["Nodes"]:
            d = n.asDict()
            node = {}
            for k in d:
                if k not in ["quotedName" , "incomingFromNode", "incomingValue"]:
                    node[k] = d[k]
                elif k in ["incomingFromNode", "incomingValue"]:
                    node[k] = d[k].asList()

                if k == "currentValue":
                    self.maxCurrent = max(self.maxCurrent, d[k])
                if k == "localValue":
                    self.maxLocal = max(self.maxLocal, d[k])
                if k == "incomingValue":
                    self.maxIncoming = max(self.maxIncoming, *d[k].asList())

            r[d["quotedName"][0]] = node

        try:
            logging.debug("Seville:\n\t%s" % r["sevilla"])
            logging.debug("max current value: %s" % self.maxCurrent)
            logging.debug("max incoming value: %s" % self.maxIncoming)
        except KeyError:
            logging.warn("Trade node Seville not found! Save file is either from a modded game or malformed!")

        self.nodeData = r