home · contact · privacy
Add ArgumentError class, improve positioning command.
authorChristian Heller <c.heller@plomlompom.de>
Wed, 23 Aug 2017 21:19:06 +0000 (23:19 +0200)
committerChristian Heller <c.heller@plomlompom.de>
Wed, 23 Aug 2017 21:19:06 +0000 (23:19 +0200)
client.py
server.py

index 2e30fe08452dc90fa51723e212a57fb80e315c43..45ef9f859ecf88e14b14cb8dafd81eed138d1917 100755 (executable)
--- a/client.py
+++ b/client.py
@@ -6,6 +6,10 @@ import socket
 import threading
 
 
 import threading
 
 
+class ArgumentError(Exception):
+    pass
+
+
 class UrwidSetup:
 
     def __init__(self, socket):
 class UrwidSetup:
 
     def __init__(self, socket):
@@ -73,7 +77,7 @@ class UrwidSetup:
     class MapWidget(urwid.Text):
         """Stores/updates/draws game map."""
         terrain_map = ' ' * 25
     class MapWidget(urwid.Text):
         """Stores/updates/draws game map."""
         terrain_map = ' ' * 25
-        position = [0, 0]
+        position = (0, 0)
 
         def draw_map(self):
             """Draw map view from .terrain_map, .position."""
 
         def draw_map(self):
             """Draw map view from .terrain_map, .position."""
@@ -89,14 +93,21 @@ class UrwidSetup:
             self.terrain_map = terrain_map
             self.draw_map()
 
             self.terrain_map = terrain_map
             self.draw_map()
 
-        def update_position_y(self, position_y_string):
-            """Update self.position[0]."""
-            self.position[0] = int(position_y_string)
-            self.draw_map()
-
-        def update_position_x(self, position_x_string):
-            """Update self.position[1]."""
-            self.position[1] = int(position_x_string)
+        def update_position(self, position_string):
+            """Update self.position."""
+
+            def get_axis_position_from_argument(axis, token):
+                if len(token) < 3 or token[:2] != axis + ':' or \
+                        not token[2:].isdigit():
+                    raise ArgumentError('Bad arg for ' + axis + ' position.')
+                return int(token[2:])
+
+            tokens = position_string.split(',')
+            if len(tokens) != 2:
+                raise ArgumentError('wrong number of ","-separated arguments')
+            y = get_axis_position_from_argument('y', tokens[0])
+            x = get_axis_position_from_argument('x', tokens[1])
+            self.position = (y, x)
             self.draw_map()
 
     class InputHandler:
             self.draw_map()
 
     class InputHandler:
@@ -144,10 +155,9 @@ class UrwidSetup:
             try:
                 found_command = (
                     mapdraw_command('TERRAIN\n', 'update_terrain') or
             try:
                 found_command = (
                     mapdraw_command('TERRAIN\n', 'update_terrain') or
-                    mapdraw_command('POSITION_Y ', 'update_position_y') or
-                    mapdraw_command('POSITION_X ', 'update_position_x'))
-            except Exception as e:
-                self.widget1.set_text('TROUBLESOME ARGUMENT: ' + msg + '\n' +
+                    mapdraw_command('POSITION ', 'update_position'))
+            except ArgumentError as e:
+                self.widget1.set_text('BAD ARGUMENT: ' + msg + '\n' +
                                       str(e))
             else:
                 if not found_command:
                                       str(e))
             else:
                 if not found_command:
index a840bd7c38caf73884a6af88b289aba7388dd13f..90e8c0b84dc8312c6d76b99408d6ec399a4e1518 100755 (executable)
--- a/server.py
+++ b/server.py
@@ -149,8 +149,8 @@ class CommandHandler:
         self.world.turn += 1
         self.send_all('NEW_TURN ' + str(self.world.turn))
         self.send_all('TERRAIN\n' + self.world.map_)
         self.world.turn += 1
         self.send_all('NEW_TURN ' + str(self.world.turn))
         self.send_all('TERRAIN\n' + self.world.map_)
-        self.send_all('POSITION_Y ' + str(self.world.player_pos[0]))
-        self.send_all('POSITION_X ' + str(self.world.player_pos[1]))
+        self.send_all('POSITION y:' + str(self.world.player_pos[0]) +
+                      ',x:' + str(self.world.player_pos[1]))
         self.pool_result = self.pool.map_async(fib, (35, 35))
 
     def cmd_get_turn(self, connection_id):
         self.pool_result = self.pool.map_async(fib, (35, 35))
 
     def cmd_get_turn(self, connection_id):