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));
137 init: function(url) {
139 this.websocket = new WebSocket(this.url);
140 this.websocket.onopen = function(event) {
141 window.setInterval(function() { server.send(['PING']) }, 30000);
142 tui.log_msg("@ server connected! :)");
145 this.websocket.onclose = function(event) {
146 tui.log_msg("@ server disconnected :(");
147 tui.log_msg("@ hint: try the ':reconnect' command");
149 this.websocket.onmessage = this.handle_event;
151 reconnect: function() {
152 this.reconnect_to(this.url);
154 reconnect_to: function(url) {
155 this.websocket.close();
158 send: function(tokens) {
159 this.websocket.send(unparser.untokenize(tokens));
161 handle_event: function(event) {
162 let tokens = parser.tokenize(event.data)[0];
163 if (tokens[0] === 'TURN') {
166 game.turn = parseInt(tokens[1]);
167 } else if (tokens[0] === 'THING_POS') {
168 game.things[tokens[1]] = parser.parse_yx(tokens[2]);
169 } else if (tokens[0] === 'MAP') {
170 game.map_size = parser.parse_yx(tokens[1]);
172 } else if (tokens[0] === 'GAME_STATE_COMPLETE') {
173 explorer.empty_info_db();
174 if (tui.mode == mode_study) {
175 explorer.query_info();
177 let player_position = [0,0];
178 for (const thing_id in game.things) {
179 if (game.player_id == thing_id) {
180 let t = game.things[thing_id];
184 if (player_position in game.portals) {
185 tui.teleport_target = game.portals[player_position];
186 tui.switch_mode(mode_teleport);
190 } else if (tokens[0] === 'CHAT') {
191 tui.log_msg('# ' + tokens[1], 1);
192 } else if (tokens[0] === 'PLAYER_ID') {
193 game.player_id = parseInt(tokens[1]);
194 } else if (tokens[0] === 'LOGIN_OK') {
195 this.send(['GET_GAMESTATE']);
197 tui.switch_mode(mode_play);
198 } else if (tokens[0] === 'PORTAL') {
199 let position = parser.parse_yx(tokens[1]);
200 game.portals[position] = tokens[2];
201 } else if (tokens[0] === 'ANNOTATION') {
202 let position = parser.parse_yx(tokens[1]);
203 explorer.update_info_db(position, tokens[2]);
204 } else if (tokens[0] === 'UNHANDLED_INPUT') {
205 tui.log_msg('? unknown command');
206 } else if (tokens[0] === 'PLAY_ERROR') {
207 terminal.blink_screen();
208 } else if (tokens[0] === 'ARGUMENT_ERROR') {
209 tui.log_msg('? syntax error: ' + tokens[1]);
210 } else if (tokens[0] === 'GAME_ERROR') {
211 tui.log_msg('? game error: ' + tokens[1]);
212 } else if (tokens[0] === 'PONG') {
215 tui.log_msg('? unhandled input: ' + event.data);
221 quote: function(str) {
223 for (let i = 0; i < str.length; i++) {
225 if (['"', '\\'].includes(c)) {
231 return quoted.join('');
233 to_yx: function(yx_coordinate) {
234 return "Y:" + yx_coordinate[0] + ",X:" + yx_coordinate[1];
236 untokenize: function(tokens) {
237 let quoted_tokens = [];
238 for (let token of tokens) {
239 quoted_tokens.push(this.quote(token));
241 return quoted_tokens.join(" ");
246 constructor(name, has_input_prompt=false, shows_info=false, is_intro=false) {
248 this.has_input_prompt = has_input_prompt;
249 this.shows_info= shows_info;
250 this.is_intro = is_intro;
253 let mode_waiting_for_server = new Mode('waiting_for_server', false, false, true);
254 let mode_login = new Mode('login', true, false, true);
255 let mode_chat = new Mode('chat / write messages to players', true, false);
256 let mode_annotate = new Mode('add message to map tile', true, true);
257 let mode_play = new Mode('play / move around', false, false);
258 let mode_study = new Mode('check map tiles for messages', false, true);
259 let mode_edit = new Mode('write ASCII char to map tile', false, false);
260 let mode_teleport = new Mode('teleport away?');
261 let mode_portal = new Mode('add portal to map tile', true, true);
264 mode: mode_waiting_for_server,
268 window_width: terminal.cols / 2,
273 this.inputEl = document.getElementById("input");
274 this.inputEl.focus();
275 this.recalc_input_lines();
276 this.height_header = this.height_turn_line + this.height_mode_line;
277 this.log_msg("@ waiting for server connection ...");
280 init_wasd: function() {
281 if (wasd_selector.value == 'w, a, s, d') {
286 } else if (wasd_selector.value == 'arrow keys') {
287 tui.key_up = 'ArrowUp';
288 tui.key_down = 'ArrowDown';
289 tui.key_left = 'ArrowLeft';
290 tui.key_right = 'ArrowRight';
292 tui.movement_keys_desc = wasd_selector.value;
294 init_login: function() {
295 this.log_msg("@ please enter your username:");
296 this.switch_mode(mode_login);
298 switch_mode: function(mode, keep_pos=false) {
299 if (mode == mode_study && !keep_pos) {
300 explorer.position = game.things[game.player_id];
304 if (mode == mode_annotate && explorer.position in explorer.info_db) {
305 let info = explorer.info_db[explorer.position];
306 if (info != "(none)") {
307 this.inputEl.value = info;
308 this.recalc_input_lines();
311 if (mode == mode_portal && explorer.position in game.portals) {
312 let portal = game.portals[explorer.position]
313 this.inputEl.value = portal;
314 this.recalc_input_lines();
315 } else if (mode == mode_teleport) {
316 tui.log_msg("Type Y or y to affirm teleportation, any other key to abort.");
317 tui.log_msg("target: " + tui.teleport_target);
321 empty_input: function(str) {
322 this.inputEl.value = "";
323 if (this.mode.has_input_prompt) {
324 this.recalc_input_lines();
326 this.height_input = 0;
329 recalc_input_lines: function() {
330 this.input_lines = this.msg_into_lines_of_width(this.input_prompt + this.inputEl.value, this.window_width);
331 this.height_input = this.input_lines.length;
333 msg_into_lines_of_width: function(msg, width) {
336 for (let i = 0, x = 0; i < msg.length; i++, x++) {
337 if (x >= width || msg[i] == "\n") {
342 if (msg[i] != "\n") {
349 log_msg: function(msg) {
351 while (this.log.length > terminal.rows * 4) {
356 log_help: function() {
357 this.log_msg("HELP:");
358 this.log_msg("chat mode commands:");
359 this.log_msg(" :nick NAME - re-name yourself to NAME");
360 this.log_msg(" :msg USER TEXT - send TEXT to USER");
361 this.log_msg(" :help - show this help");
362 this.log_msg(" :p or :play - switch to play mode");
363 this.log_msg(" :? or :study - switch to study mode");
364 this.log_msg("commands common to study and play mode:");
365 this.log_msg(" " + this.movement_keys_desc + " - move");
366 this.log_msg(" c - switch to chat mode");
367 this.log_msg("commands specific to play mode:");
368 this.log_msg(" e - write following ASCII character");
369 this.log_msg(" f - flatten surroundings");
370 this.log_msg(" ? - switch to study mode");
371 this.log_msg("commands specific to study mode:");
372 this.log_msg(" e - annotate terrain");
373 this.log_msg(" p - switch to play mode");
375 draw_map: function() {
378 for (let i = 0, j = 0; i < game.map.length; i++, j++) {
379 if (j == game.map_size[1]) {
380 map_lines.push(line);
384 line.push(game.map[i]);
386 map_lines.push(line);
387 let center_pos = [Math.floor(game.map_size[0] / 2),
388 Math.floor(game.map_size[1] / 2)];
389 for (const thing_id in game.things) {
390 let t = game.things[thing_id];
391 map_lines[t[0]][t[1]] = '@';
392 if (game.player_id == thing_id) {
396 if (tui.mode.shows_info) {
397 map_lines[explorer.position[0]][explorer.position[1]] = '?';
398 center_pos = explorer.position;
400 let offset = [(terminal.rows / 2) - center_pos[0],
401 this.window_width / 2 - center_pos[1]];
402 for (let term_y = offset[0], map_y = 0;
403 term_y < terminal.rows && map_y < game.map_size[0];
406 let to_draw = map_lines[map_y].join('').slice(0, this.window_width - offset[1]);
407 terminal.write(term_y, offset[1], to_draw);
411 draw_mode_line: function() {
412 terminal.write(0, this.window_width, 'MODE: ' + this.mode.name);
414 draw_turn_line: function(n) {
415 terminal.write(1, this.window_width, 'TURN: ' + game.turn);
417 draw_history: function() {
418 let log_display_lines = [];
419 for (let line of this.log) {
420 log_display_lines = log_display_lines.concat(this.msg_into_lines_of_width(line, this.window_width));
422 for (let y = terminal.rows - 1 - this.height_input,
423 i = log_display_lines.length - 1;
424 y >= this.height_header && i >= 0;
426 terminal.write(y, this.window_width, log_display_lines[i]);
429 draw_info: function() {
430 let lines = this.msg_into_lines_of_width(explorer.get_info(), this.window_width);
431 for (let y = this.height_header, i = 0; y < terminal.rows && i < lines.length; y++, i++) {
432 terminal.write(y, this.window_width, lines[i]);
435 draw_input: function() {
436 if (this.mode.has_input_prompt) {
437 for (let y = terminal.rows - this.height_input, i = 0; y < terminal.rows && i < this.input_lines.length; y++, i++) {
438 terminal.write(y, this.window_width, this.input_lines[i]);
442 full_refresh: function() {
443 terminal.drawBox(0, 0, terminal.rows, terminal.cols);
444 if (this.mode.is_intro) {
449 this.draw_turn_line();
450 this.draw_mode_line();
451 if (this.mode.shows_info) {
467 this.map_size = [0,0];
476 server.init(websocket_location);
481 move: function(direction) {
483 try_pos[0] = this.position[0];
484 try_pos[1] = this.position[1];
485 if (direction == 'left') {
487 } else if (direction == 'right') {
489 } else if (direction == 'up') {
491 } else if (direction == 'down') {
494 if (!(try_pos[0] < 0) &&
496 !(try_pos[0] >= game.map_size[0])
497 && !(try_pos[1] >= game.map_size[1])) {
498 this.position = try_pos;
503 update_info_db: function(yx, str) {
504 this.info_db[yx] = str;
505 if (tui.mode == mode_study) {
509 empty_info_db: function() {
511 if (tui.mode == mode_study) {
515 query_info: function() {
516 server.send(["GET_ANNOTATION", unparser.to_yx(explorer.position)]);
518 get_info: function() {
520 if (this.position in game.portals) {
521 info += "PORTAL: " + game.portals[this.position] + "\n";
523 if (this.position in this.info_db) {
524 info += "ANNOTATIONS: " + this.info_db[this.position];
530 annotate: function(msg) {
531 if (msg.length == 0) {
532 msg = " "; // triggers annotation deletion
534 server.send(["ANNOTATE", unparser.to_yx(explorer.position), msg]);
536 set_portal: function(msg) {
537 if (msg.length == 0) {
538 msg = " "; // triggers portal deletion
540 server.send(["PORTAL", unparser.to_yx(explorer.position), msg]);
544 tui.inputEl.addEventListener('input', (event) => {
545 if (tui.mode.has_input_prompt) {
546 let max_length = tui.window_width * terminal.rows - tui.input_prompt.length;
547 if (tui.inputEl.value.length > max_length) {
548 tui.inputEl.value = tui.inputEl.value.slice(0, max_length);
550 tui.recalc_input_lines();
552 } else if (tui.mode == mode_edit && tui.inputEl.value.length > 0) {
553 server.send(["TASK:WRITE", tui.inputEl.value[0]]);
554 tui.switch_mode(mode_play);
555 } else if (tui.mode == mode_teleport) {
556 if (['Y', 'y'].includes(tui.inputEl.value[0])) {
557 server.reconnect_to(tui.teleport_target);
559 tui.switch_mode(mode_play);
563 tui.inputEl.addEventListener('keydown', (event) => {
564 if (event.key == 'Enter') {
565 event.preventDefault();
567 if (tui.mode == mode_login && event.key == 'Enter') {
568 server.send(['LOGIN', tui.inputEl.value]);
569 tui.switch_mode(mode_login);
570 } else if (tui.mode == mode_portal && event.key == 'Enter') {
571 explorer.set_portal(tui.inputEl.value);
572 tui.switch_mode(mode_study, true);
573 } else if (tui.mode == mode_annotate && event.key == 'Enter') {
574 explorer.annotate(tui.inputEl.value);
575 tui.switch_mode(mode_study, true);
576 } else if (tui.mode == mode_chat && event.key == 'Enter') {
577 let [tokens, token_starts] = parser.tokenize(tui.inputEl.value);
578 if (tokens.length > 0 && tokens[0].length > 0) {
579 if (tokens[0][0] == ':') {
580 if (tokens[0] == ':play' || tokens[0] == ':p') {
581 tui.switch_mode(mode_play);
582 } else if (tokens[0] == ':study' || tokens[0] == ':?') {
583 tui.switch_mode(mode_study);
584 } else if (tokens[0] == ':help') {
586 } else if (tokens[0] == ':nick') {
587 if (tokens.length > 1) {
588 server.send(['LOGIN', tokens[1]]);
590 tui.log_msg('? need login name');
592 } else if (tokens[0] == ':msg') {
593 if (tokens.length > 2) {
594 let msg = tui.inputEl.value.slice(token_starts[2]);
595 server.send(['QUERY', tokens[1], msg]);
597 tui.log_msg('? need message target and message');
599 } else if (tokens[0] == ':reconnect') {
600 if (tokens.length > 1) {
601 server.reconnect_to(tokens[1]);
606 tui.log_msg('? unknown command');
609 server.send(['ALL', tui.inputEl.value]);
611 } else if (tui.inputEl.valuelength > 0) {
612 server.send(['ALL', tui.inputEl.value]);
616 } else if (tui.mode == mode_play) {
617 if (event.key === 'c') {
618 event.preventDefault();
619 tui.switch_mode(mode_chat);
620 } else if (event.key === 'e') {
621 event.preventDefault();
622 tui.switch_mode(mode_edit);
623 } else if (event.key === '?') {
624 tui.switch_mode(mode_study);
625 } else if (event.key === 'F1') {
627 } else if (event.key === 'f') {
628 server.send(["TASK:FLATTEN_SURROUNDINGS"]);
629 } else if (event.key === tui.key_left) {
630 server.send(['TASK:MOVE', 'LEFT']);
631 } else if (event.key === tui.key_right) {
632 server.send(['TASK:MOVE', 'RIGHT']);
633 } else if (event.key === tui.key_up) {
634 server.send(['TASK:MOVE', 'UP']);
635 } else if (event.key === tui.key_down) {
636 server.send(['TASK:MOVE', 'DOWN']);
638 } else if (tui.mode == mode_study) {
639 if (event.key === 'c') {
640 tui.switch_mode(mode_chat);
641 } else if (event.key == 'p') {
642 tui.switch_mode(mode_play);
643 } else if (event.key === 'P') {
644 event.preventDefault();
645 tui.switch_mode(mode_portal);
646 } else if (event.key === tui.key_left) {
647 explorer.move('left');
648 } else if (event.key === tui.key_right) {
649 explorer.move('right');
650 } else if (event.key === tui.key_up) {
652 } else if (event.key === tui.key_down) {
653 explorer.move('down');
654 } else if (event.key === 'e') {
655 event.preventDefault();
656 tui.switch_mode(mode_annotate);
661 wasd_selector.addEventListener('input', function() {
664 rows_selector.addEventListener('input', function() {
665 if (rows_selector.value % 2 != 0) {
668 terminal.initialize();
671 cols_selector.addEventListener('input', function() {
672 if (cols_selector.value % 4 != 0) {
675 terminal.initialize();
676 tui.window_width = terminal.cols / 2,
679 window.setInterval(function() {
680 if (!(['input', 'n_cols', 'n_rows', 'WASD_selector'].includes(document.activeElement.id))) {