- class EditToSocketWidget(urwid.Edit):
- """Extends urwid.Edit with socket to send input on 'enter' to."""
-
- def __init__(self, socket, *args, **kwargs):
- super().__init__(*args, **kwargs)
- self.socket = socket
-
- def keypress(self, size, key):
- """Extend super(): on Enter, send .edit_text, and empty it."""
- if key != 'enter':
- return super().keypress(size, key)
- plom_socket_io.send(self.socket, self.edit_text)
- self.edit_text = ''
-
- class TurnWidget(urwid.Text):
- """Displays turn number."""
-
- def set_turn(self, turn_string):
- turn_string = turn_string.strip()
- if not turn_string.isdigit():
- raise ArgumentError('Argument must be non-negative integer.')
- self.set_text('TURN: ' + turn_string)
-
- class LogWidget(urwid.Text):
- """Displays client log, newest message on top."""
-
- def add(self, text):
- """Add text plus newline to (top of) log."""
- self.set_text(text + '\n' + self.text)
-
- class MapWidget(urwid.Text):
- """Stores/updates/draws game map."""
- 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]
- 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):
-
- 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. 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_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.')
- 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.
-
- The class only exists to provide handle_input as a bound method, with
- widget and message_container pre-set, as (bound) handle_input is used
- as a callback in urwid's watch_pipe – which merely provides its
- callback target with one parameter for a pipe to read data from an
- urwid-external thread.