home · contact · privacy
Minor error message reorganization.
[plomrogue2-experiments] / server.py
index c398930a99d76a32258fac2748995d43ec214d8e..502482faf6b07d853e5e2e350200bfed8f90deb6 100755 (executable)
--- a/server.py
+++ b/server.py
@@ -106,6 +106,17 @@ class CommandHandler(game_common.Commander, server_.game.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 handle_input(self, input_, connection_id=None, store=True):
         """Process input_ to command grammar, call command handler if found."""
         from inspect import signature
@@ -119,7 +130,7 @@ class CommandHandler(game_common.Commander, server_.game.Commander):
         try:
             command = self.parser.parse(input_)
             if command is None:
-                answer(connection_id, 'UNHANDLED INPUT')
+                answer(connection_id, 'UNHANDLED_INPUT')
             else:
                 if 'connection_id' in list(signature(command).parameters):
                     command(connection_id=connection_id)
@@ -129,9 +140,9 @@ class CommandHandler(game_common.Commander, server_.game.Commander):
                         with open(self.game_file_name, 'a') as f:
                             f.write(input_ + '\n')
         except parser.ArgError as e:
-            answer(connection_id, 'ARGUMENT ERROR: ' + str(e))
+            answer(connection_id, 'ARGUMENT_ERROR ' + self.quote(str(e)))
         except server_.game.GameError as e:
-            answer(connection_id, 'GAME ERROR: ' + str(e))
+            answer(connection_id, 'GAME_ERROR ' + self.quote(str(e)))
 
     def send(self, msg, connection_id=None):
         if connection_id:
@@ -147,24 +158,14 @@ class CommandHandler(game_common.Commander, server_.game.Commander):
             """Transform tuple (y,x) into string 'Y:'+str(y)+',X:'+str(x)."""
             return 'Y:' + str(tuple_[0]) + ',X:' + str(tuple_[1])
 
-        def quoted(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)
-
         self.send('NEW_TURN ' + str(self.world.turn))
-        self.send('MAP_SIZE ' + stringify_yx(self.world.map_size))
-        for y in range(self.world.map_size[0]):
-            width = self.world.map_size[1]
-            terrain_line = self.world.terrain_map[y * width:(y + 1) * width]
-            self.send('TERRAIN_LINE %5s %s' % (y, quoted(terrain_line)))
-        for thing in self.world.things:
+        self.send('MAP_SIZE ' + stringify_yx(self.world.map_.size))
+        visible_map = self.world.get_player().get_visible_map()
+        for y in range(self.world.map_.size[0]):
+            self.send('VISIBLE_MAP_LINE %5s %s' %
+                      (y, self.quote(visible_map.get_line(y))))
+        visible_things = self.world.get_player().get_visible_things()
+        for thing in visible_things:
             self.send('THING_TYPE %s %s' % (thing.id_, thing.type_))
             self.send('THING_POS %s %s' % (thing.id_,
                                            stringify_yx(thing.position)))
@@ -178,7 +179,7 @@ class CommandHandler(game_common.Commander, server_.game.Commander):
         self.send('TURN_FINISHED ' + str(self.world.turn))
         self.world.proceed_to_next_player_turn()
         msg = str(self.world.get_player().last_task_result)
-        self.send('LAST_PLAYER_TASK_RESULT ' + msg)
+        self.send('LAST_PLAYER_TASK_RESULT ' + self.quote(msg))
         self.send_gamestate()
 
     def cmd_FIB(self, numbers, connection_id):