+ We need the empty default map because we draw the map widget
+ on any update, even before we actually receive map data.
+ """
+ super().__init__(*args, **kwargs)
+ self.game = game
+ self.map_ = self.game.map_manager.get_map_class('Hex')()
+
+
+class Game(game_common.CommonCommandsMixin):
+
+ def __init__(self):
+ self.map_manager = map_manager
+ self.world = World(self)
+ self.log_text = ''
+
+ def log(self, msg):
+ """Prefix msg plus newline to self.log_text."""
+ self.log_text = msg + '\n' + self.log_text
+
+ def symbol_for_type(self, type_):
+ symbol = '?'
+ if type_ == 'human':
+ symbol = '@'
+ elif type_ == 'monster':
+ symbol = 'm'
+ return symbol
+
+ def cmd_LAST_PLAYER_TASK_RESULT(self, msg):
+ if msg != "success":
+ self.log_text = msg + '\n' + self.log_text
+ cmd_LAST_PLAYER_TASK_RESULT.argtypes = 'string'
+
+ def cmd_TURN_FINISHED(self, n):
+ """Do nothing. (This may be extended later.)"""
+ pass
+ cmd_TURN_FINISHED.argtypes = 'int:nonneg'
+
+ def cmd_NEW_TURN(self, n):
+ """Set self.turn to n, empty self.things."""
+ self.world.turn = n
+ self.world.things = []
+ cmd_NEW_TURN.argtypes = 'int:nonneg'
+
+ def cmd_VISIBLE_MAP_LINE(self, y, terrain_line):
+ self.world.map_.set_line(y, terrain_line)
+ cmd_VISIBLE_MAP_LINE.argtypes = 'int:nonneg string'
+
+
+class WidgetManager:
+
+ def __init__(self, socket, game):
+ """Set up all urwid widgets we want on the screen."""
+ self.game = game
+ edit_widget = self.EditToSocketWidget(socket, 'SEND: ')
+ self.map_widget = urwid.Text('', wrap='clip')
+ self.turn_widget = urwid.Text('')
+ self.log_widget = urwid.Text('')
+
+ edit_map = urwid.AttrMap(edit_widget, 'foo')
+ turn_map = urwid.AttrMap(self.turn_widget, 'bar')
+ log_map = urwid.AttrMap(self.log_widget, 'baz')
+ widget_pile = urwid.Pile([edit_map,
+ urwid.Divider(),
+ turn_map,
+ urwid.Divider(),
+ log_map])
+ widget_columns = urwid.Columns([(20, widget_pile), self.map_widget],
+ dividechars=1)
+
+ self.top = urwid.Filler(widget_columns, valign='top')
+
+ def draw_map(self):
+ """Draw map view from .game.map_.terrain, .game.things."""
+ terrain_as_list = list(self.game.world.map_.terrain[:])
+ for t in self.game.world.things:
+ pos_i = self.game.world.map_.get_position_index(t.position)
+ terrain_as_list[pos_i] = self.game.symbol_for_type(t.type_)
+ return self.game.world.map_.list_terrain_to_lines(terrain_as_list)
+
+ def update(self):
+ """Redraw all non-edit widgets."""
+ self.turn_widget.set_text('TURN: ' + str(self.game.world.turn))
+ self.log_widget.set_text(self.game.log_text)
+ map_lines = self.draw_map()
+ new_map_text = []
+ for char in map_lines:
+ if char == '.':
+ new_map_text += [('foo', char)]
+ elif char in {'x', 'X', '#'}:
+ new_map_text += [('bar', char)]
+ elif char in {'@', 'm'}:
+ new_map_text += [('baz', char)]
+ else:
+ new_map_text += [char]
+ self.map_widget.set_text(new_map_text)