From: Christian Heller Date: Thu, 19 Nov 2020 23:17:55 +0000 (+0100) Subject: More mode code refactoring. X-Git-Url: https://plomlompom.com/repos/%7B%7Bprefix%7D%7D/%7B%7Bdb.prefix%7D%7D/task?a=commitdiff_plain;h=3ffed06ffef414e46ba5655316c1f60afaa95fa5;p=plomrogue2 More mode code refactoring. --- diff --git a/config.json b/config.json index 1185268..01777ee 100644 --- a/config.json +++ b/config.json @@ -7,7 +7,7 @@ "switch_to_study": "?", "switch_to_edit": "m", "switch_to_admin": "A", - "switch_to_control_pw": "C", + "switch_to_control_pw_pw": "C", "flatten": "F", "take_thing": "z", "drop_thing": "u", diff --git a/rogue_chat_curses.py b/rogue_chat_curses.py index 182a1e7..0db2695 100755 --- a/rogue_chat_curses.py +++ b/rogue_chat_curses.py @@ -11,19 +11,57 @@ from plomrogue.misc import quote from plomrogue.errors import BrokenSocketConnection mode_helps = { - 'play': 'This mode allows you to interact with the map.', - 'study': 'This mode allows you to study the map and its tiles in detail. Move the question mark over a tile, and the right half of the screen will show detailed information on it.', - 'edit': 'This mode allows you to change the map tile you currently stand on (if your map editing password authorizes you so). Just enter any printable ASCII character to imprint it on the ground below you.', - 'control_pw_type': 'This mode is the first of two steps to change the password for a tile control character. First enter the tile control character for which you want to change the password!', - 'control_pw_pw': 'This mode is the second of two steps to change the password for a tile control character. Enter the new password for the tile control character you chose.', - 'annotate': 'This mode allows you to add/edit a comment on the tile you are currently standing on (provided your map editing password authorizes you so). Hit Return to leave.', - 'portal': 'This mode allows you to imprint/edit/remove a teleportation target on the ground you are currently standing on (provided your map editing password authorizes you so). Enter or edit a URL to imprint a teleportation target; enter emptiness to remove a pre-existing teleportation target. Hit Return to leave.', - 'chat': 'This mode allows you to engage in chit-chat with other users. Any line you enter into the input prompt that does not start with a "/" will be sent out to nearby players – but barriers and distance will reduce what they can read, so stand close to them to ensure they get your message. Lines that start with a "/" are used for commands like:', - 'login': 'Pick your player name.', - 'waiting_for_server': 'Waiting for a server response.', - 'post_login_wait': 'Waiting for a server response.', - 'password': 'This mode allows you to change the password that you send to authorize yourself for editing password-protected map tiles. Hit return to confirm and leave.', - 'admin': 'This mode allows you to become admin if you know an admin password.' + 'play': { + 'short': 'play', + 'long': 'This mode allows you to interact with the map.' + }, + 'study': { + 'short': 'study', + 'long': 'This mode allows you to study the map and its tiles in detail. Move the question mark over a tile, and the right half of the screen will show detailed information on it.'}, + 'edit': { + 'short': 'terrain edit', + 'long': 'This mode allows you to change the map tile you currently stand on (if your map editing password authorizes you so). Just enter any printable ASCII character to imprint it on the ground below you.' + }, + 'control_pw_type': { + 'short': 'change tile control password', + 'long': 'This mode is the first of two steps to change the password for a tile control character. First enter the tile control character for which you want to change the password!' + }, + 'control_pw_pw': { + 'short': '', + 'long': 'This mode is the second of two steps to change the password for a tile control character. Enter the new password for the tile control character you chose.' + }, + 'annotate': { + 'short': 'annotation', + 'long': 'This mode allows you to add/edit a comment on the tile you are currently standing on (provided your map editing password authorizes you so). Hit Return to leave.' + }, + 'portal': { + 'short': 'edit portal', + 'long': 'This mode allows you to imprint/edit/remove a teleportation target on the ground you are currently standing on (provided your map editing password authorizes you so). Enter or edit a URL to imprint a teleportation target; enter emptiness to remove a pre-existing teleportation target. Hit Return to leave.' + }, + 'chat': { + 'short': 'chat mode', + 'long': 'This mode allows you to engage in chit-chat with other users. Any line you enter into the input prompt that does not start with a "/" will be sent out to nearby players – but barriers and distance will reduce what they can read, so stand close to them to ensure they get your message. Lines that start with a "/" are used for commands like:' + }, + 'login': { + 'short': '', + 'long': 'Pick your player name.' + }, + 'waiting_for_server': { + 'short': '', + 'long': 'Waiting for a server response.' + }, + 'post_login_wait': { + 'short': '', + 'long': 'Waiting for a server response.' + }, + 'password': { + 'short': 'password input', + 'long': 'This mode allows you to change the password that you send to authorize yourself for editing password-protected map tiles. Hit return to confirm and leave.' + }, + 'admin': { + 'short': 'become admin', + 'long': 'This mode allows you to become admin if you know an admin password.' + } } from ws4py.client import WebSocketBaseClient @@ -183,6 +221,7 @@ cmd_ANNOTATION.argtypes = 'yx_tuple:nonneg string' def cmd_TASKS(game, tasks_comma_separated): game.tasks = tasks_comma_separated.split(',') + game.tui.mode_edit.legal = 'WRITE' in game.tasks cmd_TASKS.argtypes = 'string' def cmd_THING_TYPE(game, thing_type, symbol_hint): @@ -245,44 +284,67 @@ class Game(GameBase): f.argtypes = self.commands[command_name].argtypes return f -class TUI: - - class Mode: +class Mode: + + def __init__(self, name, has_input_prompt=False, shows_info=False, + is_intro=False, is_single_char_entry=False): + self.name = name + self.short_desc = mode_helps[name]['short'] + self.available_modes = [] + self.has_input_prompt = has_input_prompt + self.shows_info = shows_info + self.is_intro = is_intro + self.help_intro = mode_helps[name]['long'] + self.is_single_char_entry = is_single_char_entry + self.legal = True + + def iter_available_modes(self, tui): + for mode_name in self.available_modes: + mode = getattr(tui, 'mode_' + mode_name) + if not mode.legal: + continue + key = tui.keys['switch_to_' + mode.name] + yield mode, key + + def list_available_modes(self, tui): + msg = '' + if len(self.available_modes) > 0: + msg = 'Other modes available from here:\n' + for mode, key in self.iter_available_modes(tui): + msg += '[%s] – %s\n' % (key, mode.short_desc) + return msg + + def mode_switch_on_key(self, tui, key_pressed): + for mode, key in self.iter_available_modes(tui): + if key_pressed == key: + tui.switch_mode(mode.name) + return True + return False - def __init__(self, name, has_input_prompt=False, - shows_info=False, is_intro = False, - is_single_char_entry=False): - self.name = name - self.has_input_prompt = has_input_prompt - self.shows_info = shows_info - self.is_intro = is_intro - self.help_intro = mode_helps[name] - self.is_single_char_entry = is_single_char_entry +class TUI: + mode_admin = Mode('admin', has_input_prompt=True) + mode_play = Mode('play') + mode_study = Mode('study', shows_info=True) + mode_edit = Mode('edit', is_single_char_entry=True) + mode_control_pw_type = Mode('control_pw_type', is_single_char_entry=True) + mode_control_pw_pw = Mode('control_pw_pw', has_input_prompt=True) + mode_annotate = Mode('annotate', has_input_prompt=True, shows_info=True) + mode_portal = Mode('portal', has_input_prompt=True, shows_info=True) + mode_chat = Mode('chat', has_input_prompt=True) + mode_waiting_for_server = Mode('waiting_for_server', is_intro=True) + mode_login = Mode('login', has_input_prompt=True, is_intro=True) + mode_post_login_wait = Mode('post_login_wait', is_intro=True) + mode_password = Mode('password', has_input_prompt=True) def __init__(self, host): import os import json + self.mode_play.available_modes = ["chat", "study", "edit", + "annotate", "portal", + "password", "admin", + "control_pw_type"] + self.mode_study.available_modes = ["chat", "play"] self.host = host - self.mode_play = self.Mode('play') - self.mode_study = self.Mode('study', shows_info=True) - self.mode_edit = self.Mode('edit', is_single_char_entry=True) - self.mode_control_pw_type = self.Mode('control_pw_type', - is_single_char_entry=True) - self.mode_control_pw_pw = self.Mode('control_pw_pw', - has_input_prompt=True) - self.mode_annotate = self.Mode('annotate', has_input_prompt=True, - shows_info=True) - self.mode_portal = self.Mode('portal', has_input_prompt=True, - shows_info=True) - self.mode_chat = self.Mode('chat', has_input_prompt=True) - self.mode_waiting_for_server = self.Mode('waiting_for_server', - is_intro=True) - self.mode_login = self.Mode('login', has_input_prompt=True, - is_intro=True) - self.mode_post_login_wait = self.Mode('post_login_wait', - is_intro=True) - self.mode_password = self.Mode('password', has_input_prompt=True) - self.mode_admin = self.Mode('admin', has_input_prompt=True) self.game = Game() self.game.tui = self self.parser = Parser(self.game) @@ -302,7 +364,7 @@ class TUI: 'switch_to_study': '?', 'switch_to_edit': 'm', 'switch_to_admin': 'A', - 'switch_to_control_pw': 'C', + 'switch_to_control_pw_type': 'C', 'flatten': 'F', 'take_thing': 'z', 'drop_thing': 'u', @@ -616,27 +678,17 @@ class TUI: if 'FLATTEN_SURROUNDINGS' in self.game.tasks: content += "[%s] – flatten player's surroundings\n" % self.keys['flatten'] content += '[%s] – teleport to other space\n' % self.keys['teleport'] - content += 'Other modes available from here:\n' - content += '[%s] – chat mode\n' % self.keys['switch_to_chat'] - content += '[%s] – study mode\n' % self.keys['switch_to_study'] - content += '[%s] – terrain edit mode\n' % self.keys['switch_to_edit'] - content += '[%s] – portal edit mode\n' % self.keys['switch_to_portal'] - content += '[%s] – annotation mode\n' % self.keys['switch_to_annotate'] - content += '[%s] – password input mode\n' % self.keys['switch_to_password'] - content += '[%s] – become admin\n' % self.keys['switch_to_admin'] - content += '[%s] – change tile control password' % self.keys['switch_to_control_pw'] - + content += '\n' elif self.mode.name == 'study': content += 'Available actions:\n' content += '[%s] – move question mark\n' % ','.join(self.movement_keys) content += '[%s] – toggle view between terrain, annotations, and password protection areas\n' % self.keys['toggle_map_mode'] - content += '\n\nOther modes available from here:' - content += '[%s] – chat mode\n' % self.keys['switch_to_chat'] - content += '[%s] – play mode\n' % self.keys['switch_to_play'] + content += '\n' elif self.mode.name == 'chat': content += '/nick NAME – re-name yourself to NAME\n' content += '/%s or /play – switch to play mode\n' % self.keys['switch_to_play'] content += '/%s or /study – switch to study mode\n' % self.keys['switch_to_study'] + content += self.mode.list_available_modes(self) for i in range(self.size.y): safe_addstr(i, self.window_width * (not self.mode.has_input_prompt), @@ -775,10 +827,8 @@ class TUI: self.input_ = "" self.switch_mode('play') elif self.mode.name == 'study': - if key == self.keys['switch_to_chat']: - self.switch_mode('chat') - elif key == self.keys['switch_to_play']: - self.switch_mode('play') + if self.mode.mode_switch_on_key(self, key): + continue elif key == self.keys['toggle_map_mode']: if self.map_mode == 'terrain': self.map_mode = 'annotations' @@ -789,24 +839,9 @@ class TUI: elif key in self.movement_keys: move_explorer(self.movement_keys[key]) elif self.mode.name == 'play': - if key == self.keys['switch_to_chat']: - self.switch_mode('chat') - elif key == self.keys['switch_to_study']: - self.switch_mode('study') - elif key == self.keys['switch_to_annotate']: - self.switch_mode('annotate') - elif key == self.keys['switch_to_portal']: - self.switch_mode('portal') - elif key == self.keys['switch_to_password']: - self.switch_mode('password') - elif key == self.keys['switch_to_admin']: - self.switch_mode('admin') - elif key == self.keys['switch_to_control_pw']: - self.switch_mode('control_pw_type') - if key == self.keys['switch_to_edit'] and\ - 'WRITE' in self.game.tasks: - self.switch_mode('edit') - elif key == self.keys['flatten'] and\ + if self.mode.mode_switch_on_key(self, key): + continue + if key == self.keys['flatten'] and\ 'FLATTEN_SURROUNDINGS' in self.game.tasks: self.send('TASK:FLATTEN_SURROUNDINGS ' + quote(self.password)) elif key == self.keys['take_thing'] and 'PICK_UP' in self.game.tasks: diff --git a/rogue_chat_nocanvas_monochrome.html b/rogue_chat_nocanvas_monochrome.html index 644f567..7928e24 100644 --- a/rogue_chat_nocanvas_monochrome.html +++ b/rogue_chat_nocanvas_monochrome.html @@ -31,7 +31,7 @@ terminal columns: - +

edit keybindings

(see here for non-obvious available values):