From 6eae766d1b2cdfe0bdbaaabcbf00977a4df4fbbb Mon Sep 17 00:00:00 2001 From: Christian Heller Date: Tue, 15 Jan 2019 02:31:08 +0100 Subject: [PATCH] Minor code rearrangements. --- server_/game.py | 47 ++++++++++++++++++++++++----------------------- server_/io.py | 4 ++-- 2 files changed, 26 insertions(+), 25 deletions(-) diff --git a/server_/game.py b/server_/game.py index e611a0e..b52f16e 100644 --- a/server_/game.py +++ b/server_/game.py @@ -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'}: diff --git a/server_/io.py b/server_/io.py index 7db47ca..95e7d77 100644 --- a/server_/io.py +++ b/server_/io.py @@ -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): -- 2.30.2