Forráskód Böngészése

Updated Trade Report! background/coiterate.

Now shows port percentages!  Don't trade at an
almost burnt port.
Steve Thielemann 5 éve
szülő
commit
bec88fd63a
1 módosított fájl, 100 hozzáadás és 50 törlés
  1. 100 50
      flexible.py

+ 100 - 50
flexible.py

@@ -3,6 +3,8 @@ from twisted.internet import task
 from twisted.internet import defer
 from twisted.internet import defer
 from colorama import Fore, Back, Style
 from colorama import Fore, Back, Style
 from twisted.python import log
 from twisted.python import log
+from twisted.internet.task import coiterate
+from twisted.internet.defer import gatherResults
 
 
 from itertools import cycle
 from itertools import cycle
 import pendulum
 import pendulum
@@ -638,7 +640,7 @@ class ScriptPort(object):
                             return
                             return
                         self.times_left = int(pi.keep['count'])
                         self.times_left = int(pi.keep['count'])
                         log.msg("Ok, I have:", pi.keep)
                         log.msg("Ok, I have:", pi.keep)
-                        self.queue_game.put(pformat(pi.keep).replace("\n", "\n\r"))
+                        # self.queue_game.put(pformat(pi.keep).replace("\n", "\n\r"))
                         self.state = 5
                         self.state = 5
                         self.trade()
                         self.trade()
 
 
@@ -935,6 +937,7 @@ class ProxyMenu(object):
         self.queue_game = game.queue_game
         self.queue_game = game.queue_game
         self.observer = game.observer
         self.observer = game.observer
 
 
+        # Am I using self or game?  (I think I want game, not self.)
         if hasattr(self.game, "portdata"):
         if hasattr(self.game, "portdata"):
             self.portdata = self.game.portdata
             self.portdata = self.game.portdata
         else:
         else:
@@ -976,11 +979,19 @@ class ProxyMenu(object):
             )
             )
 
 
         menu_item("D", "Diagnostics")
         menu_item("D", "Diagnostics")
-        menu_item("Q", "Quest")
+        # menu_item("Q", "Quest")
+        if hasattr(self.game, 'portdata'):
+            ports = len(self.game.portdata)
+        else:
+            ports = '?'
+        menu_item("P", "Port CIM Report ({0})".format(ports))
+        if hasattr(self.game, 'warpdata'):
+            warps = len(self.game.warpdata)
+        else:
+            warps = '?'
+        menu_item("W", "Warp CIM Report ({0})".format(warps))        
         menu_item("T", "Trading Report")
         menu_item("T", "Trading Report")
-        menu_item("P", "Port CIM Report")
         menu_item("S", "Scripts")
         menu_item("S", "Scripts")
-        menu_item("W", "Warp CIM Report")
         menu_item("X", "eXit")
         menu_item("X", "eXit")
         self.queue_game.put("   " + self.c + "-=>" + self.r + " ")
         self.queue_game.put("   " + self.c + "-=>" + self.r + " ")
 
 
@@ -990,12 +1001,88 @@ class ProxyMenu(object):
 
 
     def port_report(self, portdata: dict):
     def port_report(self, portdata: dict):
         self.portdata = portdata
         self.portdata = portdata
-        self.queue_game.put("Loaded {0} records.".format(len(portdata)) + self.nl)
+        self.game.portdata = portdata
+        self.queue_game.put("Loaded {0} ports.".format(len(portdata)) + self.nl)
         self.welcome_back()
         self.welcome_back()
 
 
     def warp_report(self, warpdata: dict):
     def warp_report(self, warpdata: dict):
         self.warpdata = warpdata
         self.warpdata = warpdata
