home · contact · privacy
Minor code rearrangements.
authorChristian Heller <c.heller@plomlompom.de>
Tue, 15 Jan 2019 01:31:08 +0000 (02:31 +0100)
committerChristian Heller <c.heller@plomlompom.de>
Tue, 15 Jan 2019 01:31:08 +0000 (02:31 +0100)
server_/game.py
server_/io.py

index e611a0ed825740cf9eff3bc74a2528738182236d..b52f16ef8eb3f20e7f2b4699c08bc34093c4d3de 100644 (file)
@@ -207,16 +207,19 @@ class CommandHandler(game_common.Commander):
         self.pool = Pool()
         self.pool_result = None
 
-    def quote(self, string):
-        """Quote & escape string so client interprets it as single token."""
-        quoted = []
-        quoted += ['"']
-        for c in string:
-            if c in {'"', '\\'}:
-                quoted += ['\\']
-            quoted += [c]
-        quoted += ['"']
-        return ''.join(quoted)
+    def send(self, msg, connection_id=None):
+        """Send message msg to server's client(s) via self.queues_out.
+
+        If a specific client is identified by connection_id, only
+        sends msg to that one. Else, sends it to all clients
+        identified in self.queues_out.
+
+        """
+        if connection_id:
+            self.queues_out[connection_id].put(msg)
+        else:
+            for connection_id in self.queues_out:
+                self.queues_out[connection_id].put(msg)
 
     def handle_input(self, input_, connection_id=None, store=True):
         """Process input_ to command grammar, call command handler if found."""
@@ -245,19 +248,16 @@ class CommandHandler(game_common.Commander):
         except game.GameError as e:
             answer(connection_id, 'GAME_ERROR ' + self.quote(str(e)))
 
-    def send(self, msg, connection_id=None):
-        """Send message msg to server's client(s) via self.queues_out.
-
-        If a specific client is identified by connection_id, only
-        sends msg to that one. Else, sends it to all clients
-        identified in self.queues_out.
-
-        """
-        if connection_id:
-            self.queues_out[connection_id].put(msg)
-        else:
-            for connection_id in self.queues_out:
-                self.queues_out[connection_id].put(msg)
+    def quote(self, string):
+        """Quote & escape string so client interprets it as single token."""
+        quoted = []
+        quoted += ['"']
+        for c in string:
+            if c in {'"', '\\'}:
+                quoted += ['\\']
+            quoted += [c]
+        quoted += ['"']
+        return ''.join(quoted)
 
     def send_gamestate(self, connection_id=None):
         """Send out game state data relevant to clients."""
@@ -324,6 +324,7 @@ class CommandHandler(game_common.Commander):
         self.world.turn += 1
         self.send_gamestate()
         self.pool_result = self.pool.map_async(fib, (35, 35))
+
     def cmd_MOVE(self, direction):
         """Set player task to 'move' with direction arg, finish player turn."""
         if direction not in {'UP', 'DOWN', 'RIGHT', 'LEFT'}:
index 7db47ca7857981f266500bc20b44fe4dd136dc96..95e7d7782f7eecf4ab318d330853cb2285881652 100644 (file)
@@ -111,10 +111,10 @@ def io_loop(q, game_command_handler):
         content = None if len(x) == 2 else x[2]
         if command_type == 'ADD_QUEUE':
             game_command_handler.queues_out[connection_id] = content
-        elif command_type == 'COMMAND':
-            game_command_handler.handle_input(content, connection_id)
         elif command_type == 'KILL_QUEUE':
             del game_command_handler.queues_out[connection_id]
+        elif command_type == 'COMMAND':
+            game_command_handler.handle_input(content, connection_id)
 
 
 def run_server_with_io_loop(command_handler):