7 movement: <select id="WASD_selector" name="WASD_selector" >
8 <option value="w, a, s, d" selected>w, a, s, d</option>
9 <option value="arrow keys">arrow keys</option>
11 rows: <input id="n_rows" type="number" step=2 min=10 value=24 />
12 cols: <input id="n_cols" type="number" step=4 min=20 value=80 />
14 <pre id="terminal" style="display: inline-block;"></pre>
15 <textarea id="input" style="opacity: 0; width: 0px;"></textarea>
18 let websocket_location = "ws://localhost:8000";
20 let wasd_selector = document.getElementById("WASD_selector");
21 let rows_selector = document.getElementById("n_rows");
22 let cols_selector = document.getElementById("n_cols");
27 initialize: function() {
28 this.rows = rows_selector.value;
29 this.cols = cols_selector.value;
30 this.pre_el = document.getElementById("terminal");
31 this.pre_el.style.color = this.foreground;
32 this.pre_el.style.backgroundColor = this.background;
35 for (let y = 0, x = 0; y <= this.rows; x++) {
39 this.content.push(line);
48 blink_screen: function() {
49 this.pre_el.style.color = this.background;
50 this.pre_el.style.backgroundColor = this.foreground;
52 this.pre_el.style.color = this.foreground;
53 this.pre_el.style.backgroundColor = this.background;
58 for (let y = 0; y < this.rows; y++) {
59 let line = this.content[y].join('');
60 pre_string += line + '\n';
62 this.pre_el.textContent = pre_string;
64 write: function(start_y, start_x, msg) {
65 for (let x = start_x, i = 0; x < this.cols && i < msg.length; x++, i++) {
66 this.content[start_y][x] = msg[i];
69 drawBox: function(start_y, start_x, height, width) {
70 let end_y = start_y + height;
71 let end_x = start_x + width;
72 for (let y = start_y, x = start_x; y < this.rows; x++) {
80 this.content[y][x] = ' ';
84 terminal.initialize();
87 tokenize: function(str) {
93 for (let i = 0; i < str.length; i++) {
99 } else if (c == '\\') {
101 } else if (c == '"') {
106 } else if (c == '"') {
108 } else if (c === ' ') {
109 if (token.length > 0) {
118 if (token.length > 0) {
121 let token_starts = [];
122 for (let i = 0; i < token_ends.length; i++) {
123 token_starts.push(token_ends[i] - tokens[i].length);
125 return [tokens, token_starts];
127 parse_yx: function(position_string) {
128 let coordinate_strings = position_string.split(',')
129 let position = [0, 0];
130 position[0] = parseInt(coordinate_strings[0].slice(2));
131 position[1] = parseInt(coordinate_strings[1].slice(2));
143 init: function(url) {
145 this.websocket = new WebSocket(this.url);
146 this.websocket.onopen = function(event) {
147 window.setInterval(function() { server.send(['PING']) }, 30000);
148 tui.log_msg("@ server connected! :)");
151 this.websocket.onclose = function(event) {
152 tui.log_msg("@ server disconnected :(");
153 tui.log_msg("@ hint: try the ':reconnect' command");
155 this.websocket.onmessage = this.handle_event;
157 reconnect: function() {
158 this.reconnect_to(this.url);
160 reconnect_to: function(url) {
161 this.websocket.close();
164 send: function(tokens) {
165 this.websocket.send(unparser.untokenize(tokens));
167 handle_event: function(event) {
168 let tokens = parser.tokenize(event.data)[0];
169 if (tokens[0] === 'TURN') {
172 game.turn = parseInt(tokens[1]);
173 } else if (tokens[0] === 'THING_POS') {
174 game.get_thing(tokens[1], true).position = parser.parse_yx(tokens[2]);
175 } else if (tokens[0] === 'THING_NAME') {
176 game.get_thing(tokens[1], true).name_ = tokens[2];
177 } else if (tokens[0] === 'MAP') {
178 game.map_size = parser.parse_yx(tokens[1]);
180 } else if (tokens[0] === 'GAME_STATE_COMPLETE') {
181 explorer.empty_info_db();
182 if (tui.mode == mode_study) {
183 explorer.query_info();
185 let t = game.get_thing(game.player_id);
186 if (t.position in game.portals) {
187 tui.teleport_target = game.portals[t.position];
188 tui.switch_mode(mode_teleport);
192 } else if (tokens[0] === 'CHAT') {
193 tui.log_msg('# ' + tokens[1], 1);
194 } else if (tokens[0] === 'PLAYER_ID') {
195 game.player_id = parseInt(tokens[1]);
196 } else if (tokens[0] === 'LOGIN_OK') {
197 this.send(['GET_GAMESTATE']);
199 tui.switch_mode(mode_play);
200 } else if (tokens[0] === 'PORTAL') {
201 let position = parser.parse_yx(tokens[1]);
202 game.portals[position] = tokens[2];
203 } else if (tokens[0] === 'ANNOTATION') {
204 let position = parser.parse_yx(tokens[1]);
205 explorer.update_info_db(position, tokens[2]);
206 } else if (tokens[0] === 'UNHANDLED_INPUT') {
207 tui.log_msg('? unknown command');
208 } else if (tokens[0] === 'PLAY_ERROR') {
209 terminal.blink_screen();
210 } else if (tokens[0] === 'ARGUMENT_ERROR') {
211 tui.log_msg('? syntax error: ' + tokens[1]);
212 } else if (tokens[0] === 'GAME_ERROR') {
213 tui.log_msg('? game error: ' + tokens[1]);
214 } else if (tokens[0] === 'PONG') {
217 tui.log_msg('? unhandled input: ' + event.data);
223 quote: function(str) {
225 for (let i = 0; i < str.length; i++) {
227 if (['"', '\\'].includes(c)) {
233 return quoted.join('');
235 to_yx: function(yx_coordinate) {
236 return "Y:" + yx_coordinate[0] + ",X:" + yx_coordinate[1];
238 untokenize: function(tokens) {
239 let quoted_tokens = [];
240 for (let token of tokens) {
241 quoted_tokens.push(this.quote(token));
243 return quoted_tokens.join(" ");
248 constructor(name, has_input_prompt=false, shows_info=false, is_intro=false) {
250 this.has_input_prompt = has_input_prompt;
251 this.shows_info= shows_info;
252 this.is_intro = is_intro;
255 let mode_waiting_for_server = new Mode('waiting_for_server', false, false, true);
256 let mode_login = new Mode('login', true, false, true);
257 let mode_chat = new Mode('chat / write messages to players', true, false);
258 let mode_annotate = new Mode('add message to map tile', true, true);
259 let mode_play = new Mode('play / move around', false, false);
260 let mode_study = new Mode('check map tiles for messages', false, true);
261 let mode_edit = new Mode('write ASCII char to map tile', false, false);
262 let mode_teleport = new Mode('teleport away?');
263 let mode_portal = new Mode('add portal to map tile', true, true);
266 mode: mode_waiting_for_server,
270 window_width: terminal.cols / 2,
275 this.inputEl = document.getElementById("input");
276 this.inputEl.focus();
277 this.recalc_input_lines();
278 this.height_header = this.height_turn_line + this.height_mode_line;
279 this.log_msg("@ waiting for server connection ...");
282 init_wasd: function() {
283 if (wasd_selector.value == 'w, a, s, d') {
288 } else if (wasd_selector.value == 'arrow keys') {
289 tui.key_up = 'ArrowUp';
290 tui.key_down = 'ArrowDown';
291 tui.key_left = 'ArrowLeft';
292 tui.key_right = 'ArrowRight';
294 tui.movement_keys_desc = wasd_selector.value;
296 init_login: function() {
297 this.log_msg("@ please enter your username:");
298 this.switch_mode(mode_login);
300 switch_mode: function(mode, keep_pos=false) {
301 if (mode == mode_study && !keep_pos) {
302 explorer.position = game.things[game.player_id].position;
306 if (mode == mode_annotate && explorer.position in explorer.info_db) {
307 let info = explorer.info_db[explorer.position];
308 if (info != "(none)") {
309 this.inputEl.value = info;
310 this.recalc_input_lines();
313 if (mode == mode_portal && explorer.position in game.portals) {
314 let portal = game.portals[explorer.position]
315 this.inputEl.value = portal;
316 this.recalc_input_lines();
317 } else if (mode == mode_teleport) {
318 tui.log_msg("@ May teleport to: " + tui.teleport_target);
319 tui.log_msg("@ Type Y or y to affirm, other keys to abort.");
323 empty_input: function(str) {
324 this.inputEl.value = "";
325 if (this.mode.has_input_prompt) {
326 this.recalc_input_lines();
328 this.height_input = 0;
331 recalc_input_lines: function() {
332 this.input_lines = this.msg_into_lines_of_width(this.input_prompt + this.inputEl.value, this.window_width);
333 this.height_input = this.input_lines.length;
335 msg_into_lines_of_width: function(msg, width) {
338 for (let i = 0, x = 0; i < msg.length; i++, x++) {
339 if (x >= width || msg[i] == "\n") {
344 if (msg[i] != "\n") {
351 log_msg: function(msg) {
353 while (this.log.length > 100) {
358 log_help: function() {
359 this.log_msg("HELP:");
360 this.log_msg("chat mode commands:");
361 this.log_msg(" :nick NAME - re-name yourself to NAME");
362 this.log_msg(" :msg USER TEXT - send TEXT to USER");
363 this.log_msg(" :help - show this help");
364 this.log_msg(" :p or :play - switch to play mode");
365 this.log_msg(" :? or :study - switch to study mode");
366 this.log_msg("commands common to study and play mode:");
367 this.log_msg(" " + this.movement_keys_desc + " - move");
368 this.log_msg(" c - switch to chat mode");
369 this.log_msg("commands specific to play mode:");
370 this.log_msg(" e - write following ASCII character");
371 this.log_msg(" f - flatten surroundings");
372 this.log_msg(" ? - switch to study mode");
373 this.log_msg("commands specific to study mode:");
374 this.log_msg(" e - annotate terrain");
375 this.log_msg(" p - switch to play mode");
377 draw_map: function() {
380 for (let i = 0, j = 0; i < game.map.length; i++, j++) {
381 if (j == game.map_size[1]) {
382 map_lines.push(line);
386 line.push(game.map[i]);
388 map_lines.push(line);
389 let center_pos = [Math.floor(game.map_size[0] / 2),
390 Math.floor(game.map_size[1] / 2)];
391 for (const thing_id in game.things) {
392 let t = game.things[thing_id];
393 map_lines[t.position[0]][t.position[1]] = '@';
394 if (game.player_id == thing_id) {
395 center_pos = t.position;
398 if (tui.mode.shows_info) {
399 map_lines[explorer.position[0]][explorer.position[1]] = '?';
400 center_pos = explorer.position;
402 let offset = [(terminal.rows / 2) - center_pos[0],
403 this.window_width / 2 - center_pos[1]];
404 for (let term_y = offset[0], map_y = 0;
405 term_y < terminal.rows && map_y < game.map_size[0];
408 let to_draw = map_lines[map_y].join('').slice(0, this.window_width - offset[1]);
409 terminal.write(term_y, offset[1], to_draw);
413 draw_mode_line: function() {
414 terminal.write(0, this.window_width, 'MODE: ' + this.mode.name);
416 draw_turn_line: function(n) {
417 terminal.write(1, this.window_width, 'TURN: ' + game.turn);
419 draw_history: function() {
420 let log_display_lines = [];
421 for (let line of this.log) {
422 log_display_lines = log_display_lines.concat(this.msg_into_lines_of_width(line, this.window_width));
424 for (let y = terminal.rows - 1 - this.height_input,
425 i = log_display_lines.length - 1;
426 y >= this.height_header && i >= 0;
428 terminal.write(y, this.window_width, log_display_lines[i]);
431 draw_info: function() {
432 let lines = this.msg_into_lines_of_width(explorer.get_info(), this.window_width);
433 for (let y = this.height_header, i = 0; y < terminal.rows && i < lines.length; y++, i++) {
434 terminal.write(y, this.window_width, lines[i]);
437 draw_input: function() {
438 if (this.mode.has_input_prompt) {
439 for (let y = terminal.rows - this.height_input, i = 0; i < this.input_lines.length; y++, i++) {
440 terminal.write(y, this.window_width, this.input_lines[i]);
444 full_refresh: function() {
445 terminal.drawBox(0, 0, terminal.rows, terminal.cols);
446 if (this.mode.is_intro) {
451 this.draw_turn_line();
452 this.draw_mode_line();
453 if (this.mode.shows_info) {
469 this.map_size = [0,0];
473 get_thing: function(id_, create_if_not_found=false) {
474 if (id_ in game.things) {
475 return game.things[id_];
476 } else if (create_if_not_found) {
477 let t = new Thing([0,0]);
478 game.things[id_] = t;
487 server.init(websocket_location);
492 move: function(direction) {
494 try_pos[0] = this.position[0];
495 try_pos[1] = this.position[1];
496 if (direction == 'left') {
498 } else if (direction == 'right') {
500 } else if (direction == 'up') {
502 } else if (direction == 'down') {
505 if (!(try_pos[0] < 0) &&
507 !(try_pos[0] >= game.map_size[0])
508 && !(try_pos[1] >= game.map_size[1])) {
509 this.position = try_pos;
514 update_info_db: function(yx, str) {
515 this.info_db[yx] = str;
516 if (tui.mode == mode_study) {
520 empty_info_db: function() {
522 if (tui.mode == mode_study) {
526 query_info: function() {
527 server.send(["GET_ANNOTATION", unparser.to_yx(explorer.position)]);
529 get_info: function() {
531 let position_i = this.position[0] * game.map_size[1] + this.position[1];
532 info += "TERRAIN: " + game.map[position_i] + "\n";
533 for (let t_id in game.things) {
534 let t = game.things[t_id];
535 if (t.position[0] == this.position[0] && t.position[1] == this.position[1]) {
538 info += ": " + t.name_;
543 if (this.position in game.portals) {
544 info += "PORTAL: " + game.portals[this.position] + "\n";
546 if (this.position in this.info_db) {
547 info += "ANNOTATIONS: " + this.info_db[this.position];
553 annotate: function(msg) {
554 if (msg.length == 0) {
555 msg = " "; // triggers annotation deletion
557 server.send(["ANNOTATE", unparser.to_yx(explorer.position), msg]);
559 set_portal: function(msg) {
560 if (msg.length == 0) {
561 msg = " "; // triggers portal deletion
563 server.send(["PORTAL", unparser.to_yx(explorer.position), msg]);
567 tui.inputEl.addEventListener('input', (event) => {
568 if (tui.mode.has_input_prompt) {
569 let max_length = tui.window_width * terminal.rows - tui.input_prompt.length;
570 if (tui.inputEl.value.length > max_length) {
571 tui.inputEl.value = tui.inputEl.value.slice(0, max_length);
573 tui.recalc_input_lines();
575 } else if (tui.mode == mode_edit && tui.inputEl.value.length > 0) {
576 server.send(["TASK:WRITE", tui.inputEl.value[0]]);
577 tui.switch_mode(mode_play);
578 } else if (tui.mode == mode_teleport) {
579 if (['Y', 'y'].includes(tui.inputEl.value[0])) {
580 server.reconnect_to(tui.teleport_target);
582 tui.log_msg("@ teleportation aborted");
583 tui.switch_mode(mode_play);
587 tui.inputEl.addEventListener('keydown', (event) => {
588 if (event.key == 'Enter') {
589 event.preventDefault();
591 if (tui.mode == mode_login && event.key == 'Enter') {
592 server.send(['LOGIN', tui.inputEl.value]);
593 tui.switch_mode(mode_login);
594 } else if (tui.mode == mode_portal && event.key == 'Enter') {
595 explorer.set_portal(tui.inputEl.value);
596 tui.switch_mode(mode_study, true);
597 } else if (tui.mode == mode_annotate && event.key == 'Enter') {
598 explorer.annotate(tui.inputEl.value);
599 tui.switch_mode(mode_study, true);
600 } else if (tui.mode == mode_chat && event.key == 'Enter') {
601 let [tokens, token_starts] = parser.tokenize(tui.inputEl.value);
602 if (tokens.length > 0 && tokens[0].length > 0) {
603 if (tokens[0][0] == ':') {
604 if (tokens[0] == ':play' || tokens[0] == ':p') {
605 tui.switch_mode(mode_play);
606 } else if (tokens[0] == ':study' || tokens[0] == ':?') {
607 tui.switch_mode(mode_study);
608 } else if (tokens[0] == ':help') {
610 } else if (tokens[0] == ':nick') {
611 if (tokens.length > 1) {
612 server.send(['LOGIN', tokens[1]]);
614 tui.log_msg('? need login name');
616 } else if (tokens[0] == ':msg') {
617 if (tokens.length > 2) {
618 let msg = tui.inputEl.value.slice(token_starts[2]);
619 server.send(['QUERY', tokens[1], msg]);
621 tui.log_msg('? need message target and message');
623 } else if (tokens[0] == ':reconnect') {
624 if (tokens.length > 1) {
625 server.reconnect_to(tokens[1]);
630 tui.log_msg('? unknown command');
633 server.send(['ALL', tui.inputEl.value]);
635 } else if (tui.inputEl.valuelength > 0) {
636 server.send(['ALL', tui.inputEl.value]);
640 } else if (tui.mode == mode_play) {
641 if (event.key === 'c') {
642 event.preventDefault();
643 tui.switch_mode(mode_chat);
644 } else if (event.key === 'e') {
645 event.preventDefault();
646 tui.switch_mode(mode_edit);
647 } else if (event.key === '?') {
648 tui.switch_mode(mode_study);
649 } else if (event.key === 'F1') {
651 } else if (event.key === 'f') {
652 server.send(["TASK:FLATTEN_SURROUNDINGS"]);
653 } else if (event.key === tui.key_left) {
654 server.send(['TASK:MOVE', 'LEFT']);
655 } else if (event.key === tui.key_right) {
656 server.send(['TASK:MOVE', 'RIGHT']);
657 } else if (event.key === tui.key_up) {
658 server.send(['TASK:MOVE', 'UP']);
659 } else if (event.key === tui.key_down) {
660 server.send(['TASK:MOVE', 'DOWN']);
662 } else if (tui.mode == mode_study) {
663 if (event.key === 'c') {
664 tui.switch_mode(mode_chat);
665 } else if (event.key == 'p') {
666 tui.switch_mode(mode_play);
667 } else if (event.key === 'P') {
668 event.preventDefault();
669 tui.switch_mode(mode_portal);
670 } else if (event.key === tui.key_left) {
671 explorer.move('left');
672 } else if (event.key === tui.key_right) {
673 explorer.move('right');
674 } else if (event.key === tui.key_up) {
676 } else if (event.key === tui.key_down) {
677 explorer.move('down');
678 } else if (event.key === 'e') {
679 event.preventDefault();
680 tui.switch_mode(mode_annotate);
685 wasd_selector.addEventListener('input', function() {
688 rows_selector.addEventListener('input', function() {
689 if (rows_selector.value % 2 != 0) {
692 terminal.initialize();
695 cols_selector.addEventListener('input', function() {
696 if (cols_selector.value % 4 != 0) {
699 terminal.initialize();
700 tui.window_width = terminal.cols / 2,
703 window.setInterval(function() {
704 if (!(['input', 'n_cols', 'n_rows', 'WASD_selector'].includes(document.activeElement.id))) {