Browse Source

Fixed going to other port loop/bug.

Added port_buying to galaxy to determine if the other
port can buy what we can't currently sell.

Fixed other errors with the new log routines.
Steve Thielemann 5 years ago
parent
commit
af18fb60c6
3 changed files with 83 additions and 49 deletions
  1. 49 48
      flexible.py
  2. 33 0
      galaxy.py
  3. 1 1
      proxy.py

+ 49 - 48
flexible.py

@@ -156,7 +156,7 @@ class PlayerInput(object):
         assert self.deferred is None
         d = defer.Deferred()
         self.deferred = d
-        log.debug("Return deferred ...", self.deferred)
+        log.debug("Return deferred ...")
         return d
 
     def get_input(self, chunk):
@@ -553,6 +553,7 @@ class ScriptPort(object):
     def deactivate(self):
         self.state = 0
         log.debug("ScriptPort.deactivate ({0})".format(self.times_left))
+        self.queue_game.put(self.nl + Boxes.alert("Trading Script deactivating...", width=50))
         assert(not self.save is None)
         self.observer.load(self.save)
         self.save = None
@@ -616,12 +617,6 @@ class ScriptPort(object):
                         self.sector2)
                     ))
 
-                    # The code is smart enough now, that this can't happen.  :(   Drat!
-                    if self.game.gamedata.ports[self.this_sector]['port'] == self.game.gamedata.ports[self.sector2]['port']:
-                        self.queue_game.put("Hey dummy! Look out the window!  These ports are the same class!" + nl)
-                        self.deactivate()
-                        return
-
                     pi = PlayerInput(self.game)
                     def got_need2(*_):
                         if pi.keep['count'].strip() == '':
@@ -639,26 +634,25 @@ class ScriptPort(object):
         elif self.state == 6:
             if re.match(r"Command \[TL=.* \(\?=Help\)\? :", prompt):
                 if self.fixable:
-                    self.queue_game.put("Ok! Let's fix this by going to the other sector..." + self.nl)
+                    # self.queue_game.put("Ok! Let's fix this by going to the other sector..." + self.nl)
+                    self.queue_game.put(self.nl + Boxes.alert("Ok, FINE.  We'll trade with the other port.", base="green", style=0))
                     log.debug("Fixing...")
-                    if self.this_sector == self.sector1:
-                        self.this_sector = self.sector2
-                        self.queue_player.put("{0}\r".format(self.sector2))
-                        self.state = 5
-                        self.trade()
-                    else:
-                        self.this_sector = self.sector1
-                        self.queue_player.put("{0}\r".format(self.sector1))
-                        self.state = 5
-                        self.trade()
+
+                    # Swap this and other sector
+                    self.this_sector, self.other_sector = (self.other_sector, self.this_sector)
+
+                    self.queue_player.put("{0}\r".format(self.sector2))
+                    self.state = 5
+                    self.trade()
+
         elif self.state == 7:
             if re.match(r"Command \[TL=.* \(\?=Help\)\? :", prompt):
                 # Done
-                if self.this_sector == self.sector1:
-                    self.this_sector = self.sector2
-                    self.queue_player.put("{0}\r".format(self.sector2))
-                    self.state = 10
-                else:
+
+                # Swap this and other sector
+                self.this_sector, self.other_sector = (self.other_sector, self.this_sector)
+
+                if self.this_sector == self.sector2:
                     self.times_left -= 1
                     if self.times_left <= 0:
                         # Ok, exit out
@@ -672,9 +666,10 @@ class ScriptPort(object):
                             self.queue_game.put(self.r + self.nl + "We don't seem to be making any money here.  I'm stopping!" + self.nl)
                             self.deactivate()
                             return
-                    self.this_sector = self.sector1
-                    self.queue_player.put("{0}\r".format(self.sector1))
-                    self.state = 10
+
+                self.queue_player.put("{0}\r".format(self.this_sector))
+                self.state = 10
+
             elif re.match(r'Your offer \[\d+\] \?', prompt):
                 if self.fix_offer:
                     # Make real offer / WHAT?@?!
@@ -732,11 +727,11 @@ class ScriptPort(object):
                 self.queue_player.put("\r")
             elif re.match(r"Command \[TL=.* \(\?=Help\)\? :", prompt):
                 # Done
-                if self.this_sector == self.sector1:
-                    self.this_sector = self.sector2
-                    self.queue_player.put("{0}\r".format(self.sector2))
-                    self.state = 10
-                else:
+
+                # Swap this and other sector
+                self.this_sector, self.other_sector = (self.other_sector, self.this_sector)
+
+                if self.this_sector == self.sector2:
                     self.times_left -= 1
                     if self.times_left <= 0:
                         # Ok, exit out
@@ -747,12 +742,21 @@ class ScriptPort(object):
                     else:
                         if self.credits <= self.last_credits:
                             log.warn("We don't seem to be making any money here...")
+                            self.queue_game.put(self.r + self.nl + "We don't seem to be making any money here.  I'm stopping!" + self.nl)                            
                             self.deactivate()
                             return                        
