From 1adb3944eb2d16361d6fc4c150be88faf7a2bffa Mon Sep 17 00:00:00 2001 From: Christian Heller Date: Tue, 1 Dec 2020 00:50:47 +0100 Subject: [PATCH] Refactor clients. --- rogue_chat.html | 152 ++++++++++++++++--------------------------- rogue_chat_curses.py | 7 +- 2 files changed, 60 insertions(+), 99 deletions(-) diff --git a/rogue_chat.html b/rogue_chat.html index 9be1052..f245644 100644 --- a/rogue_chat.html +++ b/rogue_chat.html @@ -208,10 +208,10 @@ let key_descriptions = { 'hex_move_left': 'left', 'hex_move_downleft': 'down-left', 'hex_move_downright': 'down-right', - 'square_move_right': 'right', - 'square_move_left': 'left', 'square_move_up': 'up', + 'square_move_left': 'left', 'square_move_down': 'down', + 'square_move_right': 'right', }; for (const mode_name of Object.keys(mode_helps)) { key_descriptions['switch_to_' + mode_name] = mode_helps[mode_name].short; @@ -522,6 +522,7 @@ class Mode { this.name = name; this.short_desc = mode_helps[name].short; this.available_modes = []; + this.available_actions = []; this.has_input_prompt = has_input_prompt; this.shows_info= shows_info; this.is_intro = is_intro; @@ -593,16 +594,28 @@ let tui = { mode_control_pw_pw: new Mode('control_pw_pw', true), mode_control_tile_type: new Mode('control_tile_type', true), mode_control_tile_draw: new Mode('control_tile_draw'), + action_tasks: { + 'flatten': 'FLATTEN_SURROUNDINGS', + 'take_thing': 'PICK_UP', + 'drop_thing': 'DROP', + 'move': 'MOVE', + }, init: function() { + this.mode_chat.available_modes = ["play", "study", "edit", "admin_enter"] this.mode_play.available_modes = ["chat", "study", "edit", "admin_enter"] + this.mode_play.available_actions = ["move", "take_thing", "drop_thing", "teleport"]; this.mode_study.available_modes = ["chat", "play", "admin_enter", "edit"] + this.mode_study.available_actions = ["toggle_map_mode", "move_explorer"]; this.mode_admin.available_modes = ["admin_thing_protect", "control_pw_type", "control_tile_type", "chat", "study", "play", "edit"] + this.mode_admin.available_actions = ["move"]; this.mode_control_tile_draw.available_modes = ["admin_enter"] + this.mode_control_tile_draw.available_actions = ["toggle_tile_draw"]; this.mode_edit.available_modes = ["write", "annotate", "portal", "name_thing", "password", "chat", "study", "play", "admin_enter"] + this.mode_edit.available_actions = ["move", "flatten", "toggle_map_mode"] this.mode = this.mode_waiting_for_server; this.inputEl = document.getElementById("input"); this.inputEl.focus(); @@ -640,6 +653,9 @@ let tui = { el.innerHTML = escapeHTML(action_desc) + '
' + escapeHTML(action_key) + ''; } }, + task_action_on: function(action) { + return game.tasks.includes(this.action_tasks[action]); + }, switch_mode: function(mode_name) { if (this.mode.name == 'control_tile_draw') { tui.log_msg('@ finished tile protection drawing.') @@ -689,25 +705,21 @@ let tui = { el.disabled = true; } document.getElementById("help").disabled = false; - if (this.mode.name == 'play' || this.mode.name == 'study' || this.mode.name == 'control_tile_draw' || this.mode.name == 'edit' || this.mode.name == 'admin') { - for (const move_key of document.querySelectorAll('[id*="_move_"]')) { - move_key.disabled = false; - } - } - if (!this.mode.is_intro && this.mode.name != 'play') { - document.getElementById("switch_to_play").disabled = false; - } - if (!this.mode.is_intro && this.mode.name != 'study') { - document.getElementById("switch_to_study").disabled = false; - } - if (!this.mode.is_intro && this.mode.name != 'chat') { - document.getElementById("switch_to_chat").disabled = false; - } - if (!this.mode.is_intro && this.mode.name != 'edit') { - document.getElementById("switch_to_edit").disabled = false; + for (const action of this.mode.available_actions) { + if (["move", "move_explorer"].includes(action)) { + for (const move_key of document.querySelectorAll('[id*="_move_"]')) { + move_key.disabled = false; + } + } else if (Object.keys(this.action_tasks).includes(action)) { + if (this.task_action_on(action)) { + document.getElementById(action).disabled = false; + } + } else { + document.getElementById(action).disabled = false; + }; } - if (!this.mode.is_intro && this.mode.name != 'admin' && this.mode.name != 'admin_enter') { - document.getElementById("switch_to_admin_enter").disabled = false; + for (const mode_name of this.mode.available_modes) { + document.getElementById('switch_to_' + mode_name).disabled = false; } if (this.mode.name == 'login') { if (this.login_name) { @@ -715,32 +727,6 @@ let tui = { } else { this.log_msg("? need login name"); } - } else if (this.mode.name == 'play') { - if (game.tasks.includes('PICK_UP')) { - document.getElementById("take_thing").disabled = false; - } - if (game.tasks.includes('DROP')) { - document.getElementById("drop_thing").disabled = false; - } - if (game.tasks.includes('MOVE')) { - } - document.getElementById("teleport").disabled = false; - } else if (this.mode.name == 'edit') { - if (game.tasks.includes('FLATTEN_SURROUNDINGS')) { - document.getElementById("flatten").disabled = false; - } - document.getElementById("switch_to_annotate").disabled = false; - document.getElementById("switch_to_write").disabled = false; - document.getElementById("switch_to_portal").disabled = false; - document.getElementById("switch_to_password").disabled = false; - document.getElementById("switch_to_name_thing").disabled = false; - document.getElementById("toggle_map_mode").disabled = false; - } else if (this.mode.name == 'admin') { - document.getElementById("switch_to_control_pw_type").disabled = false; - document.getElementById("switch_to_control_tile_type").disabled = false; - document.getElementById("switch_to_admin_thing_protect").disabled = false; - } else if (this.mode.name == 'study') { - document.getElementById("toggle_map_mode").disabled = false; } else if (this.mode.is_single_char_entry) { this.show_help = true; } else if (this.mode.name == 'admin_enter') { @@ -754,7 +740,6 @@ let tui = { } else if (this.mode.name == 'control_pw_pw') { this.log_msg('@ enter protection password for "' + this.tile_control_char + '":'); } else if (this.mode.name == 'control_tile_draw') { - document.getElementById("toggle_tile_draw").disabled = false; this.log_msg('@ can draw protection character "' + this.tile_control_char + '", turn drawing on/off with [' + this.keys.toggle_tile_draw + '], finish with [' + this.keys.switch_to_admin_enter + '].') } this.full_refresh(); @@ -1017,48 +1002,28 @@ let tui = { movement_keys_desc = Object.keys(this.movement_keys).join(','); } let content = this.mode.short_desc + " help\n\n" + this.mode.help_intro + "\n\n"; - if (this.mode.name == 'play') { - content += "Available actions:\n"; - if (game.tasks.includes('MOVE')) { - content += "[" + movement_keys_desc + "] – move player\n"; - } - if (game.tasks.includes('PICK_UP')) { - content += "[" + this.keys.take_thing + "] – pick up thing\n"; - } - if (game.tasks.includes('DROP')) { - content += "[" + this.keys.drop_thing + "] – drop thing\n"; - } - content += "[" + tui.keys.teleport + "] – teleport\n"; - content += '\n'; - } else if (this.mode.name == 'study') { - content += "Available actions:\n"; - content += '[' + movement_keys_desc + '] – move question mark\n'; - content += '[' + this.keys.toggle_map_mode + '] – toggle map view\n'; - content += '\n'; - } else if (this.mode.name == 'edit') { - content += "Available actions:\n"; - if (game.tasks.includes('MOVE')) { - content += "[" + movement_keys_desc + "] – move player\n"; - } - if (game.tasks.includes('FLATTEN_SURROUNDINGS')) { - content += "[" + tui.keys.flatten + "] – flatten surroundings\n"; - } - content += '[' + this.keys.toggle_map_mode + '] – toggle map view\n'; - content += '\n'; - } else if (this.mode.name == 'control_tile_draw') { - content += "Available actions:\n"; - content += "[" + tui.keys.toggle_tile_draw + "] – toggle protection character drawing\n"; - content += '\n'; - } else if (this.mode.name == 'chat') { + if (this.mode.name == 'chat') { content += '/nick NAME – re-name yourself to NAME\n'; content += '/' + this.keys.switch_to_play + ' or /play – switch to play mode\n'; content += '/' + this.keys.switch_to_study + ' or /study – switch to study mode\n'; content += '/' + this.keys.switch_to_edit + ' or /edit – switch to world edit mode\n'; content += '/' + this.keys.switch_to_admin_enter + ' or /admin – switch to admin mode\n'; - } else if (this.mode.name == 'admin') { + } else if (this.mode.available_actions.length > 0) { content += "Available actions:\n"; - if (game.tasks.includes('MOVE')) { - content += "[" + movement_keys_desc + "] – move player\n"; + for (let action of this.mode.available_actions) { + if (Object.keys(this.action_tasks).includes(action)) { + if (!this.task_action_on(action)) { + continue; + } + } + if (action == 'move_explorer') { + action = 'move'; + } + if (action == 'move') { + content += "[" + movement_keys_desc + "] – move\n" + } else { + content += "[" + this.keys[action] + "] – " + key_descriptions[action] + "\n"; + } } content += '\n'; } @@ -1408,14 +1373,11 @@ tui.inputEl.addEventListener('keydown', (event) => { } else if (tui.mode.name == 'play') { if (tui.mode.mode_switch_on_key(event)) { null; - } else if (event.key === tui.keys.take_thing - && game.tasks.includes('PICK_UP')) { + } else if (event.key === tui.keys.take_thing && tui.task_action_on('take_thing')) { server.send(["TASK:PICK_UP"]); - } else if (event.key === tui.keys.drop_thing - && game.tasks.includes('DROP')) { + } else if (event.key === tui.keys.drop_thing && tui.task_action_on('drop_thing')) { server.send(["TASK:DROP"]); - } else if (event.key in tui.movement_keys - && game.tasks.includes('MOVE')) { + } else if (event.key in tui.movement_keys && tui.task_action_on('move')) { server.send(['TASK:MOVE', tui.movement_keys[event.key]]); } else if (event.key === tui.keys.teleport) { game.teleport(); @@ -1439,18 +1401,15 @@ tui.inputEl.addEventListener('keydown', (event) => { } else if (tui.mode.name == 'admin') { if (tui.mode.mode_switch_on_key(event)) { null; - } else if (event.key in tui.movement_keys - && game.tasks.includes('MOVE')) { + } else if (event.key in tui.movement_keys && tui.task_action_on('move')) { server.send(['TASK:MOVE', tui.movement_keys[event.key]]); }; } else if (tui.mode.name == 'edit') { if (tui.mode.mode_switch_on_key(event)) { null; - } else if (event.key in tui.movement_keys - && game.tasks.includes('MOVE')) { + } else if (event.key in tui.movement_keys && tui.task_action_on('move')) { server.send(['TASK:MOVE', tui.movement_keys[event.key]]); - } else if (event.key === tui.keys.flatten - && game.tasks.includes('FLATTEN_SURROUNDINGS')) { + } else if (event.key === tui.keys.flatten && tui.task_action_on('flatten')) { server.send(["TASK:FLATTEN_SURROUNDINGS", tui.password]); } else if (event.key == tui.keys.toggle_map_mode) { tui.toggle_map_mode(); @@ -1540,7 +1499,8 @@ document.getElementById("teleport").onclick = function() { for (const move_button of document.querySelectorAll('[id*="_move_"]')) { let direction = move_button.id.split('_')[2].toUpperCase(); move_button.onclick = function() { - if (tui.mode.name == 'play' || tui.mode.name == 'edit') { + if (tui.mode.available_actions.includes("move") + || tui.mode.available_actions.includes("move_explorer")) { server.send(['TASK:MOVE', direction]); } else { explorer.move(direction); diff --git a/rogue_chat_curses.py b/rogue_chat_curses.py index e5c2dad..2066f6c 100755 --- a/rogue_chat_curses.py +++ b/rogue_chat_curses.py @@ -390,12 +390,13 @@ class TUI: "control_tile_type", "chat", "study", "play", "edit"] self.mode_admin.available_actions = ["move"] - self.mode_edit.available_actions = ["move", "flatten", "toggle_map_mode"] - self.mode_control_tile_draw.available_actions = ["toggle_tile_draw"] self.mode_control_tile_draw.available_modes = ["admin_enter"] + self.mode_control_tile_draw.available_actions = ["move_explorer", + "toggle_tile_draw"] self.mode_edit.available_modes = ["write", "annotate", "portal", "name_thing", "password", "chat", "study", "play", "admin_enter"] + self.mode_edit.available_actions = ["move", "flatten", "toggle_map_mode"] self.mode = None self.host = host self.game = Game() @@ -1023,7 +1024,7 @@ class TUI: else: self.flash = True self.log_msg('? not standing on portal') - elif key in self.movement_keys and task_action_on('move'):# 'MOVE' in self.game.tasks: + elif key in self.movement_keys and task_action_on('move'): self.send('TASK:MOVE ' + self.movement_keys[key]) elif self.mode.name == 'write': self.send('TASK:WRITE %s %s' % (key, quote(self.password))) -- 2.30.2