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];
195 draw_mode_line: function() {
196 terminal.drawBox(1, this.window_width, this.height_mode_line, this.window_width);
197 terminal.write(1, this.window_width, 'MODE ' + this.mode.name);
199 draw_history: function() {
200 if (terminal.rows <= this.height_header + this.height_input) {
203 terminal.drawBox(this.height_header, this.window_width, terminal.rows - this.height_header - this.height_input, this.window_width);
204 for (let y = terminal.rows - 1 - this.height_input,
205 i = this.log.length - 1;
206 y >= this.height_header && i >= 0;
208 terminal.write(y, this.window_width, this.log[i]);
211 draw_map: function() {
212 terminal.drawBox(0, 0, terminal.rows, this.window_width);
215 for (let i = 0, j = 0; i < game.map.length; i++, j++) {
216 if (j == game.map_size[1]) {
217 map_lines.push(line);
221 line.push(game.map[i]);
223 map_lines.push(line);
224 let player_position = [0,0];
225 let center_pos = [Math.floor(game.map_size[0] / 2),
226 Math.floor(game.map_size[1] / 2)];
227 for (const thing_id in game.things) {
228 let t = game.things[thing_id];
229 map_lines[t[0]][t[1]] = '@';
230 if (game.player_id == thing_id) {
234 if (tui.mode.shows_annotations) {
235 map_lines[explorer.position[0]][explorer.position[1]] = '?';
236 center_pos = explorer.position;
238 let offset = [(terminal.rows / 2) - center_pos[0],
239 this.window_width / 2 - center_pos[1]];
240 for (let term_y = offset[0], map_y = 0;
241 term_y < terminal.rows && map_y < game.map_size[0];
244 let to_draw = map_lines[map_y].join('').slice(0, this.window_width - offset[1]);
245 terminal.write(term_y, offset[1], to_draw);
249 draw_turn_line: function(n) {
250 terminal.drawBox(0, this.window_width, 1, this.window_width);
251 terminal.write(0, this.window_width, 'turn: ' + game.turn);
253 empty_input: function(str) {
255 if (this.mode.has_input_prompt) {
256 this.recalc_input_lines();
258 this.height_input = 0;
261 add_to_input: function(str) {
262 if (this.input_prompt.length + this.input.length + str.length > this.window_width * terminal.rows) {
266 this.recalc_input_lines();
268 recalc_input_lines: function() {
269 this.input_lines = this.msg_into_lines_of_width(this.input_prompt + this.input, this.window_width);
270 this.height_input = this.input_lines.length;
272 shorten_input: function() {
273 if (this.input.length == 0) {
274 terminal.blink_screen();
276 this.input = tui.input.slice(0, -1);
277 this.recalc_input_lines();
280 draw_input: function() {
281 terminal.drawBox(terminal.rows - this.height_input, this.window_width, this.height_input, this.window_width);
282 if (this.mode.has_input_prompt) {
283 for (let y = terminal.rows - this.height_input, i = 0; y < terminal.rows && i < this.input_lines.length; y++, i++) {
284 terminal.write(y, this.window_width, this.input_lines[i]);
288 msg_into_lines_of_width: function(msg, width) {
291 for (let i = 0, x = 0; i < msg.length; i++, x++) {
302 log_msg: function(msg) {
303 let lines = this.msg_into_lines_of_width(msg, this.window_width);
304 this.log = this.log.concat(lines);
305 while (this.log.length > terminal.rows) {
310 refresh: function() {
313 log_help: function() {
317 tui.log_msg("chat mode commands:");
318 tui.log_msg(":login USER - register as USER");
319 tui.log_msg(":msg USER TEXT - send TEXT to USER");
320 tui.log_msg(":help - show this help");
321 tui.log_msg(":play or :p - switch to play mode");
322 tui.log_msg(":study or :s - switch to study mode");
324 tui.log_msg("play mode commands:");
325 tui.log_msg("w, a, s, d - move avatar");
326 tui.log_msg("f - flatten surroundings");
327 tui.log_msg("e - write following ASCII character");
328 tui.log_msg("c - switch to chat mode");
329 tui.log_msg("? - switch to study mode");
331 tui.log_msg("study mode commands:");
332 tui.log_msg("w, a, s, d - move question mark");
333 tui.log_msg("A - annotate terrain");
334 tui.log_msg("c - switch to chat mode");
335 tui.log_msg("p - switch to play mode");
338 draw_info: function() {
339 terminal.drawBox(this.height_header, this.window_width, terminal.rows - this.height_header - this.height_input, this.window_width);
340 let lines = this.msg_into_lines_of_width(explorer.get_info(), this.window_width);
341 for (let y = this.height_header, i = 0; y < terminal.rows && i < lines.length; y++, i++) {
342 terminal.write(y, this.window_width, lines[i]);
345 full_refresh: function() {
347 this.draw_turn_line();
348 this.draw_mode_line();
349 if (this.mode.shows_annotations) {
367 terminal.initialize();
372 server.init(websocket_location);
373 server.websocket.onmessage = function (event) {
374 let tokens = parser.tokenize(event.data)[0];
375 if (tokens[0] === 'TURN') {
377 game.turn = parseInt(tokens[1]);
378 } else if (tokens[0] === 'THING_POS') {
379 game.things[tokens[1]] = parser.parse_yx(tokens[2]);
380 } else if (tokens[0] === 'MAP') {
381 game.map_size = parser.parse_yx(tokens[1]);
383 } else if (tokens[0] === 'GAME_STATE_COMPLETE') {
384 explorer.empty_info_db();
385 if (tui.mode == mode_study) {
386 explorer.query_info();
388 tui.draw_turn_line();
391 } else if (tokens[0] === 'CHAT') {
392 tui.log_msg('# ' + tokens[1], 1);
394 } else if (tokens[0] === 'PLAYER_ID') {
395 game.player_id = parseInt(tokens[1]);
396 } else if (tokens[0] === 'META') {
397 tui.log_msg('@ ' + tokens[1]);
399 } else if (tokens[0] === 'ANNOTATION') {
400 let position = parser.parse_yx(tokens[1]);
401 explorer.update_info_db(position, tokens[2]);
402 } else if (tokens[0] === 'UNHANDLED_INPUT') {
403 tui.log_msg('? unknown command');
405 } else if (tokens[0] === 'PLAY_ERROR') {
406 terminal.blink_screen();
407 } else if (tokens[0] === 'ARGUMENT_ERROR') {
408 tui.log_msg('? syntax error: ' + tokens[1]);
410 } else if (tokens[0] === 'GAME_ERROR') {
411 tui.log_msg('? game error: ' + tokens[1]);
413 } else if (tokens[0] === 'PONG') {
416 tui.log_msg('? unhandled input: ' + event.data);
424 move: function(direction) {
426 try_pos[0] = this.position[0];
427 try_pos[1] = this.position[1];
428 if (direction == 'left') {
430 } else if (direction == 'right') {
432 } else if (direction == 'up') {
434 } else if (direction == 'down') {
437 if (!(try_pos[0] < 0) &&
439 !(try_pos[0] >= game.map_size[0])
440 && !(try_pos[1] >= game.map_size[1])) {
441 this.position = try_pos;
448 update_info_db: function(yx, str) {
449 this.info_db[yx] = str;
450 if (tui.mode == mode_study) {
455 empty_info_db: function() {
457 if (tui.mode == mode_study) {
462 query_info: function() {
463 server.send(["GET_ANNOTATION", unparser.to_yx(explorer.position)]);
465 get_info: function() {
466 if (this.position in this.info_db) {
467 return this.info_db[this.position];
472 annotate: function(msg) {
473 if (msg.length == 0) {
474 msg = " "; // triggers annotation deletion
476 server.send(["ANNOTATE", unparser.to_yx(explorer.position), msg]);
480 document.addEventListener('keydown', (event) => {
481 if (tui.mode.has_input_prompt && event.key.length === 1) {
482 tui.add_to_input(event.key);
484 } else if (tui.mode.has_input_prompt && event.key == 'Backspace') {
487 } else if (tui.mode == mode_annotate && event.key == 'Enter') {
488 explorer.annotate(tui.input);
489 tui.switch_mode(mode_study, true);
490 } else if (tui.mode == mode_chat && event.key == 'Enter') {
491 let [tokens, token_starts] = parser.tokenize(tui.input);
492 if (tokens.length > 0 && tokens[0].length > 0) {
493 if (tokens[0][0] == ':') {
494 if (tokens[0] == ':play' || tokens[0] == ':p') {
495 tui.switch_mode(mode_play);
496 } else if (tokens[0] == ':study' || tokens[0] == ':s') {
497 tui.switch_mode(mode_study);
498 } else if (tokens[0] == ':help') {
501 } else if (tokens[0] == ':login') {
502 if (tokens.length > 1) {
503 server.send(['LOGIN', tokens[1]]);
505 tui.log_msg('? need login name');
507 } else if (tokens[0] == ':msg') {
508 if (tokens.length > 2) {
509 let msg = tui.input.slice(token_starts[2]);
510 server.send(['QUERY', tokens[1], msg]);
512 tui.log_msg('? need message target and message');
515 tui.log_msg('? unknown command');
518 server.send(['ALL', tui.input]);
523 } else if (tui.mode == mode_play) {
524 if (event.key === 'c') {
525 tui.switch_mode(mode_chat);
526 } else if (event.key === 'e') {
527 tui.switch_mode(mode_edit);
528 } else if (event.key === '?') {
529 tui.switch_mode(mode_study);
530 } else if (event.key === 'F1') {
533 } else if (event.key === 'f') {
534 server.send(["TASK:FLATTEN_SURROUNDINGS"]);
535 } else if (event.key === 'a') {
536 server.send(['TASK:MOVE', 'LEFT']);
537 } else if (event.key === 'd') {
538 server.send(['TASK:MOVE', 'RIGHT']);
539 } else if (event.key === 'w') {
540 server.send(['TASK:MOVE', 'UP']);
541 } else if (event.key === 's') {
542 server.send(['TASK:MOVE', 'DOWN']);
544 } else if (tui.mode == mode_edit) {
545 if (event.key != "Shift" && event.key.length == 1) {
546 server.send(["TASK:WRITE", event.key]);
547 tui.switch_mode(mode_play);
549 } else if (tui.mode == mode_study) {
550 if (event.key === 'c') {
551 tui.switch_mode(mode_chat);
552 } else if (event.key == 'p') {
553 tui.switch_mode(mode_play);
554 } else if (event.key === 'a') {
555 explorer.move('left');
556 } else if (event.key === 'd') {
557 explorer.move('right');
558 } else if (event.key === 'w') {
560 } else if (event.key === 's') {
561 explorer.move('down');
562 } else if (event.key === 'A') {
563 tui.switch_mode(mode_annotate);