home · contact · privacy
Improve keybinding help in web client.
[plomrogue2-experiments] / new2 / rogue_chat_nocanvas_monochrome.html
1 <!DOCTYPE html>
2 <html><head>
3 <style>
4 </style>
5 </head><body>
6 <div>
7 terminal rows: <input id="n_rows" type="number" step=4 min=8 value=24 />
8 terminal columns: <input id="n_cols" type="number" step=4 min=20 value=80 />
9 </div>
10 <pre id="terminal" style="display: inline-block;"></pre>
11 <textarea id="input" style="opacity: 0; width: 0px;"></textarea>
12 <div>
13 keys (see <a href="https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key/Key_Values">here</a> for non-obvious available values):<br />
14 move up (square grid): <input id="key_square_move_up" type="text" value="w" /> (hint: ArrowUp)<br />
15 move left (square grid): <input id="key_square_move_left" type="text" value="a" /> (hint: ArrowLeft)<br />
16 move down (square grid): <input id="key_square_move_down" type="text" value="s" /> (hint: ArrowDown)<br />
17 move right (square grid): <input id="key_square_move_right" type="text" value="d" /> (hint: ArrowRight)<br />
18 move up-left (hex grid): <input id="key_hex_move_upleft" type="text" value="w" /><br />
19 move up-right (hex grid): <input id="key_hex_move_upright" type="text" value="e" /><br />
20 move right (hex grid): <input id="key_hex_move_right" type="text" value="d" /><br />
21 move down-right (hex grid): <input id="key_hex_move_downright" type="text" value="x" /><br />
22 move down-left (hex grid): <input id="key_hex_move_downleft" type="text" value="y" /><br />
23 move left (hex grid): <input id="key_hex_move_left" type="text" value="a" /><br />
24 flatten surroundings: <input id="key_flatten" type="text" value="F" /><br />
25 switch to chat mode: <input id="key_switch_to_chat" type="text" value="t" /><br />
26 switch to play mode: <input id="key_switch_to_play" type="text" value="p" /><br />
27 switch to study mode: <input id="key_switch_to_study" type="text" value="?" /><br />
28 edit terrain (from play mode): <input id="key_switch_to_edit" type="text" value="m" /><br />
29 annotate terrain (from study mode): <input id="key_switch_to_annotate" type="text" value="m" /><br />
30 annotate portal (from study mode): <input id="key_switch_to_portal" type="text" value="P" /><br />
31 </div>
32 <script>
33 "use strict";
34 let websocket_location = "ws://localhost:8000";
35
36 let rows_selector = document.getElementById("n_rows");
37 let cols_selector = document.getElementById("n_cols");
38 let key_selectors = document.querySelectorAll('[id^="key_"]');
39
40 function restore_selector_value(selector) {
41     let stored_selection = window.localStorage.getItem(selector.id);
42     if (stored_selection) {
43         selector.value = stored_selection;
44     }
45 }
46 restore_selector_value(rows_selector);
47 restore_selector_value(cols_selector);
48 for (let key_selector of key_selectors) {
49     restore_selector_value(key_selector);
50 }
51
52 let terminal = {
53   foreground: 'white',
54   background: 'black',
55   initialize: function() {
56     this.rows = rows_selector.value;
57     this.cols = cols_selector.value;
58     this.pre_el = document.getElementById("terminal");
59     this.pre_el.style.color = this.foreground;
60     this.pre_el.style.backgroundColor = this.background;
61     this.content = [];
62       let line = []
63     for (let y = 0, x = 0; y <= this.rows; x++) {
64         if (x == this.cols) {
65             x = 0;
66             y += 1;
67             this.content.push(line);
68             line = [];
69             if (y == this.rows) {
70                 break;
71             }
72         }
73         line.push(' ');
74     }
75   },
76   blink_screen: function() {
77       this.pre_el.style.color = this.background;
78       this.pre_el.style.backgroundColor = this.foreground;
79       setTimeout(() => {
80           this.pre_el.style.color = this.foreground;
81           this.pre_el.style.backgroundColor = this.background;
82       }, 100);
83   },
84   refresh: function() {
85       let pre_string = '';
86       for (let y = 0; y < this.rows; y++) {
87           let line = this.content[y].join('');
88           pre_string += line + '\n';
89       }
90       this.pre_el.textContent = pre_string;
91   },
92   write: function(start_y, start_x, msg) {
93       for (let x = start_x, i = 0; x < this.cols && i < msg.length; x++, i++) {
94           this.content[start_y][x] = msg[i];
95       }
96   },
97   drawBox: function(start_y, start_x, height, width) {
98     let end_y = start_y + height;
99     let end_x = start_x + width;
100     for (let y = start_y, x = start_x; y < this.rows; x++) {
101       if (x == end_x) {
102         x = start_x;
103         y += 1;
104         if (y == end_y) {
105             break;
106         }
107       };
108       this.content[y][x] = ' ';
109     }
110   },
111 }
112 terminal.initialize();
113
114 let parser = {
115   tokenize: function(str) {
116     let token_ends = [];
117     let tokens = [];
118     let token = ''
119     let quoted = false;
120     let escaped = false;
121     for (let i = 0; i < str.length; i++) {
122       let c = str[i];
123       if (quoted) {
124         if (escaped) {
125           token += c;
126           escaped = false;
127         } else if (c == '\\') {
128           escaped = true;
129         } else if (c == '"') {
130           quoted = false
131         } else {
132           token += c;
133         }
134       } else if (c == '"') {
135         quoted = true
136       } else if (c === ' ') {
137         if (token.length > 0) {
138           token_ends.push(i);
139           tokens.push(token);
140           token = '';
141         }
142       } else {
143         token += c;
144       }
145     }
146     if (token.length > 0) {
147       tokens.push(token);
148     }
149     let token_starts = [];
150     for (let i = 0; i < token_ends.length; i++) {
151       token_starts.push(token_ends[i] - tokens[i].length);
152     };
153     return [tokens, token_starts];
154   },
155   parse_yx: function(position_string) {
156     let coordinate_strings = position_string.split(',')
157     let position = [0, 0];
158     position[0] = parseInt(coordinate_strings[0].slice(2));
159     position[1] = parseInt(coordinate_strings[1].slice(2));
160     return position;
161   },
162 }
163
164 class Thing {
165     constructor(yx) {
166         this.position = yx;
167     }
168 }
169
170 let server = {
171     init: function(url) {
172         this.url = url;
173         this.websocket = new WebSocket(this.url);
174         this.websocket.onopen = function(event) {
175             window.setInterval(function() { server.send(['PING']) }, 30000);
176             tui.log_msg("@ server connected! :)");
177             tui.switch_mode(mode_login);
178         };
179         this.websocket.onclose = function(event) {
180             tui.log_msg("@ server disconnected :(");
181             tui.log_msg("@ hint: try the '/reconnect' command");
182         };
183             this.websocket.onmessage = this.handle_event;
184         },
185     reconnect: function() {
186            this.reconnect_to(this.url);
187     },
188     reconnect_to: function(url) {
189         this.websocket.close();
190         this.init(url);
191     },
192     send: function(tokens) {
193         this.websocket.send(unparser.untokenize(tokens));
194     },
195     handle_event: function(event) {
196         let tokens = parser.tokenize(event.data)[0];
197         if (tokens[0] === 'TURN') {
198             game.turn_complete = false;
199             game.things = {};
200             game.portals = {};
201             game.turn = parseInt(tokens[1]);
202         } else if (tokens[0] === 'THING_POS') {
203             game.get_thing(tokens[1], true).position = parser.parse_yx(tokens[2]);
204         } else if (tokens[0] === 'THING_NAME') {
205             game.get_thing(tokens[1], true).name_ = tokens[2];
206         } else if (tokens[0] === 'MAP') {
207             game.map_geometry = tokens[1];
208             tui.init_keys();
209             game.map_size = parser.parse_yx(tokens[2]);
210             game.map = tokens[3]
211         } else if (tokens[0] === 'GAME_STATE_COMPLETE') {
212             game.turn_complete = true;
213             explorer.empty_info_db();
214             if (tui.mode == mode_post_login_wait) {
215                 tui.switch_mode(mode_play);
216                 tui.log_help();
217             } else if (tui.mode == mode_study) {
218                 explorer.query_info();
219             }
220             let t = game.get_thing(game.player_id);
221             if (t.position in game.portals) {
222                 tui.teleport_target = game.portals[t.position];
223                 tui.switch_mode(mode_teleport);
224                 return;
225             }
226             tui.full_refresh();
227         } else if (tokens[0] === 'CHAT') {
228              tui.log_msg('# ' + tokens[1], 1);
229         } else if (tokens[0] === 'PLAYER_ID') {
230             game.player_id = parseInt(tokens[1]);
231         } else if (tokens[0] === 'LOGIN_OK') {
232             this.send(['GET_GAMESTATE']);
233             tui.switch_mode(mode_post_login_wait);
234         } else if (tokens[0] === 'PORTAL') {
235             let position = parser.parse_yx(tokens[1]);
236             game.portals[position] = tokens[2];
237         } else if (tokens[0] === 'ANNOTATION') {
238             let position = parser.parse_yx(tokens[1]);
239             explorer.update_info_db(position, tokens[2]);
240         } else if (tokens[0] === 'UNHANDLED_INPUT') {
241             tui.log_msg('? unknown command');
242         } else if (tokens[0] === 'PLAY_ERROR') {
243             terminal.blink_screen();
244         } else if (tokens[0] === 'ARGUMENT_ERROR') {
245             tui.log_msg('? syntax error: ' + tokens[1]);
246         } else if (tokens[0] === 'GAME_ERROR') {
247             tui.log_msg('? game error: ' + tokens[1]);
248         } else if (tokens[0] === 'PONG') {
249             console.log('PONG');
250         } else {
251             tui.log_msg('? unhandled input: ' + event.data);
252         }
253     }
254 }
255
256 let unparser = {
257     quote: function(str) {
258         let quoted = ['"'];
259         for (let i = 0; i < str.length; i++) {
260             let c = str[i];
261             if (['"', '\\'].includes(c)) {
262                 quoted.push('\\');
263             };
264             quoted.push(c);
265         }
266         quoted.push('"');
267         return quoted.join('');
268     },
269     to_yx: function(yx_coordinate) {
270         return "Y:" + yx_coordinate[0] + ",X:" + yx_coordinate[1];
271     },
272     untokenize: function(tokens) {
273         let quoted_tokens = [];
274         for (let token of tokens) {
275             quoted_tokens.push(this.quote(token));
276         }
277         return quoted_tokens.join(" ");
278     }
279 }
280
281 class Mode {
282     constructor(name, has_input_prompt=false, shows_info=false, is_intro=false) {
283         this.name = name;
284         this.has_input_prompt = has_input_prompt;
285         this.shows_info= shows_info;
286         this.is_intro = is_intro;
287     }
288 }
289 let mode_waiting_for_server = new Mode('waiting_for_server', false, false, true);
290 let mode_login = new Mode('login', true, false, true);
291 let mode_post_login_wait = new Mode('waiting for game world', false, false, true);
292 let mode_chat = new Mode('chat / write messages to players', true, false);
293 let mode_annotate = new Mode('add message to map tile', true, true);
294 let mode_play = new Mode('play / move around', false, false);
295 let mode_study = new Mode('check map tiles for messages', false, true);
296 let mode_edit = new Mode('write ASCII char to map tile', false, false);
297 let mode_teleport = new Mode('teleport away?', true);
298 let mode_portal = new Mode('add portal to map tile', true, true);
299
300 let tui = {
301   mode: mode_waiting_for_server,
302   log: [],
303   input_prompt: '> ',
304   input_lines: [],
305   window_width: terminal.cols / 2,
306   height_turn_line: 1,
307   height_mode_line: 1,
308   height_input: 1,
309   init: function() {
310       this.inputEl = document.getElementById("input");
311       this.inputEl.focus();
312       this.recalc_input_lines();
313       this.height_header = this.height_turn_line + this.height_mode_line;
314       this.log_msg("@ waiting for server connection ...");
315       this.init_keys();
316   },
317   init_keys: function() {
318     this.keys = {};
319     for (let key_selector of key_selectors) {
320         this.keys[key_selector.id.slice(4)] = key_selector.value;
321     }
322     if (game.map_geometry == 'Square') {
323         this.movement_keys = {
324             [this.keys.square_move_up]: 'UP',
325             [this.keys.square_move_left]: 'LEFT',
326             [this.keys.square_move_down]: 'DOWN',
327             [this.keys.square_move_right]: 'RIGHT'
328         };
329     } else if (game.map_geometry == 'Hex') {
330         this.movement_keys = {
331             [this.keys.hex_move_upleft]: 'UPLEFT',
332             [this.keys.hex_move_upright]: 'UPRIGHT',
333             [this.keys.hex_move_right]: 'RIGHT',
334             [this.keys.hex_move_downright]: 'DOWNRIGHT',
335             [this.keys.hex_move_downleft]: 'DOWNLEFT',
336             [this.keys.hex_move_left]: 'LEFT'
337         };
338     };
339   },
340   switch_mode: function(mode, keep_pos=false) {
341     if (mode == mode_study && !keep_pos && game.player_id in game.things) {
342       explorer.position = game.things[game.player_id].position;
343     }
344     this.mode = mode;
345     this.empty_input();
346     if (mode == mode_annotate && explorer.position in explorer.info_db) {
347         let info = explorer.info_db[explorer.position];
348         if (info != "(none)") {
349             this.inputEl.value = info;
350             this.recalc_input_lines();
351         }
352     }
353     if (mode == mode_login) {
354         if (this.login_name) {
355             server.send(['LOGIN', this.login_name]);
356         } else {
357             this.log_msg("? need login name");
358         }
359     } else if (mode == mode_portal && explorer.position in game.portals) {
360         let portal = game.portals[explorer.position]
361         this.inputEl.value = portal;
362         this.recalc_input_lines();
363     } else if (mode == mode_teleport) {
364         tui.log_msg("@ May teleport to: " + tui.teleport_target);
365         tui.log_msg("@ Enter 'YES!' to entusiastically affirm.");
366     }
367     this.full_refresh();
368   },
369   empty_input: function(str) {
370       this.inputEl.value = "";
371       if (this.mode.has_input_prompt) {
372           this.recalc_input_lines();
373       } else {
374           this.height_input = 0;
375       }
376   },
377   recalc_input_lines: function() {
378       this.input_lines = this.msg_into_lines_of_width(this.input_prompt + this.inputEl.value, this.window_width);
379       this.height_input = this.input_lines.length;
380   },
381   msg_into_lines_of_width: function(msg, width) {
382     let chunk = "";
383     let lines = [];
384     for (let i = 0, x = 0; i < msg.length; i++, x++) {
385       if (x >= width || msg[i] == "\n") {
386         lines.push(chunk);
387         chunk = "";
388         x = 0;
389       };
390       if (msg[i] != "\n") {
391         chunk += msg[i];
392       }
393     }
394     lines.push(chunk);
395     return lines;
396   },
397   log_msg: function(msg) {
398       this.log.push(msg);
399       while (this.log.length > 100) {
400         this.log.shift();
401       };
402       this.full_refresh();
403   },
404   log_help: function() {
405     let movement_keys_desc = Object.keys(this.movement_keys).join(',');
406     this.log_msg("HELP:");
407     this.log_msg("chat mode commands:");
408     this.log_msg("  /nick NAME - re-name yourself to NAME");
409     this.log_msg("  /msg USER TEXT - send TEXT to USER");
410     this.log_msg("  /help - show this help");
411     this.log_msg("  /" + this.keys.switch_to_play + " or /play - switch to play mode");
412     this.log_msg("  /" + this.keys.switch_to_study + " or /study - switch to study mode");
413     this.log_msg("commands common to study and play mode:");
414     this.log_msg("  " + movement_keys_desc + " - move");
415     this.log_msg("  " + this.keys.switch_to_chat + " - switch to chat mode");
416     this.log_msg("commands specific to play mode:");
417     this.log_msg("  " + this.keys.switch_to_edit + " - write following ASCII character");
418     this.log_msg("  " + this.keys.flatten + " - flatten surroundings");
419     this.log_msg("  " + this.keys.switch_to_study + " - switch to study mode");
420     this.log_msg("commands specific to study mode:");
421     this.log_msg("  " + this.keys.switch_to_annotate + " - annotate terrain");
422     this.log_msg("  " + this.keys.switch_to_play + " - switch to play mode");
423   },
424   draw_map: function() {
425     let map_lines_split = [];
426     let line = [];
427     for (let i = 0, j = 0; i < game.map.length; i++, j++) {
428         if (j == game.map_size[1]) {
429             map_lines_split.push(line);
430             line = [];
431             j = 0;
432         };
433         line.push(game.map[i]);
434     };
435     map_lines_split.push(line);
436     for (const thing_id in game.things) {
437         let t = game.things[thing_id];
438         map_lines_split[t.position[0]][t.position[1]] = '@';
439     };
440     if (tui.mode.shows_info) {
441         map_lines_split[explorer.position[0]][explorer.position[1]] = '?';
442     }
443     let map_lines = []
444     if (game.map_geometry == 'Square') {
445         for (let line_split of map_lines_split) {
446             map_lines.push(line_split.join(' '));
447         };
448     } else if (game.map_geometry == 'Hex') {
449         let indent = 0
450         for (let line_split of map_lines_split) {
451             map_lines.push(' '.repeat(indent) + line_split.join(' '));
452             if (indent == 0) {
453                 indent = 1;
454             } else {
455                 indent = 0;
456             };
457         };
458     }
459     let window_center = [terminal.rows / 2, this.window_width / 2];
460     let player = game.things[game.player_id];
461     let center_position = [player.position[0], player.position[1]];
462     if (tui.mode.shows_info) {
463         center_position = [explorer.position[0], explorer.position[1]];
464     }
465     center_position[1] = center_position[1] * 2;
466     let offset = [center_position[0] - window_center[0],
467                   center_position[1] - window_center[1]]
468     if (game.map_geometry == 'Hex' && offset[0] % 2) {
469         offset[1] += 1;
470     };
471     let term_y = Math.max(0, -offset[0]);
472     let term_x = Math.max(0, -offset[1]);
473     let map_y = Math.max(0, offset[0]);
474     let map_x = Math.max(0, offset[1]);
475     for (; term_y < terminal.rows && map_y < game.map_size[0]; term_y++, map_y++) {
476         let to_draw = map_lines[map_y].slice(map_x, this.window_width + offset[1]);
477         terminal.write(term_y, term_x, to_draw);
478     }
479   },
480   draw_mode_line: function() {
481       terminal.write(0, this.window_width, 'MODE: ' + this.mode.name);
482   },
483   draw_turn_line: function(n) {
484     terminal.write(1, this.window_width, 'TURN: ' + game.turn);
485   },
486   draw_history: function() {
487       let log_display_lines = [];
488       for (let line of this.log) {
489           log_display_lines = log_display_lines.concat(this.msg_into_lines_of_width(line, this.window_width));
490       };
491       for (let y = terminal.rows - 1 - this.height_input,
492                i = log_display_lines.length - 1;
493            y >= this.height_header && i >= 0;
494            y--, i--) {
495           terminal.write(y, this.window_width, log_display_lines[i]);
496       }
497   },
498   draw_info: function() {
499     let lines = this.msg_into_lines_of_width(explorer.get_info(), this.window_width);
500     for (let y = this.height_header, i = 0; y < terminal.rows && i < lines.length; y++, i++) {
501       terminal.write(y, this.window_width, lines[i]);
502     }
503   },
504   draw_input: function() {
505     if (this.mode.has_input_prompt) {
506         for (let y = terminal.rows - this.height_input, i = 0; i < this.input_lines.length; y++, i++) {
507             terminal.write(y, this.window_width, this.input_lines[i]);
508         }
509     }
510   },
511   full_refresh: function() {
512     terminal.drawBox(0, 0, terminal.rows, terminal.cols);
513     if (this.mode.is_intro) {
514         this.draw_history();
515         this.draw_input();
516     } else {
517         if (game.turn_complete) {
518             this.draw_map();
519             this.draw_turn_line();
520         }
521         this.draw_mode_line();
522         if (this.mode.shows_info) {
523           this.draw_info();
524         } else {
525           this.draw_history();
526         }
527         this.draw_input();
528     }
529     terminal.refresh();
530   }
531 }
532
533 let game = {
534     init: function() {
535         this.things = {};
536         this.turn = -1;
537         this.map = "";
538         this.map_size = [0,0];
539         this.player_id = -1;
540         this.portals = {};
541     },
542     get_thing: function(id_, create_if_not_found=false) {
543         if (id_ in game.things) {
544             return game.things[id_];
545         } else if (create_if_not_found) {
546             let t = new Thing([0,0]);
547             game.things[id_] = t;
548             return t;
549         };
550     },
551     move: function(start_position, direction) {
552         let target = [start_position[0], start_position[1]];
553         if (direction == 'LEFT') {
554             target[1] -= 1;
555         } else if (direction == 'RIGHT') {
556             target[1] += 1;
557         } else if (game.map_geometry == 'Square') {
558             if (direction == 'UP') {
559                 target[0] -= 1;
560             } else if (direction == 'DOWN') {
561                 target[0] += 1;
562             };
563         } else if (game.map_geometry == 'Hex') {
564             let start_indented = start_position[0] % 2;
565             if (direction == 'UPLEFT') {
566                 target[0] -= 1;
567                 if (!start_indented) {
568                     target[1] -= 1;
569                 }
570             } else if (direction == 'UPRIGHT') {
571                 target[0] -= 1;
572                 if (start_indented) {
573                     target[1] += 1;
574                 }
575             } else if (direction == 'DOWNLEFT') {
576                 target[0] += 1;
577                 if (!start_indented) {
578                     target[1] -= 1;
579                 }
580             } else if (direction == 'DOWNRIGHT') {
581                 target[0] += 1;
582                 if (start_indented) {
583                     target[1] += 1;
584                 }
585             };
586         };
587         if (target[0] < 0 || target[1] < 0 ||
588             target[0] >= this.map_size[0] || target[1] >= this.map_size[1]) {
589             return null;
590         };
591         return target;
592     }
593 }
594
595 game.init();
596 tui.init();
597 tui.full_refresh();
598 server.init(websocket_location);
599
600 let explorer = {
601     position: [0,0],
602     info_db: {},
603     move: function(direction) {
604         let target = game.move(this.position, direction);
605         if (target) {
606             this.position = target
607             this.query_info();
608             tui.full_refresh();
609         } else {
610             terminal.blink_screen();
611         };
612     },
613     update_info_db: function(yx, str) {
614         this.info_db[yx] = str;
615         if (tui.mode == mode_study) {
616             tui.full_refresh();
617         }
618     },
619     empty_info_db: function() {
620         this.info_db = {};
621         if (tui.mode == mode_study) {
622             tui.full_refresh();
623         }
624     },
625     query_info: function() {
626         server.send(["GET_ANNOTATION", unparser.to_yx(explorer.position)]);
627     },
628     get_info: function() {
629         let info = "";
630         let position_i = this.position[0] * game.map_size[1] + this.position[1];
631         info += "TERRAIN: " + game.map[position_i] + "\n";
632         for (let t_id in game.things) {
633              let t = game.things[t_id];
634              if (t.position[0] == this.position[0] && t.position[1] == this.position[1]) {
635                  info += "PLAYER @";
636                  if (t.name_) {
637                      info += ": " + t.name_;
638                  }
639                  info += "\n";
640              }
641         }
642         if (this.position in game.portals) {
643             info += "PORTAL: " + game.portals[this.position] + "\n";
644         }
645         if (this.position in this.info_db) {
646             info += "ANNOTATIONS: " + this.info_db[this.position];
647         } else {
648             info += 'waiting …';
649         }
650         return info;
651     },
652     annotate: function(msg) {
653         if (msg.length == 0) {
654             msg = " ";  // triggers annotation deletion
655         }
656         server.send(["ANNOTATE", unparser.to_yx(explorer.position), msg]);
657     },
658     set_portal: function(msg) {
659         if (msg.length == 0) {
660             msg = " ";  // triggers portal deletion
661         }
662         server.send(["PORTAL", unparser.to_yx(explorer.position), msg]);
663     }
664 }
665
666 tui.inputEl.addEventListener('input', (event) => {
667     if (tui.mode.has_input_prompt) {
668         let max_length = tui.window_width * terminal.rows - tui.input_prompt.length;
669         if (tui.inputEl.value.length > max_length) {
670             tui.inputEl.value = tui.inputEl.value.slice(0, max_length);
671         };
672         tui.recalc_input_lines();
673         tui.full_refresh();
674     } else if (tui.mode == mode_edit && tui.inputEl.value.length > 0) {
675         server.send(["TASK:WRITE", tui.inputEl.value[0]]);
676         tui.switch_mode(mode_play);
677     } else if (tui.mode == mode_teleport) {
678         if (['Y', 'y'].includes(tui.inputEl.value[0])) {
679             server.reconnect_to(tui.teleport_target);
680         } else {
681             tui.log_msg("@ teleportation aborted");
682             tui.switch_mode(mode_play);
683         }
684     }
685 }, false);
686 tui.inputEl.addEventListener('keydown', (event) => {
687     if (event.key == 'Enter') {
688         event.preventDefault();
689     }
690     if (tui.mode == mode_login && event.key == 'Enter') {
691         tui.login_name = tui.inputEl.value;
692         server.send(['LOGIN', tui.inputEl.value]);
693         tui.empty_input();
694     } else if (tui.mode == mode_portal && event.key == 'Enter') {
695         explorer.set_portal(tui.inputEl.value);
696         tui.switch_mode(mode_study, true);
697     } else if (tui.mode == mode_annotate && event.key == 'Enter') {
698         explorer.annotate(tui.inputEl.value);
699         tui.switch_mode(mode_study, true);
700     } else if (tui.mode == mode_teleport && event.key == 'Enter') {
701         if (tui.inputEl.value == 'YES!') {
702             server.reconnect_to(tui.teleport_target);
703         } else {
704             tui.log_msg('@ teleport aborted');
705             tui.switch_mode(mode_play);
706         };
707     } else if (tui.mode == mode_chat && event.key == 'Enter') {
708         let [tokens, token_starts] = parser.tokenize(tui.inputEl.value);
709         if (tokens.length > 0 && tokens[0].length > 0) {
710             if (tui.inputEl.value[0][0] == '/') {
711                 if (tokens[0].slice(1) == 'play' || tokens[0].slice(1) == 'P') {
712                     tui.switch_mode(mode_play);
713                 } else if (tokens[0].slice(1) == 'study' || tokens[0].slice(1) == '?') {
714                     tui.switch_mode(mode_study);
715                 } else if (tokens[0].slice(1) == 'help') {
716                     tui.log_help();
717                 } else if (tokens[0].slice(1) == 'nick') {
718                     if (tokens.length > 1) {
719                         server.send(['LOGIN', tokens[1]]);
720                     } else {
721                         tui.log_msg('? need login name');
722                     }
723                 } else if (tokens[0].slice(1) == 'msg') {
724                     if (tokens.length > 2) {
725                         let msg = tui.inputEl.value.slice(token_starts[2]);
726                         server.send(['QUERY', tokens[1], msg]);
727                     } else {
728                         tui.log_msg('? need message target and message');
729                     }
730                 } else if (tokens[0].slice(1) == 'reconnect') {
731                     if (tokens.length > 1) {
732                         server.reconnect_to(tokens[1]);
733                     } else {
734                         server.reconnect();
735                     }
736                 } else {
737                     tui.log_msg('? unknown command');
738                 }
739             } else {
740                 server.send(['ALL', tui.inputEl.value]);
741             }
742         } else if (tui.inputEl.valuelength > 0) {
743             server.send(['ALL', tui.inputEl.value]);
744         }
745         tui.empty_input();
746         tui.full_refresh();
747       } else if (tui.mode == mode_play) {
748           if (event.key === tui.keys.switch_to_chat) {
749               event.preventDefault();
750               tui.switch_mode(mode_chat);
751           } else if (event.key === tui.keys.switch_to_edit) {
752               event.preventDefault();
753               tui.switch_mode(mode_edit);
754           } else if (event.key === tui.keys.switch_to_study) {
755               tui.switch_mode(mode_study);
756           } else if (event.key === tui.keys.flatten) {
757               server.send(["TASK:FLATTEN_SURROUNDINGS"]);
758           } else if (event.key in tui.movement_keys) {
759               server.send(['TASK:MOVE', tui.movement_keys[event.key]]);
760           };
761     } else if (tui.mode == mode_study) {
762         if (event.key === tui.keys.switch_to_chat) {
763             event.preventDefault();
764             tui.switch_mode(mode_chat);
765         } else if (event.key == tui.keys.switch_to_play) {
766             tui.switch_mode(mode_play);
767         } else if (event.key === tui.keys.switch_to_portal) {
768             event.preventDefault();
769             tui.switch_mode(mode_portal);
770         } else if (event.key in tui.movement_keys) {
771             explorer.move(tui.movement_keys[event.key]);
772         } else if (event.key === tui.keys.switch_to_annotate) {
773           event.preventDefault();
774           tui.switch_mode(mode_annotate);
775         };
776     }
777 }, false);
778
779 rows_selector.addEventListener('input', function() {
780     if (rows_selector.value % 4 != 0) {
781         return;
782     }
783     window.localStorage.setItem(rows_selector.id, rows_selector.value);
784     terminal.initialize();
785     tui.full_refresh();
786 }, false);
787 cols_selector.addEventListener('input', function() {
788     if (cols_selector.value % 4 != 0) {
789         return;
790     }
791     window.localStorage.setItem(cols_selector.id, cols_selector.value);
792     terminal.initialize();
793     tui.window_width = terminal.cols / 2,
794     tui.full_refresh();
795 }, false);
796 for (let key_selector of key_selectors) {
797     key_selector.addEventListener('input', function() {
798         window.localStorage.setItem(key_selector.id, key_selector.value);
799         tui.init_keys();
800     }, false);
801 }
802 window.setInterval(function() {
803     if (!(['input', 'n_cols', 'n_rows'].includes(document.activeElement.id)
804           || document.activeElement.id.startsWith('key_'))) {
805         tui.inputEl.focus();
806     }
807 }, 100);
808 </script>
809 </body></html>