From 3ffed06ffef414e46ba5655316c1f60afaa95fa5 Mon Sep 17 00:00:00 2001
From: Christian Heller <c.heller@plomlompom.de>
Date: Fri, 20 Nov 2020 00:17:55 +0100
Subject: [PATCH] More mode code refactoring.

---
 config.json                         |   2 +-
 rogue_chat_curses.py                | 197 ++++++++++++++++------------
 rogue_chat_nocanvas_monochrome.html | 156 ++++++++++++++--------
 3 files changed, 221 insertions(+), 134 deletions(-)

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: <input id="n_cols" type="number" step=4 min=80 value=80 />
 <button id="switch_to_portal">edit portal link</button>
 <button id="toggle_map_mode">toggle terrain/annotations/control view</button>
 <button id="switch_to_admin">become admin</button>
-<button id="switch_to_control_pw">change tile control password</button>
+<button id="switch_to_control_pw_type">change tile control password</button>
 </div>
 <h3>edit keybindings</h3> (see <a href="https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key/Key_Values">here</a> for non-obvious available values):<br />
 <ul>
@@ -56,7 +56,7 @@ terminal columns: <input id="n_cols" type="number" step=4 min=80 value=80 />
 <li>edit tile (from play mode): <input id="key_switch_to_edit" type="text" value="m" />
 <li>enter tile password (from play mode): <input id="key_switch_to_password" type="text" value="P" />
 <li>enter admin password (from play mode): <input id="key_switch_to_admin" type="text" value="A" />
-<li>change tile control password (from play mode): <input id="key_switch_to_control_pw" type="text" value="C" />
+<li>change tile control password (from play mode): <input id="key_switch_to_control_pw_type" type="text" value="C" />
 <li>annotate tile (from play mode): <input id="key_switch_to_annotate" type="text" value="M" />
 <li>annotate portal (from play mode): <input id="key_switch_to_portal" type="text" value="T" />
 <li>toggle terrain/annotations/control view (from study mode): <input id="key_toggle_map_mode" type="text" value="M" />
@@ -68,18 +68,57 @@ let websocket_location = "wss://plomlompom.com/rogue_chat/";
 //let websocket_location = "ws://localhost:8000/";
 
 let 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.',
-    '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.'
+    }
 }
 
 let rows_selector = document.getElementById("n_rows");
