From: Christian Heller Date: Wed, 23 Aug 2017 22:06:37 +0000 (+0200) Subject: Add MAP_SIZE reply, add constraint checks. X-Git-Url: https://plomlompom.com/repos/?a=commitdiff_plain;h=31831b9e6147a89b20a97a52f4fe5e01acc283d0;p=plomrogue2-experiments Add MAP_SIZE reply, add constraint checks. --- diff --git a/client.py b/client.py index 45ef9f8..7e9f18e 100755 --- a/client.py +++ b/client.py @@ -76,25 +76,58 @@ class UrwidSetup: class MapWidget(urwid.Text): """Stores/updates/draws game map.""" + map_size = (5, 5) terrain_map = ' ' * 25 position = (0, 0) def draw_map(self): - """Draw map view from .terrain_map, .position.""" + """Draw map view from .map_size, .terrain_map, .position.""" whole_map = [] for c in self.terrain_map: whole_map += [c] - pos_i = self.position[0] * (5 + 1) + self.position[1] + pos_i = self.position[0] * (self.map_size[1] + 1) + self.position[1] whole_map[pos_i] = '@' self.set_text(''.join(whole_map)) + def get_yx(self, yx_string): + + 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 = yx_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]) + return (y, x) + + def update_map_size(self, size_string): + """Set map size, redo self.terrain_map in new size, '?'-filled.""" + new_map_size = self.get_yx(size_string) + if 0 in new_map_size: + raise ArgumentError('size value for either axis must be >0') + self.map_size = new_map_size + self.terrain_map = '' + for y in range(self.map_size[0]): + self.terrain_map += '?' * self.map_size[1] + '\n' + self.draw_map() + def update_terrain(self, terrain_map): - """Update self.terrain_map.""" + """Update self.terrain_map. Ensure size matching self.map_size.""" + lines = terrain_map.split('\n') + if len(lines) != self.map_size[0]: + raise ArgumentError('wrong map height') + for line in lines: + if len(line) != self.map_size[1]: + raise ArgumentError('wrong map width') self.terrain_map = terrain_map self.draw_map() def update_position(self, position_string): - """Update self.position.""" + """Update self.position, ensure it's within map bounds.""" def get_axis_position_from_argument(axis, token): if len(token) < 3 or token[:2] != axis + ':' or \ @@ -102,12 +135,11 @@ class UrwidSetup: 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) + new_position = self.get_yx(position_string) + if new_position[0] >= self.map_size[0] or \ + new_position[1] >= self.map_size[1]: + raise ArgumentError('Position outside of map size bounds.') + self.position = new_position self.draw_map() class InputHandler: @@ -155,7 +187,8 @@ class UrwidSetup: try: found_command = ( mapdraw_command('TERRAIN\n', 'update_terrain') or - mapdraw_command('POSITION ', 'update_position')) + mapdraw_command('POSITION ', 'update_position') or + mapdraw_command('MAP_SIZE ', 'update_map_size')) except ArgumentError as e: self.widget1.set_text('BAD ARGUMENT: ' + msg + '\n' + str(e)) diff --git a/server.py b/server.py index 90e8c0b..a3fa8e4 100755 --- a/server.py +++ b/server.py @@ -81,6 +81,7 @@ class IO_Handler(socketserver.BaseRequestHandler): class World: turn = 0 + map_size = (5, 5) map_ = 'xxxxx\nx...x\nx.X.x\nx...x\nxxxxx' player_pos = (3, 3) @@ -142,15 +143,19 @@ class CommandHandler: until a further INC finishes the turn. """ from time import sleep + + def stringify_yx(tuple_): + return 'Y:' + str(tuple_[0]) + ',X:' + str(tuple_[1]) + if self.pool_result is not None: self.pool_result.wait() self.send_all('TURN_FINISHED ' + str(self.world.turn)) sleep(1) self.world.turn += 1 self.send_all('NEW_TURN ' + str(self.world.turn)) + self.send_all('MAP_SIZE ' + stringify_yx(self.world.map_size)) self.send_all('TERRAIN\n' + self.world.map_) - self.send_all('POSITION y:' + str(self.world.player_pos[0]) + - ',x:' + str(self.world.player_pos[1])) + self.send_all('POSITION ' + stringify_yx(self.world.player_pos)) self.pool_result = self.pool.map_async(fib, (35, 35)) def cmd_get_turn(self, connection_id):