home · contact · privacy
In web client, also hide help screen on any click.
[plomrogue2] / rogue_chat.html
index 59d03a98141cda17de586e956b1abd6d88bf8dff..51ac81d5e4cd43016a26a7f741cc4d2c7cb9b41a 100644 (file)
@@ -42,34 +42,37 @@ terminal columns: <input id="n_cols" type="number" step=4 min=80 value=80 />
   <tr>
     <td><button id="switch_to_chat">chat mode</button><br /></td>
   </tr>
+  <tr>
     <td><button id="switch_to_study">study mode</button></td>
-    <td><button id="toggle_map_mode">toggle terrain/annotations/control view</button>
+    <td><button id="toggle_map_mode">toggle everything/terrain/annotations view</button>
+  </tr>
   <tr>
     <td><button id="switch_to_play">play mode</button></td>
     <td>
-      <table>
-        <tr>
-          <td><button id="take_thing">take thing</button></td>
-          <td><button id="switch_to_edit">change tile</button></td>
-          <td><button id="switch_to_admin">become admin</button></td>
-        </tr>
-        <tr>
-          <td><button id="drop_thing">drop thing</button></td>
-          <td><button id="switch_to_password">change tile editing password</button></td>
-          <td><button id="switch_to_control_pw_type">change tile control password</button></td>
-        </tr>
-        <tr>
-          <td><button id="flatten">flatten surroundings</button></td>
-          <td><button id="switch_to_annotate">annotate tile</button></td>
-          <td><button id="switch_to_control_tile_type">change tiles control</button></td>
-        </tr>
-        <tr>
-          <td><button id="teleport">teleport</button></td>
-          <td><button id="switch_to_portal">edit portal link</button></td>
-        </tr>
-      </table>
+      <button id="take_thing">take thing</button>
+      <button id="teleport">teleport</button>
+      <button id="drop_thing">drop thing</button>
+    </td>
+  </tr>
+  <tr>
+    <td><button id="switch_to_edit">map edit mode</button></td>
+    <td>
+      <button id="switch_to_write">change tile</button>
+      <button id="flatten">flatten surroundings</button>
+      <button id="switch_to_annotate">annotate tile</button>
+      <button id="switch_to_portal">edit portal link</button>
+      <button id="switch_to_password">change tile editing password</button>
     </td>
   </tr>
+  <tr>
+    <td><button id="switch_to_admin_enter">admin mode</button></td>
+    <td>
+      <button id="switch_to_control_pw_type">change tile control password</button>
+      <button id="switch_to_control_tile_type">change tiles control</button>
+      <button id="toggle_tile_draw">toggle tiles control drawing</button>
+    </td>
+  <tr>
+  </tr>
 </table>
 <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>
@@ -91,14 +94,16 @@ terminal columns: <input id="n_cols" type="number" step=4 min=80 value=80 />
 <li><input id="key_switch_to_chat" type="text" value="t" />
 <li><input id="key_switch_to_play" type="text" value="p" />
 <li><input id="key_switch_to_study" type="text" value="?" />
-<li><input id="key_switch_to_edit" type="text" value="m" />
+<li><input id="key_switch_to_edit" type="text" value="E" />
+<li><input id="key_switch_to_write" type="text" value="m" />
 <li><input id="key_switch_to_password" type="text" value="P" />
-<li><input id="key_switch_to_admin" type="text" value="A" />
+<li><input id="key_switch_to_admin_enter" type="text" value="A" />
 <li><input id="key_switch_to_control_pw_type" type="text" value="C" />
 <li><input id="key_switch_to_control_tile_type" type="text" value="Q" />
 <li><input id="key_switch_to_annotate" type="text" value="M" />
 <li><input id="key_switch_to_portal" type="text" value="T" />
-<li>toggle terrain/annotations/control view: <input id="key_toggle_map_mode" type="text" value="M" />
+<li>toggle everything/terrain/annotations view: <input id="key_toggle_map_mode" type="text" value="M" />
+<li>toggle everything/terrain/annotations view: <input id="key_toggle_tile_draw" type="text" value="m" />
 </ul>
 </div>
 <script>
