- 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.
+
+class PlomRogueClient:
+
+ def __init__(self, game, socket):
+ """Build client urwid interface around socket communication.
+
+ Sets up all widgets for writing to the socket and representing data
+ from it. Sending via a WidgetManager.EditToSocket widget is
+ straightforward; polling the socket for input from the server in
+ parallel to the urwid main loop not so much:
+
+ The urwid developers warn against sharing urwid resources among
+ threads, so having a socket polling thread for writing to an urwid
+ widget while other widgets are handled in other threads would be
+ dangerous. Urwid developers recommend using urwid's watch_pipe
+ mechanism instead: using a pipe from non-urwid threads into a single
+ urwid thread. We use self.recv_loop_thread to poll the socket, therein
+ write socket.recv output to an object that is then linked to by
+ self.server_output (which is known to the urwid thread), then use the
+ pipe to urwid to trigger it pulling new data from self.server_output to
+ handle via self.handle_input. (We *could* pipe socket.recv output
+ directly, but then we get complicated buffering situations here as well
+ as in the urwid code that receives the pipe output. It's easier to just
+ tell the urwid code where it finds full new server messages to handle.)