Browse Source

Fixed not making money. Better port info display.

Steve Thielemann 5 years ago
parent
commit
d099c140f9
1 changed files with 69 additions and 33 deletions
  1. 69 33
      flexible.py

+ 69 - 33
flexible.py

@@ -536,6 +536,8 @@ class ScriptPort(object):
         self.sector2 = None         # Next Sector Stop
         self.percent = 5            # Stick with the good default.
         self.credits = 0
+        self.last_credits = None
+        self.times_left = 0
 
         # Activate
         self.prompt = game.buffer
@@ -572,7 +574,7 @@ class ScriptPort(object):
 
     def deactivate(self):
         self.state = 0
-        log.msg("ScriptPort.deactivate")
+        log.msg("ScriptPort.deactivate ({0})".format(self.times_left))
         assert(not self.save is None)
         self.observer.load(self.save)
         self.save = None
@@ -593,9 +595,11 @@ class ScriptPort(object):
                 log.msg("Ok, state 4")
                 if self.sector2 is None:
                     # Ok, we need to prompt for this.
-                    self.queue_game.put(self.r + self.nl + "Which sector to trade with? {0} ({1})".format(self.this_sector, self.game.portdata[self.this_sector]['port']) + self.nl + Fore.CYAN)
+                    self.queue_game.put(self.r + self.nl + 
+                        "Which sector to trade with? {0}".format(port_show_part(self.this_sector, self.game.portdata[self.this_sector])) +
+                        self.nl + Fore.CYAN)
                     for i, p in enumerate(self.possible):
-                        self.queue_game.put(" " + str(i + 1) + " : " + str(p) + " (" + self.game.portdata[p]['port'] + ")" + self.nl)
+                        self.queue_game.put(" " + str(i + 1) + " : " + port_show_part(p, self.game.portdata[p]) + self.nl)
                     
                     pi = PlayerInput(self.game)
                     def got_need1(*_):
@@ -628,6 +632,15 @@ class ScriptPort(object):
                         self.game.portdata[self.this_sector]['port'],
                         self.sector2, self.game.portdata[self.sector2]['port']) + self.nl
                         )
+
+                    self.queue_game.put(self.r + self.nl + "Trading {0}".format(
+                        port_show(self.this_sector, 
+                            self.game.portdata[self.this_sector],
+                            self.sector2, 
+                            self.game.portdata[self.sector2])
+                    ))
+
+                    # The code is smart enough now, that this can't happen.  :(   Drat!
                     if self.game.portdata[self.this_sector]['port'] == self.game.portdata[self.sector2]['port']:
                         self.queue_game.put("Hey dummy! Look out the window!  These ports are the same class!" + nl)
                         self.deactivate()
@@ -661,6 +674,14 @@ class ScriptPort(object):
                         # Ok, exit out
                         self.deactivate()
                         return
+                    if self.last_credits is None:
+                        self.last_credits = self.credits
+                    else:
+                        if self.credits <= self.last_credits:
+                            log.msg("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
@@ -713,6 +734,13 @@ class ScriptPort(object):
                         # Ok, exit out
                         self.deactivate()
                         return
+                    if self.last_credits is None:
+                        self.last_credits = self.credits
+                    else:
+                        if self.credits <= self.last_credits:
+                            log.msg("We don't seem to be making any money here...")
+                            self.deactivate()
+                            return                        
                     self.this_sector = self.sector1
                     self.queue_player.put("{0}\r".format(self.sector1))
                     self.state = 10
@@ -750,6 +778,12 @@ class ScriptPort(object):
         pass
 
     def game_line(self, line: str):
+        if line.startswith("You have ") and 'credits and' in line:
+            parts = line.replace(',', '').split()
+            credits = int(parts[2])
+            log.msg("Credits: {0}".format(credits))
+            self.credits = credits
+
         if self.state == 1:
             # First exploration
             if line.startswith("Sector  :"):
@@ -872,16 +906,6 @@ class ScriptPort(object):
                 # Buy
                 self.state = 8
                 self.sell_perc = 100 - self.percent      
-            if line.startswith("You have ") and 'credits and' in line:
-                parts = line.replace(',', '').split()
-                credits = int(parts[2])
-                if self.credits == 0:
-                    self.credits = credits
-                else:
-                    if credits <= self.credits:
-                        log.msg("We don't appear to be making any money here {0}.".format(credits))
-                        self.deactivate()
-                        return
             if "We're not interested." in line:
                 log.msg("Try, try again.  :(")
                 self.state = 5
@@ -916,6 +940,27 @@ class ScriptPort(object):
         #     self.deactivate()
         #     return
 
+def port_pct(port):
+    # Make sure these exist in the port data given.
+    if all( x in port for x in ['fuel', 'org', 'equ']):
+        return "{0:3},{1:3},{2:3}%".format(
+            port['fuel']['pct'],
+            port['org']['pct'],
+            port['equ']['pct'])
+    else:
+        return "---,---,---%"
+
+def port_show_part(sector, sector_port):
+    return "{0:5} ({1}) {2}".format(sector, sector_port['port'], port_pct(sector_port))
+
+def port_show(sector, sector_port, warp, warp_port):
+    sector_pct = port_pct(sector_port)
+    warp_pct = port_pct(warp_port)
+    return "{0} -=- {1}".format(
+        port_show_part(sector, sector_port),
+        port_show_part(warp, warp_port)
+    )
+
 class ProxyMenu(object):
     """ Display ProxyMenu 
     
@@ -948,6 +993,11 @@ class ProxyMenu(object):
         else:
             self.warpdata = {}
 
+        if hasattr(self.game, 'trade_report'):
+            self.trade_report = self.game.trade_report
+        else:
+            self.trade_report = []
+
         # Yes, at this point we would activate
         self.prompt = game.buffer
         self.save = self.observer.save()
@@ -978,7 +1028,7 @@ class ProxyMenu(object):
                 " " + self.c1 + ch + self.c2 + " - " + self.c1 + desc + self.nl
             )
 
-        menu_item("D", "Diagnostics")
+        menu_item("D", "Display Report again")
         # menu_item("Q", "Quest")
         if hasattr(self.game, 'portdata'):
             ports = len(self.game.portdata)
@@ -1016,23 +1066,6 @@ class ProxyMenu(object):
         ok_trades = []
         best_trades = []
 
-        def port_pct(port):
-            return "{0:3},{1:3},{2:3}%".format(
-                port['fuel']['pct'],
-                port['org']['pct'],
-                port['equ']['pct'])
-
-        def port_show(sector, sector_port, warp, warp_port):
-            sector_pct = port_pct(sector_port)
-            warp_pct = port_pct(warp_port)
-            return "{0:5} ({1}) {2}-=- {3:5} ({4}) {5}".format(
-                sector,
-                sector_port['port'],
-                port_pct(sector_port),
-                warp,
-                warp_port['port'],
-                port_pct(warp_port)
-            )
 
         for sector, pd in self.game.portdata.items():
             if not port_burnt(pd):
@@ -1081,6 +1114,7 @@ class ProxyMenu(object):
         # self.queue_game.put("OK TRADES:" + self.nl + self.nl.join(ok_trades) + self.nl)
 
     def show_trade_report(self, *_):
+        self.game.trade_report = self.trade_report
         for t in self.trade_report:
             self.queue_game.put(t + self.nl)
         self.welcome_back()
@@ -1134,8 +1168,10 @@ class ProxyMenu(object):
             return
         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)    
+            for t in self.trade_report:
+                self.queue_game.put(t + 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)