@@ -115,12 +120,16 @@ let mode_helps = {
         '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',
+        'short': 'map edit',
+        'long': 'This mode allows you to change the map in various ways.'
+    },
+    'write': {
+        'short': 'terrain write',
         '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 tiles 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!'
+        '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': 'change tiles control password',
@@ -132,7 +141,7 @@ let mode_helps = {
     },
     'control_tile_draw': {
         'short': 'change tiles control',
-        'long': 'This mode is the second of two steps to change tile control areas on the map.  Move cursor around the map to draw selected tile control character'
+        'long': 'This mode is the second of two steps to change tile control areas on the map.  Toggle tile control drawing on, then move cursor around the map to draw selected tile control character.'
     },
     'annotate': {
         'short': 'annotate tile',
@@ -162,9 +171,13 @@ let mode_helps = {
         'short': 'map edit password',
         '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': {
+    'admin_enter': {
         'short': 'become admin',
         'long': 'This mode allows you to become admin if you know an admin password.'
+    },
+    'admin': {
+        'short': 'admin',
+        'long': 'This mode allows you access to actions limited to administrators.'
     }
 }
 
@@ -380,7 +393,7 @@ let server = {
             };
         } else if (tokens[0] === 'TASKS') {
             game.tasks = tokens[1].split(',');
-            tui.mode_edit.legal = game.tasks.includes('WRITE');
+            tui.mode_write.legal = game.tasks.includes('WRITE');
         } else if (tokens[0] === 'THING_TYPE') {
             game.thing_types[tokens[1]] = tokens[2]
         } else if (tokens[0] === 'TERRAIN') {
@@ -409,6 +422,10 @@ let server = {
         } else if (tokens[0] === 'LOGIN_OK') {
             this.send(['GET_GAMESTATE']);
             tui.switch_mode('post_login_wait');
+        } else if (tokens[0] === 'ADMIN_OK') {
+            tui.is_admin = true;
+            tui.log_msg('@ you now have admin rights');
+            tui.switch_mode('admin');
         } else if (tokens[0] === 'PORTAL') {
             let position = parser.parse_yx(tokens[1]);
             game.portals[position] = tokens[2];
@@ -517,6 +534,8 @@ let tui = {
   height_input: 1,
   password: 'foo',
   show_help: false,
+  is_admin: false,
+  tile_draw: false,
   mode_waiting_for_server: new Mode('waiting_for_server',
                                      false, false, true),
   mode_login: new Mode('login', true, false, true),
@@ -525,24 +544,26 @@ let tui = {
   mode_annotate: new Mode('annotate', true, true),
   mode_play: new Mode('play'),
   mode_study: new Mode('study', false, true),
-  mode_edit: new Mode('edit', false, false, false, true),
-  mode_control_pw_type: new Mode('control_pw_type',
-                                  false, false, false, true),
+  mode_write: new Mode('write', false, false, false, true),
+  mode_edit: new Mode('edit'),
+  mode_control_pw_type: new Mode('control_pw_type', true),
   mode_portal: new Mode('portal', true, true),
   mode_password: new Mode('password', true),
-  mode_admin: new Mode('admin', true),
+  mode_admin_enter: new Mode('admin_enter', true),
+  mode_admin: new Mode('admin'),
   mode_control_pw_pw: new Mode('control_pw_pw', true),
-  mode_control_tile_type: new Mode('control_tile_type',
-                                   false, false, false, true),
+  mode_control_tile_type: new Mode('control_tile_type', true),
   mode_control_tile_draw: new Mode('control_tile_draw'),
   init: function() {
-      this.mode_play.available_modes = ["chat", "study", "edit",
-                                        "annotate", "portal",
-                                        "password", "admin",
-                                        "control_pw_type",
-                                        "control_tile_type"]
-      this.mode_study.available_modes = ["chat", "play"]
-      this.mode_control_tile_draw.available_modes = ["play"]
+      this.mode_play.available_modes = ["chat", "study", "edit", "admin_enter"]
+      this.mode_study.available_modes = ["chat", "play", "admin_enter", "edit"]
+      this.mode_admin.available_modes = ["control_pw_type",
+                                         "control_tile_type", "chat",
+                                         "study", "play", "edit"]
+      this.mode_control_tile_draw.available_modes = ["admin_enter"]
+      this.mode_edit.available_modes = ["write", "annotate", "portal",
+                                        "password", "chat", "study", "play",
+                                        "admin_enter"]
       this.mode = this.mode_waiting_for_server;
       this.inputEl = document.getElementById("input");
       this.inputEl.focus();
@@ -588,15 +609,16 @@ let tui = {
   },
   switch_mode: function(mode_name) {
     this.inputEl.focus();
-    this.map_mode = 'terrain';
+    this.map_mode = 'terrain + things';
+    this.tile_draw = false;
+    if (mode_name == 'admin_enter' && this.is_admin) {
+        mode_name = 'admin';
+    };
     this.mode = this['mode_' + mode_name];
     if (game.player_id in game.things && (this.mode.shows_info || this.mode.name == 'control_tile_draw')) {
         explorer.position = game.things[game.player_id].position;
         if (this.mode.shows_info) {
-        explorer.query_info();
-        } else if (this.mode.name == 'control_tile_draw') {
-            explorer.send_tile_control_command();
-            this.map_mode = 'control';
+            explorer.query_info();
         }
     }
     this.empty_input();
@@ -605,7 +627,7 @@ 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') {
+    if (this.mode.name == 'play' || this.mode.name == 'study' || this.mode.name == 'control_tile_draw' || this.mode.name == 'edit') {
         for (const move_key of document.querySelectorAll('[id^="move_"]')) {
             move_key.disabled = false;
         }
@@ -619,6 +641,12 @@ let tui = {
     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;
+    }
+    if (!this.mode.is_intro && this.mode.name != 'admin' && this.mode.name != 'admin_enter') {
+        document.getElementById("switch_to_admin_enter").disabled = false;
+    }
     if (this.mode.name == 'login') {
         if (this.login_name) {
             server.send(['LOGIN', this.login_name]);
@@ -632,29 +660,35 @@ let tui = {
         if (game.tasks.includes('DROP')) {
             document.getElementById("drop_thing").disabled = false;
         }
-        if (game.tasks.includes('FLATTEN_SURROUNDINGS')) {
-            document.getElementById("flatten").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_edit").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_admin").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;
     } 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') {
+    } else if (this.mode.name == 'admin_enter') {
         this.log_msg('@ enter admin password:')
+    } else if (this.mode.name == 'control_pw_type') {
+        this.log_msg('@ enter tile control character for which you want to change the password:')
+    } else if (this.mode.name == 'control_tile_type') {
+        this.log_msg('@ enter tile control character which you want to draw:')
     } else if (this.mode.name == 'control_pw_pw') {
         this.log_msg('@ enter tile control password for "' + this.tile_control_char + '":');
-    } else if (this.mode.name == 'control_pw_pw') {
-        this.log_msg('@ enter tile control 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 tile control 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();
   },
@@ -764,27 +798,29 @@ let tui = {
   draw_map: function() {
     let map_lines_split = [];
     let line = [];
-    let map_content = game.map;
-    if (this.map_mode == 'control') {
-        map_content = game.map_control;
-    }
     for (let i = 0, j = 0; i < game.map.length; i++, j++) {
         if (j == game.map_size[1]) {
             map_lines_split.push(line);
             line = [];
             j = 0;
         };
-        line.push(map_content[i] + ' ');
+        if (['edit', 'write', 'control_tile_draw',
+             'control_tile_type'].includes(this.mode.name)) {
+            line.push(game.map[i] + game.map_control[i]);
+        } else {
+            line.push(game.map[i] + ' ');
+        }
     };
     map_lines_split.push(line);
-    if (this.map_mode == 'annotations') {
+    if (this.map_mode == 'terrain + annotations') {
         for (const coordinate of explorer.info_hints) {
             map_lines_split[coordinate[0]][coordinate[1]] = 'A ';
         }
-    } else if (this.map_mode == 'terrain') {
+    } else if (this.map_mode == 'terrain + things') {
         for (const p in game.portals) {
             let coordinate = p.split(',')
-            map_lines_split[coordinate[0]][coordinate[1]] = 'P ';
+            let original = map_lines_split[coordinate[0]][coordinate[1]];
+            map_lines_split[coordinate[0]][coordinate[1]] = original[0] + 'P';
         }
         let used_positions = [];
         for (const thing_id in game.things) {
@@ -908,13 +944,10 @@ let tui = {
               content += "[" + movement_keys_desc + "] – move player\n";
           }
           if (game.tasks.includes('PICK_UP')) {
-              content += "[" + this.keys.take_thing + "] – take thing under player\n";
+              content += "[" + this.keys.take_thing + "] – pick up thing\n";
           }
           if (game.tasks.includes('DROP')) {
-              content += "[" + this.keys.drop_thing + "] – drop carried thing\n";
-          }
-          if (game.tasks.includes('FLATTEN_SURROUNDINGS')) {
-              content += "[" + tui.keys.flatten + "] – flatten player's surroundings\n";
+              content += "[" + this.keys.drop_thing + "] – drop picked up thing\n";
           }
           content += "[" + tui.keys.teleport + "] – teleport to other space\n";
           content += '\n';
@@ -923,10 +956,22 @@ let tui = {
           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 += '\n';
+      } else if (this.mode.name == 'edit') {
+          content += "Available actions:\n";
+          if (game.tasks.includes('FLATTEN_SURROUNDINGS')) {
+              content += "[" + tui.keys.flatten + "] – flatten player's surroundings\n";
+          }
+          content += '\n';
+      } else if (this.mode.name == 'control_tile_draw') {
+          content += "Available actions:\n";
+          content += "[" + tui.keys.toggle_tile_draw + "] – toggle tile control drawing\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.keys.switch_to_edit + ' or /edit – switch to map edit mode\n';
+          content += '/' + this.keys.switch_to_admin_enter + ' or /admin – switch to admin mode\n';
       }
       content += this.mode.list_available_modes();
       let start_x = 0;
@@ -939,6 +984,22 @@ let tui = {
           terminal.write(y, start_x, lines[i]);
       }
   },
+  toggle_tile_draw: function() {
+      if (tui.tile_draw) {
+          tui.tile_draw = false;
+      } else {
+          tui.tile_draw = true;
+      }
+  },
+  toggle_map_mode: function() {
+      if (tui.map_mode == 'terrain only') {
+          tui.map_mode = 'terrain + annotations';
+      } else if (tui.map_mode == 'terrain + annotations') {
+          tui.map_mode = 'terrain + things';
+      } else {
+          tui.map_mode = 'terrain only';
+      }
+  },
   full_refresh: function() {
     this.links = {};
     terminal.drawBox(0, 0, terminal.rows, terminal.cols);
@@ -1053,7 +1114,7 @@ let explorer = {
             this.position = target
             if (tui.mode.shows_info) {
                 this.query_info();
-            } else if (tui.mode.name == 'control_tile_draw') {
+            } else if (tui.tile_draw) {
                 this.send_tile_control_command();
             }
         } else {
@@ -1077,11 +1138,11 @@ let explorer = {
         server.send(["GET_ANNOTATION", unparser.to_yx(explorer.position)]);
     },
     get_info: function() {
+        let info = "MAP VIEW: " + tui.map_mode + "\n";
         let position_i = this.position[0] * game.map_size[1] + this.position[1];
         if (game.fov[position_i] != '.') {
-            return 'outside field of view';
+            return info + 'outside field of view';
         };
-        let info = "";
         let terrain_char = game.map[position_i]
         let terrain_desc = '?'
         if (game.terrains[terrain_char]) {
@@ -1141,18 +1202,15 @@ tui.inputEl.addEventListener('input', (event) => {
             tui.inputEl.value = tui.inputEl.value.slice(0, max_length);
         };
         tui.recalc_input_lines();
-    } else if (tui.mode.name == 'edit' && tui.inputEl.value.length > 0) {
+    } else if (tui.mode.name == 'write' && tui.inputEl.value.length > 0) {
         server.send(["TASK:WRITE", tui.inputEl.value[0], tui.password]);
-        tui.switch_mode('play');
-    } else if (tui.mode.name == 'control_pw_type' && tui.inputEl.value.length > 0) {
-        tui.tile_control_char = tui.inputEl.value[0];
-        tui.switch_mode('control_pw_pw');
-    } else if (tui.mode.name == 'control_tile_type' && tui.inputEl.value.length > 0) {
-        tui.tile_control_char = tui.inputEl.value[0];
-        tui.switch_mode('control_tile_draw');
+        tui.switch_mode('edit');
     }
     tui.full_refresh();
 }, false);
+document.onclick = function() {
+    tui.show_help = false;
+};
 tui.inputEl.addEventListener('keydown', (event) => {
     tui.show_help = false;
     if (event.key == 'Enter') {
@@ -1176,22 +1234,38 @@ tui.inputEl.addEventListener('keydown', (event) => {
             server.send(['SET_MAP_CONTROL_PASSWORD',
                         tui.tile_control_char, tui.inputEl.value]);
         }
-        tui.switch_mode('play');
+        tui.switch_mode('admin');
     } else if (tui.mode.name == 'portal' && event.key == 'Enter') {
         explorer.set_portal(tui.inputEl.value);
-        tui.switch_mode('play');
+        tui.switch_mode('edit');
     } else if (tui.mode.name == 'annotate' && event.key == 'Enter') {
         explorer.annotate(tui.inputEl.value);
-        tui.switch_mode('play');
+        tui.switch_mode('edit');
     } else if (tui.mode.name == 'password' && event.key == 'Enter') {
         if (tui.inputEl.value.length == 0) {
             tui.inputEl.value = " ";
         }
         tui.password = tui.inputEl.value
-        tui.switch_mode('play');
-    } else if (tui.mode.name == 'admin' && event.key == 'Enter') {
+        tui.switch_mode('edit');
+    } else if (tui.mode.name == 'admin_enter' && event.key == 'Enter') {
         server.send(['BECOME_ADMIN', tui.inputEl.value]);
         tui.switch_mode('play');
+    } else if (tui.mode.name == 'control_pw_type' && event.key == 'Enter') {
+        if (tui.inputEl.value.length != 1) {
+            tui.log_msg('@ entered non-single-char, therefore aborted');
+            tui.switch_mode('admin');
+        } else {
+            tui.tile_control_char = tui.inputEl.value[0];
+            tui.switch_mode('control_pw_pw');
+        }
+    } else if (tui.mode.name == 'control_tile_type' && event.key == 'Enter') {
+        if (tui.inputEl.value.length != 1) {
+            tui.log_msg('@ entered non-single-char, therefore aborted');
+            tui.switch_mode('admin');
+        } else {
+            tui.tile_control_char = tui.inputEl.value[0];
+            tui.switch_mode('control_tile_draw');
+        }
     } else if (tui.mode.name == 'chat' && event.key == 'Enter') {
         let tokens = parser.tokenize(tui.inputEl.value);
         if (tokens.length > 0 && tokens[0].length > 0) {
@@ -1200,6 +1274,10 @@ tui.inputEl.addEventListener('keydown', (event) => {
                     tui.switch_mode('play');
                 } else if (tokens[0].slice(1) == 'study' || tokens[0][1] == tui.keys.switch_to_study) {
                     tui.switch_mode('study');
+                } else if (tokens[0].slice(1) == 'edit' || tokens[0][1] == tui.keys.switch_to_edit) {
+                    tui.switch_mode('edit');
+                } else if (tokens[0].slice(1) == 'admin' || tokens[0][1] == tui.keys.switch_to_admin_enter) {
+                    tui.switch_mode('admin_enter');
                 } else if (tokens[0].slice(1) == 'nick') {
                     if (tokens.length > 1) {
                         server.send(['NICK', tokens[1]]);
@@ -1219,9 +1297,6 @@ 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.flatten
-                     && game.tasks.includes('FLATTEN_SURROUNDINGS')) {
-              server.send(["TASK:FLATTEN_SURROUNDINGS", tui.password]);
           } else if (event.key === tui.keys.take_thing
                      && game.tasks.includes('PICK_UP')) {
               server.send(["TASK:PICK_UP"]);
@@ -1233,12 +1308,6 @@ tui.inputEl.addEventListener('keydown', (event) => {
               server.send(['TASK:MOVE', tui.movement_keys[event.key]]);
           } else if (event.key === tui.keys.teleport) {
               game.teleport();
-          } else if (event.key === tui.keys.switch_to_portal) {
-              event.preventDefault();
-              tui.switch_mode('portal');
-          } else if (event.key === tui.keys.switch_to_annotate) {
-              event.preventDefault();
-              tui.switch_mode('annotate');
           };
     } else if (tui.mode.name == 'study') {
         if (tui.mode.mode_switch_on_key(event)) {
@@ -1246,20 +1315,30 @@ tui.inputEl.addEventListener('keydown', (event) => {
         } else if (event.key in tui.movement_keys) {
             explorer.move(tui.movement_keys[event.key]);
         } else if (event.key == tui.keys.toggle_map_mode) {
-            if (tui.map_mode == 'terrain') {
-                tui.map_mode = 'annotations';
-            } else if (tui.map_mode == 'annotations') {
-                tui.map_mode = 'control';
-            } else {
-                tui.map_mode = 'terrain';
-            }
+            tui.toggle_map_mode();
         };
     } else if (tui.mode.name == 'control_tile_draw') {
         if (tui.mode.mode_switch_on_key(event)) {
               null;
         } else if (event.key in tui.movement_keys) {
             explorer.move(tui.movement_keys[event.key]);
+        } else if (event.key === tui.keys.toggle_tile_draw) {
+            tui.toggle_tile_draw();
         };
+    } else if (tui.mode.name == 'admin') {
+        if (tui.mode.mode_switch_on_key(event)) {
+              null;
+        };
+    } 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')) {
+            server.send(['TASK:MOVE', tui.movement_keys[event.key]]);
+        } else if (event.key === tui.keys.flatten
+                   && game.tasks.includes('FLATTEN_SURROUNDINGS')) {
+            server.send(["TASK:FLATTEN_SURROUNDINGS", tui.password]);
+        }
     }
     tui.full_refresh();
 }, false);
@@ -1302,54 +1381,18 @@ document.getElementById("help").onclick = function() {
     tui.show_help = true;
     tui.full_refresh();
 };
-document.getElementById("switch_to_play").onclick = function() {
-    tui.switch_mode('play');
-    tui.full_refresh();
-};
-document.getElementById("switch_to_study").onclick = function() {
-    tui.switch_mode('study');
-    tui.full_refresh();
-};
-document.getElementById("switch_to_chat").onclick = function() {
-    tui.switch_mode('chat');
-    tui.full_refresh();
-};
-document.getElementById("switch_to_password").onclick = function() {
-    tui.switch_mode('password');
-    tui.full_refresh();
-};
-document.getElementById("switch_to_edit").onclick = function() {
-    tui.switch_mode('edit');
-    tui.full_refresh();
-};
-document.getElementById("switch_to_annotate").onclick = function() {
-    tui.switch_mode('annotate');
-    tui.full_refresh();
-};
-document.getElementById("switch_to_portal").onclick = function() {
-    tui.switch_mode('portal');
-    tui.full_refresh();
-};
-document.getElementById("switch_to_admin").onclick = function() {
-    tui.switch_mode('admin');
-    tui.full_refresh();
-};
-document.getElementById("switch_to_control_pw_type").onclick = function() {
-    tui.switch_mode('control_pw_type');
-    tui.full_refresh();
-};
-document.getElementById("switch_to_control_tile_type").onclick = function() {
-    tui.switch_mode('control_tile_type');
-    tui.full_refresh();
+for (const switchEl  of document.querySelectorAll('[id^="switch_to_"]')) {
+    const mode = switchEl.id.slice("switch_to_".length);
+    switchEl.onclick = function() {
+        tui.switch_mode(mode);
+        tui.full_refresh();
+    }
 };
+document.getElementById("toggle_tile_draw").onclick = function() {
+    tui.toggle_tile_draw();
+}
 document.getElementById("toggle_map_mode").onclick = function() {
-    if (tui.map_mode == 'terrain') {
-        tui.map_mode = 'annotations';
-    } else if (tui.map_mode == 'annotations') {
-        tui.map_mode = 'control';
-    } else {
-        tui.map_mode = 'terrain';
-    }
+    tui.toggle_map_mode();
     tui.full_refresh();
 };
 document.getElementById("take_thing").onclick = function() {