@@ -261,7 +300,8 @@ let server = {
                 t.player_char = tokens[2];
             };
         } else if (tokens[0] === 'TASKS') {
-            game.tasks = tokens[1].split(',')
+            game.tasks = tokens[1].split(',');
+            tui.mode_edit.legal = game.tasks.includes('WRITE');
         } else if (tokens[0] === 'THING_TYPE') {
             game.thing_types[tokens[1]] = tokens[2]
         } else if (tokens[0] === 'TERRAIN') {
@@ -347,11 +387,44 @@ class Mode {
     constructor(name, has_input_prompt=false, shows_info=false,
                 is_intro=false, is_single_char_entry=false) {
         this.name = name;
+        this.short_desc = mode_helps[name].short;
+        this.available_modes = [];
         this.has_input_prompt = has_input_prompt;
         this.shows_info= shows_info;
         this.is_intro = is_intro;
-        this.help_intro = mode_helps[name];
+        this.help_intro = mode_helps[name].long;
         this.is_single_char_entry = is_single_char_entry;
+        this.legal = true;
+    }
+    *iter_available_modes() {
+        for (let mode_name of this.available_modes) {
+            let mode = tui['mode_' + mode_name];
+            if (!mode.legal) {
+                continue;
+            }
+            let key = tui.keys['switch_to_' + mode.name];
+            yield [mode, key]
+        }
+    }
+    list_available_modes() {
+        let msg = ''
+        if (this.available_modes.length > 0) {
+            msg += 'Other modes available from here:\n';
+            for (let [mode, key] of this.iter_available_modes()) {
+                msg += '[' + key + '] – ' + mode.short_desc + '\n';
+            }
+        }
+        return msg;
+    }
+    mode_switch_on_key(key_event) {
+        for (let [mode, key] of this.iter_available_modes()) {
+            if (key_event.key == key) {
+                event.preventDefault();
+                tui.switch_mode(mode.name);
+                return true;
+            };
+        }
+        return false;
     }
 }
 let tui = {
@@ -380,6 +453,11 @@ let tui = {
   mode_admin: new Mode('admin', true),
   mode_control_pw_pw: new Mode('control_pw_pw', true),
   init: function() {
+      this.mode_play.available_modes = ["chat", "study", "edit",
+                                        "annotate", "portal",
+                                        "password", "admin",
+                                        "control_pw_type"]
+      this.mode_study.available_modes = ["chat", "play"]
       this.mode = this.mode_waiting_for_server;
       this.inputEl = document.getElementById("input");
       this.inputEl.focus();
@@ -433,7 +511,7 @@ let tui = {
     document.getElementById("switch_to_annotate").disabled = true;
     document.getElementById("switch_to_password").disabled = true;
     document.getElementById("switch_to_admin").disabled = true;
-    document.getElementById("switch_to_control_pw").disabled = true;
+    document.getElementById("switch_to_control_pw_type").disabled = true;
     document.getElementById("move_left").disabled = true;
     document.getElementById("move_upleft").disabled = true;
     document.getElementById("move_up").disabled = true;
@@ -488,7 +566,7 @@ let tui = {
         document.getElementById("switch_to_portal").disabled = false;
         document.getElementById("switch_to_password").disabled = false;
         document.getElementById("switch_to_admin").disabled = false;
-        document.getElementById("switch_to_control_pw").disabled = false;
+        document.getElementById("switch_to_control_pw_type").disabled = false;
     } else if (this.mode.name == 'study') {
         document.getElementById("toggle_map_mode").disabled = false;
     } else if (this.mode.is_single_char_entry) {
@@ -684,27 +762,18 @@ let tui = {
               content += "[" + tui.keys.flatten + "] – flatten player's surroundings\n";
           }
           content += "[" + tui.keys.teleport + "] – teleport to other space\n";
-          content += '\nOther modes available from here:\n';
-          content += '[' + this.keys.switch_to_chat + '] – chat mode\n';
-          content += '[' + this.keys.switch_to_study + '] – study mode\n';
-          content += '[' + this.keys.switch_to_edit + '] – terrain edit mode\n';
-          content += '[' + this.keys.switch_to_portal + '] – portal edit mode\n';
-          content += '[' + this.keys.switch_to_annotate + '] – annotation mode\n';
-          content += '[' + this.keys.switch_to_password + '] – password input mode\n';
-          content += '[' + this.keys.switch_to_admin + '] – become admin\n';
-          content += '[' + this.keys.switch_to_control_pw + '] – change tile control password\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 view between terrain, annotations, and password protection areas\n';
-          content += '\nOther modes available from here:\n';
-          content += '[' + this.keys.switch_to_chat + '] – chat mode\n';
-          content += '[' + this.keys.switch_to_play + '] – play mode\n';
+          content += '\n';
       } else 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.mode.list_available_modes();
       let start_x = 0;
       if (!this.mode.has_input_prompt) {
           start_x = this.window_width
@@ -982,24 +1051,8 @@ tui.inputEl.addEventListener('keydown', (event) => {
         }
         tui.empty_input();
     } else if (tui.mode.name == 'play') {
-          if (event.key === tui.keys.switch_to_chat) {
-              event.preventDefault();
-              tui.switch_mode('chat');
-          } else if (event.key === tui.keys.switch_to_edit
-                     && game.tasks.includes('WRITE')) {
-              event.preventDefault();
-              tui.switch_mode('edit');
-          } else if (event.key === tui.keys.switch_to_study) {
-              tui.switch_mode('study');
-          } else if (event.key === tui.keys.switch_to_admin) {
-              event.preventDefault();
-              tui.switch_mode('admin');
-          } else if (event.key === tui.keys.switch_to_control_pw) {
-              event.preventDefault();
-              tui.switch_mode('control_pw_type');
-          } else if (event.key === tui.keys.switch_to_password) {
-              event.preventDefault();
-              tui.switch_mode('password');
+          if (tui.mode.mode_switch_on_key(event)) {
+              null;
           } else if (event.key === tui.keys.flatten
                      && game.tasks.includes('FLATTEN_SURROUNDINGS')) {
               server.send(["TASK:FLATTEN_SURROUNDINGS", tui.password]);
@@ -1022,9 +1075,8 @@ tui.inputEl.addEventListener('keydown', (event) => {
               tui.switch_mode('annotate');
           };
     } else if (tui.mode.name == 'study') {
-        if (event.key === tui.keys.switch_to_chat) {
-            event.preventDefault();
-            tui.switch_mode('chat');
+        if (tui.mode.mode_switch_on_key(event)) {
+              null;
         } else if (event.key == tui.keys.switch_to_play) {
             tui.switch_mode('play');
         } else if (event.key in tui.movement_keys) {
@@ -1112,7 +1164,7 @@ document.getElementById("switch_to_admin").onclick = function() {
     tui.switch_mode('admin');
     tui.full_refresh();
 };
-document.getElementById("switch_to_control_pw").onclick = function() {
+document.getElementById("switch_to_control_pw_type").onclick = function() {
     tui.switch_mode('control_pw_type');
     tui.full_refresh();
 };
-- 
2.30.2