6 from parser import ArgError, Parser
13 terrain_map = ('?'*5+'\n')*4+'?'*5
17 def __init__(self, position, symbol):
18 self.position = position
22 """Prefix msg plus newline to self.log_text."""
23 self.log_text = msg + '\n' + self.log_text
25 def cmd_THING(self, type_, yx):
26 """Add to self.things at .position yx with .symbol defined by type_."""
28 if type_ == 'TYPE:human':
30 elif type_ == 'TYPE:monster':
32 self.things += [self.Thing(yx, symbol)]
33 cmd_THING.argtypes = 'string yx_tuple:nonneg'
35 def cmd_MAP_SIZE(self, yx):
36 """Set self.map_size to yx, redraw self.terrain_map as '?' cells."""
38 self.map_size = (y, x)
40 for y in range(self.map_size[0]):
41 self.terrain_map += '?' * self.map_size[1] + '\n'
42 self.terrain_map = self.terrain_map[:-1]
43 cmd_MAP_SIZE.argtypes = 'yx_tuple:nonneg'
45 def cmd_TURN_FINISHED(self, n):
46 """Do nothing. (This may be extended later.)"""
48 cmd_TURN_FINISHED.argtypes = 'int:nonneg'
50 def cmd_NEW_TURN(self, n):
51 """Set self.turn to n, empty self.things."""
54 cmd_NEW_TURN.argtypes = 'int:nonneg'
56 def cmd_TERRAIN(self, terrain_map):
57 """Reset self.terrain_map from terrain_map."""
58 lines = terrain_map.split('\n')
59 if len(lines) != self.map_size[0]:
60 raise ArgError('wrong map height %s' % len(lines))
62 if len(line) != self.map_size[1]:
63 raise ArgError('wrong map width')
64 self.terrain_map = terrain_map
65 cmd_TERRAIN.argtypes = 'string'
70 def __init__(self, socket, game):
71 """Set up all urwid widgets we want on the screen."""
73 edit_widget = self.EditToSocketWidget(socket, 'SEND: ')
74 self.map_widget = urwid.Text('', wrap='clip')
75 self.turn_widget = urwid.Text('')
76 self.log_widget = urwid.Text('')
77 map_box = urwid.Padding(self.map_widget, width=50)
78 widget_pile = urwid.Pile([edit_widget, map_box, self.turn_widget,
80 self.top = urwid.Filler(widget_pile, valign='top')
83 """Draw map view from .game.terrain_map, .game.things."""
85 for c in self.game.terrain_map:
87 for t in self.game.things:
88 pos_i = t.position[0] * (self.game.map_size[1] + 1) + t.position[1]
89 whole_map[pos_i] = t.symbol
90 return ''.join(whole_map)
93 """Redraw all non-edit widgets."""
94 self.turn_widget.set_text('TURN: ' + str(self.game.turn))
95 self.log_widget.set_text(self.game.log_text)
96 self.map_widget.set_text(self.draw_map())
98 class EditToSocketWidget(urwid.Edit):
99 """Extends urwid.Edit with socket to send input on 'enter' to."""
101 def __init__(self, socket, *args, **kwargs):
102 super().__init__(*args, **kwargs)
105 def keypress(self, size, key):
106 """Extend super(): on Enter, send .edit_text, and empty it."""
108 return super().keypress(size, key)
109 plom_socket_io.send(self.socket, self.edit_text)
113 class PlomRogueClient:
115 def __init__(self, game, socket):
116 """Build client urwid interface around socket communication.
118 Sets up all widgets for writing to the socket and representing data
119 from it. Sending via a WidgetManager.EditToSocket widget is
120 straightforward; polling the socket for input from the server in
121 parallel to the urwid main loop not so much:
123 The urwid developers warn against sharing urwid resources among
124 threads, so having a socket polling thread for writing to an urwid
125 widget while other widgets are handled in other threads would be
126 dangerous. Urwid developers recommend using urwid's watch_pipe
127 mechanism instead: using a pipe from non-urwid threads into a single
128 urwid thread. We use self.recv_loop_thread to poll the socket, therein
129 write socket.recv output to an object that is then linked to by
130 self.server_output (which is known to the urwid thread), then use the
131 pipe to urwid to trigger it pulling new data from self.server_output to
132 handle via self.handle_input. (We *could* pipe socket.recv output
133 directly, but then we get complicated buffering situations here as well
134 as in the urwid code that receives the pipe output. It's easier to just
135 tell the urwid code where it finds full new server messages to handle.)
138 self.parser = Parser(self.game)
140 self.widget_manager = WidgetManager(self.socket, self.game)
141 self.server_output = []
142 self.urwid_loop = urwid.MainLoop(self.widget_manager.top)
143 self.urwid_pipe_write_fd = self.urwid_loop.watch_pipe(self.
145 self.recv_loop_thread = threading.Thread(target=self.recv_loop)
147 def handle_input(self, trigger):
148 """On input from recv_loop thread, parse and enact commands.
150 Serves as a receiver to urwid's watch_pipe mechanism, with trigger the
151 data that a pipe defined by watch_pipe delivers. To avoid buffering
152 trouble, we don't care for that data beyond the fact that its receival
153 triggers this function: The sender is to write the data it wants to
154 deliver into the container referenced by self.server_output, and just
155 pipe the trigger to inform us about this.
157 If the message delivered is 'BYE', quits Urwid. Otherwise tries to
158 parse it as a command, and enact it. In all cases but the 'BYE', calls
159 self.widget_manager.update.
161 msg = self.server_output[0]
163 raise urwid.ExitMainLoop()
165 command = self.parser.parse(msg)
167 self.game.log('UNHANDLED INPUT: ' + msg)
170 except ArgError as e:
171 self.game.log('ARGUMENT ERROR: ' + msg + '\n' + str(e))
172 self.widget_manager.update()
173 del self.server_output[0]
176 """Loop to receive messages from socket, deliver them to urwid thread.
178 Waits for self.server_output to become empty (this signals that the
179 input handler is finished / ready to receive new input), then writes
180 finished message from socket to self.server_output, then sends a single
181 b' ' through self.urwid_pipe_write_fd to trigger the input handler.
184 for msg in plom_socket_io.recv(self.socket):
185 while len(self.server_output) > 0: # Wait until self.server_output
186 pass # is emptied by input handler.
187 self.server_output += [msg]
188 os.write(self.urwid_pipe_write_fd, b' ')
191 """Run in parallel urwid_loop and recv_loop threads."""
192 self.recv_loop_thread.start()
193 self.urwid_loop.run()
194 self.recv_loop_thread.join()
197 if __name__ == '__main__':
199 s = socket.create_connection(('127.0.0.1', 5000))
200 p = PlomRogueClient(game, s)