Ver código fonte

Added CIMWarpReport.

Steve Thielemann 5 anos atrás
pai
commit
c7af488927
1 arquivos alterados com 123 adições e 0 exclusões
  1. 123 0
      flexible.py

+ 123 - 0
flexible.py

@@ -208,6 +208,108 @@ PORT_CLASSES = {
 CLASSES_PORT = {v: k for k, v in PORT_CLASSES.items()}
 import re
 
+class CIMWarpReport(object):
+    def __init__(self, game):
+        self.game = game
+        self.queue_game = game.queue_game
+        self.queue_player = game.queue_player
+        self.observer = game.observer
+        # Yes, at this point we would activate
+        self.prompt = game.buffer
+        self.save = self.observer.save()
+
+        # I actually don't want the player input, but I'll grab it anyway.
+        self.observer.connect("player", self.player)
+
+        self.observer.connect("prompt", self.game_prompt)
+        self.observer.connect("game-line", self.game_line)
+
+        # If we want it, it's here.
+        self.defer = None
+        self.to_player = self.game.to_player
+
+        # Hide what's happening from the player
+        self.game.to_player = False
+
+        self.queue_player.put("^")  # Activate CIM
+        self.state = 1
+        self.warpdata = {}
+        self.warpcycle = cycle(["/", "-", "\\", "|"])
+
+    def game_prompt(self, prompt):
+        if prompt == ": ":
+            if self.state == 1:
+                # Ok, then we're ready to request the port report
+                self.warpcycle = cycle(["/", "-", "\\", "|"])
+                self.queue_player.put("I")
+                self.state = 2
+            elif self.state == 2:
+                self.queue_player.put("Q")
+                self.state = 3
+        if re.match(r"Command \[TL=.* \(\?=Help\)\? :", prompt):
+            if self.state == 3:
+                # Ok, time to exit
+                # exit from this...
+                self.game.to_player = self.to_player
+                self.observer.load(self.save)
+                self.save = None
+                self.game.warpdata = self.warpdata
+                self.queue_game.put("\b \b\r\n")
+
+                if not self.defer is None:
+                    self.defer.callback(self.warpdata)
+                    self.defer = None
+
+    def game_line(self, line):
+        if line == "" or line == ": ":
+            return
+        if line == ": ENDINTERROG":
+            return
+
+        # This should be the CIM Report Data -- parse it
+        if self.warpcycle:
+            if len(self.warpdata) % 10 == 0:
+                self.queue_game.put("\b" + next(self.warpcycle))
+
+        work = line.strip()
+        parts = re.split(r"(?<=\d)\s", work)
+        parts = [int(x) for x in parts]
+        sector = parts.pop(0)
+        # tuples are nicer on memory, and the warpdata map isn't going to be changing.
+        self.warpdata[sector] = tuple(parts)
+
+    def __del__(self):
+        log.msg("CIMWarpReport {0} RIP".format(self))
+
+    def whenDone(self):
+        self.defer = defer.Deferred()
+        # Call this to chain something after we exit.
+        return self.defer
+
+    def player(self, chunk):
+        """ Data from player (in bytes). """
+        chunk = chunk.decode("utf-8", "ignore")
+        key = chunk.upper()
+        log.msg("CIMWarpReport.player({0}) : I AM stopping...".format(key))
+
+        # Stop the keepalive if we are activating something else
+        # or leaving...
+        # self.keepalive.stop()
+
+        self.queue_game.put("\b \b\r\n")
+
+        if not self.defer is None:
+            # We have something, so:
+            self.game.to_player = self.to_player
+            self.observer.load(self.save)
+            self.save = None
+            self.defer.errback(Exception("User Abort"))
+            self.defer = None
+        else:
+            # Still "exit" out.
+            self.game.to_player = self.to_player
+            self.observer.load(self.save)
+
 
 class CIMPortReport(object):
     """ Parse data from CIM Port Report 
@@ -461,6 +563,12 @@ class ScriptPort(object):
                     d.addCallback(got_need1)
                 else:
                     # We already have our target port, so...
+
+                    self.queue_game.put(self.r + self.nl + "Trading from {0} ({1}) to default {2} ({3}).".format(
+                        self.this_sector, 
+                        self.game.portdata[self.this_sector]['port'],
+                        self.sector2, self.game.portdata[self.sector2]['port']) + self.nl
+                        )
                     pi = PlayerInput(self.game)
                     def got_need2(*_):
                         if pi.keep['count'].strip() == '':
@@ -793,6 +901,7 @@ class ProxyMenu(object):
         menu_item("T", "Display current Time")
         menu_item("P", "Port CIM Report")
         menu_item("S", "Scripts")
+        menu_item("W", "Warp CIM Report")
         menu_item("X", "eXit")
         self.queue_game.put("   " + self.c + "-=>" + self.r + " ")
 
@@ -805,6 +914,11 @@ class ProxyMenu(object):
         self.queue_game.put("Loaded {0} records.".format(len(portdata)) + self.nl)
         self.welcome_back()
 
+    def warp_report(self, warpdata: dict):
+        self.warpdata = warpdata
+        self.queue_game.put("Loaded {0} records.".format(len(warpdata)) + self.nl)
+        self.welcome_back()
+
     def player(self, chunk: bytes):
         """ Data from player (in bytes). """
         chunk = chunk.decode("utf-8", "ignore")
@@ -830,6 +944,14 @@ class ProxyMenu(object):
             d.addCallback(self.port_report)
             d.addErrback(self.welcome_back)
             return
+        elif key == "W":
+            self.queue_game.put(self.c + key + self.r + self.nl)
+            # Activate CIM Port Report
+            report = CIMWarpReport(self.game)
+            d = report.whenDone()
+            d.addCallback(self.warp_report)
+            d.addErrback(self.welcome_back)            
+            return
         elif key == "S":
             self.queue_game.put(self.c + key + self.r + self.nl)
             self.activate_scripts_menu()
@@ -837,6 +959,7 @@ class ProxyMenu(object):
         elif key == "D":
             self.queue_game.put(self.c + key + self.r + self.nl)
             self.queue_game.put(pformat(self.portdata).replace("\n", "\n\r") + self.nl)
+            self.queue_game.put(pformat(self.warpdata).replace("\n", "\n\r") + self.nl)    
         elif key == "Q":
             self.queue_game.put(self.c + key + self.r + self.nl)