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