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