Przeglądaj źródła

black format + pylint. (bugs found/fixed).

Steve Thielemann 5 lat temu
rodzic
commit
940cc003cb
1 zmienionych plików z 55 dodań i 52 usunięć
  1. 55 52
      tcp-proxy.py

+ 55 - 52
tcp-proxy.py

@@ -129,7 +129,7 @@ class Game(protocol.Protocol):
         else:
             # Pass received data to the server
             if type(chunk) == str:
-                self.transport.write(chunk.encode('latin-1'))
+                self.transport.write(chunk.encode("latin-1"))
                 log.msg(">> [{0}]".format(chunk))
             else:
                 self.transport.write(chunk)
@@ -200,69 +200,75 @@ class Game(protocol.Protocol):
                 self.linestate = "cim"
 
     def sectorline(self, line: str):
-        log.msg("sector:", self.current_sector, ':', line)
-        if line.startswith('Beacon  : '):
-            pass # get beacon text
-        elif line.startswith('Ports   : '):
+        log.msg("sector:", self.current_sector, ":", line)
+        if line.startswith("Beacon  : "):
+            pass  # get beacon text
+        elif line.startswith("Ports   : "):
             # Ports   : Ballista, Class 1 (BBS)
-            self.sector_state = 'port'            
-            if '<=-DANGER-=>' in line:
+            self.sector_state = "port"
+            if "<=-DANGER-=>" in line:
                 # Port is destroyed
-                if sector in self.gamedate.ports:
-                    del self.gamedata.ports[sector]
-            elif '(StarDock)' not in line:
-                _, _, class_port = line.partition(', Class ')
-                c, port = class_port.split(' ')
+                if self.current_sector in self.gamedata.ports:
+                    del self.gamedata.ports[self.current_sector]
+            elif "(StarDock)" not in line:
+                _, _, class_port = line.partition(", Class ")
+                c, port = class_port.split(" ")
                 c = int(c)
-                port = port.replace('(', '').replace(')', '')                
-                data = { 'port': port, 'class': c }
+                port = port.replace("(", "").replace(")", "")
+                data = {"port": port, "class": c}
                 self.gamedata.set_port(self.current_sector, data)
 
-        elif line.startswith('Planets : '):
+        elif line.startswith("Planets : "):
             # Planets : (O) Flipper
-            self.sector_state = 'planet'
-        elif line.startswith('Traders : '):
-            self.sector_state = 'trader'
-        elif line.startswith('Ships   : '):
-            self.sector_state = 'ship'
-        elif line.startswith('Fighters: '):
-            self.sector_state = 'fighter'
-        elif line.startswith('NavHaz  : '):
+            self.sector_state = "planet"
+        elif line.startswith("Traders : "):
+            self.sector_state = "trader"
+        elif line.startswith("Ships   : "):
+            self.sector_state = "ship"
+        elif line.startswith("Fighters: "):
+            self.sector_state = "fighter"
+        elif line.startswith("NavHaz  : "):
             pass
-        elif line.startswith('Mines   : '):
-            self.sector_state = 'mine'
-        elif line.startswith('        '):
+        elif line.startswith("Mines   : "):
+            self.sector_state = "mine"
+        elif line.startswith("        "):
             # continues
-            if self.sector_state == 'mines':
+            if self.sector_state == "mines":
                 pass
-            if self.sector_state == 'planet':
+            if self.sector_state == "planet":
                 pass
-            if self.sector_state == 'trader':
+            if self.sector_state == "trader":
                 pass
-            if self.sector_state == 'ship':
+            if self.sector_state == "ship":
                 pass
-        elif len(line) > 8 and line[8] == ':':
-            self.sector_state = 'normal'
-        elif line.startswith('Warps to Sector(s) :'):
+        elif len(line) > 8 and line[8] == ":":
+            self.sector_state = "normal"
+        elif line.startswith("Warps to Sector(s) :"):
             # Warps to Sector(s) :  5468
