X-Git-Url: https://plomlompom.com/repos/?a=blobdiff_plain;f=new%2Fexample_client.py;h=ee71241110e28d0121a83571c8ddf93ff4118416;hb=373b553f83f45d34e424676a3c8e863a8507bdd4;hp=1e910704fe5104a767f9cd63c1215b00ef92e865;hpb=729e72408fa1c3180275ad3c0e6689143a0f2f38;p=plomrogue2-experiments diff --git a/new/example_client.py b/new/example_client.py index 1e91070..ee71241 100755 --- a/new/example_client.py +++ b/new/example_client.py @@ -9,6 +9,7 @@ from plomrogue.mapping import MapBase from plomrogue.io import PlomSocket from plomrogue.things import ThingBase import types +import queue class Map(MapBase): @@ -96,8 +97,6 @@ def cmd_TURN(game, n): game.world.turn = n game.world.things = [] game.world.pickable_items = [] - game.to_update['turn'] = False - game.to_update['map'] = False cmd_TURN.argtypes = 'int:nonneg' def cmd_VISIBLE_MAP_LINE(game, y, terrain_line): @@ -105,8 +104,9 @@ def cmd_VISIBLE_MAP_LINE(game, y, terrain_line): cmd_VISIBLE_MAP_LINE.argtypes = 'int:nonneg string' def cmd_GAME_STATE_COMPLETE(game): - game.to_update['turn'] = True - game.to_update['map'] = True + game.tui.to_update['turn'] = True + game.tui.to_update['map'] = True + game.tui.to_update['inventory'] = True def cmd_THING_TYPE(game, i, type_): t = game.world.get_thing(i) @@ -119,7 +119,7 @@ cmd_PLAYER_INVENTORY.argtypes = 'seq:int:nonneg' def cmd_PICKABLE_ITEMS(game, ids): game.world.pickable_items = ids - game.to_update['map'] = True + game.tui.to_update['pickable_items'] = True cmd_PICKABLE_ITEMS.argtypes = 'seq:int:nonneg' @@ -141,13 +141,8 @@ class Game: 'THING_TYPE': cmd_THING_TYPE, 'THING_POS': cmd_THING_POS} self.log_text = '' - self.to_update = { - 'log': True, - 'map': True, - 'turn': True, - } self.do_quit = False - self.to_update_lock = False + self.tui = None def get_command(self, command_name): from functools import partial @@ -170,17 +165,15 @@ class Game: command, args = self.parser.parse(msg) if command is None: self.log('UNHANDLED INPUT: ' + msg) - self.to_update['log'] = True else: command(*args) except ArgError as e: self.log('ARGUMENT ERROR: ' + msg + '\n' + str(e)) - self.to_update['log'] = True def log(self, msg): """Prefix msg plus newline to self.log_text.""" self.log_text = msg + '\n' + self.log_text - self.to_update['log'] = True + self.tui.to_update['log'] = True def symbol_for_type(self, type_): symbol = '?' @@ -197,23 +190,23 @@ ASCII_printable = ' !"#$%&\'\(\)*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWX' 'YZ[\\]^_\`abcdefghijklmnopqrstuvwxyz{|}~' -def recv_loop(plom_socket, game): +def recv_loop(plom_socket, game, q): for msg in plom_socket.recv(): - game.handle_input(msg) + q.put(msg) class Widget: - def __init__(self, tui, start, size, check_game=[], check_tui=[]): - self.check_game = check_game - self.check_tui = check_tui + def __init__(self, tui, start, size, check_updates=[], visible=True): + self.check_updates = check_updates self.tui = tui self.start = start self.win = curses.newwin(1, 1, self.start[0], self.start[1]) self.size_def = size # store for re-calling .size on SIGWINCH self.size = size self.do_update = True - self.visible = True + self.visible = visible + self.children = [] @property def size(self): @@ -267,22 +260,21 @@ class Widget: self.win.addstr(char_with_attr[0], char_with_attr[1]) def ensure_freshness(self, do_refresh=False): - if not self.visible: - return - if not do_refresh: - for key in self.check_game: - if key in self.tui.game.to_update and self.tui.game.to_update[key]: - do_refresh = True - break - if not do_refresh: - for key in self.check_tui: - if key in self.tui.to_update and self.tui.to_update[key]: - do_refresh = True - break - if do_refresh: - self.win.erase() - self.draw() - self.win.refresh() + did_refresh = False + if self.visible: + if not do_refresh: + for key in self.check_updates: + if key in self.tui.to_update and self.tui.to_update[key]: + do_refresh = True + break + if do_refresh: + self.win.erase() + self.draw() + self.win.refresh() + did_refresh = True + for child in self.children: + did_refresh = child.ensure_freshness(do_refresh) | did_refresh + return did_refresh class EditWidget(Widget): @@ -319,21 +311,9 @@ class PopUpWidget(Widget): offset_x = int((self.tui.stdscr.getmaxyx()[1] / 2) - (size[1] / 2)) self.start = (offset_y, offset_x) self.win.mvwin(self.start[0], self.start[1]) - self.ensure_freshness(True) - -class MapWidget(Widget): - - def draw(self): - if self.tui.view == 'map': - self.draw_map() - elif self.tui.view == 'inventory': - self.draw_item_selector('INVENTORY:', - self.tui.game.world.player_inventory) - elif self.tui.view == 'pickable_items': - self.draw_item_selector('PICKABLE:', - self.tui.game.world.pickable_items) +class ItemsSelectorWidget(Widget): def draw_item_selector(self, title, selection): lines = [title] @@ -352,7 +332,23 @@ class MapWidget(Widget): to_join += [line + ' '*to_pad] self.safe_write((''.join(to_join), curses.color_pair(3))) - def draw_map(self): + +class InventoryWidget(ItemsSelectorWidget): + + def draw(self): + self.draw_item_selector('INVENTORY:', + self.tui.game.world.player_inventory) + +class PickableItemsWidget(ItemsSelectorWidget): + + def draw(self): + self.draw_item_selector('PICKABLE:', + self.tui.game.world.pickable_items) + + +class MapWidget(Widget): + + def draw(self): def terrain_with_objects(): terrain_as_list = list(self.tui.game.world.map_.terrain[:]) @@ -415,64 +411,118 @@ class TurnWidget(Widget): self.safe_write((str(self.tui.game.world.turn), curses.color_pair(2))) +class TextLineWidget(Widget): + + def __init__(self, text_line, *args, **kwargs): + self.text_line = text_line + super().__init__(*args, **kwargs) + + def draw(self): + self.safe_write(self.text_line) + + class TUI: - def __init__(self, plom_socket, game): + def __init__(self, plom_socket, game, q): self.socket = plom_socket self.game = game + self.game.tui = self + self.queue = q self.parser = Parser(self.game) - self.to_update = {'edit': False} + self.to_update = {} self.item_pointer = 0 curses.wrapper(self.loop) - def draw_screen(self): - self.stdscr.addstr(0, 0, 'SEND:') - self.stdscr.addstr(2, 0, 'TURN:') + def loop(self, stdscr): - def setup_screen(self, stdscr): - self.stdscr = stdscr - self.stdscr.refresh() # will be called by getkey else, clearing screen - self.stdscr.timeout(10) - self.draw_screen() + def setup_screen(stdscr): + self.stdscr = stdscr + self.stdscr.refresh() # will be called by getkey else, clearing screen + self.stdscr.timeout(10) + + def switch_widgets(widget_1, widget_2): + widget_1.visible = False + widget_2.visible = True + trigger = widget_2.check_updates[0] + self.to_update[trigger] = True + + def pick_or_drop_menu(action_key, widget, selectables, task, + bonus_command=None): + if len(selectables) < self.item_pointer + 1 and\ + self.item_pointer > 0: + self.item_pointer = len(selectables) - 1 + if key == 'c': + switch_widgets(widget, map_widget) + elif key == 'j': + self.item_pointer += 1 + elif key == 'k' and self.item_pointer > 0: + self.item_pointer -= 1 + elif key == action_key and len(selectables) > 0: + id_ = selectables[self.item_pointer] + self.socket.send('TASK:%s %s' % (task, id_)) + if bonus_command: + self.socket.send(bonus_command) + if self.item_pointer > 0: + self.item_pointer -= 1 + else: + return + trigger = widget.check_updates[0] + self.to_update[trigger] = True - def loop(self, stdscr): - self.setup_screen(stdscr) + setup_screen(stdscr) curses.init_pair(1, curses.COLOR_BLACK, curses.COLOR_RED) curses.init_pair(2, curses.COLOR_BLACK, curses.COLOR_GREEN) curses.init_pair(3, curses.COLOR_BLACK, curses.COLOR_BLUE) curses.init_pair(4, curses.COLOR_BLACK, curses.COLOR_YELLOW) curses.curs_set(False) # hide cursor self.to_send = [] - self.edit = EditWidget(self, (0, 6), (1, 14), check_tui = ['edit']) - self.turn = TurnWidget(self, (2, 6), (1, 14), ['turn']) - self.log = LogWidget(self, (4, 0), (None, 20), ['log']) - self.map_ = MapWidget(self, (0, 21), (None, None), ['map']) - self.popup = PopUpWidget(self, (0, 0), (1, 1), ['popup']) - self.popup.visible = False + edit_widget = TextLineWidget('SEND:', self, (0, 0), (1, 20)) + edit_line_widget = EditWidget(self, (0, 6), (1, 14), ['edit']) + edit_widget.children += [edit_line_widget] + turn_widget = TextLineWidget('TURN:', self, (2, 0), (1, 20)) + turn_widget.children += [TurnWidget(self, (2, 6), (1, 14), ['turn'])] + log_widget = LogWidget(self, (4, 0), (None, 20), ['log']) + map_widget = MapWidget(self, (0, 21), (None, None), ['map']) + inventory_widget = InventoryWidget(self, (0, 21), (None, None), + ['inventory'], False) + pickable_items_widget = PickableItemsWidget(self, (0, 21), (None, None), + ['pickable_items'], False) + top_widgets = [edit_widget, turn_widget, log_widget, map_widget, + inventory_widget, pickable_items_widget] + popup_widget = PopUpWidget(self, (0, 0), (1, 1), visible=False) self.popup_text = 'Hi bob' - widgets = (self.edit, self.turn, self.log, self.map_, self.popup) write_mode = True - self.view = 'map' + for w in top_widgets: + w.ensure_freshness(True) + draw_popup_if_visible = True while True: - for w in widgets: - w.ensure_freshness() - for key in self.game.to_update: - self.game.to_update[key] = False - for key in self.to_update: - self.to_update[key] = False + for w in top_widgets: + did_refresh = w.ensure_freshness() + draw_popup_if_visible = did_refresh | draw_popup_if_visible + if popup_widget.visible and draw_popup_if_visible: + popup_widget.ensure_freshness(True) + draw_popup_if_visible = False + for k in self.to_update.keys(): + self.to_update[k] = False + while True: + try: + command = self.queue.get(block=False) + except queue.Empty: + break + self.game.handle_input(command) try: key = self.stdscr.getkey() if key == 'KEY_RESIZE': curses.endwin() - self.setup_screen(curses.initscr()) - for w in widgets: + setup_screen(curses.initscr()) + for w in top_widgets: w.size = w.size_def w.ensure_freshness(True) elif key == '\t': # Tabulator key. write_mode = False if write_mode else True elif write_mode: if len(key) == 1 and key in ASCII_printable and \ - len(self.to_send) < len(self.edit): + len(self.to_send) < len(edit_line_widget): self.to_send += [key] self.to_update['edit'] = True elif key == 'KEY_BACKSPACE': @@ -482,7 +532,7 @@ class TUI: self.socket.send(''.join(self.to_send)) self.to_send[:] = [] self.to_update['edit'] = True - elif self.view == 'map': + elif map_widget.visible: if key == 'w': self.socket.send('TASK:MOVE UPLEFT') elif key == 'e': @@ -496,60 +546,30 @@ class TUI: elif key == 'c': self.socket.send('TASK:MOVE DOWNRIGHT') elif key == 't': - if not self.popup.visible: + if not popup_widget.visible: self.to_update['popup'] = True - self.popup.visible = True - self.popup.reconfigure() + popup_widget.visible = True + popup_widget.reconfigure() + draw_popup_if_visible = True else: - self.popup.visible = False - self.stdscr.erase() # we'll call refresh here so - self.stdscr.refresh() # getkey doesn't, erasing screen - self.draw_screen() - for w in widgets: + popup_widget.visible = False + for w in top_widgets: w.ensure_freshness(True) elif key == 'p': self.socket.send('GET_PICKABLE_ITEMS') self.item_pointer = 0 - self.view = 'pickable_items' + switch_widgets(map_widget, pickable_items_widget) elif key == 'i': self.item_pointer = 0 - self.view = 'inventory' - self.game.to_update['map'] = True - elif self.view == 'pickable_items': - if key == 'c': - self.view = 'map' - elif key == 'j' and \ - len(self.game.world.pickable_items) > \ - self.item_pointer + 1: - self.item_pointer += 1 - elif key == 'k' and self.item_pointer > 0: - self.item_pointer -= 1 - elif key == 'p' and \ - len(self.game.world.pickable_items) > 0: - id_ = self.game.world.pickable_items[self.item_pointer] - self.socket.send('TASK:PICKUP %s' % id_) - self.view = 'map' - else: - continue - self.game.to_update['map'] = True - elif self.view == 'inventory': - if key == 'c': - self.view = 'map' - elif key == 'j' and \ - len(self.game.world.player_inventory) > \ - self.item_pointer + 1: - self.item_pointer += 1 - elif key == 'k' and self.item_pointer > 0: - self.item_pointer -= 1 - elif key == 'd' and \ - len(self.game.world.player_inventory) > 0: - id_ = self.game.world.player_inventory[self.item_pointer] - self.socket.send('TASK:DROP %s' % id_) - if self.item_pointer > 0: - self.item_pointer -= 1 - else: - continue - self.game.to_update['map'] = True + switch_widgets(map_widget, inventory_widget) + elif pickable_items_widget.visible: + pick_or_drop_menu('p', pickable_items_widget, + self.game.world.pickable_items, + 'PICKUP', 'GET_PICKABLE_ITEMS') + elif inventory_widget.visible: + pick_or_drop_menu('d', inventory_widget, + self.game.world.player_inventory, + 'DROP') except curses.error: pass if self.game.do_quit: @@ -559,6 +579,7 @@ class TUI: s = socket.create_connection(('127.0.0.1', 5000)) plom_socket = PlomSocket(s) game = Game() -t = threading.Thread(target=recv_loop, args=(plom_socket, game)) +q = queue.Queue() +t = threading.Thread(target=recv_loop, args=(plom_socket, game, q)) t.start() -TUI(plom_socket, game) +TUI(plom_socket, game, q)