home · contact · privacy
72ac10c5a5c7fbbfc268acaec4e232609eba3de2
[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;; 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(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 function quote(str) {
111     let quoted = ['"'];
112     for (let i = 0; i < str.length; i++) {
113         let c = str[i];
114         if (c in ['"', '\\']) {
115             quoted.push('\\');
116         };
117         quoted.push(c);
118     }
119     quoted.push('"');
120     return quoted.join('');
121 }
122
123 let tui = {
124   mode: 'chat',
125   log: [],
126   input_line: '',
127   switch_mode: function(mode_name, keep_pos=false) {
128     if (mode_name == 'study' && !keep_pos) {
129       explorer.position = game.things[game.player_id];
130     }
131     this.mode = mode_name;
132     this.full_refresh();
133   },
134   draw_history: function() {
135     terminal.drawBox(1, terminal.cols / 2, terminal.rows - 2, terminal.cols / 2);
136     let i = 0;
137     for (let line of this.log) {
138       terminal.write(terminal.rows - 2 - i, terminal.cols / 2, line);
139       i += 1;
140     }
141   },
142   draw_map: function() {
143     terminal.drawBox(0, 0, terminal.rows, terminal.cols / 2);
144     let map_lines = [];
145     let line = [];
146     for (let i = 0, j = 0; i < game.map.length; i++, j++) {
147         if (j == game.map_size[1]) {
148             map_lines.push(line);
149             line = [];
150             j = 0;
151         };
152         line.push(game.map[i]);
153     };
154     map_lines.push(line);
155     let player_position = [0,0];
156     let center_pos = [Math.floor(game.map_size[0] / 2),
157                       Math.floor(game.map_size[1] / 2)];
158     for (const thing_id in game.things) {
159         let t = game.things[thing_id];
160         map_lines[t[0]][t[1]] = '@';
161         if (game.player_id == thing_id) {
162             center_pos = t;
163         }
164     };
165     if (tui.mode == 'study' || tui.mode == 'annotate') {
166         map_lines[explorer.position[0]][explorer.position[1]] = '?';
167         center_pos = explorer.position;
168     }
169     let offset = [(terminal.rows / 2) - center_pos[0],
170                   terminal.cols / 4 - center_pos[1]];
171       for (let term_y = offset[0], map_y = 0;
172            term_y < terminal.rows && map_y < game.map_size[0];
173            term_y++, map_y++) {
174         if (term_y >= 0) {
175             let to_draw = map_lines[map_y].join('').slice(0, terminal.cols / 2 - offset[1]);
176             terminal.write(term_y, offset[1], to_draw);
177         }
178     }
179   },
180   draw_turn_line: function(n) {
181     terminal.drawBox(0, terminal.cols / 2, 1, terminal.cols / 2);
182     terminal.write(0, terminal.cols / 2, 'turn: ' + game.turn);
183   },
184   draw_input_line: function() {
185     terminal.drawBox(terminal.rows - 1, terminal.cols / 2, 1, terminal.cols / 2);
186     if (this.mode == 'chat' || this.mode == 'annotate') {
187       terminal.write(terminal.rows - 1, terminal.cols / 2, '> ' + this.input_line);
188     }
189   },
190   msg_into_lines_of_width: function(msg, width) {
191     let chunk = "";
192     let lines = [];
193     for (let i = 0, x = 0; i < msg.length; i++, x++) {
194       if (x >= width) {
195         lines.unshift(chunk);
196         chunk = "";
197         x = 0;
198       };
199       chunk += msg[i];
200     }
201     lines.unshift(chunk);
202     return lines;
203   },
204   log_msg: function(msg) {
205     let line_length = (terminal.cols / 2);
206     let chunk = "";
207     this.log = this.msg_into_lines_of_width(msg, terminal.cols / 2).concat(this.log);
208     while (this.log.length > terminal.rows - 2) {
209       this.log.pop();
210     };
211     this.draw_history();
212   },
213   refresh: function() {
214     terminal.refresh();
215   },
216   log_help: function() {
217     tui.log_msg("");
218     tui.log_msg("HELP");
219     tui.log_msg("");
220     tui.log_msg("chat mode commands:");
221     tui.log_msg(":login USER - register as USER");
222     tui.log_msg(":msg USER TEXT - send TEXT to USER");
223     tui.log_msg(":help - show this help");
224     tui.log_msg(":play or :p - switch to play mode");
225     tui.log_msg(":study or :s - switch to study mode");
226     tui.log_msg("");
227     tui.log_msg("play mode commands:");
228     tui.log_msg("w, a, s, d - move avatar");
229     tui.log_msg("f - flatten surroundings");
230     tui.log_msg("e - write following ASCII character");
231     tui.log_msg("c - switch to chat mode");
232     tui.log_msg("? - switch to study mode");
233     tui.log_msg("");
234     tui.log_msg("study mode commands:");
235     tui.log_msg("w, a, s, d - move question mark");
236     tui.log_msg("A - annotate terrain");
237     tui.log_msg("c - switch to chat mode");
238     tui.log_msg("p - switch to play mode");
239     tui.log_msg("");
240   },
241   draw_info: function() {
242     terminal.drawBox(1, terminal.cols / 2, terminal.rows - 2, terminal.cols / 2);
243     let lines = this.msg_into_lines_of_width(explorer.get_info(), terminal.cols / 2);
244     lines.reverse();
245     for (let y = 1, i = 0; y < terminal.rows && i < lines.length; y++, i++) {
246       terminal.write(y, terminal.cols / 2, lines[i]);
247     }
248   },
249   full_refresh: function() {
250     this.draw_map();
251     this.draw_turn_line();
252     if (this.mode == 'study' || this.mode == 'annotate') {
253       this.draw_info();
254     } else {
255       this.draw_history();
256     }
257     this.draw_input_line();
258     this.refresh();
259   }
260 }
261
262 let game = {
263   things: {},
264   turn: 0,
265   map: "",
266   map_size: [0,0],
267   player_id: 0
268 }
269
270 terminal.initialize();
271 tui.log_help();
272 tui.full_refresh();
273
274 let websocket = new WebSocket(websocket_location);
275 websocket.onopen = function (event) {
276     window.setInterval(function() { websocket.send('PING') }, 30000);
277     websocket.send('GET_GAMESTATE');
278 }
279 websocket.onmessage = function (event) {
280   let tokens = parser.tokenize(event.data)[0];
281   if (tokens[0] === 'TURN') {
282     game.things = {}
283     game.turn = parseInt(tokens[1]);
284   } else if (tokens[0] === 'THING_POS') {
285     game.things[tokens[1]] = parser.parse_yx(tokens[2]);
286   } else if (tokens[0] === 'MAP') {
287     game.map_size = parser.parse_yx(tokens[1]);
288     game.map = tokens[2]
289   } else if (tokens[0] === 'GAME_STATE_COMPLETE') {
290     explorer.empty_info_db();
291     if (tui.mode == 'study') {
292       explorer.query_info();
293     }
294     tui.draw_turn_line();
295     tui.draw_map();
296     tui.refresh();
297   } else if (tokens[0] === 'CHAT') {
298      tui.log_msg('# ' + tokens[1], 1);
299      tui.refresh();
300   } else if (tokens[0] === 'PLAYER_ID') {
301       game.player_id = parseInt(tokens[1]);
302   } else if (tokens[0] === 'META') {
303      tui.log_msg('@ ' + tokens[1]);
304      tui.refresh();
305   } else if (tokens[0] === 'ANNOTATION') {
306      let position = parser.parse_yx(tokens[1]);
307      explorer.update_info_db(position, tokens[2]);
308   } else if (tokens[0] === 'UNHANDLED_INPUT') {
309      tui.log_msg('? unknown command');
310      tui.refresh();
311   } else if (tokens[0] === 'ARGUMENT_ERROR') {
312      tui.log_msg('? syntax error: ' + tokens[1]);
313      tui.refresh();
314   } else if (tokens[0] === 'GAME_ERROR') {
315      tui.log_msg('? game error: ' + tokens[1]);
316      tui.refresh();
317   } else if (tokens[0] === 'PONG') {
318     console.log('PONG');
319   } else {
320      tui.log_msg('? unhandled input: ' + event.data);
321      tui.refresh();
322   }
323 }
324
325 let explorer = {
326     position: [0,0],
327     info_db: {},
328     move: function(direction) {
329         let try_pos = [0,0];
330         try_pos[0] = this.position[0];
331         try_pos[1] = this.position[1];
332         if (direction == 'left') {
333             try_pos[1] -= 1;
334         } else if (direction == 'right') {
335             try_pos[1] += 1;
336         } else if (direction == 'up') {
337             try_pos[0] -= 1;
338         } else if (direction == 'down') {
339             try_pos[0] += 1;
340         };
341         if (!(try_pos[0] < 0) &&
342             !(try_pos[1] < 0) &&
343             !(try_pos[0] >= game.map_size[0])
344             && !(try_pos[1] >= game.map_size[1])) {
345             this.position = try_pos;
346             this.query_info();
347             tui.draw_map();
348             tui.draw_info();
349             tui.refresh();
350         }
351     },
352     update_info_db: function(yx, str) {
353         this.info_db[yx] = str;
354         if (tui.mode == 'study') {
355             tui.draw_info();
356             tui.refresh();
357         }
358     },
359     empty_info_db: function() {
360         this.info_db = {};
361         if (tui.mode == 'study') {
362             tui.draw_info();
363             tui.refresh();
364         }
365     },
366     query_info: function() {
367         websocket.send("GET_ANNOTATION Y:" + explorer.position[0] + ",X:" + explorer.position[1]);
368     },
369     get_info: function() {
370         if (this.position in this.info_db) {
371             return this.info_db[this.position];
372         } else {
373             return 'waiting …';
374         }
375     },
376     annotate: function(msg) {
377         if (msg.length == 0) {
378             msg = " ";  // triggers annotation deletion
379         }
380         websocket.send("ANNOTATE Y:" + explorer.position[0] + ",X:" + explorer.position[1] + " " + quote(msg));
381     }
382 }
383
384 document.addEventListener('keydown', (event) => {
385     if (tui.mode == 'chat') {
386         if (event.key.length === 1) {
387             tui.input_line += event.key;
388             tui.draw_input_line();
389             tui.refresh();
390         } else if (event.key == 'Backspace') {
391             tui.input_line = tui.input_line.slice(0, -1);
392             tui.draw_input_line();
393             tui.refresh();
394         } else if (event.key == 'Enter') {
395             let [tokens, token_starts] = parser.tokenize(tui.input_line);
396             if (tokens.length > 0 && tokens[0].length > 0) {
397                 if (tokens[0][0] == ':') {
398                     if (tokens[0] == ':play' || tokens[0] == ':p') {
399                         tui.switch_mode('play');
400                     } else if (tokens[0] == ':study' || tokens[0] == ':s') {
401                         tui.switch_mode('study');
402                     } else if (tokens[0] == ':help') {
403                         tui.log_help();
404                         tui.refresh();
405                     } else if (tokens[0] == ':login') {
406                         if (tokens.length > 1) {
407                             websocket.send('LOGIN ' + quote(tokens[1]));
408                         } else {
409                             tui.log_msg('? need login name');
410                         }
411                     } else if (tokens[0] == ':msg') {
412                         if (tokens.length > 2) {
413                             let msg = tui.input_line.slice(token_starts[2]);
414                             websocket.send('QUERY ' + quote(tokens[1]) + ' ' + quote(msg));
415                         } else {
416                             tui.log_msg('? need message target and message');
417                         }
418                     } else {
419                         tui.log_msg('? unknown command');
420                     }
421                 } else {
422                     websocket.send('ALL ' + quote(tui.input_line));
423                 }
424             }
425             tui.input_line = '';
426             tui.draw_input_line();
427             tui.refresh();
428         }
429       } else if (tui.mode == 'play') {
430           if (event.key === 'c') {
431               tui.switch_mode('chat');
432           } else if (event.key === 'e') {
433               tui.switch_mode('edit');
434           } else if (event.key === '?') {
435               tui.switch_mode('study');
436           } else if (event.key === 'F1') {
437               tui.log_help();
438               tui.refresh();
439           } else if (event.key === 'f') {
440               websocket.send("TASK:FLATTEN_SURROUNDINGS");
441           } else if (event.key === 'a') {
442               websocket.send('TASK:MOVE LEFT');
443           } else if (event.key === 'd') {
444               websocket.send('TASK:MOVE RIGHT');
445           } else if (event.key === 'w') {
446               websocket.send('TASK:MOVE UP');
447           } else if (event.key === 's') {
448               websocket.send('TASK:MOVE DOWN');
449           };
450     } else if (tui.mode == 'edit') {
451         if (event.key != "Shift" && event.key.length == 1) {
452             websocket.send("TASK:WRITE " + quote(event.key));
453             tui.switch_mode('play');
454         }
455     } else if (tui.mode == 'study') {
456         if (event.key === 'c') {
457             tui.switch_mode('chat');
458         } else if (event.key == 'p') {
459             tui.switch_mode('play');
460         } else if (event.key === 'a') {
461               explorer.move('left');
462         } else if (event.key === 'd') {
463               explorer.move('right');
464         } else if (event.key === 'w') {
465               explorer.move('up');
466         } else if (event.key === 's') {
467               explorer.move('down');
468         } else if (event.key === 'A') {
469           tui.switch_mode('annotate');
470           tui.draw_info();
471           tui.refresh();
472         };
473     } else if (tui.mode == 'annotate') {
474         if (event.key.length === 1) {
475             tui.input_line += event.key;
476             tui.draw_input_line();
477             tui.refresh();
478         } else if (event.key == 'Backspace') {
479             tui.input_line = tui.input_line.slice(0, -1);
480             tui.draw_input_line();
481             tui.refresh();
482         } else if (event.key == 'Enter') {
483             explorer.annotate(tui.input_line);
484             tui.input_line = '';
485             tui.switch_mode('study', true);
486         }
487     }
488 }, false);
489 </script>
490 </body></html>