home · contact · privacy
Add mode line.
[plomrogue2-experiments] / new2 / rogue_chat_nocanvas_monochrome.html
1 <!DOCTYPE html>
2 <html><head>
3 <style>
4 </style>
5 </head><body>
6 <pre id="terminal" style="display: inline-block; color: white; background-color: black;"></pre>
7 <script>
8 "use strict";
9 let websocket_location = "ws://localhost:8000";
10
11 let terminal = {
12   rows: 24,
13   cols: 80,
14   initialize: function() {
15     this.pre_el = document.getElementById("terminal");
16     this.content = [];
17       let line = []
18     for (let y = 0, x = 0; y <= this.rows; x++) {
19         if (x == this.cols) {
20             x = 0;
21             y += 1;
22             this.content.push(line);
23             line = [];
24             if (y == this.rows) {
25                 break;
26             }
27         }
28         line.push(' ');
29     }
30   },
31   refresh: function() {
32       let pre_string = '';
33       for (let y = 0; y < this.rows; y++) {
34           let line = this.content[y].join('');
35           pre_string += line + '\n';
36       }
37       this.pre_el.textContent = pre_string;
38   },
39   write: function(start_y, start_x, msg) {
40       for (let x = start_x, i = 0; x < this.cols && i < msg.length; x++, i++) {
41           this.content[start_y][x] = msg[i];
42       }
43   },
44   drawBox: function(start_y, start_x, height, width) {
45     let end_y = start_y + height;
46     let end_x = start_x + width;
47     for (let y = start_y, x = start_x; y < this.rows; x++) {
48       if (x == end_x) {
49         x = start_x;
50         y += 1;
51         if (y == end_y) {
52             break;
53         }
54       };
55       this.content[y][x] = ' ';
56     }
57   },
58 }
59
60 let parser = {
61   tokenize: function(str) {
62     let token_ends = [];
63     let tokens = [];
64     let token = ''
65     let quoted = false;
66     let escaped = false;
67     for (let i = 0; i < str.length; i++) {
68       let c = str[i];
69       if (quoted) {
70         if (escaped) {
71           token += c;
72           escaped = false;
73         } else if (c == '\\') {
74           escaped = true;
75         } else if (c == '"') {
76           quoted = false
77         } else {
78           token += c;
79         }
80       } else if (c == '"') {
81         quoted = true
82       } else if (c === ' ') {
83         if (token.length > 0) {
84           token_ends.push(i);
85           tokens.push(token);
86           token = '';
87         }
88       } else {
89         token += c;
90       }
91     }
92     if (token.length > 0) {
93       tokens.push(token);
94     }
95     let token_starts = [];
96     for (let i = 0; i < token_ends.length; i++) {
97       token_starts.push(token_ends[i] - tokens[i].length);
98     };
99     return [tokens, token_starts];
100   },
101   parse_yx: function(position_string) {
102     let coordinate_strings = position_string.split(',')
103     let position = [0, 0];
104     position[0] = parseInt(coordinate_strings[0].slice(2));
105     position[1] = parseInt(coordinate_strings[1].slice(2));
106     return position;
107   },
108 }
109
110 let server = {
111     init: function(url) {
112         this.websocket = new WebSocket(url);
113         this.websocket.onopen = function(event) {
114             window.setInterval(function() { server.send(['PING']) }, 30000);
115             server.send(['GET_GAMESTATE']);
116         };
117     },
118     send: function(tokens) {
119         this.websocket.send(unparser.untokenize(tokens));
120     }
121 }
122
123 let unparser = {
124     quote: function(str) {
125         let quoted = ['"'];
126         for (let i = 0; i < str.length; i++) {
127             let c = str[i];
128             if (c in ['"', '\\']) {
129                 quoted.push('\\');
130             };
131             quoted.push(c);
132         }
133         quoted.push('"');
134         return quoted.join('');
135     },
136     to_yx: function(yx_coordinate) {
137         return "Y:" + yx_coordinate[0] + ",X:" + yx_coordinate[1];
138     },
139     untokenize: function(tokens) {
140         let quoted_tokens = [];
141         for (let token of tokens) {
142             quoted_tokens.push(this.quote(token));
143         }
144         return quoted_tokens.join(" ");
145     }
146 }
147
148 let tui = {
149   mode: 'chat',
150   log: [],
151   input: '',
152   input_lines: [],
153   window_width: terminal.cols / 2,
154   height_turn_line: 1,
155   height_mode_line: 1,
156   height_input: 1,
157   init: function() {
158       this.recalc_input_lines();
159       this.height_header = this.height_turn_line + this.height_mode_line;
160   },
161   switch_mode: function(mode_name, keep_pos=false) {
162     if (mode_name == 'study' && !keep_pos) {
163       explorer.position = game.things[game.player_id];
164     }
165     this.mode = mode_name;
166     this.empty_input();
167     this.full_refresh();
168   },
169   draw_mode_line: function() {
170       terminal.drawBox(1, this.window_width, this.height_mode_line, this.window_width);
171       terminal.write(1, this.window_width, 'MODE ' + this.mode);
172   },
173   draw_history: function() {
174     if (terminal.rows <= this.height_header + this.height_input) {
175         return;
176     }
177     terminal.drawBox(this.height_header, this.window_width, terminal.rows - this.height_header - this.height_input, this.window_width);
178       for (let y = terminal.rows - 1 - this.height_input,
179                i = this.log.length - 1;
180            y >= this.height_header && i >= 0;
181            y--, i--) {
182           terminal.write(y, this.window_width, this.log[i]);
183       }
184   },
185   draw_map: function() {
186     terminal.drawBox(0, 0, terminal.rows, this.window_width);
187     let map_lines = [];
188     let line = [];
189     for (let i = 0, j = 0; i < game.map.length; i++, j++) {
190         if (j == game.map_size[1]) {
191             map_lines.push(line);
192             line = [];
193             j = 0;
194         };
195         line.push(game.map[i]);
196     };
197     map_lines.push(line);
198     let player_position = [0,0];
199     let center_pos = [Math.floor(game.map_size[0] / 2),
200                       Math.floor(game.map_size[1] / 2)];
201     for (const thing_id in game.things) {
202         let t = game.things[thing_id];
203         map_lines[t[0]][t[1]] = '@';
204         if (game.player_id == thing_id) {
205             center_pos = t;
206         }
207     };
208     if (tui.mode == 'study' || tui.mode == 'annotate') {
209         map_lines[explorer.position[0]][explorer.position[1]] = '?';
210         center_pos = explorer.position;
211     }
212     let offset = [(terminal.rows / 2) - center_pos[0],
213                   this.window_width / 2 - center_pos[1]];
214       for (let term_y = offset[0], map_y = 0;
215            term_y < terminal.rows && map_y < game.map_size[0];
216            term_y++, map_y++) {
217         if (term_y >= 0) {
218             let to_draw = map_lines[map_y].join('').slice(0, this.window_width - offset[1]);
219             terminal.write(term_y, offset[1], to_draw);
220         }
221     }
222   },
223   draw_turn_line: function(n) {
224     terminal.drawBox(0, this.window_width, 1, this.window_width);
225     terminal.write(0, this.window_width, 'turn: ' + game.turn);
226   },
227   empty_input: function(str) {
228       this.input = "";
229       if (this.mode == 'annotate' || this.mode == 'chat') {
230           this.recalc_input_lines();
231       } else {
232           this.height_input = 0;
233       }
234   },
235   add_to_input: function(str) {
236       if (this.input.length + str.length > this.window_width * terminal.rows) {
237           return;
238       }
239       this.input += str;
240       this.recalc_input_lines();
241   },
242   recalc_input_lines: function() {
243       this.input_lines = this.msg_into_lines_of_width("> " + this.input, this.window_width);
244       this.height_input = this.input_lines.length;
245   },
246   shorten_input: function() {
247       this.input = tui.input.slice(0, -1);
248       this.recalc_input_lines();
249   },
250   draw_input: function() {
251     terminal.drawBox(terminal.rows - this.height_input, this.window_width, this.height_input, this.window_width);
252     if (this.mode == 'chat' || this.mode == 'annotate') {
253         for (let y = terminal.rows - this.height_input, i = 0; y < terminal.rows && i < this.input_lines.length; y++, i++) {
254             terminal.write(y, this.window_width, this.input_lines[i]);
255         }
256     }
257   },
258   msg_into_lines_of_width: function(msg, width) {
259     let chunk = "";
260     let lines = [];
261     for (let i = 0, x = 0; i < msg.length; i++, x++) {
262       if (x >= width) {
263         lines.push(chunk);
264         chunk = "";
265         x = 0;
266       };
267       chunk += msg[i];
268     }
269     lines.push(chunk);
270     return lines;
271   },
272   log_msg: function(msg) {
273       let lines = this.msg_into_lines_of_width(msg, this.window_width);
274       this.log = this.log.concat(lines);
275       while (this.log.length > terminal.rows) {
276         this.log.shift();
277       };
278       this.draw_history();
279   },
280   refresh: function() {
281     terminal.refresh();
282   },
283   log_help: function() {
284     tui.log_msg("");
285     tui.log_msg("HELP");
286     tui.log_msg("");
287     tui.log_msg("chat mode commands:");
288     tui.log_msg(":login USER - register as USER");
289     tui.log_msg(":msg USER TEXT - send TEXT to USER");
290     tui.log_msg(":help - show this help");
291     tui.log_msg(":play or :p - switch to play mode");
292     tui.log_msg(":study or :s - switch to study mode");
293     tui.log_msg("");
294     tui.log_msg("play mode commands:");
295     tui.log_msg("w, a, s, d - move avatar");
296     tui.log_msg("f - flatten surroundings");
297     tui.log_msg("e - write following ASCII character");
298     tui.log_msg("c - switch to chat mode");
299     tui.log_msg("? - switch to study mode");
300     tui.log_msg("");
301     tui.log_msg("study mode commands:");
302     tui.log_msg("w, a, s, d - move question mark");
303     tui.log_msg("A - annotate terrain");
304     tui.log_msg("c - switch to chat mode");
305     tui.log_msg("p - switch to play mode");
306     tui.log_msg("");
307   },
308   draw_info: function() {
309     terminal.drawBox(this.height_header, this.window_width, terminal.rows - this.height_header - this.height_input, this.window_width);
310     let lines = this.msg_into_lines_of_width(explorer.get_info(), this.window_width);
311     for (let y = this.height_header, i = 0; y < terminal.rows && i < lines.length; y++, i++) {
312       terminal.write(y, this.window_width, lines[i]);
313     }
314   },
315   full_refresh: function() {
316     this.draw_map();
317     this.draw_turn_line();
318     this.draw_mode_line();
319     if (this.mode == 'study' || this.mode == 'annotate') {
320       this.draw_info();
321     } else {
322       this.draw_history();
323     }
324     this.draw_input();
325     this.refresh();
326   }
327 }
328
329 let game = {
330   things: {},
331   turn: 0,
332   map: "",
333   map_size: [0,0],
334   player_id: 0
335 }
336
337 terminal.initialize();
338 tui.init();
339 tui.log_help();
340 tui.full_refresh();
341
342 server.init(websocket_location);
343 server.websocket.onmessage = function (event) {
344   let tokens = parser.tokenize(event.data)[0];
345   if (tokens[0] === 'TURN') {
346     game.things = {}
347     game.turn = parseInt(tokens[1]);
348   } else if (tokens[0] === 'THING_POS') {
349     game.things[tokens[1]] = parser.parse_yx(tokens[2]);
350   } else if (tokens[0] === 'MAP') {
351     game.map_size = parser.parse_yx(tokens[1]);
352     game.map = tokens[2]
353   } else if (tokens[0] === 'GAME_STATE_COMPLETE') {
354     explorer.empty_info_db();
355     if (tui.mode == 'study') {
356       explorer.query_info();
357     }
358     tui.draw_turn_line();
359     tui.draw_map();
360     tui.refresh();
361   } else if (tokens[0] === 'CHAT') {
362      tui.log_msg('# ' + tokens[1], 1);
363      tui.refresh();
364   } else if (tokens[0] === 'PLAYER_ID') {
365       game.player_id = parseInt(tokens[1]);
366   } else if (tokens[0] === 'META') {
367      tui.log_msg('@ ' + tokens[1]);
368      tui.refresh();
369   } else if (tokens[0] === 'ANNOTATION') {
370      let position = parser.parse_yx(tokens[1]);
371      explorer.update_info_db(position, tokens[2]);
372   } else if (tokens[0] === 'UNHANDLED_INPUT') {
373      tui.log_msg('? unknown command');
374      tui.refresh();
375   } else if (tokens[0] === 'ARGUMENT_ERROR') {
376      tui.log_msg('? syntax error: ' + tokens[1]);
377      tui.refresh();
378   } else if (tokens[0] === 'GAME_ERROR') {
379      tui.log_msg('? game error: ' + tokens[1]);
380      tui.refresh();
381   } else if (tokens[0] === 'PONG') {
382     console.log('PONG');
383   } else {
384      tui.log_msg('? unhandled input: ' + event.data);
385      tui.refresh();
386   }
387 }
388
389 let explorer = {
390     position: [0,0],
391     info_db: {},
392     move: function(direction) {
393         let try_pos = [0,0];
394         try_pos[0] = this.position[0];
395         try_pos[1] = this.position[1];
396         if (direction == 'left') {
397             try_pos[1] -= 1;
398         } else if (direction == 'right') {
399             try_pos[1] += 1;
400         } else if (direction == 'up') {
401             try_pos[0] -= 1;
402         } else if (direction == 'down') {
403             try_pos[0] += 1;
404         };
405         if (!(try_pos[0] < 0) &&
406             !(try_pos[1] < 0) &&
407             !(try_pos[0] >= game.map_size[0])
408             && !(try_pos[1] >= game.map_size[1])) {
409             this.position = try_pos;
410             this.query_info();
411             tui.draw_map();
412             tui.draw_info();
413             tui.refresh();
414         }
415     },
416     update_info_db: function(yx, str) {
417         this.info_db[yx] = str;
418         if (tui.mode == 'study') {
419             tui.draw_info();
420             tui.refresh();
421         }
422     },
423     empty_info_db: function() {
424         this.info_db = {};
425         if (tui.mode == 'study') {
426             tui.draw_info();
427             tui.refresh();
428         }
429     },
430     query_info: function() {
431         server.send(["GET_ANNOTATION", unparser.to_yx(explorer.position)]);
432     },
433     get_info: function() {
434         if (this.position in this.info_db) {
435             return this.info_db[this.position];
436         } else {
437             return 'waiting …';
438         }
439     },
440     annotate: function(msg) {
441         if (msg.length == 0) {
442             msg = " ";  // triggers annotation deletion
443         }
444         server.send(["ANNOTATE", unparser.to_yx(explorer.position), msg]);
445     }
446 }
447
448 document.addEventListener('keydown', (event) => {
449     if (tui.mode == 'chat') {
450         if (event.key.length === 1) {
451             tui.add_to_input(event.key);
452             tui.full_refresh();
453         } else if (event.key == 'Backspace') {
454             tui.shorten_input();
455             tui.full_refresh();
456         } else if (event.key == 'Enter') {
457             let [tokens, token_starts] = parser.tokenize(tui.input);
458             if (tokens.length > 0 && tokens[0].length > 0) {
459                 if (tokens[0][0] == ':') {
460                     if (tokens[0] == ':play' || tokens[0] == ':p') {
461                         tui.switch_mode('play');
462                     } else if (tokens[0] == ':study' || tokens[0] == ':s') {
463                         tui.switch_mode('study');
464                     } else if (tokens[0] == ':help') {
465                         tui.log_help();
466                         tui.refresh();
467                     } else if (tokens[0] == ':login') {
468                         if (tokens.length > 1) {
469                             server.send(['LOGIN', tokens[1]]);
470                         } else {
471                             tui.log_msg('? need login name');
472                         }
473                     } else if (tokens[0] == ':msg') {
474                         if (tokens.length > 2) {
475                             let msg = tui.input.slice(token_starts[2]);
476                             server.send(['QUERY', tokens[1], msg]);
477                         } else {
478                             tui.log_msg('? need message target and message');
479                         }
480                     } else {
481                         tui.log_msg('? unknown command');
482                     }
483                 } else {
484                     server.send(['ALL', tui.input]);
485                 }
486             }
487             tui.empty_input();
488             tui.full_refresh();
489         }
490       } else if (tui.mode == 'play') {
491           if (event.key === 'c') {
492               tui.switch_mode('chat');
493           } else if (event.key === 'e') {
494               tui.switch_mode('edit');
495           } else if (event.key === '?') {
496               tui.switch_mode('study');
497           } else if (event.key === 'F1') {
498               tui.log_help();
499               tui.refresh();
500           } else if (event.key === 'f') {
501               server.send(["TASK:FLATTEN_SURROUNDINGS"]);
502           } else if (event.key === 'a') {
503               server.send(['TASK:MOVE', 'LEFT']);
504           } else if (event.key === 'd') {
505               server.send(['TASK:MOVE', 'RIGHT']);
506           } else if (event.key === 'w') {
507               server.send(['TASK:MOVE', 'UP']);
508           } else if (event.key === 's') {
509               server.send(['TASK:MOVE', 'DOWN']);
510           };
511     } else if (tui.mode == 'edit') {
512         if (event.key != "Shift" && event.key.length == 1) {
513             server.send(["TASK:WRITE", event.key]);
514             tui.switch_mode('play');
515         }
516     } else if (tui.mode == 'study') {
517         if (event.key === 'c') {
518             tui.switch_mode('chat');
519         } else if (event.key == 'p') {
520             tui.switch_mode('play');
521         } else if (event.key === 'a') {
522               explorer.move('left');
523         } else if (event.key === 'd') {
524               explorer.move('right');
525         } else if (event.key === 'w') {
526               explorer.move('up');
527         } else if (event.key === 's') {
528               explorer.move('down');
529         } else if (event.key === 'A') {
530           tui.switch_mode('annotate');
531           tui.draw_info();
532           tui.refresh();
533         };
534     } else if (tui.mode == 'annotate') {
535         if (event.key.length === 1) {
536             tui.add_to_input(event.key);
537             tui.full_refresh();
538         } else if (event.key == 'Backspace') {
539             tui.shorten_input();
540             tui.full_refresh();
541         } else if (event.key == 'Enter') {
542             explorer.annotate(tui.input);
543             tui.switch_mode('study', true);
544         }
545     }
546 }, false);
547 </script>
548 </body></html>