Browse Source

We now inject our activate message. No more jumping around!

The displayed message is consistent!
Updated notes on mcp.py (it should be replaced).
Notes: Whatever receives emit - always has a parameter.
I can't get rid of it, the reactor.callLater insists
on sending one.
Steve Thielemann 5 years ago
parent
commit
14321d6c75
3 changed files with 47 additions and 20 deletions
  1. 4 0
      mcp.py
  2. 2 2
      observer.py
  3. 41 18
      tcp-proxy.py

+ 4 - 0
mcp.py

@@ -4,6 +4,10 @@ from twisted.internet import defer
 from colorama import Fore, Back, Style
 from twisted.python import log
 
+""" This entire object, will likely be replaced with something
+    smaller, simpler, and much more reliable.
+"""
+
 
 class MCP(object):
     def __init__(self, game):

+ 2 - 2
observer.py

@@ -5,14 +5,14 @@ class Observer(object):
     def __init__(self):
         self.dispatch = {}
 
-    def emit(self, signal, message):
+    def emit(self, signal, *msg):
         """ emit a signal, return True if sent somewhere. """
         if signal in self.dispatch:
             # key exists, but is there anything in the list?
             if self.dispatch[signal]:
                 # Yes there is.
                 for listener in self.dispatch[signal]:
-                    reactor.callLater(0, listener, message)
+                    reactor.callLater(0, listener, *msg)
                 return True
             return False
         return False

+ 41 - 18
tcp-proxy.py

@@ -155,12 +155,10 @@ class PlayerInput(object):
                 line = self.input
                 self.input = ''
                 assert(not self.deferred is None)
-                log.msg(self.deferred)
-                self.deferred.callback(line)
-                # log.msg("callLater in 3 secs defer.callback with line entered...")
-                # reactor.callLater(2, self.deferred.callback, line)
-                
-                # Is it possible that setting this to None calleds the callback?
+
+                # Ok, use deferred.callback, or reactor.callLater?
+                # self.deferred.callback(line)
+                reactor.callLater(0, self.deferred.callback, line)
                 self.deferred = None
             if ch.isprintable():
                 if len(self.input) + 1 <= self.limit:
@@ -230,7 +228,8 @@ class ProxyMenu(object):
         key = chunk.upper()
         log.msg("ProxyMenu.player({0})".format(key))
 
-        # Weird / long running task / something odd happening here?
+        # Stop the keepalive if we are activating something else
+        # or leaving...
         self.keepalive.stop()
 
         if key == "T":
@@ -240,14 +239,17 @@ class ProxyMenu(object):
             self.queue_game.put(self.nl + self.c1 + "Current time " + now.to_datetime_string() + self.nl)
         elif key == 'Q':
             self.queue_game.put(self.c + key + self.r + self.nl)
-            # This isn't working here, because we don't "stop"...
-            # we just redisplay the menu and keep going.  :(
+
+            # Ok, keepalive is stop(), and we are leaving this.
+            # So, when the PlayerInput is done, have it call welcome_back,
+            # which reinstates keepalive, and displays the menu.
+
             ask = PlayerInput(self.game)
             d = ask.prompt("What is your quest? ", 20)
-            # d.callback(lambda ignore: ask.prompt("What is your favorite color?", 10))
 
+            # Display the user's input
             d.addCallback(ask.output)
-            # This causes twisted.internet.defer.AlreadyCalledError:            
+            # To "return" to the ProxyMenu, call self.welcome_back
             d.addCallback(self.welcome_back)
             return
 
@@ -325,12 +327,15 @@ class Game(protocol.Protocol):
         if LOG_LINES:
             log.msg(">> [{0}]".format(line))
 
-        if "TWGS v2.20b" in line and "www.eisonline.com" in line:
-            self.queue_game.put(
-                "TWGS Proxy build "
-                + version
-                + " is active. \x1b[1;34m~\x1b[0m to activate.\n\r\n\r",
-            )
+        # if "TWGS v2.20b" in line and "www.eisonline.com" in line:
+            # I would still love to "inject" this into the stream
+            # so it is consistent.
+
+            # 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
@@ -366,6 +371,20 @@ class Game(protocol.Protocol):
         FUTURE: trigger on prompt. [cleanANSI(buffer)]
         """
 
+        # Store the text into the buffer before we inject into it.
+
+        self.buffer += chunk.decode("utf-8", "ignore")
+        # log.msg("data: [{0}]".format(repr(chunk))) 
+
+        if b'TWGS v2.20b' in chunk and b'www.eisonline.com' in chunk:
+            # Ok, we have a possible target.
+            target = b'www.eisonline.com\n\r'
+            pos = chunk.find(target)
+            if pos != -1:
+                # Found it!  Inject!
+                message = "TWGS Proxy build " + version + ".  ~ to activate in game.\n\r"
+                chunk = chunk[0:pos + len(target)] + message.encode() + chunk[pos + len(target):]
+
         # Sequence error:
         # If I don't put the chunk(I received) to the player.
         # anything I display -- lineReceive() put() ... would
@@ -375,7 +394,11 @@ class Game(protocol.Protocol):
         if self.to_player:
             self.queue_game.put(chunk)
 
-        self.buffer += chunk.decode("utf-8", "ignore")
+        # self.buffer += chunk.decode("utf-8", "ignore")
+
+        #
+        # Begin processing the buffer
+        #
 
         # Process any backspaces
         while "\b" in self.buffer: