Przeglądaj źródła

Save Class 0, 9 port information.

Steve Thielemann 5 lat temu
rodzic
commit
edd2177907
2 zmienionych plików z 78 dodań i 4 usunięć
  1. 20 2
      flexible.py
  2. 58 2
      galaxy.py

+ 20 - 2
flexible.py

@@ -869,7 +869,7 @@ class ScriptPort(object):
 
                 self.queue_player.put("{0}\r".format(self.this_sector))
                 self.state = 10
-                
+
         elif self.state == 10:
             # DANGER! You have marked sector X to be avoided!
             if prompt.startswith('Do you really want to warp there? (Y/N) '):
@@ -2555,9 +2555,23 @@ class ProxyMenu(object):
         self.game.queue_player.put(" ")
 
     def port_report(self, portdata: dict):
+        # Check to see if the old data is close to the new data.
+        # If so, don't toss out the special!
+        matches = 0
+        for k, v in self.old_ports.items():
+            if k in self.game.gamedata.ports:
+                # Ok, key exists.  Is the class the same
+                if self.game.gamedata.ports[k]['class'] == v['class']:
+                    matches += 1
+        log.info("Got {0} matches old ports to new.".format( matches))
+        if matches > 12:
+            self.queue_game.put("Restoring (SPECIAL) class ports ({0}).".format(len(self.specials)) + self.nl)            
+            for p in self.specials.keys():
+                self.game.gamedata.ports[int(p)] = self.specials[p]
+
         # self.portdata = portdata
         # self.game.portdata = portdata
-        self.queue_game.put("Loaded {0} ports.".format(len(portdata)) + self.nl)
+        self.queue_game.put("Loaded {0} ports.".format(len(self.game.ports)) + self.nl)
         self.welcome_back()
 
     def warp_report(self, warpdata: dict):
@@ -2714,6 +2728,10 @@ class ProxyMenu(object):
                 return      
         elif key == "P":
             self.queue_game.put(self.c + key + self.r + self.nl)
+            # Save specials, save 10 ports
+            self.specials = self.game.gamedata.special_ports()
+            # Save 20 ports.  https://stackoverflow.com/questions/7971618/python-return-first-n-keyvalue-pairs-from-dict#7971655
+            self.old_ports = {k: self.game.gamedata.ports[k] for k in list(self.game.gamedata.ports)[:20]}
             self.game.gamedata.reset_ports()            
             # Activate CIM Port Report
             report = CIMPortReport(self.game)

+ 58 - 2
galaxy.py

@@ -54,6 +54,12 @@ class GameData(object):
     def reset_warps(self):
         self.warps = {}
 
+    def special_ports(self):
+        """ Save the special class ports 0, 9 """
+        return {
+            p: self.ports[p] for p in self.ports if self.ports[p]["class"] in (0, 9)
+        }
+
     def display(self):
         pprint(self.warps)
         pprint(self.ports)
@@ -88,7 +94,7 @@ class GameData(object):
                 # writer.write(w)
                 # yield
 
-            if len(w["warp"]) > 1:
+            if len(w["warp"]) > 0:
                 writer.write(w)
 
             # for sector, port in self.ports.items():
@@ -101,7 +107,57 @@ class GameData(object):
                     writer.write(p)
                     p = {"port": {}}
                     yield
-            if len(p["port"]) > 1:
+            if len(p["port"]) > 0:
+                writer.write(p)
+
+        log.info(
+            "Saved {0} {1}/{2}/{3}".format(
+                filename, len(self.ports), len(self.warps), len(self.config)
+            )
+        )
+
+    def untwisted_save(self, *_):
+        """ save gamedata as jsonlines.
+
+        Enable sort_keys=True to provide stable json data output.
+        We also sorted(.keys()) to keep records in order.
+        Note:  There's a "bug" when serializing to json, keys must be strings!
+        """
+        filename = self.storage_filename()
+
+        with jsonlines.open(filename, mode="w", sort_keys=True) as writer:
+            # for warp, sectors in self.warps.items():
+            c = {"config": self.config}
+            writer.write(c)
+
+            w = {"warp": {}}
+
+            for warp in sorted(self.warps.keys()):
+                sectors = self.warps[warp]
+                # log.debug("save:", warp, sectors)
+                sects = sorted(list(sectors))  # make a list
+                w["warp"][warp] = sects
+                if len(w["warp"]) >= self.warp_groups:
+                    writer.write(w)
+                    w = {"warp": {}}
+
+                # log.debug(w)
+                # writer.write(w)
+                # yield
+
+            if len(w["warp"]) > 0:
+                writer.write(w)
+
+            # for sector, port in self.ports.items():
+            p = {"port": {}}
+
+            for sector in sorted(self.ports.keys()):
+                port = self.ports[sector]
+                p["port"][sector] = port
+                if len(p["port"]) >= self.port_groups:
+                    writer.write(p)
+                    p = {"port": {}}
+            if len(p["port"]) > 0:
                 writer.write(p)
 
         log.info(