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