From: Christian Heller Date: Thu, 24 Aug 2017 22:35:00 +0000 (+0200) Subject: Add Thing types and map them to symbols to display in client. X-Git-Url: https://plomlompom.com/repos/%7B%7B%20web_path%20%7D%7D/%7B%7Bprefix%7D%7D/bar%20baz.html?a=commitdiff_plain;h=0aaa22e9e7c123fb0eda849136c5b69659ba7abc;p=plomrogue2-experiments Add Thing types and map them to symbols to display in client. --- diff --git a/client.py b/client.py index 5e91828..2acd7bc 100755 --- a/client.py +++ b/client.py @@ -86,14 +86,21 @@ class UrwidSetup: map_size = (5, 5) terrain_map = ' ' * 25 position = (0, 0) + things = [] + + class Thing: + def __init__(self, position, symbol): + self.position = position + self.symbol = symbol def draw_map(self): """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] * (self.map_size[1] + 1) + self.position[1] - whole_map[pos_i] = '@' + for t in self.things: + pos_i = t.position[0] * (self.map_size[1] + 1) + t.position[1] + whole_map[pos_i] = t.symbol self.set_text(''.join(whole_map)) def get_yx(self, yx_string): @@ -133,22 +140,30 @@ class UrwidSetup: self.terrain_map = terrain_map self.draw_map() - def update_position(self, position_string): - """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 \ - not token[2:].isdigit(): - raise ArgumentError('Bad arg for ' + axis + ' position.') - return int(token[2:]) - - new_position = self.get_yx(position_string) - if new_position[0] >= self.map_size[0] or \ - new_position[1] >= self.map_size[1]: + def update_things(self, thing_description): + """Append thing of thing_description to self.things.""" + thing_types = {'human': '@', 'monster': 'M'} + tokens = thing_description.split() + if len(tokens) != 2: + raise ArgumentError('Wrong number of tokens.') + yx = self.get_yx(tokens[1]) + if yx[0] >= self.map_size[0] or yx[1] >= self.map_size[1]: raise ArgumentError('Position outside of map size bounds.') - self.position = new_position + type_token = tokens[0] + prefix = 'TYPE:' + type_ = '?' + if len(type_token) <= len(prefix) or \ + type_token[:len(prefix)] != prefix: + raise ArgumentError('Invalid type token.') + type_ = type_token[len(prefix):] + if type_ not in thing_types: + raise ArgumentError('Unknown thing type.') + self.things += [self.Thing(yx, thing_types[type_])] self.draw_map() + def clear_things(self, _): + self.things = [] + class InputHandler: """Delivers data from other thread to widget via message_container. @@ -193,8 +208,9 @@ class UrwidSetup: found_command = False try: found_command = ( + mapdraw_command('NEW_TURN ', 'clear_things') or mapdraw_command('TERRAIN\n', 'update_terrain') or - mapdraw_command('POSITION ', 'update_position') or + mapdraw_command('THING ', 'update_things') or mapdraw_command('MAP_SIZE ', 'update_map_size')) except ArgumentError as e: self.log_widget.add('ARGUMENT ERROR: ' + msg + '\n' + str(e)) diff --git a/server.py b/server.py index 215317d..e8bca6a 100755 --- a/server.py +++ b/server.py @@ -90,7 +90,8 @@ class Task: class Thing: - def __init__(self, position): + def __init__(self, type_, position): + self.type = type_ self.position = position self.task = Task('wait') @@ -135,7 +136,7 @@ class World: 'x.X.x\n'+\ 'x...x\n'+\ 'xxxxx' - self.things = [Thing(position=[3, 3]), Thing([1, 1])] + self.things = [Thing('human', [3, 3]), Thing('monster', [1, 1])] self.player_i = 0 self.player = self.things[self.player_i] @@ -201,7 +202,9 @@ class CommandHandler: self.send_all('NEW_TURN ' + str(self.world.turn)) self.send_all('MAP_SIZE ' + self.stringify_yx(self.world.map_size)) self.send_all('TERRAIN\n' + self.world.map_) - self.send_all('POSITION ' + self.stringify_yx(self.world.player.position)) + for thing in self.world.things: + self.send_all('THING TYPE:' + thing.type + ' ' + + self.stringify_yx(thing.position)) def cmd_fib(self, tokens, connection_id): """Reply with n-th Fibonacci numbers, n taken from tokens[1:]. @@ -244,7 +247,9 @@ class CommandHandler: self.send_all('NEW_TURN ' + str(self.world.turn)) self.send_all('MAP_SIZE ' + self.stringify_yx(self.world.map_size)) self.send_all('TERRAIN\n' + self.world.map_) - self.send_all('POSITION ' + self.stringify_yx(self.world.player.position)) + for thing in self.world.things: + self.send_all('THING TYPE:' + thing.type + ' ' + + self.stringify_yx(thing.position)) self.pool_result = self.pool.map_async(fib, (35, 35)) def cmd_get_turn(self, connection_id):