6 <pre id="terminal" style="display: inline-block; color: white; background-color: black;"></pre>
9 let websocket_location = "ws://localhost:8000";
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;
22 for (let y = 0, x = 0; y <= this.rows; x++) {
26 this.content.push(line);
35 blink_screen: function() {
36 this.pre_el.style.color = this.background;
37 this.pre_el.style.backgroundColor = this.foreground;
39 this.pre_el.style.color = this.foreground;
40 this.pre_el.style.backgroundColor = this.background;
45 for (let y = 0; y < this.rows; y++) {
46 let line = this.content[y].join('');
47 pre_string += line + '\n';
49 this.pre_el.textContent = pre_string;
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];
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++) {
67 this.content[y][x] = ' ';
73 tokenize: function(str) {
79 for (let i = 0; i < str.length; i++) {
85 } else if (c == '\\') {
87 } else if (c == '"') {
92 } else if (c == '"') {
94 } else if (c === ' ') {
95 if (token.length > 0) {
104 if (token.length > 0) {
107 let token_starts = [];
108 for (let i = 0; i < token_ends.length; i++) {
109 token_starts.push(token_ends[i] - tokens[i].length);
111 return [tokens, token_starts];
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));
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']);
130 send: function(tokens) {
131 this.websocket.send(unparser.untokenize(tokens));
136 quote: function(str) {
138 for (let i = 0; i < str.length; i++) {
140 if (c in ['"', '\\']) {
146 return quoted.join('');
148 to_yx: function(yx_coordinate) {
149 return "Y:" + yx_coordinate[0] + ",X:" + yx_coordinate[1];
151 untokenize: function(tokens) {
152 let quoted_tokens = [];
153 for (let token of tokens) {
154 quoted_tokens.push(this.quote(token));
156 return quoted_tokens.join(" ");
161 constructor(name, has_input_prompt=false, shows_annotations=false) {
163 this.has_input_prompt = has_input_prompt;
164 this.shows_annotations = shows_annotations;
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);
179 window_width: terminal.cols / 2,
184 this.recalc_input_lines();
185 this.height_header = this.height_turn_line + this.height_mode_line;
187 switch_mode: function(mode, keep_pos=false) {
188 if (mode == mode_study && !keep_pos) {
189 explorer.position = game.things[game.player_id];
193 if (mode == mode_annotate && explorer.position in explorer.info_db) {
194 let info = explorer.info_db[explorer.position];
195 if (info != "(none)") {
196 this.add_to_input(explorer.info_db[explorer.position]);
201 draw_mode_line: function() {
202 terminal.drawBox(1, this.window_width, this.height_mode_line, this.window_width);
203 terminal.write(1, this.window_width, 'MODE ' + this.mode.name);
205 draw_history: function() {
206 if (terminal.rows <= this.height_header + this.height_input) {
209 terminal.drawBox(this.height_header, this.window_width, terminal.rows - this.height_header - this.height_input, this.window_width);
210 for (let y = terminal.rows - 1 - this.height_input,
211 i = this.log.length - 1;
212 y >= this.height_header && i >= 0;
214 terminal.write(y, this.window_width, this.log[i]);
217 draw_map: function() {
218 terminal.drawBox(0, 0, terminal.rows, this.window_width);
221 for (let i = 0, j = 0; i < game.map.length; i++, j++) {
222 if (j == game.map_size[1]) {
223 map_lines.push(line);
227 line.push(game.map[i]);
229 map_lines.push(line);
230 let player_position = [0,0];
231 let center_pos = [Math.floor(game.map_size[0] / 2),
232 Math.floor(game.map_size[1] / 2)];
233 for (const thing_id in game.things) {
234 let t = game.things[thing_id];
235 map_lines[t[0]][t[1]] = '@';
236 if (game.player_id == thing_id) {
240 if (tui.mode.shows_annotations) {
241 map_lines[explorer.position[0]][explorer.position[1]] = '?';
242 center_pos = explorer.position;
244 let offset = [(terminal.rows / 2) - center_pos[0],
245 this.window_width / 2 - center_pos[1]];
246 for (let term_y = offset[0], map_y = 0;
247 term_y < terminal.rows && map_y < game.map_size[0];
250 let to_draw = map_lines[map_y].join('').slice(0, this.window_width - offset[1]);
251 terminal.write(term_y, offset[1], to_draw);
255 draw_turn_line: function(n) {
256 terminal.drawBox(0, this.window_width, 1, this.window_width);
257 terminal.write(0, this.window_width, 'turn: ' + game.turn);
259 empty_input: function(str) {
261 if (this.mode.has_input_prompt) {
262 this.recalc_input_lines();
264 this.height_input = 0;
267 add_to_input: function(str) {
268 if (this.input_prompt.length + this.input.length + str.length > this.window_width * terminal.rows) {
272 this.recalc_input_lines();
274 recalc_input_lines: function() {
275 this.input_lines = this.msg_into_lines_of_width(this.input_prompt + this.input, this.window_width);
276 this.height_input = this.input_lines.length;
278 shorten_input: function() {
279 if (this.input.length == 0) {
280 terminal.blink_screen();
282 this.input = tui.input.slice(0, -1);
283 this.recalc_input_lines();
286 draw_input: function() {
287 terminal.drawBox(terminal.rows - this.height_input, this.window_width, this.height_input, this.window_width);
288 if (this.mode.has_input_prompt) {
289 for (let y = terminal.rows - this.height_input, i = 0; y < terminal.rows && i < this.input_lines.length; y++, i++) {
290 terminal.write(y, this.window_width, this.input_lines[i]);
294 msg_into_lines_of_width: function(msg, width) {
297 for (let i = 0, x = 0; i < msg.length; i++, x++) {
308 log_msg: function(msg) {
309 let lines = this.msg_into_lines_of_width(msg, this.window_width);
310 this.log = this.log.concat(lines);
311 while (this.log.length > terminal.rows) {
316 refresh: function() {
319 log_help: function() {
323 tui.log_msg("chat mode commands:");
324 tui.log_msg(":login USER - register as USER");
325 tui.log_msg(":msg USER TEXT - send TEXT to USER");
326 tui.log_msg(":help - show this help");
327 tui.log_msg(":play or :p - switch to play mode");
328 tui.log_msg(":study or :s - switch to study mode");
330 tui.log_msg("play mode commands:");
331 tui.log_msg("w, a, s, d - move avatar");
332 tui.log_msg("f - flatten surroundings");
333 tui.log_msg("e - write following ASCII character");
334 tui.log_msg("c - switch to chat mode");
335 tui.log_msg("? - switch to study mode");
337 tui.log_msg("study mode commands:");
338 tui.log_msg("w, a, s, d - move question mark");
339 tui.log_msg("A - annotate terrain");
340 tui.log_msg("c - switch to chat mode");
341 tui.log_msg("p - switch to play mode");
344 draw_info: function() {
345 terminal.drawBox(this.height_header, this.window_width, terminal.rows - this.height_header - this.height_input, this.window_width);
346 let lines = this.msg_into_lines_of_width(explorer.get_info(), this.window_width);
347 for (let y = this.height_header, i = 0; y < terminal.rows && i < lines.length; y++, i++) {
348 terminal.write(y, this.window_width, lines[i]);
351 full_refresh: function() {
353 this.draw_turn_line();
354 this.draw_mode_line();
355 if (this.mode.shows_annotations) {
373 terminal.initialize();
378 server.init(websocket_location);
379 server.websocket.onmessage = function (event) {
380 let tokens = parser.tokenize(event.data)[0];
381 if (tokens[0] === 'TURN') {
383 game.turn = parseInt(tokens[1]);
384 } else if (tokens[0] === 'THING_POS') {
385 game.things[tokens[1]] = parser.parse_yx(tokens[2]);
386 } else if (tokens[0] === 'MAP') {
387 game.map_size = parser.parse_yx(tokens[1]);
389 } else if (tokens[0] === 'GAME_STATE_COMPLETE') {
390 explorer.empty_info_db();
391 if (tui.mode == mode_study) {
392 explorer.query_info();
394 tui.draw_turn_line();
397 } else if (tokens[0] === 'CHAT') {
398 tui.log_msg('# ' + tokens[1], 1);
400 } else if (tokens[0] === 'PLAYER_ID') {
401 game.player_id = parseInt(tokens[1]);
402 } else if (tokens[0] === 'META') {
403 tui.log_msg('@ ' + tokens[1]);
405 } else if (tokens[0] === 'ANNOTATION') {
406 let position = parser.parse_yx(tokens[1]);
407 explorer.update_info_db(position, tokens[2]);
408 } else if (tokens[0] === 'UNHANDLED_INPUT') {
409 tui.log_msg('? unknown command');
411 } else if (tokens[0] === 'PLAY_ERROR') {
412 terminal.blink_screen();
413 } else if (tokens[0] === 'ARGUMENT_ERROR') {
414 tui.log_msg('? syntax error: ' + tokens[1]);
416 } else if (tokens[0] === 'GAME_ERROR') {
417 tui.log_msg('? game error: ' + tokens[1]);
419 } else if (tokens[0] === 'PONG') {
422 tui.log_msg('? unhandled input: ' + event.data);
430 move: function(direction) {
432 try_pos[0] = this.position[0];
433 try_pos[1] = this.position[1];
434 if (direction == 'left') {
436 } else if (direction == 'right') {
438 } else if (direction == 'up') {
440 } else if (direction == 'down') {
443 if (!(try_pos[0] < 0) &&
445 !(try_pos[0] >= game.map_size[0])
446 && !(try_pos[1] >= game.map_size[1])) {
447 this.position = try_pos;
454 update_info_db: function(yx, str) {
455 this.info_db[yx] = str;
456 if (tui.mode == mode_study) {
461 empty_info_db: function() {
463 if (tui.mode == mode_study) {
468 query_info: function() {
469 server.send(["GET_ANNOTATION", unparser.to_yx(explorer.position)]);
471 get_info: function() {
472 if (this.position in this.info_db) {
473 return this.info_db[this.position];
478 annotate: function(msg) {
479 if (msg.length == 0) {
480 msg = " "; // triggers annotation deletion
482 server.send(["ANNOTATE", unparser.to_yx(explorer.position), msg]);
486 document.addEventListener('keydown', (event) => {
487 if (tui.mode.has_input_prompt && event.key.length === 1) {
488 tui.add_to_input(event.key);
490 } else if (tui.mode.has_input_prompt && event.key == 'Backspace') {
493 } else if (tui.mode == mode_annotate && event.key == 'Enter') {
494 explorer.annotate(tui.input);
495 tui.switch_mode(mode_study, true);
496 } else if (tui.mode == mode_chat && event.key == 'Enter') {
497 let [tokens, token_starts] = parser.tokenize(tui.input);
498 if (tokens.length > 0 && tokens[0].length > 0) {
499 if (tokens[0][0] == ':') {
500 if (tokens[0] == ':play' || tokens[0] == ':p') {
501 tui.switch_mode(mode_play);
502 } else if (tokens[0] == ':study' || tokens[0] == ':s') {
503 tui.switch_mode(mode_study);
504 } else if (tokens[0] == ':help') {
507 } else if (tokens[0] == ':login') {
508 if (tokens.length > 1) {
509 server.send(['LOGIN', tokens[1]]);
511 tui.log_msg('? need login name');
513 } else if (tokens[0] == ':msg') {
514 if (tokens.length > 2) {
515 let msg = tui.input.slice(token_starts[2]);
516 server.send(['QUERY', tokens[1], msg]);
518 tui.log_msg('? need message target and message');
521 tui.log_msg('? unknown command');
524 server.send(['ALL', tui.input]);
529 } else if (tui.mode == mode_play) {
530 if (event.key === 'c') {
531 tui.switch_mode(mode_chat);
532 } else if (event.key === 'e') {
533 tui.switch_mode(mode_edit);
534 } else if (event.key === '?') {
535 tui.switch_mode(mode_study);
536 } else if (event.key === 'F1') {
539 } else if (event.key === 'f') {
540 server.send(["TASK:FLATTEN_SURROUNDINGS"]);
541 } else if (event.key === 'a') {
542 server.send(['TASK:MOVE', 'LEFT']);
543 } else if (event.key === 'd') {
544 server.send(['TASK:MOVE', 'RIGHT']);
545 } else if (event.key === 'w') {
546 server.send(['TASK:MOVE', 'UP']);
547 } else if (event.key === 's') {
548 server.send(['TASK:MOVE', 'DOWN']);
550 } else if (tui.mode == mode_edit) {
551 if (event.key != "Shift" && event.key.length == 1) {
552 server.send(["TASK:WRITE", event.key]);
553 tui.switch_mode(mode_play);
555 } else if (tui.mode == mode_study) {
556 if (event.key === 'c') {
557 tui.switch_mode(mode_chat);
558 } else if (event.key == 'p') {
559 tui.switch_mode(mode_play);
560 } else if (event.key === 'a') {
561 explorer.move('left');
562 } else if (event.key === 'd') {
563 explorer.move('right');
564 } else if (event.key === 'w') {
566 } else if (event.key === 's') {
567 explorer.move('down');
568 } else if (event.key === 'A') {
569 tui.switch_mode(mode_annotate);