Pārlūkot izejas kodu

Initial work on PlanetUpScript.

So far:  We find the planets (C)omputer (Y)our planets, (Q)uit.
We then get the current sector and see if any are in it.
If so, (and there's just one), we'll default to upgrading that
planet.  Otherwise, we'll have to ask which one.
Steve Thielemann 5 gadi atpakaļ
vecāks
revīzija
0fd1e6a51a
1 mainītis faili ar 117 papildinājumiem un 3 dzēšanām
  1. 117 3
      flexible.py

+ 117 - 3
flexible.py

@@ -229,6 +229,7 @@ class PlayerInput(object):
 import re
 
 # The CIMWarpReport -- is only needed if the json file gets damaged in some way.
+# Like when the universe gets bigbanged. :P
 # or needs to be reset.  The warps should automatically update themselves now.
 
 class CIMWarpReport(object):
@@ -1726,7 +1727,14 @@ class ScriptSpace(object):
 
 
 class ScriptTerror(object):
-    """ Terror script. """
+    """ Terror script. 
+
+    This uses the Port Trading script.
+    Basically, we look for the next best port trading pair.
+    Move to it, fire off the Port Trading script.
+    Repeat until our loop is done, or there's no more
+    pairs.
+    """
     def __init__(self, game, proxy, count):
         self.game = game
         self.queue_game = game.queue_game
@@ -1916,6 +1924,109 @@ class ScriptTerror(object):
                 d.addCallback(self.journey_on)
                 d.addErrback(self.journey_on)
 
+class PlanetUpScript(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
+        self.planets = {}
+
+        # Hide what's happening from the player
+        self.game.to_player = False
+
+        self.queue_player.put("CYQ")  # Computer -> Your Planets -> Quit
+        self.state = 1
+        # self.warpdata = {}
+        self.queue_game.put(Boxes.alert("Let me see what I can see here..."))  
+
+    def game_prompt(self, prompt):
+        if self.state == 2 and re.match(r"Command \[TL=.* \(\?=Help\)\? :", prompt):
+            # For now we output the information, and exit
+            self.queue_game.put("{0}\r\n".format(self.planets))
+            # Command [TL=00:00:00]:[10202] (?=Help)? :
+            _, _, part = prompt.partition(']:[')
+            sector, _, _ = part.partition(']')
+            self.current_sector = int(sector)
+            self.queue_game.put("Current: {0}\r\n".format(sector))
+            # Only ONE planet right in front of us?  Default to upgrading it!
+            # Otherwise, we'll have to ask.
+            obvious = [ s for s in self.planets.keys() if self.planets[s]['sector'] == self.current_sector ]
+            self.queue_game.put(Boxes.alert("Choices: {0}".format(obvious)))
+
+            # Wooah Hoss!  What if there's more then one planet in this sector?!
+
+            self.deactivate()
+
+    def game_line(self, line):
+        if self.state == 1:
+            if 'Personal Planet Scan' in line:
+                self.state = 2
+        elif self.state == 2:
+            # Ok, we're in the planet scan part of this
+            # 10202   #3    Home of Bugz             Class M, Earth Type        No Citadel]
+            if '#' in line:
+                # Ok, we have a planet detail line.
+                detail, _, _ = line.partition('Class')
+                detail = detail.strip() # Sector  #X  Name of planet
+                sector, number, name = re.split(r'\s+', detail, 2)
+                sector = int(sector)
+                number = int(number[1:])
+                self.planets[number] = {"sector": sector, "name": name}
+                log.info("Planet # {0} in {1} called {2}".format( number, sector, name))
+
+    def __del__(self):
+        log.debug("PlanetUpScript {0} RIP".format(self))
+
+    def whenDone(self):
+        self.defer = defer.Deferred()
+        # Call this to chain something after we exit.
+        return self.defer
+
+    def deactivate(self):
+        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.callback(1)
+            self.defer = None
+        else:
+            # Still "exit" out.
+            self.game.to_player = self.to_player
+            self.observer.load(self.save)
+
+    def player(self, chunk):
+        """ Data from player (in bytes). """
+        chunk = chunk.decode("latin-1", "ignore")
+        key = chunk.upper()
+        log.warn("PlanetUpScript.player({0}) : I AM stopping...".format(key))
+
+        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 ProxyMenu(object):
     """ Display ProxyMenu 
@@ -2513,13 +2624,16 @@ class ProxyMenu(object):
             self.queue_game.put(self.c + key + self.r + self.nl)
             space = ScriptSpace(self.game)
             d = space.whenDone()
-
             d.addCallback(self.deactivate_scripts_menu)
             d.addErrback(self.deactivate_scripts_menu)
             return
         elif key == '4':
             self.queue_game.put(self.c + key + self.r + self.nl)
-            # NNY.
+            upgrade = PlanetUpScript(self.game)
+            d = upgrade.whenDone()
+            d.addCallback(self.deactivate_scripts_menu)
+            d.addErrback(self.deactivate_scripts_menu)
+            return
         elif key == 'X':
             self.queue_game.put(self.c + key + self.r + self.nl)            
             self.deactivate_scripts_menu()