-            _, _, work = line.partition(':')
-            work = work.strip().replace('(', '').replace(')', '').replace(' - ', ' ')
-            parts = [ int(x) for x in work.split(' ')]
+            _, _, work = line.partition(":")
+            work = work.strip().replace("(", "").replace(")", "").replace(" - ", " ")
+            parts = [int(x) for x in work.split(" ")]
             log.msg("Sectorline warps", parts)
             self.gamedata.warp_to(self.current_sector, *parts)
-            self.sector_state = 'normal'
-            self.linestate = ''
+            self.sector_state = "normal"
+            self.linestate = ""
 
     def portline(self, line: str):
         # Map these items to which keys
         log.msg("portline({0}): {1}".format(self.current_sector, line))
-        mapto = { 'Fuel': 'fuel', 'Organics': 'org', 'Equipment': 'equ'}
+        mapto = {"Fuel": "fuel", "Organics": "org", "Equipment": "equ"}
 
-        if '%' in line:
+        if "%" in line:
             # Fuel Ore   Buying    2890    100%       0
-            work = line.replace('Fuel Ore', 'Fuel').replace('%', '')
+            work = line.replace("Fuel Ore", "Fuel").replace("%", "")
             parts = re.split(r"\s+", work)
-            data = { mapto[parts[0]] : { 'sale': parts[1][0], 'units': parts[2], 'pct': int(parts[3]) } }
+            data = {
+                mapto[parts[0]]: {
+                    "sale": parts[1][0],
+                    "units": parts[2],
+                    "pct": int(parts[3]),
+                }
+            }
             # log.msg("Setting {0} to {1}".format(self.current_sector, data))
             self.gamedata.set_port(self.current_sector, data)
             # log.msg("NOW: {0}".format(self.gamedata.ports[self.current_sector]))
@@ -332,9 +338,9 @@ class Game(protocol.Protocol):
             self.current_sector = int(parts[2])
         elif self.linestate == "sector":
             self.sectorline(line)
-        elif self.linestate == 'port':
-            if line == '':
-                self.linestate = ''
+        elif self.linestate == "port":
+            if line == "":
+                self.linestate = ""
             else:
                 self.portline(line)
 
@@ -374,7 +380,7 @@ class Game(protocol.Protocol):
                 )
                 chunk = (
                     chunk[0 : pos + len(target)]
-                    + message.encode('latin-1')
+                    + message.encode("latin-1")
                     + chunk[pos + len(target) :]
                 )
 
@@ -460,9 +466,9 @@ class Player(protocol.Protocol):
             if type(chunk) == bytes:
                 self.transport.write(chunk)
             elif type(chunk) == str:
-                self.transport.write(chunk.encode('latin-1'))
+                self.transport.write(chunk.encode("latin-1"))
             else:
-                log.err("gameDataReceived: type (%s) given!".format(type(chunk)))
+                log.err("gameDataReceived: type ({0}) given!".format(type(chunk)))
                 self.transport.write(chunk)
             self.setGameReceived()
 
@@ -531,7 +537,6 @@ class Player(protocol.Protocol):
         log.msg("connectionFailed: %s" % why)
 
 
-
 class GlueFactory(protocol.ClientFactory):
     # class GlueFactory(protocol.Factory):
     maxDelay = 10
@@ -550,7 +555,7 @@ class GlueFactory(protocol.ClientFactory):
 
     def getUser(self, user):
         log.msg("getUser( %s )" % user)
-        self.twgs.logUser(user)
+        self.game.logUser(user)
 
     # This was needed when I replaced ClientFactory with Factory.
     # def clientConnectionLost(self, connector, why):
@@ -566,7 +571,6 @@ class GlueFactory(protocol.ClientFactory):
         reactor.callLater(2, self.closeIt)
 
 
-
 if __name__ == "__main__":
     if "logfile" in config and config["logfile"]:
         log.startLogging(DailyLogFile("proxy.log", "."))
@@ -582,7 +586,6 @@ else:
     # I can't seem to get twistd -y tcp-proxy.py
     # to work.  Missing imports?
     pass
-    # application = service.Application("TradeWarsGameServer-Proxy")
     # factory = protocol.Factory()
     # factory.protocol = Player
     # internet.TCPServer(config["listen_port"], factory).setServiceParent(application)