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