home · contact · privacy
Add TASK command to request available commands.
[plomrogue2] / 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 = "wss://plomlompom.com/rogue_chat/";
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             this.send('TASKS');
177             tui.log_msg("@ server connected! :)");
178             tui.switch_mode(mode_login);
179         };
180         this.websocket.onclose = function(event) {
181             tui.log_msg("@ server disconnected :(");
182             tui.log_msg("@ hint: try the '/reconnect' command");
183         };
184             this.websocket.onmessage = this.handle_event;
185         },
186     reconnect: function() {
187            this.reconnect_to(this.url);
188     },
189     reconnect_to: function(url) {
190         this.websocket.close();
191         this.init(url);
192     },
193     send: function(tokens) {
194         this.websocket.send(unparser.untokenize(tokens));
195     },
196     handle_event: function(event) {
197         let tokens = parser.tokenize(event.data)[0];
198         if (tokens[0] === 'TURN') {
199             game.turn_complete = false;
200             game.things = {};
201             game.portals = {};
202             game.turn = parseInt(tokens[1]);
203         } else if (tokens[0] === 'THING_POS') {
204             game.get_thing(tokens[1], true).position = parser.parse_yx(tokens[2]);
205         } else if (tokens[0] === 'THING_NAME') {
206             game.get_thing(tokens[1], true).name_ = tokens[2];
207         } else if (tokens[0] === 'TASKS') {
208             game.tasks = tokens[1].split(',')
209         } else if (tokens[0] === 'MAP') {
210             game.map_geometry = tokens[1];
211             tui.init_keys();
212             game.map_size = parser.parse_yx(tokens[2]);
213             game.map = tokens[3]
214         } else if (tokens[0] === 'GAME_STATE_COMPLETE') {
215             game.turn_complete = true;
216             explorer.empty_info_db();
217             if (tui.mode == mode_post_login_wait) {
218                 tui.switch_mode(mode_play);
219                 tui.log_help();
220             } else if (tui.mode == mode_study) {
221                 explorer.query_info();
222             }
223             let t = game.get_thing(game.player_id);
224             if (t.position in game.portals) {
225                 tui.teleport_target = game.portals[t.position];
226                 tui.switch_mode(mode_teleport);
227                 return;
228             }
229             tui.full_refresh();
230         } else if (tokens[0] === 'CHAT') {
231              tui.log_msg('# ' + tokens[1], 1);
232         } else if (tokens[0] === 'PLAYER_ID') {
233             game.player_id = parseInt(tokens[1]);
234         } else if (tokens[0] === 'LOGIN_OK') {
235             this.send(['GET_GAMESTATE']);
236             tui.switch_mode(mode_post_login_wait);
237         } else if (tokens[0] === 'PORTAL') {
238             let position = parser.parse_yx(tokens[1]);
239             game.portals[position] = tokens[2];
240         } else if (tokens[0] === 'ANNOTATION') {
241             let position = parser.parse_yx(tokens[1]);
242             explorer.update_info_db(position, tokens[2]);
243         } else if (tokens[0] === 'UNHANDLED_INPUT') {
244             tui.log_msg('? unknown command');
245         } else if (tokens[0] === 'PLAY_ERROR') {
246             terminal.blink_screen();
247         } else if (tokens[0] === 'ARGUMENT_ERROR') {
248             tui.log_msg('? syntax error: ' + tokens[1]);
249         } else if (tokens[0] === 'GAME_ERROR') {
250             tui.log_msg('? game error: ' + tokens[1]);
251         } else if (tokens[0] === 'PONG') {
252             console.log('PONG');
253         } else {
254             tui.log_msg('? unhandled input: ' + event.data);
255         }
256     }
257 }
258
259 let unparser = {
260     quote: function(str) {
261         let quoted = ['"'];
262         for (let i = 0; i < str.length; i++) {
263             let c = str[i];
264             if (['"', '\\'].includes(c)) {
265                 quoted.push('\\');
266             };
267             quoted.push(c);
268         }
269         quoted.push('"');
270         return quoted.join('');
271     },
272     to_yx: function(yx_coordinate) {
273         return "Y:" + yx_coordinate[0] + ",X:" + yx_coordinate[1];
274     },
275     untokenize: function(tokens) {
276         let quoted_tokens = [];
277         for (let token of tokens) {
278             quoted_tokens.push(this.quote(token));
279         }
280         return quoted_tokens.join(" ");
281     }
282 }
283
284 class Mode {
285     constructor(name, has_input_prompt=false, shows_info=false, is_intro=false) {
286         this.name = name;
287         this.has_input_prompt = has_input_prompt;
288         this.shows_info= shows_info;
289         this.is_intro = is_intro;
290     }
291 }
292 let mode_waiting_for_server = new Mode('waiting_for_server', false, false, true);
293 let mode_login = new Mode('login', true, false, true);
294 let mode_post_login_wait = new Mode('waiting for game world', false, false, true);
295 let mode_chat = new Mode('chat / write messages to players', true, false);
296 let mode_annotate = new Mode('add message to map tile', true, true);
297 let mode_play = new Mode('play / move around', false, false);
298 let mode_study = new Mode('check map tiles for messages', false, true);
299 let mode_edit = new Mode('write ASCII char to map tile', false, false);
300 let mode_teleport = new Mode('teleport away?', true);
301 let mode_portal = new Mode('add portal to map tile', true, true);
302
303 let tui = {
304   mode: mode_waiting_for_server,
305   log: [],
306   input_prompt: '> ',
307   input_lines: [],
308   window_width: terminal.cols / 2,
309   height_turn_line: 1,
310   height_mode_line: 1,
311   height_input: 1,
312   init: function() {
313       this.inputEl = document.getElementById("input");
314       this.inputEl.focus();
315       this.recalc_input_lines();
316       this.height_header = this.height_turn_line + this.height_mode_line;
317       this.log_msg("@ waiting for server connection ...");
318       this.init_keys();
319   },
320   init_keys: function() {
321     this.keys = {};
322     for (let key_selector of key_selectors) {
323         this.keys[key_selector.id.slice(4)] = key_selector.value;
324     }
325     if (game.map_geometry == 'Square') {
326         this.movement_keys = {
327             [this.keys.square_move_up]: 'UP',
328             [this.keys.square_move_left]: 'LEFT',
329             [this.keys.square_move_down]: 'DOWN',
330             [this.keys.square_move_right]: 'RIGHT'
331         };
332     } else if (game.map_geometry == 'Hex') {
333         this.movement_keys = {
334             [this.keys.hex_move_upleft]: 'UPLEFT',
335             [this.keys.hex_move_upright]: 'UPRIGHT',
336             [this.keys.hex_move_right]: 'RIGHT',
337             [this.keys.hex_move_downright]: 'DOWNRIGHT',
338             [this.keys.hex_move_downleft]: 'DOWNLEFT',
339             [this.keys.hex_move_left]: 'LEFT'
340         };
341     };
342   },
343   switch_mode: function(mode, keep_pos=false) {
344     if (mode == mode_study && !keep_pos && game.player_id in game.things) {
345       explorer.position = game.things[game.player_id].position;
346     }
347     this.mode = mode;
348     this.empty_input();
349     if (mode == mode_annotate && explorer.position in explorer.info_db) {
350         let info = explorer.info_db[explorer.position];
351         if (info != "(none)") {
352             this.inputEl.value = info;
353             this.recalc_input_lines();
354         }
355     }
356     if (mode == mode_login) {
357         if (this.login_name) {
358             server.send(['LOGIN', this.login_name]);
359         } else {
360             this.log_msg("? need login name");
361         }
362     } else if (mode == mode_portal && explorer.position in game.portals) {
363         let portal = game.portals[explorer.position]
364         this.inputEl.value = portal;
365         this.recalc_input_lines();
366     } else if (mode == mode_teleport) {
367         tui.log_msg("@ May teleport to: " + tui.teleport_target);
368         tui.log_msg("@ Enter 'YES!' to entusiastically affirm.");
369     }
370     this.full_refresh();
371   },
372   empty_input: function(str) {
373       this.inputEl.value = "";
374       if (this.mode.has_input_prompt) {
375           this.recalc_input_lines();
376       } else {
377           this.height_input = 0;
378       }
379   },
380   recalc_input_lines: function() {
381       this.input_lines = this.msg_into_lines_of_width(this.input_prompt + this.inputEl.value, this.window_width);
382       this.height_input = this.input_lines.length;
383   },
384   msg_into_lines_of_width: function(msg, width) {
385     let chunk = "";
386     let lines = [];
387     for (let i = 0, x = 0; i < msg.length; i++, x++) {
388       if (x >= width || msg[i] == "\n") {
389         lines.push(chunk);
390         chunk = "";
391         x = 0;
392       };
393       if (msg[i] != "\n") {
394         chunk += msg[i];
395       }
396     }
397     lines.push(chunk);
398     return lines;
399   },
400   log_msg: function(msg) {
401       this.log.push(msg);
402       while (this.log.length > 100) {
403         this.log.shift();
404       };
405       this.full_refresh();
406   },
407   log_help: function() {
408     let movement_keys_desc = Object.keys(this.movement_keys).join(',');
409     this.log_msg("HELP:");
410     this.log_msg("chat mode commands:");
411     this.log_msg("  /nick NAME - re-name yourself to NAME");
412     this.log_msg("  /msg USER TEXT - send TEXT to USER");
413     this.log_msg("  /help - show this help");
414     this.log_msg("  /" + this.keys.switch_to_play + " or /play - switch to play mode");
415     this.log_msg("  /" + this.keys.switch_to_study + " or /study - switch to study mode");
416     this.log_msg("commands common to study and play mode:");
417     if (game.tasks.includes('MOVE')) {
418         this.log_msg("  " + movement_keys_desc + " - move");
419     }
420     this.log_msg("  " + this.keys.switch_to_chat + " - switch to chat mode");
421     this.log_msg("commands specific to play mode:");
422     if (game.tasks.includes('WRITE')) {
423         this.log_msg("  " + this.keys.switch_to_edit + " - write following ASCII character");
424     }
425     if (game.tasks.includes('FLATTEN_SURROUNDINGS')) {
426         this.log_msg("  " + this.keys.flatten + " - flatten surroundings");
427     }
428     this.log_msg("  " + this.keys.switch_to_study + " - switch to study mode");
429     this.log_msg("commands specific to study mode:");
430     if (!game.tasks.includes('MOVE')) {
431         this.log_msg("  " + movement_keys_desc + " - move");
432     }
433     this.log_msg("  " + this.keys.switch_to_annotate + " - annotate terrain");
434     this.log_msg("  " + this.keys.switch_to_play + " - switch to play mode");
435   },
436   draw_map: function() {
437     let map_lines_split = [];
438     let line = [];
439     for (let i = 0, j = 0; i < game.map.length; i++, j++) {
440         if (j == game.map_size[1]) {
441             map_lines_split.push(line);
442             line = [];
443             j = 0;
444         };
445         line.push(game.map[i]);
446     };
447     map_lines_split.push(line);
448     for (const thing_id in game.things) {
449         let t = game.things[thing_id];
450         map_lines_split[t.position[0]][t.position[1]] = '@';
451     };
452     if (tui.mode.shows_info) {
453         map_lines_split[explorer.position[0]][explorer.position[1]] = '?';
454     }
455     let map_lines = []
456     if (game.map_geometry == 'Square') {
457         for (let line_split of map_lines_split) {
458             map_lines.push(line_split.join(' '));
459         };
460     } else if (game.map_geometry == 'Hex') {
461         let indent = 0
462         for (let line_split of map_lines_split) {
463             map_lines.push(' '.repeat(indent) + line_split.join(' '));
464             if (indent == 0) {
465                 indent = 1;
466             } else {
467                 indent = 0;
468             };
469         };
470     }
471     let window_center = [terminal.rows / 2, this.window_width / 2];
472     let player = game.things[game.player_id];
473     let center_position = [player.position[0], player.position[1]];
474     if (tui.mode.shows_info) {
475         center_position = [explorer.position[0], explorer.position[1]];
476     }
477     center_position[1] = center_position[1] * 2;
478     let offset = [center_position[0] - window_center[0],
479                   center_position[1] - window_center[1]]
480     if (game.map_geometry == 'Hex' && offset[0] % 2) {
481         offset[1] += 1;
482     };
483     let term_y = Math.max(0, -offset[0]);
484     let term_x = Math.max(0, -offset[1]);
485     let map_y = Math.max(0, offset[0]);
486     let map_x = Math.max(0, offset[1]);
487     for (; term_y < terminal.rows && map_y < game.map_size[0]; term_y++, map_y++) {
488         let to_draw = map_lines[map_y].slice(map_x, this.window_width + offset[1]);
489         terminal.write(term_y, term_x, to_draw);
490     }
491   },
492   draw_mode_line: function() {
493       terminal.write(0, this.window_width, 'MODE: ' + this.mode.name);
494   },
495   draw_turn_line: function(n) {
496     terminal.write(1, this.window_width, 'TURN: ' + game.turn);
497   },
498   draw_history: function() {
499       let log_display_lines = [];
500       for (let line of this.log) {
501           log_display_lines = log_display_lines.concat(this.msg_into_lines_of_width(line, this.window_width));
502       };
503       for (let y = terminal.rows - 1 - this.height_input,
504                i = log_display_lines.length - 1;
505            y >= this.height_header && i >= 0;
506            y--, i--) {
507           terminal.write(y, this.window_width, log_display_lines[i]);
508       }
509   },
510   draw_info: function() {
511     let lines = this.msg_into_lines_of_width(explorer.get_info(), this.window_width);
512     for (let y = this.height_header, i = 0; y < terminal.rows && i < lines.length; y++, i++) {
513       terminal.write(y, this.window_width, lines[i]);
514     }
515   },
516   draw_input: function() {
517     if (this.mode.has_input_prompt) {
518         for (let y = terminal.rows - this.height_input, i = 0; i < this.input_lines.length; y++, i++) {
519             terminal.write(y, this.window_width, this.input_lines[i]);
520         }
521     }
522   },
523   full_refresh: function() {
524     terminal.drawBox(0, 0, terminal.rows, terminal.cols);
525     if (this.mode.is_intro) {
526         this.draw_history();
527         this.draw_input();
528     } else {
529         if (game.turn_complete) {
530             this.draw_map();
531             this.draw_turn_line();
532         }
533         this.draw_mode_line();
534         if (this.mode.shows_info) {
535           this.draw_info();
536         } else {
537           this.draw_history();
538         }
539         this.draw_input();
540     }
541     terminal.refresh();
542   }
543 }
544
545 let game = {
546     init: function() {
547         this.things = {};
548         this.turn = -1;
549         this.map = "";
550         this.map_size = [0,0];
551         this.player_id = -1;
552         this.portals = {};
553         this.tasks = {};
554     },
555     get_thing: function(id_, create_if_not_found=false) {
556         if (id_ in game.things) {
557             return game.things[id_];
558         } else if (create_if_not_found) {
559             let t = new Thing([0,0]);
560             game.things[id_] = t;
561             return t;
562         };
563     },
564     move: function(start_position, direction) {
565         let target = [start_position[0], start_position[1]];
566         if (direction == 'LEFT') {
567             target[1] -= 1;
568         } else if (direction == 'RIGHT') {
569             target[1] += 1;
570         } else if (game.map_geometry == 'Square') {
571             if (direction == 'UP') {
572                 target[0] -= 1;
573             } else if (direction == 'DOWN') {
574                 target[0] += 1;
575             };
576         } else if (game.map_geometry == 'Hex') {
577             let start_indented = start_position[0] % 2;
578             if (direction == 'UPLEFT') {
579                 target[0] -= 1;
580                 if (!start_indented) {
581                     target[1] -= 1;
582                 }
583             } else if (direction == 'UPRIGHT') {
584                 target[0] -= 1;
585                 if (start_indented) {
586                     target[1] += 1;
587                 }
588             } else if (direction == 'DOWNLEFT') {
589                 target[0] += 1;
590                 if (!start_indented) {
591                     target[1] -= 1;
592                 }
593             } else if (direction == 'DOWNRIGHT') {
594                 target[0] += 1;
595                 if (start_indented) {
596                     target[1] += 1;
597                 }
598             };
599         };
600         if (target[0] < 0 || target[1] < 0 ||
601             target[0] >= this.map_size[0] || target[1] >= this.map_size[1]) {
602             return null;
603         };
604         return target;
605     }
606 }
607
608 game.init();
609 tui.init();
610 tui.full_refresh();
611 server.init(websocket_location);
612
613 let explorer = {
614     position: [0,0],
615     info_db: {},
616     move: function(direction) {
617         let target = game.move(this.position, direction);
618         if (target) {
619             this.position = target
620             this.query_info();
621             tui.full_refresh();
622         } else {
623             terminal.blink_screen();
624         };
625     },
626     update_info_db: function(yx, str) {
627         this.info_db[yx] = str;
628         if (tui.mode == mode_study) {
629             tui.full_refresh();
630         }
631     },
632     empty_info_db: function() {
633         this.info_db = {};
634         if (tui.mode == mode_study) {
635             tui.full_refresh();
636         }
637     },
638     query_info: function() {
639         server.send(["GET_ANNOTATION", unparser.to_yx(explorer.position)]);
640     },
641     get_info: function() {
642         let info = "";
643         let position_i = this.position[0] * game.map_size[1] + this.position[1];
644         info += "TERRAIN: " + game.map[position_i] + "\n";
645         for (let t_id in game.things) {
646              let t = game.things[t_id];
647              if (t.position[0] == this.position[0] && t.position[1] == this.position[1]) {
648                  info += "PLAYER @";
649                  if (t.name_) {
650                      info += ": " + t.name_;
651                  }
652                  info += "\n";
653              }
654         }
655         if (this.position in game.portals) {
656             info += "PORTAL: " + game.portals[this.position] + "\n";
657         }
658         if (this.position in this.info_db) {
659             info += "ANNOTATIONS: " + this.info_db[this.position];
660         } else {
661             info += 'waiting …';
662         }
663         return info;
664     },
665     annotate: function(msg) {
666         if (msg.length == 0) {
667             msg = " ";  // triggers annotation deletion
668         }
669         server.send(["ANNOTATE", unparser.to_yx(explorer.position), msg]);
670     },
671     set_portal: function(msg) {
672         if (msg.length == 0) {
673             msg = " ";  // triggers portal deletion
674         }
675         server.send(["PORTAL", unparser.to_yx(explorer.position), msg]);
676     }
677 }
678
679 tui.inputEl.addEventListener('input', (event) => {
680     if (tui.mode.has_input_prompt) {
681         let max_length = tui.window_width * terminal.rows - tui.input_prompt.length;
682         if (tui.inputEl.value.length > max_length) {
683             tui.inputEl.value = tui.inputEl.value.slice(0, max_length);
684         };
685         tui.recalc_input_lines();
686         tui.full_refresh();
687     } else if (tui.mode == mode_edit && tui.inputEl.value.length > 0) {
688         server.send(["TASK:WRITE", tui.inputEl.value[0]]);
689         tui.switch_mode(mode_play);
690     } else if (tui.mode == mode_teleport) {
691         if (['Y', 'y'].includes(tui.inputEl.value[0])) {
692             server.reconnect_to(tui.teleport_target);
693         } else {
694             tui.log_msg("@ teleportation aborted");
695             tui.switch_mode(mode_play);
696         }
697     }
698 }, false);
699 tui.inputEl.addEventListener('keydown', (event) => {
700     if (event.key == 'Enter') {
701         event.preventDefault();
702     }
703     if (tui.mode == mode_login && event.key == 'Enter') {
704         tui.login_name = tui.inputEl.value;
705         server.send(['LOGIN', tui.inputEl.value]);
706         tui.empty_input();
707     } else if (tui.mode == mode_portal && event.key == 'Enter') {
708         explorer.set_portal(tui.inputEl.value);
709         tui.switch_mode(mode_study, true);
710     } else if (tui.mode == mode_annotate && event.key == 'Enter') {
711         explorer.annotate(tui.inputEl.value);
712         tui.switch_mode(mode_study, true);
713     } else if (tui.mode == mode_teleport && event.key == 'Enter') {
714         if (tui.inputEl.value == 'YES!') {
715             server.reconnect_to(tui.teleport_target);
716         } else {
717             tui.log_msg('@ teleport aborted');
718             tui.switch_mode(mode_play);
719         };
720     } else if (tui.mode == mode_chat && event.key == 'Enter') {
721         let [tokens, token_starts] = parser.tokenize(tui.inputEl.value);
722         if (tokens.length > 0 && tokens[0].length > 0) {
723             if (tui.inputEl.value[0][0] == '/') {
724                 if (tokens[0].slice(1) == 'play' || tokens[0][1] == tui.keys.switch_to_play) {
725                     tui.switch_mode(mode_play);
726                 } else if (tokens[0].slice(1) == 'study' || tokens[0][1] == tui.keys.switch_to_study) {
727                     tui.switch_mode(mode_study);
728                 } else if (tokens[0].slice(1) == 'help') {
729                     tui.log_help();
730                 } else if (tokens[0].slice(1) == 'nick') {
731                     if (tokens.length > 1) {
732                         server.send(['NICK', tokens[1]]);
733                     } else {
734                         tui.log_msg('? need new name');
735                     }
736                 } else if (tokens[0].slice(1) == 'msg') {
737                     if (tokens.length > 2) {
738                         let msg = tui.inputEl.value.slice(token_starts[2]);
739                         server.send(['QUERY', tokens[1], msg]);
740                     } else {
741                         tui.log_msg('? need message target and message');
742                     }
743                 } else if (tokens[0].slice(1) == 'reconnect') {
744                     if (tokens.length > 1) {
745                         server.reconnect_to(tokens[1]);
746                     } else {
747                         server.reconnect();
748                     }
749                 } else {
750                     tui.log_msg('? unknown command');
751                 }
752             } else {
753                 server.send(['ALL', tui.inputEl.value]);
754             }
755         } else if (tui.inputEl.valuelength > 0) {
756             server.send(['ALL', tui.inputEl.value]);
757         }
758         tui.empty_input();
759         tui.full_refresh();
760       } else if (tui.mode == mode_play) {
761           if (event.key === tui.keys.switch_to_chat) {
762               event.preventDefault();
763               tui.switch_mode(mode_chat);
764           } else if (event.key === tui.keys.switch_to_edit
765                      && game.tasks.includes('WRITE')) {
766               event.preventDefault();
767               tui.switch_mode(mode_edit);
768           } else if (event.key === tui.keys.switch_to_study) {
769               tui.switch_mode(mode_study);
770           } else if (event.key === tui.keys.flatten
771                      && game.tasks.includes('FLATTEN_SURROUNDINGS')) {
772               server.send(["TASK:FLATTEN_SURROUNDINGS"]);
773           } else if (event.key in tui.movement_keys
774                      && game.tasks.includes('MOVE')) {
775               server.send(['TASK:MOVE', tui.movement_keys[event.key]]);
776           };
777     } else if (tui.mode == mode_study) {
778         if (event.key === tui.keys.switch_to_chat) {
779             event.preventDefault();
780             tui.switch_mode(mode_chat);
781         } else if (event.key == tui.keys.switch_to_play) {
782             tui.switch_mode(mode_play);
783         } else if (event.key === tui.keys.switch_to_portal) {
784             event.preventDefault();
785             tui.switch_mode(mode_portal);
786         } else if (event.key in tui.movement_keys) {
787             explorer.move(tui.movement_keys[event.key]);
788         } else if (event.key === tui.keys.switch_to_annotate) {
789           event.preventDefault();
790           tui.switch_mode(mode_annotate);
791         };
792     }
793 }, false);
794
795 rows_selector.addEventListener('input', function() {
796     if (rows_selector.value % 4 != 0) {
797         return;
798     }
799     window.localStorage.setItem(rows_selector.id, rows_selector.value);
800     terminal.initialize();
801     tui.full_refresh();
802 }, false);
803 cols_selector.addEventListener('input', function() {
804     if (cols_selector.value % 4 != 0) {
805         return;
806     }
807     window.localStorage.setItem(cols_selector.id, cols_selector.value);
808     terminal.initialize();
809     tui.window_width = terminal.cols / 2,
810     tui.full_refresh();
811 }, false);
812 for (let key_selector of key_selectors) {
813     key_selector.addEventListener('input', function() {
814         window.localStorage.setItem(key_selector.id, key_selector.value);
815         tui.init_keys();
816     }, false);
817 }
818 window.setInterval(function() {
819     if (!(['input', 'n_cols', 'n_rows'].includes(document.activeElement.id)
820           || document.activeElement.id.startsWith('key_'))) {
821         tui.inputEl.focus();
822     }
823 }, 100);
824 </script>
825 </body></html>