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?');
263 mode: mode_waiting_for_server,
267 window_width: terminal.cols / 2,
272 this.inputEl = document.getElementById("input");
273 this.inputEl.focus();
274 this.recalc_input_lines();
275 this.height_header = this.height_turn_line + this.height_mode_line;
276 this.log_msg("@ waiting for server connection ...");
279 init_wasd: function() {
280 if (wasd_selector.value == 'w, a, s, d') {
285 } else if (wasd_selector.value == 'arrow keys') {
286 tui.key_up = 'ArrowUp';
287 tui.key_down = 'ArrowDown';
288 tui.key_left = 'ArrowLeft';
289 tui.key_right = 'ArrowRight';
291 tui.movement_keys_desc = wasd_selector.value;
293 init_login: function() {
294 this.log_msg("@ please enter your username:");
295 this.switch_mode(mode_login);
297 switch_mode: function(mode, keep_pos=false) {
298 if (mode == mode_study && !keep_pos) {
299 explorer.position = game.things[game.player_id];
303 if (mode == mode_annotate && explorer.position in explorer.info_db) {
304 let info = explorer.info_db[explorer.position];
305 if (info != "(none)") {
306 this.inputEl.value = explorer.info_db[explorer.position];
307 this.recalc_input_lines();
309 } else if (mode == mode_teleport) {
310 tui.log_msg("Type Y or y to affirm teleportation, any other key to abort.");
311 tui.log_msg("target: " + tui.teleport_target);
315 empty_input: function(str) {
316 this.inputEl.value = "";
317 if (this.mode.has_input_prompt) {
318 this.recalc_input_lines();
320 this.height_input = 0;
323 recalc_input_lines: function() {
324 this.input_lines = this.msg_into_lines_of_width(this.input_prompt + this.inputEl.value, this.window_width);
325 this.height_input = this.input_lines.length;
327 msg_into_lines_of_width: function(msg, width) {
330 for (let i = 0, x = 0; i < msg.length; i++, x++) {
331 if (x >= width || msg[i] == "\n") {
336 if (msg[i] != "\n") {
343 log_msg: function(msg) {
345 while (this.log.length > terminal.rows * 4) {
350 log_help: function() {
351 this.log_msg("HELP:");
352 this.log_msg("chat mode commands:");
353 this.log_msg(" :nick NAME - re-name yourself to NAME");
354 this.log_msg(" :msg USER TEXT - send TEXT to USER");
355 this.log_msg(" :help - show this help");
356 this.log_msg(" :p or :play - switch to play mode");
357 this.log_msg(" :? or :study - switch to study mode");
358 this.log_msg("commands common to study and play mode:");
359 this.log_msg(" " + this.movement_keys_desc + " - move");
360 this.log_msg(" c - switch to chat mode");
361 this.log_msg("commands specific to play mode:");
362 this.log_msg(" e - write following ASCII character");
363 this.log_msg(" f - flatten surroundings");
364 this.log_msg(" ? - switch to study mode");
365 this.log_msg("commands specific to study mode:");
366 this.log_msg(" e - annotate terrain");
367 this.log_msg(" p - switch to play mode");
369 draw_map: function() {
372 for (let i = 0, j = 0; i < game.map.length; i++, j++) {
373 if (j == game.map_size[1]) {
374 map_lines.push(line);
378 line.push(game.map[i]);
380 map_lines.push(line);
381 let center_pos = [Math.floor(game.map_size[0] / 2),
382 Math.floor(game.map_size[1] / 2)];
383 for (const thing_id in game.things) {
384 let t = game.things[thing_id];
385 map_lines[t[0]][t[1]] = '@';
386 if (game.player_id == thing_id) {
390 if (tui.mode.shows_info) {
391 map_lines[explorer.position[0]][explorer.position[1]] = '?';
392 center_pos = explorer.position;
394 let offset = [(terminal.rows / 2) - center_pos[0],
395 this.window_width / 2 - center_pos[1]];
396 for (let term_y = offset[0], map_y = 0;
397 term_y < terminal.rows && map_y < game.map_size[0];
400 let to_draw = map_lines[map_y].join('').slice(0, this.window_width - offset[1]);
401 terminal.write(term_y, offset[1], to_draw);
405 draw_mode_line: function() {
406 terminal.write(0, this.window_width, 'MODE: ' + this.mode.name);
408 draw_turn_line: function(n) {
409 terminal.write(1, this.window_width, 'TURN: ' + game.turn);
411 draw_history: function() {
412 let log_display_lines = [];
413 for (let line of this.log) {
414 log_display_lines = log_display_lines.concat(this.msg_into_lines_of_width(line, this.window_width));
416 for (let y = terminal.rows - 1 - this.height_input,
417 i = log_display_lines.length - 1;
418 y >= this.height_header && i >= 0;
420 terminal.write(y, this.window_width, log_display_lines[i]);
423 draw_info: function() {
424 let lines = this.msg_into_lines_of_width(explorer.get_info(), this.window_width);
425 for (let y = this.height_header, i = 0; y < terminal.rows && i < lines.length; y++, i++) {
426 terminal.write(y, this.window_width, lines[i]);
429 draw_input: function() {
430 if (this.mode.has_input_prompt) {
431 for (let y = terminal.rows - this.height_input, i = 0; y < terminal.rows && i < this.input_lines.length; y++, i++) {
432 terminal.write(y, this.window_width, this.input_lines[i]);
436 full_refresh: function() {
437 terminal.drawBox(0, 0, terminal.rows, terminal.cols);
438 if (this.mode.is_intro) {
443 this.draw_turn_line();
444 this.draw_mode_line();
445 if (this.mode.shows_info) {
461 this.map_size = [0,0];
470 server.init(websocket_location);
475 move: function(direction) {
477 try_pos[0] = this.position[0];
478 try_pos[1] = this.position[1];
479 if (direction == 'left') {
481 } else if (direction == 'right') {
483 } else if (direction == 'up') {
485 } else if (direction == 'down') {
488 if (!(try_pos[0] < 0) &&
490 !(try_pos[0] >= game.map_size[0])
491 && !(try_pos[1] >= game.map_size[1])) {
492 this.position = try_pos;
497 update_info_db: function(yx, str) {
498 this.info_db[yx] = str;
499 if (tui.mode == mode_study) {
503 empty_info_db: function() {
505 if (tui.mode == mode_study) {
509 query_info: function() {
510 server.send(["GET_ANNOTATION", unparser.to_yx(explorer.position)]);
512 get_info: function() {
514 if (this.position in game.portals) {
515 info += "PORTAL: " + game.portals[this.position] + "\n";
517 if (this.position in this.info_db) {
518 info += "ANNOTATIONS: " + this.info_db[this.position];
524 annotate: function(msg) {
525 if (msg.length == 0) {
526 msg = " "; // triggers annotation deletion
528 server.send(["ANNOTATE", unparser.to_yx(explorer.position), msg]);
532 tui.inputEl.addEventListener('input', (event) => {
533 if (tui.mode.has_input_prompt) {
534 let max_length = tui.window_width * terminal.rows - tui.input_prompt.length;
535 if (tui.inputEl.value.length > max_length) {
536 tui.inputEl.value = tui.inputEl.value.slice(0, max_length);
538 tui.recalc_input_lines();
540 } else if (tui.mode == mode_edit && tui.inputEl.value.length > 0) {
541 server.send(["TASK:WRITE", tui.inputEl.value[0]]);
542 tui.switch_mode(mode_play);
543 } else if (tui.mode == mode_teleport) {
544 if (['Y', 'y'].includes(tui.inputEl.value[0])) {
545 server.reconnect_to(tui.teleport_target);
547 tui.switch_mode(mode_play);
551 tui.inputEl.addEventListener('keydown', (event) => {
552 if (event.key == 'Enter') {
553 event.preventDefault();
555 if (tui.mode == mode_login && event.key == 'Enter') {
556 server.send(['LOGIN', tui.inputEl.value]);
557 tui.switch_mode(mode_login);
558 } else if (tui.mode == mode_annotate && event.key == 'Enter') {
559 explorer.annotate(tui.inputEl.value);
560 tui.switch_mode(mode_study, true);
561 } else if (tui.mode == mode_chat && event.key == 'Enter') {
562 let [tokens, token_starts] = parser.tokenize(tui.inputEl.value);
563 if (tokens.length > 0 && tokens[0].length > 0) {
564 if (tokens[0][0] == ':') {
565 if (tokens[0] == ':play' || tokens[0] == ':p') {
566 tui.switch_mode(mode_play);
567 } else if (tokens[0] == ':study' || tokens[0] == ':?') {
568 tui.switch_mode(mode_study);
569 } else if (tokens[0] == ':help') {
571 } else if (tokens[0] == ':nick') {
572 if (tokens.length > 1) {
573 server.send(['LOGIN', tokens[1]]);
575 tui.log_msg('? need login name');
577 } else if (tokens[0] == ':msg') {
578 if (tokens.length > 2) {
579 let msg = tui.inputEl.value.slice(token_starts[2]);
580 server.send(['QUERY', tokens[1], msg]);
582 tui.log_msg('? need message target and message');
584 } else if (tokens[0] == ':reconnect') {
585 if (tokens.length > 1) {
586 server.reconnect_to(tokens[1]);
591 tui.log_msg('? unknown command');
594 server.send(['ALL', tui.inputEl.value]);
596 } else if (tui.inputEl.valuelength > 0) {
597 server.send(['ALL', tui.inputEl.value]);
601 } else if (tui.mode == mode_play) {
602 if (event.key === 'c') {
603 event.preventDefault();
604 tui.switch_mode(mode_chat);
605 } else if (event.key === 'e') {
606 event.preventDefault();
607 tui.switch_mode(mode_edit);
608 } else if (event.key === '?') {
609 tui.switch_mode(mode_study);
610 } else if (event.key === 'F1') {
612 } else if (event.key === 'f') {
613 server.send(["TASK:FLATTEN_SURROUNDINGS"]);
614 } else if (event.key === tui.key_left) {
615 server.send(['TASK:MOVE', 'LEFT']);
616 } else if (event.key === tui.key_right) {
617 server.send(['TASK:MOVE', 'RIGHT']);
618 } else if (event.key === tui.key_up) {
619 server.send(['TASK:MOVE', 'UP']);
620 } else if (event.key === tui.key_down) {
621 server.send(['TASK:MOVE', 'DOWN']);
623 } else if (tui.mode == mode_study) {
624 if (event.key === 'c') {
625 tui.switch_mode(mode_chat);
626 } else if (event.key == 'p') {
627 tui.switch_mode(mode_play);
628 } else if (event.key === tui.key_left) {
629 explorer.move('left');
630 } else if (event.key === tui.key_right) {
631 explorer.move('right');
632 } else if (event.key === tui.key_up) {
634 } else if (event.key === tui.key_down) {
635 explorer.move('down');
636 } else if (event.key === 'e') {
637 event.preventDefault();
638 tui.switch_mode(mode_annotate);
643 wasd_selector.addEventListener('input', function() {
646 rows_selector.addEventListener('input', function() {
647 if (rows_selector.value % 2 != 0) {
650 terminal.initialize();
653 cols_selector.addEventListener('input', function() {
654 if (cols_selector.value % 4 != 0) {
657 terminal.initialize();
658 tui.window_width = terminal.cols / 2,
661 window.setInterval(function() {
662 if (!(['input', 'n_cols', 'n_rows', 'WASD_selector'].includes(document.activeElement.id))) {