-        self.queue_game.put("Loaded {0} records.".format(len(warpdata)) + self.nl)
+        self.game.warpdata = warpdata
+        self.queue_game.put("Loaded {0} sectors.".format(len(warpdata)) + self.nl)
+        self.welcome_back()
+
+    def make_trade_report(self):
+        log.msg("make_trade_report()")
+        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):
+                pc = pd['class']
+
+                # Ok, let's look into it.
+                if not sector in self.game.warpdata:
+                    continue
+
+                warps = self.game.warpdata[sector]
+                for w in warps:
+                    # Verify that we have that warp's info, and that the sector is in it.
+                    # (We can get back from it)
+                    if w in self.game.warpdata and sector in self.game.warpdata[w]:
+                        # Ok, we can get there -- and get back!
+                        if w > sector and w in self.game.portdata and not port_burnt(self.game.portdata[w]):
+                            # it is > and has a port.
+                            wd = self.game.portdata[w]
+                            wc = wd['class']
+
+                            # 1: "BBS",
+                            # 2: "BSB",
+                            # 3: "SBB",
+                            # 4: "SSB",
+                            # 5: "SBS",
+                            # 6: "BSS",
+                            # 7: "SSS",
+                            # 8: "BBB",
+
+                            if pc in (1,5) and wc in (2,4):
+                                best_trades.append(port_show(sector, pd, w, wd))
+                                # best_trades.append( "{0:5} -=- {1:5}".format(sector, w))
+                            elif pc in (2,4) and wc in (1,5):
+                                best_trades.append(port_show(sector, pd, w, wd))
+                                # best_trades.append( "{0:5} -=- {1:5}".format(sector, w))
+                            elif port_trading(pd['port'], self.game.portdata[w]['port']):
+                                # ok_trades.append( "{0:5} -=- {1:5}".format(sector,w))
+                                ok_trades.append(port_show(sector, pd, w, wd))
+            yield
+
+        self.trade_report.append("Best Trades: (org/equ)")
+        self.trade_report.extend(best_trades)
+        self.trade_report.append("Ok Trades:")
+        self.trade_report.extend(ok_trades)
+        # self.queue_game.put("BEST TRADES:" + self.nl + self.nl.join(best_trades) + self.nl)
+        # self.queue_game.put("OK TRADES:" + self.nl + self.nl.join(ok_trades) + self.nl)
+
+    def show_trade_report(self, *_):
+        for t in self.trade_report:
+            self.queue_game.put(t + self.nl)
         self.welcome_back()
         self.welcome_back()
 
 
     def player(self, chunk: bytes):
     def player(self, chunk: bytes):
@@ -1011,6 +1098,8 @@ class ProxyMenu(object):
         if key == "T":
         if key == "T":
             self.queue_game.put(self.c + key + self.r + self.nl)
             self.queue_game.put(self.c + key + self.r + self.nl)
 
 
+            # do we have enough information to do this?
+
             if not hasattr(self.game, 'portdata') and not hasattr(self.game, 'warpdata'):
             if not hasattr(self.game, 'portdata') and not hasattr(self.game, 'warpdata'):
                 self.queue_game.put("Missing portdata and warpdata." + self.nl)
                 self.queue_game.put("Missing portdata and warpdata." + self.nl)
             elif not hasattr(self.game, 'portdata'):
             elif not hasattr(self.game, 'portdata'):
@@ -1018,50 +1107,11 @@ class ProxyMenu(object):
             elif not hasattr(self.game, 'warpdata'):
             elif not hasattr(self.game, 'warpdata'):
                 self.queue_game.put("Missing warpdata." + self.nl)
                 self.queue_game.put("Missing warpdata." + self.nl)
             else:
             else:
-                # Ok, for each port
-                ok_trades = []
-                best_trades = []
-
-                # This is a very BAD idea to do something like this in twisted!
-                # (At least like this).  TO FIX. 
-                for sector, pd in self.game.portdata.items():
-                    if not port_burnt(pd):
-                        pc = pd['class']
-
-                        # Ok, let's look into it.
-                        if not sector in self.game.warpdata:
-                            continue
-
-                        warps = self.game.warpdata[sector]
-                        for w in warps:
-                            # Verify that we have that warp's info, and that the sector is in it.
-                            # (We can get back from it)
-                            if w in self.game.warpdata and sector in self.game.warpdata[w]:
-                                # Ok, we can get there -- and get back!
-                                if w > sector and w in self.game.portdata and not port_burnt(self.game.portdata[w]):
-                                    # it is > and has a port.
-                                    wc = self.game.portdata[w]['class']
-
-                                    # 1: "BBS",
-                                    # 2: "BSB",
-                                    # 3: "SBB",
-                                    # 4: "SSB",
-                                    # 5: "SBS",
-                                    # 6: "BSS",
-                                    # 7: "SSS",
-                                    # 8: "BBB",
-
-                                    if pc in (1,5) and wc in (2,4):
-                                        best_trades.append( "{0:5} -=- {1:5}".format(sector, w))
-                                    elif pc in (2,4) and wc in (1,5):
-                                        best_trades.append( "{0:5} -=- {1:5}".format(sector, w))
-                                    elif port_trading(pd['port'], self.game.portdata[w]['port']):
-                                        ok_trades.append( "{0:5} -=- {1:5}".format(sector,w))
-
-                self.queue_game.put("BEST TRADES:" + self.nl + self.nl.join(best_trades) + self.nl)
-                self.queue_game.put("OK TRADES:" + self.nl + self.nl.join(ok_trades) + self.nl)
-                
-
+                # Yes, so let's start!
+                self.trade_report = []
+                d = coiterate(self.make_trade_report())
+                d.addCallback(self.show_trade_report)
+                return      
         elif key == "P":
         elif key == "P":
             self.queue_game.put(self.c + key + self.r + self.nl)
             self.queue_game.put(self.c + key + self.r + self.nl)
             # Activate CIM Port Report
             # Activate CIM Port Report