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