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) {
138 this.websocket = new WebSocket(url);
139 this.websocket.onopen = function(event) {
140 window.setInterval(function() { server.send(['PING']) }, 30000);
141 tui.log_msg("@ server connected! :)");
144 this.websocket.onclose = function(event) {
145 tui.log_msg('@ server disconnected :(');
148 send: function(tokens) {
149 this.websocket.send(unparser.untokenize(tokens));
154 quote: function(str) {
156 for (let i = 0; i < str.length; i++) {
158 if (['"', '\\'].includes(c)) {
164 return quoted.join('');
166 to_yx: function(yx_coordinate) {
167 return "Y:" + yx_coordinate[0] + ",X:" + yx_coordinate[1];
169 untokenize: function(tokens) {
170 let quoted_tokens = [];
171 for (let token of tokens) {
172 quoted_tokens.push(this.quote(token));
174 return quoted_tokens.join(" ");
179 constructor(name, has_input_prompt=false, shows_annotations=false, is_intro=false) {
181 this.has_input_prompt = has_input_prompt;
182 this.shows_annotations = shows_annotations;
183 this.is_intro = is_intro;
186 let mode_waiting_for_server = new Mode('waiting_for_server', false, false, true);
187 let mode_login = new Mode('login', true, false, true);
188 let mode_chat = new Mode('chat / write messages to players', true, false);
189 let mode_annotate = new Mode('add message to map tile', true, true);
190 let mode_play = new Mode('play / move around', false, false);
191 let mode_study = new Mode('check map tiles for messages', false, true);
192 let mode_edit = new Mode('write ASCII char to map tile', false, false);
195 mode: mode_waiting_for_server,
199 window_width: terminal.cols / 2,
207 movement_keys_desc: 'w, a, s, d',
209 this.inputEl = document.getElementById("input");
210 this.inputEl.focus();
211 this.recalc_input_lines();
212 this.height_header = this.height_turn_line + this.height_mode_line;
213 this.log_msg("@ waiting for server connection ...");
215 init_login: function() {
216 this.log_msg("@ please enter your username:");
217 this.switch_mode(mode_login);
219 switch_mode: function(mode, keep_pos=false) {
220 if (mode == mode_study && !keep_pos) {
221 explorer.position = game.things[game.player_id];
225 if (mode == mode_annotate && explorer.position in explorer.info_db) {
226 let info = explorer.info_db[explorer.position];
227 if (info != "(none)") {
228 this.inputEl.value = explorer.info_db[explorer.position];
229 this.recalc_input_lines();
234 empty_input: function(str) {
235 this.inputEl.value = "";
236 if (this.mode.has_input_prompt) {
237 this.recalc_input_lines();
239 this.height_input = 0;
242 recalc_input_lines: function() {
243 this.input_lines = this.msg_into_lines_of_width(this.input_prompt + this.inputEl.value, this.window_width);
244 this.height_input = this.input_lines.length;
246 msg_into_lines_of_width: function(msg, width) {
249 for (let i = 0, x = 0; i < msg.length; i++, x++) {
260 log_msg: function(msg) {
262 while (this.log.length > terminal.rows * 4) {
267 log_help: function() {
268 this.log_msg("HELP:");
269 this.log_msg("chat mode commands:");
270 this.log_msg(" :nick NAME - re-name yourself to NAME");
271 this.log_msg(" :msg USER TEXT - send TEXT to USER");
272 this.log_msg(" :help - show this help");
273 this.log_msg(" :p or :play - switch to play mode");
274 this.log_msg(" :? or :study - switch to study mode");
275 this.log_msg("commands common to study and play mode:");
276 this.log_msg(" " + this.movement_keys_desc + " - move");
277 this.log_msg(" c - switch to chat mode");
278 this.log_msg("commands specific to play mode:");
279 this.log_msg(" e - write following ASCII character");
280 this.log_msg(" f - flatten surroundings");
281 this.log_msg(" ? - switch to study mode");
282 this.log_msg("commands specific to study mode:");
283 this.log_msg(" e - annotate terrain");
284 this.log_msg(" p - switch to play mode");
286 draw_map: function() {
289 for (let i = 0, j = 0; i < game.map.length; i++, j++) {
290 if (j == game.map_size[1]) {
291 map_lines.push(line);
295 line.push(game.map[i]);
297 map_lines.push(line);
298 let player_position = [0,0];
299 let center_pos = [Math.floor(game.map_size[0] / 2),
300 Math.floor(game.map_size[1] / 2)];
301 for (const thing_id in game.things) {
302 let t = game.things[thing_id];
303 map_lines[t[0]][t[1]] = '@';
304 if (game.player_id == thing_id) {
308 if (tui.mode.shows_annotations) {
309 map_lines[explorer.position[0]][explorer.position[1]] = '?';
310 center_pos = explorer.position;
312 let offset = [(terminal.rows / 2) - center_pos[0],
313 this.window_width / 2 - center_pos[1]];
314 for (let term_y = offset[0], map_y = 0;
315 term_y < terminal.rows && map_y < game.map_size[0];
318 let to_draw = map_lines[map_y].join('').slice(0, this.window_width - offset[1]);
319 terminal.write(term_y, offset[1], to_draw);
323 draw_mode_line: function() {
324 terminal.write(0, this.window_width, 'MODE: ' + this.mode.name);
326 draw_turn_line: function(n) {
327 terminal.write(1, this.window_width, 'TURN: ' + game.turn);
329 draw_history: function() {
330 let log_display_lines = [];
331 for (let line of this.log) {
332 log_display_lines = log_display_lines.concat(this.msg_into_lines_of_width(line, this.window_width));
334 for (let y = terminal.rows - 1 - this.height_input,
335 i = log_display_lines.length - 1;
336 y >= this.height_header && i >= 0;
338 terminal.write(y, this.window_width, log_display_lines[i]);
341 draw_info: function() {
342 let lines = this.msg_into_lines_of_width(explorer.get_info(), this.window_width);
343 for (let y = this.height_header, i = 0; y < terminal.rows && i < lines.length; y++, i++) {
344 terminal.write(y, this.window_width, lines[i]);
347 draw_input: function() {
348 if (this.mode.has_input_prompt) {
349 for (let y = terminal.rows - this.height_input, i = 0; y < terminal.rows && i < this.input_lines.length; y++, i++) {
350 terminal.write(y, this.window_width, this.input_lines[i]);
354 full_refresh: function() {
355 terminal.drawBox(0, 0, terminal.rows, terminal.cols);
356 if (this.mode.is_intro) {
361 this.draw_turn_line();
362 this.draw_mode_line();
363 if (this.mode.shows_annotations) {
385 server.init(websocket_location);
386 server.websocket.onmessage = function (event) {
387 let tokens = parser.tokenize(event.data)[0];
388 if (tokens[0] === 'TURN') {
390 game.turn = parseInt(tokens[1]);
391 } else if (tokens[0] === 'THING_POS') {
392 game.things[tokens[1]] = parser.parse_yx(tokens[2]);
393 } else if (tokens[0] === 'MAP') {
394 game.map_size = parser.parse_yx(tokens[1]);
396 } else if (tokens[0] === 'GAME_STATE_COMPLETE') {
397 explorer.empty_info_db();
398 if (tui.mode == mode_study) {
399 explorer.query_info();
402 } else if (tokens[0] === 'CHAT') {
403 tui.log_msg('# ' + tokens[1], 1);
404 } else if (tokens[0] === 'PLAYER_ID') {
405 game.player_id = parseInt(tokens[1]);
406 } else if (tokens[0] === 'LOGIN_OK') {
407 server.send(['GET_GAMESTATE']);
409 tui.switch_mode(mode_play);
410 } else if (tokens[0] === 'ANNOTATION') {
411 let position = parser.parse_yx(tokens[1]);
412 explorer.update_info_db(position, tokens[2]);
413 } else if (tokens[0] === 'UNHANDLED_INPUT') {
414 tui.log_msg('? unknown command');
415 } else if (tokens[0] === 'PLAY_ERROR') {
416 terminal.blink_screen();
417 } else if (tokens[0] === 'ARGUMENT_ERROR') {
418 tui.log_msg('? syntax error: ' + tokens[1]);
419 } else if (tokens[0] === 'GAME_ERROR') {
420 tui.log_msg('? game error: ' + tokens[1]);
421 } else if (tokens[0] === 'PONG') {
424 tui.log_msg('? unhandled input: ' + event.data);
431 move: function(direction) {
433 try_pos[0] = this.position[0];
434 try_pos[1] = this.position[1];
435 if (direction == 'left') {
437 } else if (direction == 'right') {
439 } else if (direction == 'up') {
441 } else if (direction == 'down') {
444 if (!(try_pos[0] < 0) &&
446 !(try_pos[0] >= game.map_size[0])
447 && !(try_pos[1] >= game.map_size[1])) {
448 this.position = try_pos;
453 update_info_db: function(yx, str) {
454 this.info_db[yx] = str;
455 if (tui.mode == mode_study) {
459 empty_info_db: function() {
461 if (tui.mode == mode_study) {
465 query_info: function() {
466 server.send(["GET_ANNOTATION", unparser.to_yx(explorer.position)]);
468 get_info: function() {
469 if (this.position in this.info_db) {
470 return this.info_db[this.position];
475 annotate: function(msg) {
476 if (msg.length == 0) {
477 msg = " "; // triggers annotation deletion
479 server.send(["ANNOTATE", unparser.to_yx(explorer.position), msg]);
483 tui.inputEl.addEventListener('input', (event) => {
484 if (tui.mode.has_input_prompt) {
485 let max_length = tui.window_width * terminal.rows - tui.input_prompt.length;
486 if (tui.inputEl.value.length > max_length) {
487 tui.inputEl.value = tui.inputEl.value.slice(0, max_length);
489 tui.recalc_input_lines();
491 } else if (tui.mode == mode_edit && tui.inputEl.value.length > 0) {
492 server.send(["TASK:WRITE", tui.inputEl.value[0]]);
493 tui.switch_mode(mode_play);
496 tui.inputEl.addEventListener('keydown', (event) => {
497 if (event.key == 'Enter') {
498 event.preventDefault();
500 if (tui.mode == mode_login && event.key == 'Enter') {
501 server.send(['LOGIN', tui.inputEl.value]);
502 tui.switch_mode(mode_login);
503 } else if (tui.mode == mode_annotate && event.key == 'Enter') {
504 explorer.annotate(tui.inputEl.value);
505 tui.switch_mode(mode_study, true);
506 } else if (tui.mode == mode_chat && event.key == 'Enter') {
507 let [tokens, token_starts] = parser.tokenize(tui.inputEl.value);
508 if (tokens.length > 0 && tokens[0].length > 0) {
509 if (tokens[0][0] == ':') {
510 if (tokens[0] == ':play' || tokens[0] == ':p') {
511 tui.switch_mode(mode_play);
512 } else if (tokens[0] == ':study' || tokens[0] == ':?') {
513 tui.switch_mode(mode_study);
514 } else if (tokens[0] == ':help') {
516 } else if (tokens[0] == ':nick') {
517 if (tokens.length > 1) {
518 server.send(['LOGIN', tokens[1]]);
520 tui.log_msg('? need login name');
522 } else if (tokens[0] == ':msg') {
523 if (tokens.length > 2) {
524 let msg = tui.inputEl.value.slice(token_starts[2]);
525 server.send(['QUERY', tokens[1], msg]);
527 tui.log_msg('? need message target and message');
530 tui.log_msg('? unknown command');
533 server.send(['ALL', tui.inputEl.value]);
535 } else if (tui.inputEl.valuelength > 0) {
536 server.send(['ALL', tui.inputEl.value]);
540 } else if (tui.mode == mode_play) {
541 if (event.key === 'c') {
542 event.preventDefault();
543 tui.switch_mode(mode_chat);
544 } else if (event.key === 'e') {
545 event.preventDefault();
546 tui.switch_mode(mode_edit);
547 } else if (event.key === '?') {
548 tui.switch_mode(mode_study);
549 } else if (event.key === 'F1') {
551 } else if (event.key === 'f') {
552 server.send(["TASK:FLATTEN_SURROUNDINGS"]);
553 } else if (event.key === tui.key_left) {
554 server.send(['TASK:MOVE', 'LEFT']);
555 } else if (event.key === tui.key_right) {
556 server.send(['TASK:MOVE', 'RIGHT']);
557 } else if (event.key === tui.key_up) {
558 server.send(['TASK:MOVE', 'UP']);
559 } else if (event.key === tui.key_down) {
560 server.send(['TASK:MOVE', 'DOWN']);
562 } else if (tui.mode == mode_study) {
563 if (event.key === 'c') {
564 tui.switch_mode(mode_chat);
565 } else if (event.key == 'p') {
566 tui.switch_mode(mode_play);
567 } else if (event.key === tui.key_left) {
568 explorer.move('left');
569 } else if (event.key === tui.key_right) {
570 explorer.move('right');
571 } else if (event.key === tui.key_up) {
573 } else if (event.key === tui.key_down) {
574 explorer.move('down');
575 } else if (event.key === 'e') {
576 event.preventDefault();
577 tui.switch_mode(mode_annotate);
582 wasd_selector.addEventListener('input', function() {
583 if (wasd_selector.value == 'w, a, s, d') {
588 } else if (wasd_selector.value == 'arrow keys') {
589 tui.key_up = 'ArrowUp';
590 tui.key_down = 'ArrowDown';
591 tui.key_left = 'ArrowLeft';
592 tui.key_right = 'ArrowRight';
594 tui.movement_keys_desc = wasd_selector.value;
596 rows_selector.addEventListener('input', function() {
597 terminal.initialize();
600 cols_selector.addEventListener('input', function() {
601 terminal.initialize();
602 tui.window_width = terminal.cols / 2,
605 window.setInterval(function() {
606 if (!(['input', 'n_cols', 'n_rows', 'WASD_selector'].includes(document.activeElement.id))) {