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 />
13 command character: <select id="command_char"" >
14 <option value=":" selected>:</option>
15 <option value="/">/</option>
18 <pre id="terminal" style="display: inline-block;"></pre>
19 <textarea id="input" style="opacity: 0; width: 0px;"></textarea>
22 let websocket_location = "ws://localhost:8000";
24 let wasd_selector = document.getElementById("WASD_selector");
25 let rows_selector = document.getElementById("n_rows");
26 let cols_selector = document.getElementById("n_cols");
27 let command_char_selector = document.getElementById("command_char");
32 initialize: function() {
33 this.rows = rows_selector.value;
34 this.cols = cols_selector.value;
35 this.pre_el = document.getElementById("terminal");
36 this.pre_el.style.color = this.foreground;
37 this.pre_el.style.backgroundColor = this.background;
40 for (let y = 0, x = 0; y <= this.rows; x++) {
44 this.content.push(line);
53 blink_screen: function() {
54 this.pre_el.style.color = this.background;
55 this.pre_el.style.backgroundColor = this.foreground;
57 this.pre_el.style.color = this.foreground;
58 this.pre_el.style.backgroundColor = this.background;
63 for (let y = 0; y < this.rows; y++) {
64 let line = this.content[y].join('');
65 pre_string += line + '\n';
67 this.pre_el.textContent = pre_string;
69 write: function(start_y, start_x, msg) {
70 for (let x = start_x, i = 0; x < this.cols && i < msg.length; x++, i++) {
71 this.content[start_y][x] = msg[i];
74 drawBox: function(start_y, start_x, height, width) {
75 let end_y = start_y + height;
76 let end_x = start_x + width;
77 for (let y = start_y, x = start_x; y < this.rows; x++) {
85 this.content[y][x] = ' ';
89 terminal.initialize();
92 tokenize: function(str) {
98 for (let i = 0; i < str.length; i++) {
104 } else if (c == '\\') {
106 } else if (c == '"') {
111 } else if (c == '"') {
113 } else if (c === ' ') {
114 if (token.length > 0) {
123 if (token.length > 0) {
126 let token_starts = [];
127 for (let i = 0; i < token_ends.length; i++) {
128 token_starts.push(token_ends[i] - tokens[i].length);
130 return [tokens, token_starts];
132 parse_yx: function(position_string) {
133 let coordinate_strings = position_string.split(',')
134 let position = [0, 0];
135 position[0] = parseInt(coordinate_strings[0].slice(2));
136 position[1] = parseInt(coordinate_strings[1].slice(2));
148 init: function(url) {
150 this.websocket = new WebSocket(this.url);
151 this.websocket.onopen = function(event) {
152 window.setInterval(function() { server.send(['PING']) }, 30000);
153 tui.log_msg("@ server connected! :)");
156 this.websocket.onclose = function(event) {
157 tui.log_msg("@ server disconnected :(");
158 tui.log_msg("@ hint: try the '" + command_char_selector.value + "reconnect' command");
160 this.websocket.onmessage = this.handle_event;
162 reconnect: function() {
163 this.reconnect_to(this.url);
165 reconnect_to: function(url) {
166 this.websocket.close();
169 send: function(tokens) {
170 this.websocket.send(unparser.untokenize(tokens));
172 handle_event: function(event) {
173 let tokens = parser.tokenize(event.data)[0];
174 if (tokens[0] === 'TURN') {
177 game.turn = parseInt(tokens[1]);
178 } else if (tokens[0] === 'THING_POS') {
179 game.get_thing(tokens[1], true).position = parser.parse_yx(tokens[2]);
180 } else if (tokens[0] === 'THING_NAME') {
181 game.get_thing(tokens[1], true).name_ = tokens[2];
182 } else if (tokens[0] === 'MAP') {
183 game.map_size = parser.parse_yx(tokens[1]);
185 } else if (tokens[0] === 'GAME_STATE_COMPLETE') {
186 explorer.empty_info_db();
187 if (tui.mode == mode_study) {
188 explorer.query_info();
190 let t = game.get_thing(game.player_id);
191 if (t.position in game.portals) {
192 tui.teleport_target = game.portals[t.position];
193 tui.switch_mode(mode_teleport);
197 } else if (tokens[0] === 'CHAT') {
198 tui.log_msg('# ' + tokens[1], 1);
199 } else if (tokens[0] === 'PLAYER_ID') {
200 game.player_id = parseInt(tokens[1]);
201 } else if (tokens[0] === 'LOGIN_OK') {
202 this.send(['GET_GAMESTATE']);
204 // TODO wait for game state for this switch, use intermediary mode?
205 tui.switch_mode(mode_play);
206 } else if (tokens[0] === 'PORTAL') {
207 let position = parser.parse_yx(tokens[1]);
208 game.portals[position] = tokens[2];
209 } else if (tokens[0] === 'ANNOTATION') {
210 let position = parser.parse_yx(tokens[1]);
211 explorer.update_info_db(position, tokens[2]);
212 } else if (tokens[0] === 'UNHANDLED_INPUT') {
213 tui.log_msg('? unknown command');
214 } else if (tokens[0] === 'PLAY_ERROR') {
215 terminal.blink_screen();
216 } else if (tokens[0] === 'ARGUMENT_ERROR') {
217 tui.log_msg('? syntax error: ' + tokens[1]);
218 } else if (tokens[0] === 'GAME_ERROR') {
219 tui.log_msg('? game error: ' + tokens[1]);
220 } else if (tokens[0] === 'PONG') {
223 tui.log_msg('? unhandled input: ' + event.data);
229 quote: function(str) {
231 for (let i = 0; i < str.length; i++) {
233 if (['"', '\\'].includes(c)) {
239 return quoted.join('');
241 to_yx: function(yx_coordinate) {
242 return "Y:" + yx_coordinate[0] + ",X:" + yx_coordinate[1];
244 untokenize: function(tokens) {
245 let quoted_tokens = [];
246 for (let token of tokens) {
247 quoted_tokens.push(this.quote(token));
249 return quoted_tokens.join(" ");
254 constructor(name, has_input_prompt=false, shows_info=false, is_intro=false) {
256 this.has_input_prompt = has_input_prompt;
257 this.shows_info= shows_info;
258 this.is_intro = is_intro;
261 let mode_waiting_for_server = new Mode('waiting_for_server', false, false, true);
262 let mode_login = new Mode('login', true, false, true);
263 let mode_chat = new Mode('chat / write messages to players', true, false);
264 let mode_annotate = new Mode('add message to map tile', true, true);
265 let mode_play = new Mode('play / move around', false, false);
266 let mode_study = new Mode('check map tiles for messages', false, true);
267 let mode_edit = new Mode('write ASCII char to map tile', false, false);
268 let mode_teleport = new Mode('teleport away?');
269 let mode_portal = new Mode('add portal to map tile', true, true);
272 mode: mode_waiting_for_server,
276 window_width: terminal.cols / 2,
281 this.inputEl = document.getElementById("input");
282 this.inputEl.focus();
283 this.recalc_input_lines();
284 this.height_header = this.height_turn_line + this.height_mode_line;
285 this.log_msg("@ waiting for server connection ...");
288 init_wasd: function() {
289 if (wasd_selector.value == 'w, a, s, d') {
294 } else if (wasd_selector.value == 'arrow keys') {
295 tui.key_up = 'ArrowUp';
296 tui.key_down = 'ArrowDown';
297 tui.key_left = 'ArrowLeft';
298 tui.key_right = 'ArrowRight';
300 tui.movement_keys_desc = wasd_selector.value;
302 init_login: function() {
303 this.log_msg("@ please enter your username:");
304 this.switch_mode(mode_login);
306 switch_mode: function(mode, keep_pos=false) {
307 if (mode == mode_study && !keep_pos) {
308 explorer.position = game.things[game.player_id].position;
312 if (mode == mode_annotate && explorer.position in explorer.info_db) {
313 let info = explorer.info_db[explorer.position];
314 if (info != "(none)") {
315 this.inputEl.value = info;
316 this.recalc_input_lines();
319 if (mode == mode_portal && explorer.position in game.portals) {
320 let portal = game.portals[explorer.position]
321 this.inputEl.value = portal;
322 this.recalc_input_lines();
323 } else if (mode == mode_teleport) {
324 tui.log_msg("@ May teleport to: " + tui.teleport_target);
325 tui.log_msg("@ Type Y or y to affirm, other keys to abort.");
329 empty_input: function(str) {
330 this.inputEl.value = "";
331 if (this.mode.has_input_prompt) {
332 this.recalc_input_lines();
334 this.height_input = 0;
337 recalc_input_lines: function() {
338 this.input_lines = this.msg_into_lines_of_width(this.input_prompt + this.inputEl.value, this.window_width);
339 this.height_input = this.input_lines.length;
341 msg_into_lines_of_width: function(msg, width) {
344 for (let i = 0, x = 0; i < msg.length; i++, x++) {
345 if (x >= width || msg[i] == "\n") {
350 if (msg[i] != "\n") {
357 log_msg: function(msg) {
359 while (this.log.length > 100) {
364 log_help: function() {
365 this.log_msg("HELP:");
366 this.log_msg("chat mode commands:");
367 this.log_msg(" " + command_char_selector.value + "nick NAME - re-name yourself to NAME");
368 this.log_msg(" " + command_char_selector.value + "msg USER TEXT - send TEXT to USER");
369 this.log_msg(" " + command_char_selector.value + "help - show this help");
370 this.log_msg(" " + command_char_selector.value + "p or " + command_char_selector.value + "play - switch to play mode");
371 this.log_msg(" " + command_char_selector.value + "? or " + command_char_selector.value + "study - switch to study mode");
372 this.log_msg("commands common to study and play mode:");
373 this.log_msg(" " + this.movement_keys_desc + " - move");
374 this.log_msg(" c - switch to chat mode");
375 this.log_msg("commands specific to play mode:");
376 this.log_msg(" e - write following ASCII character");
377 this.log_msg(" f - flatten surroundings");
378 this.log_msg(" ? - switch to study mode");
379 this.log_msg("commands specific to study mode:");
380 this.log_msg(" e - annotate terrain");
381 this.log_msg(" p - switch to play mode");
383 draw_map: function() {
386 for (let i = 0, j = 0; i < game.map.length; i++, j++) {
387 if (j == game.map_size[1]) {
388 map_lines.push(line);
392 line.push(game.map[i]);
394 map_lines.push(line);
395 let center_pos = [Math.floor(game.map_size[0] / 2),
396 Math.floor(game.map_size[1] / 2)];
397 for (const thing_id in game.things) {
398 let t = game.things[thing_id];
399 map_lines[t.position[0]][t.position[1]] = '@';
400 if (game.player_id == thing_id) {
401 center_pos = t.position;
404 if (tui.mode.shows_info) {
405 map_lines[explorer.position[0]][explorer.position[1]] = '?';
406 center_pos = explorer.position;
408 let offset = [(terminal.rows / 2) - center_pos[0],
409 this.window_width / 2 - center_pos[1]];
410 for (let term_y = offset[0], map_y = 0;
411 term_y < terminal.rows && map_y < game.map_size[0];
414 let to_draw = map_lines[map_y].join('').slice(0, this.window_width - offset[1]);
415 terminal.write(term_y, offset[1], to_draw);
419 draw_mode_line: function() {
420 terminal.write(0, this.window_width, 'MODE: ' + this.mode.name);
422 draw_turn_line: function(n) {
423 terminal.write(1, this.window_width, 'TURN: ' + game.turn);
425 draw_history: function() {
426 let log_display_lines = [];
427 for (let line of this.log) {
428 log_display_lines = log_display_lines.concat(this.msg_into_lines_of_width(line, this.window_width));
430 for (let y = terminal.rows - 1 - this.height_input,
431 i = log_display_lines.length - 1;
432 y >= this.height_header && i >= 0;
434 terminal.write(y, this.window_width, log_display_lines[i]);
437 draw_info: function() {
438 let lines = this.msg_into_lines_of_width(explorer.get_info(), this.window_width);
439 for (let y = this.height_header, i = 0; y < terminal.rows && i < lines.length; y++, i++) {
440 terminal.write(y, this.window_width, lines[i]);
443 draw_input: function() {
444 if (this.mode.has_input_prompt) {
445 for (let y = terminal.rows - this.height_input, i = 0; i < this.input_lines.length; y++, i++) {
446 terminal.write(y, this.window_width, this.input_lines[i]);
450 full_refresh: function() {
451 terminal.drawBox(0, 0, terminal.rows, terminal.cols);
452 if (this.mode.is_intro) {
457 this.draw_turn_line();
458 this.draw_mode_line();
459 if (this.mode.shows_info) {
475 this.map_size = [0,0];
479 get_thing: function(id_, create_if_not_found=false) {
480 if (id_ in game.things) {
481 return game.things[id_];
482 } else if (create_if_not_found) {
483 let t = new Thing([0,0]);
484 game.things[id_] = t;
493 server.init(websocket_location);
498 move: function(direction) {
500 try_pos[0] = this.position[0];
501 try_pos[1] = this.position[1];
502 if (direction == 'left') {
504 } else if (direction == 'right') {
506 } else if (direction == 'up') {
508 } else if (direction == 'down') {
511 if (!(try_pos[0] < 0) &&
513 !(try_pos[0] >= game.map_size[0])
514 && !(try_pos[1] >= game.map_size[1])) {
515 this.position = try_pos;
520 update_info_db: function(yx, str) {
521 this.info_db[yx] = str;
522 if (tui.mode == mode_study) {
526 empty_info_db: function() {
528 if (tui.mode == mode_study) {
532 query_info: function() {
533 server.send(["GET_ANNOTATION", unparser.to_yx(explorer.position)]);
535 get_info: function() {
537 let position_i = this.position[0] * game.map_size[1] + this.position[1];
538 info += "TERRAIN: " + game.map[position_i] + "\n";
539 for (let t_id in game.things) {
540 let t = game.things[t_id];
541 if (t.position[0] == this.position[0] && t.position[1] == this.position[1]) {
544 info += ": " + t.name_;
549 if (this.position in game.portals) {
550 info += "PORTAL: " + game.portals[this.position] + "\n";
552 if (this.position in this.info_db) {
553 info += "ANNOTATIONS: " + this.info_db[this.position];
559 annotate: function(msg) {
560 if (msg.length == 0) {
561 msg = " "; // triggers annotation deletion
563 server.send(["ANNOTATE", unparser.to_yx(explorer.position), msg]);
565 set_portal: function(msg) {
566 if (msg.length == 0) {
567 msg = " "; // triggers portal deletion
569 server.send(["PORTAL", unparser.to_yx(explorer.position), msg]);
573 tui.inputEl.addEventListener('input', (event) => {
574 if (tui.mode.has_input_prompt) {
575 let max_length = tui.window_width * terminal.rows - tui.input_prompt.length;
576 if (tui.inputEl.value.length > max_length) {
577 tui.inputEl.value = tui.inputEl.value.slice(0, max_length);
579 tui.recalc_input_lines();
581 } else if (tui.mode == mode_edit && tui.inputEl.value.length > 0) {
582 server.send(["TASK:WRITE", tui.inputEl.value[0]]);
583 tui.switch_mode(mode_play);
584 } else if (tui.mode == mode_teleport) {
585 if (['Y', 'y'].includes(tui.inputEl.value[0])) {
586 server.reconnect_to(tui.teleport_target);
588 tui.log_msg("@ teleportation aborted");
589 tui.switch_mode(mode_play);
593 tui.inputEl.addEventListener('keydown', (event) => {
594 if (event.key == 'Enter') {
595 event.preventDefault();
597 if (tui.mode == mode_login && event.key == 'Enter') {
598 server.send(['LOGIN', tui.inputEl.value]);
599 tui.switch_mode(mode_login);
600 } else if (tui.mode == mode_portal && event.key == 'Enter') {
601 explorer.set_portal(tui.inputEl.value);
602 tui.switch_mode(mode_study, true);
603 } else if (tui.mode == mode_annotate && event.key == 'Enter') {
604 explorer.annotate(tui.inputEl.value);
605 tui.switch_mode(mode_study, true);
606 } else if (tui.mode == mode_chat && event.key == 'Enter') {
607 let [tokens, token_starts] = parser.tokenize(tui.inputEl.value);
608 if (tokens.length > 0 && tokens[0].length > 0) {
609 if (tui.inputEl.value[0][0] == command_char_selector.value) {
610 if (tokens[0].slice(1) == 'play' || tokens[0].slice(1) == 'p') {
611 tui.switch_mode(mode_play);
612 } else if (tokens[0].slice(1) == 'study' || tokens[0].slice(1) == '?') {
613 tui.switch_mode(mode_study);
614 } else if (tokens[0].slice(1) == 'help') {
616 } else if (tokens[0].slice(1) == 'nick') {
617 if (tokens.length > 1) {
618 server.send(['LOGIN', tokens[1]]);
620 tui.log_msg('? need login name');
622 } else if (tokens[0].slice(1) == 'msg') {
623 if (tokens.length > 2) {
624 let msg = tui.inputEl.value.slice(token_starts[2]);
625 server.send(['QUERY', tokens[1], msg]);
627 tui.log_msg('? need message target and message');
629 } else if (tokens[0].slice(1) == 'reconnect') {
630 if (tokens.length > 1) {
631 server.reconnect_to(tokens[1]);
636 tui.log_msg('? unknown command');
639 server.send(['ALL', tui.inputEl.value]);
641 } else if (tui.inputEl.valuelength > 0) {
642 server.send(['ALL', tui.inputEl.value]);
646 } else if (tui.mode == mode_play) {
647 if (event.key === 'c') {
648 event.preventDefault();
649 tui.switch_mode(mode_chat);
650 } else if (event.key === 'e') {
651 event.preventDefault();
652 tui.switch_mode(mode_edit);
653 } else if (event.key === '?') {
654 tui.switch_mode(mode_study);
655 } else if (event.key === 'F1') {
657 } else if (event.key === 'f') {
658 server.send(["TASK:FLATTEN_SURROUNDINGS"]);
659 } else if (event.key === tui.key_left) {
660 server.send(['TASK:MOVE', 'LEFT']);
661 } else if (event.key === tui.key_right) {
662 server.send(['TASK:MOVE', 'RIGHT']);
663 } else if (event.key === tui.key_up) {
664 server.send(['TASK:MOVE', 'UP']);
665 } else if (event.key === tui.key_down) {
666 server.send(['TASK:MOVE', 'DOWN']);
668 } else if (tui.mode == mode_study) {
669 if (event.key === 'c') {
670 event.preventDefault();
671 tui.switch_mode(mode_chat);
672 } else if (event.key == 'p') {
673 tui.switch_mode(mode_play);
674 } else if (event.key === 'P') {
675 event.preventDefault();
676 tui.switch_mode(mode_portal);
677 } else if (event.key === tui.key_left) {
678 explorer.move('left');
679 } else if (event.key === tui.key_right) {
680 explorer.move('right');
681 } else if (event.key === tui.key_up) {
683 } else if (event.key === tui.key_down) {
684 explorer.move('down');
685 } else if (event.key === 'e') {
686 event.preventDefault();
687 tui.switch_mode(mode_annotate);
692 wasd_selector.addEventListener('input', function() {
695 rows_selector.addEventListener('input', function() {
696 if (rows_selector.value % 2 != 0) {
699 terminal.initialize();
702 cols_selector.addEventListener('input', function() {
703 if (cols_selector.value % 4 != 0) {
706 terminal.initialize();
707 tui.window_width = terminal.cols / 2,
710 window.setInterval(function() {
711 if (!(['input', 'n_cols', 'n_rows', 'WASD_selector', 'command_char'].includes(document.activeElement.id))) {