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 tui.log_msg("@ server connected!");
131 send: function(tokens) {
132 if (this.websocket.readyState !== WebSocket.OPEN) {
133 tui.log_msg('server disconnected :(');
135 this.websocket.send(unparser.untokenize(tokens));
141 quote: function(str) {
143 for (let i = 0; i < str.length; i++) {
145 if (c in ['"', '\\']) {
151 return quoted.join('');
153 to_yx: function(yx_coordinate) {
154 return "Y:" + yx_coordinate[0] + ",X:" + yx_coordinate[1];
156 untokenize: function(tokens) {
157 let quoted_tokens = [];
158 for (let token of tokens) {
159 quoted_tokens.push(this.quote(token));
161 return quoted_tokens.join(" ");
166 constructor(name, has_input_prompt=false, shows_annotations=false) {
168 this.has_input_prompt = has_input_prompt;
169 this.shows_annotations = shows_annotations;
172 let mode_waiting_for_server = new Mode('waiting_for_server', false, false);
173 let mode_login = new Mode('login', true, false);
174 let mode_chat = new Mode('chat', true, false);
175 let mode_annotate = new Mode('annotate', true, true);
176 let mode_play = new Mode('play', false, false);
177 let mode_study = new Mode('study', false, true);
178 let mode_edit = new Mode('edit', false, false);
181 mode: mode_waiting_for_server,
186 window_width: terminal.cols / 2,
191 this.recalc_input_lines();
192 this.height_header = this.height_turn_line + this.height_mode_line;
193 this.log_msg("@ waiting for server connection ...");
195 init_login: function() {
196 this.log_msg("@ please enter your username:");
197 this.switch_mode(mode_login);
199 switch_mode: function(mode, keep_pos=false) {
200 if (mode == mode_study && !keep_pos) {
201 explorer.position = game.things[game.player_id];
205 if (mode == mode_annotate && explorer.position in explorer.info_db) {
206 let info = explorer.info_db[explorer.position];
207 if (info != "(none)") {
208 this.add_to_input(explorer.info_db[explorer.position]);
213 draw_mode_line: function() {
214 terminal.drawBox(1, this.window_width, this.height_mode_line, this.window_width);
215 terminal.write(1, this.window_width, 'MODE ' + this.mode.name);
217 draw_history: function() {
218 if (terminal.rows <= this.height_header + this.height_input) {
221 terminal.drawBox(this.height_header, this.window_width, terminal.rows - this.height_header - this.height_input, this.window_width);
222 for (let y = terminal.rows - 1 - this.height_input,
223 i = this.log.length - 1;
224 y >= this.height_header && i >= 0;
226 terminal.write(y, this.window_width, this.log[i]);
229 draw_map: function() {
230 terminal.drawBox(0, 0, terminal.rows, this.window_width);
233 for (let i = 0, j = 0; i < game.map.length; i++, j++) {
234 if (j == game.map_size[1]) {
235 map_lines.push(line);
239 line.push(game.map[i]);
241 map_lines.push(line);
242 let player_position = [0,0];
243 let center_pos = [Math.floor(game.map_size[0] / 2),
244 Math.floor(game.map_size[1] / 2)];
245 for (const thing_id in game.things) {
246 let t = game.things[thing_id];
247 map_lines[t[0]][t[1]] = '@';
248 if (game.player_id == thing_id) {
252 if (tui.mode.shows_annotations) {
253 map_lines[explorer.position[0]][explorer.position[1]] = '?';
254 center_pos = explorer.position;
256 let offset = [(terminal.rows / 2) - center_pos[0],
257 this.window_width / 2 - center_pos[1]];
258 for (let term_y = offset[0], map_y = 0;
259 term_y < terminal.rows && map_y < game.map_size[0];
262 let to_draw = map_lines[map_y].join('').slice(0, this.window_width - offset[1]);
263 terminal.write(term_y, offset[1], to_draw);
267 draw_turn_line: function(n) {
268 terminal.drawBox(0, this.window_width, 1, this.window_width);
269 terminal.write(0, this.window_width, 'turn: ' + game.turn);
271 empty_input: function(str) {
273 if (this.mode.has_input_prompt) {
274 this.recalc_input_lines();
276 this.height_input = 0;
279 add_to_input: function(str) {
280 if (this.input_prompt.length + this.input.length + str.length > this.window_width * terminal.rows) {
284 this.recalc_input_lines();
286 recalc_input_lines: function() {
287 this.input_lines = this.msg_into_lines_of_width(this.input_prompt + this.input, this.window_width);
288 this.height_input = this.input_lines.length;
290 shorten_input: function() {
291 if (this.input.length == 0) {
292 terminal.blink_screen();
294 this.input = tui.input.slice(0, -1);
295 this.recalc_input_lines();
298 draw_input: function() {
299 terminal.drawBox(terminal.rows - this.height_input, this.window_width, this.height_input, this.window_width);
300 if (this.mode.has_input_prompt) {
301 for (let y = terminal.rows - this.height_input, i = 0; y < terminal.rows && i < this.input_lines.length; y++, i++) {
302 terminal.write(y, this.window_width, this.input_lines[i]);
306 msg_into_lines_of_width: function(msg, width) {
309 for (let i = 0, x = 0; i < msg.length; i++, x++) {
320 log_msg: function(msg) {
321 let lines = this.msg_into_lines_of_width(msg, this.window_width);
322 this.log = this.log.concat(lines);
323 while (this.log.length > terminal.rows) {
328 refresh: function() {
331 log_help: function() {
333 this.log_msg("HELP");
335 this.log_msg("chat mode commands:");
336 this.log_msg(":nick NAME - re-name yourself to NAME");
337 this.log_msg(":msg USER TEXT - send TEXT to USER");
338 this.log_msg(":help - show this help");
339 this.log_msg(":play or :p - switch to play mode");
340 this.log_msg(":study or :s - switch to study mode");
342 this.log_msg("play mode commands:");
343 this.log_msg("w, a, s, d - move avatar");
344 this.log_msg("f - flatten surroundings");
345 this.log_msg("e - write following ASCII character");
346 this.log_msg("c - switch to chat mode");
347 this.log_msg("? - switch to study mode");
349 this.log_msg("study mode commands:");
350 this.log_msg("w, a, s, d - move question mark");
351 this.log_msg("A - annotate terrain");
352 this.log_msg("c - switch to chat mode");
353 this.log_msg("p - switch to play mode");
356 draw_info: function() {
357 terminal.drawBox(this.height_header, this.window_width, terminal.rows - this.height_header - this.height_input, this.window_width);
358 let lines = this.msg_into_lines_of_width(explorer.get_info(), this.window_width);
359 for (let y = this.height_header, i = 0; y < terminal.rows && i < lines.length; y++, i++) {
360 terminal.write(y, this.window_width, lines[i]);
363 full_refresh: function() {
365 this.draw_turn_line();
366 this.draw_mode_line();
367 if (this.mode.shows_annotations) {
385 terminal.initialize();
389 server.init(websocket_location);
390 server.websocket.onmessage = function (event) {
391 let tokens = parser.tokenize(event.data)[0];
392 if (tokens[0] === 'TURN') {
394 game.turn = parseInt(tokens[1]);
395 } else if (tokens[0] === 'THING_POS') {
396 game.things[tokens[1]] = parser.parse_yx(tokens[2]);
397 } else if (tokens[0] === 'MAP') {
398 game.map_size = parser.parse_yx(tokens[1]);
400 } else if (tokens[0] === 'GAME_STATE_COMPLETE') {
401 explorer.empty_info_db();
402 if (tui.mode == mode_study) {
403 explorer.query_info();
405 tui.draw_turn_line();
408 } else if (tokens[0] === 'CHAT') {
409 tui.log_msg('# ' + tokens[1], 1);
411 } else if (tokens[0] === 'PLAYER_ID') {
412 game.player_id = parseInt(tokens[1]);
413 } else if (tokens[0] === 'META') {
414 tui.log_msg('@ ' + tokens[1]);
416 } else if (tokens[0] === 'LOGIN_OK') {
417 server.send(['GET_GAMESTATE']);
419 tui.log_msg('@ ' + tokens[1]);
420 tui.switch_mode(mode_chat);
421 } else if (tokens[0] === 'ANNOTATION') {
422 let position = parser.parse_yx(tokens[1]);
423 explorer.update_info_db(position, tokens[2]);
424 } else if (tokens[0] === 'UNHANDLED_INPUT') {
425 tui.log_msg('? unknown command');
427 } else if (tokens[0] === 'PLAY_ERROR') {
428 terminal.blink_screen();
429 } else if (tokens[0] === 'ARGUMENT_ERROR') {
430 tui.log_msg('? syntax error: ' + tokens[1]);
432 } else if (tokens[0] === 'GAME_ERROR') {
433 tui.log_msg('? game error: ' + tokens[1]);
435 } else if (tokens[0] === 'PONG') {
438 tui.log_msg('? unhandled input: ' + event.data);
446 move: function(direction) {
448 try_pos[0] = this.position[0];
449 try_pos[1] = this.position[1];
450 if (direction == 'left') {
452 } else if (direction == 'right') {
454 } else if (direction == 'up') {
456 } else if (direction == 'down') {
459 if (!(try_pos[0] < 0) &&
461 !(try_pos[0] >= game.map_size[0])
462 && !(try_pos[1] >= game.map_size[1])) {
463 this.position = try_pos;
470 update_info_db: function(yx, str) {
471 this.info_db[yx] = str;
472 if (tui.mode == mode_study) {
477 empty_info_db: function() {
479 if (tui.mode == mode_study) {
484 query_info: function() {
485 server.send(["GET_ANNOTATION", unparser.to_yx(explorer.position)]);
487 get_info: function() {
488 if (this.position in this.info_db) {
489 return this.info_db[this.position];
494 annotate: function(msg) {
495 if (msg.length == 0) {
496 msg = " "; // triggers annotation deletion
498 server.send(["ANNOTATE", unparser.to_yx(explorer.position), msg]);
502 document.addEventListener('keydown', (event) => {
503 if (tui.mode.has_input_prompt && event.key.length === 1) {
504 tui.add_to_input(event.key);
506 } else if (tui.mode.has_input_prompt && event.key == 'Backspace') {
509 } else if (tui.mode == mode_login && event.key == 'Enter') {
510 server.send(['LOGIN', tui.input]);
511 tui.switch_mode(mode_login);
512 } else if (tui.mode == mode_annotate && event.key == 'Enter') {
513 explorer.annotate(tui.input);
514 tui.switch_mode(mode_study, true);
515 } else if (tui.mode == mode_chat && event.key == 'Enter') {
516 let [tokens, token_starts] = parser.tokenize(tui.input);
517 if (tokens.length > 0 && tokens[0].length > 0) {
518 if (tokens[0][0] == ':') {
519 if (tokens[0] == ':play' || tokens[0] == ':p') {
520 tui.switch_mode(mode_play);
521 } else if (tokens[0] == ':study' || tokens[0] == ':s') {
522 tui.switch_mode(mode_study);
523 } else if (tokens[0] == ':help') {
526 } else if (tokens[0] == ':nick') {
527 if (tokens.length > 1) {
528 server.send(['LOGIN', tokens[1]]);
530 tui.log_msg('? need login name');
532 } else if (tokens[0] == ':msg') {
533 if (tokens.length > 2) {
534 let msg = tui.input.slice(token_starts[2]);
535 server.send(['QUERY', tokens[1], msg]);
537 tui.log_msg('? need message target and message');
540 tui.log_msg('? unknown command');
543 server.send(['ALL', tui.input]);
548 } else if (tui.mode == mode_play) {
549 if (event.key === 'c') {
550 tui.switch_mode(mode_chat);
551 } else if (event.key === 'e') {
552 tui.switch_mode(mode_edit);
553 } else if (event.key === '?') {
554 tui.switch_mode(mode_study);
555 } else if (event.key === 'F1') {
558 } else if (event.key === 'f') {
559 server.send(["TASK:FLATTEN_SURROUNDINGS"]);
560 } else if (event.key === 'a') {
561 server.send(['TASK:MOVE', 'LEFT']);
562 } else if (event.key === 'd') {
563 server.send(['TASK:MOVE', 'RIGHT']);
564 } else if (event.key === 'w') {
565 server.send(['TASK:MOVE', 'UP']);
566 } else if (event.key === 's') {
567 server.send(['TASK:MOVE', 'DOWN']);
569 } else if (tui.mode == mode_edit) {
570 if (event.key != "Shift" && event.key.length == 1) {
571 server.send(["TASK:WRITE", event.key]);
572 tui.switch_mode(mode_play);
574 } else if (tui.mode == mode_study) {
575 if (event.key === 'c') {
576 tui.switch_mode(mode_chat);
577 } else if (event.key == 'p') {
578 tui.switch_mode(mode_play);
579 } else if (event.key === 'a') {
580 explorer.move('left');
581 } else if (event.key === 'd') {
582 explorer.move('right');
583 } else if (event.key === 'w') {
585 } else if (event.key === 's') {
586 explorer.move('down');
587 } else if (event.key === 'A') {
588 tui.switch_mode(mode_annotate);