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