|  | @@ -21,15 +21,7 @@ try:
 | 
	
		
			
				|  |  |  except ModuleNotFoundError:
 | 
	
		
			
				|  |  |      from config import *
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -# from config import *
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -# Connect to:
 | 
	
		
			
				|  |  | -# HOST = "twgs"
 | 
	
		
			
				|  |  | -# PORT = 2002
 | 
	
		
			
				|  |  | -# Listen on:
 | 
	
		
			
				|  |  | -# LISTEN_PORT = 2002
 | 
	
		
			
				|  |  | -# LISTEN_ON = "0.0.0.0"
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | +# Extract the version information from git.
 | 
	
		
			
				|  |  |  version = check_output(
 | 
	
		
			
				|  |  |      [
 | 
	
		
			
				|  |  |          "git",
 | 
	
	
		
			
				|  | @@ -47,6 +39,9 @@ version = check_output(
 | 
	
		
			
				|  |  |  # Cleans all ANSI
 | 
	
		
			
				|  |  |  cleaner = re.compile(r"\x1b\[[0-9;]*[A-Zmh]")
 | 
	
		
			
				|  |  |  # Looks for ANSI (that should be considered to be a newline)
 | 
	
		
			
				|  |  | +# This needs to see what is send when something enters / leaves
 | 
	
		
			
				|  |  | +# the player's current sector.  (That doesn't work/isn't
 | 
	
		
			
				|  |  | +# detected.  NNY!)
 | 
	
		
			
				|  |  |  makeNL = re.compile(r"\x1b\[[0-9;]*[J]")
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  
 | 
	
	
		
			
				|  | @@ -66,13 +61,13 @@ def cleanANSI(line):
 | 
	
		
			
				|  |  |  class Game(protocol.Protocol):
 | 
	
		
			
				|  |  |      def __init__(self):
 | 
	
		
			
				|  |  |          self.buffer = ""
 | 
	
		
			
				|  |  | +        self.game = ""
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      def connectionMade(self):
 | 
	
		
			
				|  |  |          log.msg("Connected to Game Server")
 | 
	
		
			
				|  |  |          self.queue_player = self.factory.queue_player
 | 
	
		
			
				|  |  |          self.queue_game = self.factory.queue_game
 | 
	
		
			
				|  |  |          self.setPlayerReceived()
 | 
	
		
			
				|  |  | -        # self.queue_twgs.get().addCallback(self.serverDataReceived)
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      def setPlayerReceived(self):
 | 
	
		
			
				|  |  |          """ Get deferred from client queue, callback clientDataReceived. """
 | 
	
	
		
			
				|  | @@ -80,19 +75,81 @@ class Game(protocol.Protocol):
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      def playerDataReceived(self, chunk):
 | 
	
		
			
				|  |  |          if chunk is False:
 | 
	
		
			
				|  |  | -            self.queue_game = None
 | 
	
		
			
				|  |  | +            self.queue_player = None
 | 
	
		
			
				|  |  |              log.msg("Player: disconnected, close connection to game")
 | 
	
		
			
				|  |  | +            # I don't believe I need this if I'm using protocol.Factory
 | 
	
		
			
				|  |  |              self.factory.continueTrying = False
 | 
	
		
			
				|  |  |              self.transport.loseConnection()
 | 
	
		
			
				|  |  |          else:
 | 
	
		
			
				|  |  |              # Pass received data to the server
 | 
	
		
			
				|  |  |              self.transport.write(chunk)
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -        self.setPlayerReceived()
 | 
	
		
			
				|  |  | +            self.setPlayerReceived()
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    def lineReceived(self, line):
 | 
	
		
			
				|  |  | +        """ line received from the game. """
 | 
	
		
			
				|  |  | +        log.msg(">> [{0}]".format(line))
 | 
	
		
			
				|  |  | +        if "TWGS v2.20b" in line and "www.eisonline.com" in line:
 | 
	
		
			
				|  |  | +            # Must not be unicode
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +            # Is there a way to NOT have this logged?
 | 
	
		
			
				|  |  | +            self.queue_game.put(
 | 
	
		
			
				|  |  | +                "TWGS Proxy build "
 | 
	
		
			
				|  |  | +                + version
 | 
	
		
			
				|  |  | +                + " is active. \x1b[1;34m~\x1b[0m to activate.\n\r\n\r",
 | 
	
		
			
				|  |  | +            )
 | 
	
		
			
				|  |  | +        if "TradeWars Game Server" in line and "Copyright (C) EIS" in line:
 | 
	
		
			
				|  |  | +            # We are not in a game
 | 
	
		
			
				|  |  | +            self.game = ""
 | 
	
		
			
				|  |  | +        if "Selection (? for menu): " in line:
 | 
	
		
			
				|  |  | +            game = line[-1]
 | 
	
		
			
				|  |  | +            if game >= "A" and game < "Q":
 | 
	
		
			
				|  |  | +                self.game = game
 | 
	
		
			
				|  |  | +                log.msg("Game: {0}".format(self.game))
 | 
	
		
			
				|  |  | +                self.game = game
 | 
	
		
			
				|  |  | +            #     self.setGame(game)
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      def dataReceived(self, chunk):
 | 
	
		
			
				|  |  | +        """ Data received from the Game. 
 | 
	
		
			
				|  |  | +        
 | 
	
		
			
				|  |  | +        Remove backspaces.
 | 
	
		
			
				|  |  | +        Treat some ANSI codes as NewLine.        
 | 
	
		
			
				|  |  | +        Remove ANSI.
 | 
	
		
			
				|  |  | +        Break into lines.
 | 
	
		
			
				|  |  | +        Trim out carriage returns.
 | 
	
		
			
				|  |  | +        Call lineReceived().
 | 
	
		
			
				|  |  | +        
 | 
	
		
			
				|  |  | +        "Optionally" pass data to player.
 | 
	
		
			
				|  |  | +        FUTURE: trigger on prompt. [cleanANSI(buffer)]
 | 
	
		
			
				|  |  | +        """
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        # Sequence error:
 | 
	
		
			
				|  |  | +        # If I don't put the chunk(I received) to the player.
 | 
	
		
			
				|  |  | +        # anything I display -- lineReceive() put() ... would
 | 
	
		
			
				|  |  | +        # be out of order.  (I'd be responding -- before it
 | 
	
		
			
				|  |  | +        # was displayed to the user.)
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        # Possibly:  if self.passon ... send to player
 | 
	
		
			
				|  |  |          self.queue_game.put(chunk)
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | +        self.buffer += chunk.decode("utf-8", "ignore")
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        # Process any backspaces
 | 
	
		
			
				|  |  | +        while "\x08" in self.buffer:
 | 
	
		
			
				|  |  | +            part = self.buffer.partition("\x08")
 | 
	
		
			
				|  |  | +            self.buffer = part[0][:-1] + part[2]
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        # Treat some ANSI codes as a newline
 | 
	
		
			
				|  |  | +        self.buffer = treatAsNL(self.buffer)
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        # Break into lines
 | 
	
		
			
				|  |  | +        while "\n" in self.buffer:
 | 
	
		
			
				|  |  | +            part = self.buffer.partition("\n")
 | 
	
		
			
				|  |  | +            line = part[0].replace("\r", "")
 | 
	
		
			
				|  |  | +            # Clean ANSI codes from line
 | 
	
		
			
				|  |  | +            line = cleanANSI(line)
 | 
	
		
			
				|  |  | +            self.lineReceived(line)
 | 
	
		
			
				|  |  | +            self.buffer = part[2]
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |      def connectionLost(self, why):
 | 
	
		
			
				|  |  |          log.msg("Game connectionLost because: %s" % why)
 | 
	
		
			
				|  |  |          self.queue_game.put(False)
 | 
	
	
		
			
				|  | @@ -166,7 +223,13 @@ class Player(protocol.Protocol):
 | 
	
		
			
				|  |  |          if chunk is False:
 | 
	
		
			
				|  |  |              self.transport.loseConnection()
 | 
	
		
			
				|  |  |          else:
 | 
	
		
			
				|  |  | -            self.transport.write(chunk)
 | 
	
		
			
				|  |  | +            if type(chunk) == bytes:
 | 
	
		
			
				|  |  | +                self.transport.write(chunk)
 | 
	
		
			
				|  |  | +            elif type(chunk) == str:
 | 
	
		
			
				|  |  | +                self.transport.write(chunk.encode())
 | 
	
		
			
				|  |  | +            else:
 | 
	
		
			
				|  |  | +                log.err("gameDataReceived: type (%s) given!", type(chunk))
 | 
	
		
			
				|  |  | +                self.transport.write(chunk)
 | 
	
		
			
				|  |  |              self.setGameReceived()
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      def dataReceived(self, chunk):
 |