-                    self.this_sector = self.sector1
-                    self.queue_player.put("{0}\r".format(self.sector1))
-                    self.state = 10
 
+                self.queue_player.put("{0}\r".format(self.this_sector))
+                self.state = 10
+
+        elif self.state == 99:
+            # This is a good place to deactivate at.
+            if re.match(r"Command \[TL=.* \(\?=Help\)\? :", prompt):
+                if hasattr(self, 'message'):
+                    if self.message is not None:
+                        self.queue_game.put(self.message)
+                    self.message = None
+                self.deactivate()
 
 
         
@@ -762,10 +766,14 @@ class ScriptPort(object):
         self.queue_player.put("pt")   # Port Trade
 
         self.this_port = self.game.gamedata.ports[self.this_sector]
+        # I think other_sector will alway be correct, but leaving this
+        # for now.   FUTURE: TODO:  REMOVE
         if self.this_sector == self.sector1:
-            self.other_port = self.game.gamedata.ports[self.sector2]
+            self.other_sector = self.sector2
         else:
-            self.other_port = self.game.gamedata.ports[self.sector1]
+            self.other_sector = self.sector1
+
+        self.other_port = self.game.gamedata.ports[self.other_sector]
 
         # Ok, perform some calculations
         self.tpc = self.this_port['class']
@@ -832,7 +840,7 @@ class ScriptPort(object):
                             return
 
                     possible = [ x for x in self.warps if x in self.game.gamedata.ports ]
-                    log.debug("Possible:", possible)
+                    log.debug("Possible: {0}".format(possible))
 
                     # BUG:  Sometimes links to another sector, don't link back!
                     # This causes the game to plot a course / autopilot.
@@ -849,7 +857,7 @@ class ScriptPort(object):
                         return
 
                     possible = [ x for x in possible if not GameData.port_burnt(self.game.gamedata.ports[x]) ]
-                    log.debug("Possible:", possible)
+                    log.debug("Possible: {0}".format(possible))
 
                     if len(possible) == 0:
                         self.state = 0
@@ -901,20 +909,13 @@ class ScriptPort(object):
                 if parts[-1] != '0' and parts[2] != '0' and parts[1] != 'Buying':
                     log.warn("We have a problem -- they aren't buying what we have in stock!")
                     stuff = line[0]  # F O or E.
-                    if stuff == 'F':
-                        spos = 0
-                    elif stuff == 'O':
-                        spos = 1
-                    else:
-                        spos = 2
-                    other = self.other_port['port']
-                    if other[spos] == 'B':
-                        self.fixable = 1
+                    if self.game.gamedata.port_buying(self.other_sector, stuff):
+                        self.fixable = True
 
             if "You don't have anything they want" in line:
                 # Neither!  DRAT!
                 if not self.fixable:
-                    self.deactivate()
+                    self.state = 99
                     return
             if "We're not interested." in line:
                 log.warn("Try, try again.  :(")

+ 33 - 0
galaxy.py

@@ -211,6 +211,39 @@ class GameData(object):
                 self.ports[sector]["class"] = CLASSES_PORT[port]
                 log.debug("completed {0} : {1}".format(sector, self.ports[sector]))
 
+    def port_buying(self, sector: int, cargo: str):
+        """ Given a sector, is this port buying this? 
+
+        cargo is a char (F/O/E)
+        """
+        cargo_to_index = {"F": 0, "O": 1, "E": 2}
+        cargo_types = ("fuel", "org", "equ")
+
+        cargo = cargo[0]
+        if sector not in self.ports:
+            log.warn("port_buying( {0}, {1}): sector unknown!".format(sector, cargo))
+            return False
+        port = self.ports[sector]
+        if "port" not in port:
+            log.warn("port_buying( {0}, {1}): port unknown!".format(sector, cargo))
+            return True
+
+        cargo_index = cargo_to_index[cargo]
+        if port["port"][cargo_index] == "S":
+            log.warn("port_buying( {0}, {1}): not buying cargo".format(sector, cargo))
+            return False
+        # ok, they buy it, but *WILL THEY* really buy it?
+        cargo_key = cargo_types[cargo_index]
+        if cargo_key in port:
+            if int(port[cargo_key]["units"]) > 40:
+                log.warn(
+                    "port_buying( {0}, {1}): Yes, buying {2}".format(
+                        sector, cargo, port[cargo_key]["sale"]
+                    )
+                )
+                return True
+        return False
+
     @staticmethod
     def port_burnt(port: dict):
         """ Is this port burned out? """

+ 1 - 1
proxy.py

@@ -113,7 +113,7 @@ class Game(protocol.Protocol):
             self.setPlayerReceived()
 
     def warpline(self, line: str):
-        self.log.debug("warp:", line)
+        self.log.debug("warp: " + line)
         # 1 > 3 > 5 > 77 > 999
         last_sector = self.lastwarp
         line = line.replace("(", "").replace(")", "").replace(">", "").strip()