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>
17 let websocket_location = "ws://localhost:8000";
24 initialize: function() {
25 this.pre_el = document.getElementById("terminal");
26 this.pre_el.style.color = this.foreground;
27 this.pre_el.style.backgroundColor = this.background;
30 for (let y = 0, x = 0; y <= this.rows; x++) {
34 this.content.push(line);
43 blink_screen: function() {
44 this.pre_el.style.color = this.background;
45 this.pre_el.style.backgroundColor = this.foreground;
47 this.pre_el.style.color = this.foreground;
48 this.pre_el.style.backgroundColor = this.background;
53 for (let y = 0; y < this.rows; y++) {
54 let line = this.content[y].join('');
55 pre_string += line + '\n';
57 this.pre_el.textContent = pre_string;
59 write: function(start_y, start_x, msg) {
60 for (let x = start_x, i = 0; x < this.cols && i < msg.length; x++, i++) {
61 this.content[start_y][x] = msg[i];
64 drawBox: function(start_y, start_x, height, width) {
65 let end_y = start_y + height;
66 let end_x = start_x + width;
67 for (let y = start_y, x = start_x; y < this.rows; x++) {
75 this.content[y][x] = ' ';
81 tokenize: function(str) {
87 for (let i = 0; i < str.length; i++) {
93 } else if (c == '\\') {
95 } else if (c == '"') {
100 } else if (c == '"') {
102 } else if (c === ' ') {
103 if (token.length > 0) {
112 if (token.length > 0) {
115 let token_starts = [];
116 for (let i = 0; i < token_ends.length; i++) {
117 token_starts.push(token_ends[i] - tokens[i].length);
119 return [tokens, token_starts];
121 parse_yx: function(position_string) {
122 let coordinate_strings = position_string.split(',')
123 let position = [0, 0];
124 position[0] = parseInt(coordinate_strings[0].slice(2));
125 position[1] = parseInt(coordinate_strings[1].slice(2));
131 init: function(url) {
132 this.websocket = new WebSocket(url);
133 this.websocket.onopen = function(event) {
134 window.setInterval(function() { server.send(['PING']) }, 30000);
135 tui.log_msg("@ server connected! :)");
138 this.websocket.onclose = function(event) {
139 tui.log_msg('@ server disconnected :(');
142 send: function(tokens) {
143 this.websocket.send(unparser.untokenize(tokens));
148 quote: function(str) {
150 for (let i = 0; i < str.length; i++) {
152 if (['"', '\\'].includes(c)) {
158 return quoted.join('');
160 to_yx: function(yx_coordinate) {
161 return "Y:" + yx_coordinate[0] + ",X:" + yx_coordinate[1];
163 untokenize: function(tokens) {
164 let quoted_tokens = [];
165 for (let token of tokens) {
166 quoted_tokens.push(this.quote(token));
168 return quoted_tokens.join(" ");
173 constructor(name, has_input_prompt=false, shows_annotations=false, is_intro=false) {
175 this.has_input_prompt = has_input_prompt;
176 this.shows_annotations = shows_annotations;
177 this.is_intro = is_intro;
180 let mode_waiting_for_server = new Mode('waiting_for_server', false, false, true);
181 let mode_login = new Mode('login', true, false, true);
182 let mode_chat = new Mode('chat / write messages to players', true, false);
183 let mode_annotate = new Mode('add message to map tile', true, true);
184 let mode_play = new Mode('play / move around', false, false);
185 let mode_study = new Mode('check map tiles for messages', false, true);
186 let mode_edit = new Mode('write ASCII char to map tile', false, false);
189 mode: mode_waiting_for_server,
194 window_width: terminal.cols / 2,
202 movement_keys_desc: 'w, a, s, d',
204 this.recalc_input_lines();
205 this.height_header = this.height_turn_line + this.height_mode_line;
206 this.log_msg("@ waiting for server connection ...");
208 init_login: function() {
209 this.log_msg("@ please enter your username:");
210 this.switch_mode(mode_login);
212 switch_mode: function(mode, keep_pos=false) {
213 if (mode == mode_study && !keep_pos) {
214 explorer.position = game.things[game.player_id];
218 if (mode == mode_annotate && explorer.position in explorer.info_db) {
219 let info = explorer.info_db[explorer.position];
220 if (info != "(none)") {
221 this.add_to_input(explorer.info_db[explorer.position]);
226 empty_input: function(str) {
228 if (this.mode.has_input_prompt) {
229 this.recalc_input_lines();
231 this.height_input = 0;
234 add_to_input: function(str) {
235 if (this.input_prompt.length + this.input.length + str.length > this.window_width * terminal.rows) {
239 this.recalc_input_lines();
242 recalc_input_lines: function() {
243 this.input_lines = this.msg_into_lines_of_width(this.input_prompt + this.input, this.window_width);
244 this.height_input = this.input_lines.length;
246 shorten_input: function() {
247 if (this.input.length == 0) {
248 terminal.blink_screen();
250 this.input = tui.input.slice(0, -1);
251 this.recalc_input_lines();
255 msg_into_lines_of_width: function(msg, width) {
258 for (let i = 0, x = 0; i < msg.length; i++, x++) {
269 log_msg: function(msg) {
271 while (this.log.length > terminal.rows * 4) {
276 log_help: function() {
277 this.log_msg("HELP:");
278 this.log_msg("chat mode commands:");
279 this.log_msg(" :nick NAME - re-name yourself to NAME");
280 this.log_msg(" :msg USER TEXT - send TEXT to USER");
281 this.log_msg(" :help - show this help");
282 this.log_msg(" :p or :play - switch to play mode");
283 this.log_msg(" :? or :study - switch to study mode");
284 this.log_msg("commands common to study and play mode:");
285 this.log_msg(" " + this.movement_keys_desc + " - move");
286 this.log_msg(" c - switch to chat mode");
287 this.log_msg("commands specific to play mode:");
288 this.log_msg(" e - write following ASCII character");
289 this.log_msg(" f - flatten surroundings");
290 this.log_msg(" ? - switch to study mode");
291 this.log_msg("commands specific to study mode:");
292 this.log_msg(" e - annotate terrain");
293 this.log_msg(" p - switch to play mode");
295 draw_map: function() {
298 for (let i = 0, j = 0; i < game.map.length; i++, j++) {
299 if (j == game.map_size[1]) {
300 map_lines.push(line);
304 line.push(game.map[i]);
306 map_lines.push(line);
307 let player_position = [0,0];
308 let center_pos = [Math.floor(game.map_size[0] / 2),
309 Math.floor(game.map_size[1] / 2)];
310 for (const thing_id in game.things) {
311 let t = game.things[thing_id];
312 map_lines[t[0]][t[1]] = '@';
313 if (game.player_id == thing_id) {
317 if (tui.mode.shows_annotations) {
318 map_lines[explorer.position[0]][explorer.position[1]] = '?';
319 center_pos = explorer.position;
321 let offset = [(terminal.rows / 2) - center_pos[0],
322 this.window_width / 2 - center_pos[1]];
323 for (let term_y = offset[0], map_y = 0;
324 term_y < terminal.rows && map_y < game.map_size[0];
327 let to_draw = map_lines[map_y].join('').slice(0, this.window_width - offset[1]);
328 terminal.write(term_y, offset[1], to_draw);
332 draw_mode_line: function() {
333 terminal.write(0, this.window_width, 'MODE: ' + this.mode.name);
335 draw_turn_line: function(n) {
336 terminal.write(1, this.window_width, 'TURN: ' + game.turn);
338 draw_history: function() {
339 let log_display_lines = [];
340 for (let line of this.log) {
341 log_display_lines = log_display_lines.concat(this.msg_into_lines_of_width(line, this.window_width));
343 for (let y = terminal.rows - 1 - this.height_input,
344 i = log_display_lines.length - 1;
345 y >= this.height_header && i >= 0;
347 terminal.write(y, this.window_width, log_display_lines[i]);
350 draw_info: function() {
351 let lines = this.msg_into_lines_of_width(explorer.get_info(), this.window_width);
352 for (let y = this.height_header, i = 0; y < terminal.rows && i < lines.length; y++, i++) {
353 terminal.write(y, this.window_width, lines[i]);
356 draw_input: function() {
357 if (this.mode.has_input_prompt) {
358 for (let y = terminal.rows - this.height_input, i = 0; y < terminal.rows && i < this.input_lines.length; y++, i++) {
359 terminal.write(y, this.window_width, this.input_lines[i]);
363 full_refresh: function() {
364 terminal.drawBox(0, 0, terminal.rows, terminal.cols);
365 if (this.mode.is_intro) {
370 this.draw_turn_line();
371 this.draw_mode_line();
372 if (this.mode.shows_annotations) {
391 terminal.initialize();
395 server.init(websocket_location);
396 server.websocket.onmessage = function (event) {
397 let tokens = parser.tokenize(event.data)[0];
398 if (tokens[0] === 'TURN') {
400 game.turn = parseInt(tokens[1]);
401 } else if (tokens[0] === 'THING_POS') {
402 game.things[tokens[1]] = parser.parse_yx(tokens[2]);
403 } else if (tokens[0] === 'MAP') {
404 game.map_size = parser.parse_yx(tokens[1]);
406 } else if (tokens[0] === 'GAME_STATE_COMPLETE') {
407 explorer.empty_info_db();
408 if (tui.mode == mode_study) {
409 explorer.query_info();
412 } else if (tokens[0] === 'CHAT') {
413 tui.log_msg('# ' + tokens[1], 1);
414 } else if (tokens[0] === 'PLAYER_ID') {
415 game.player_id = parseInt(tokens[1]);
416 } else if (tokens[0] === 'LOGIN_OK') {
417 server.send(['GET_GAMESTATE']);
419 tui.switch_mode(mode_play);
420 } else if (tokens[0] === 'ANNOTATION') {
421 let position = parser.parse_yx(tokens[1]);
422 explorer.update_info_db(position, tokens[2]);
423 } else if (tokens[0] === 'UNHANDLED_INPUT') {
424 tui.log_msg('? unknown command');
425 } else if (tokens[0] === 'PLAY_ERROR') {
426 terminal.blink_screen();
427 } else if (tokens[0] === 'ARGUMENT_ERROR') {
428 tui.log_msg('? syntax error: ' + tokens[1]);
429 } else if (tokens[0] === 'GAME_ERROR') {
430 tui.log_msg('? game error: ' + tokens[1]);
431 } else if (tokens[0] === 'PONG') {
434 tui.log_msg('? unhandled input: ' + event.data);
441 move: function(direction) {
443 try_pos[0] = this.position[0];
444 try_pos[1] = this.position[1];
445 if (direction == 'left') {
447 } else if (direction == 'right') {
449 } else if (direction == 'up') {
451 } else if (direction == 'down') {
454 if (!(try_pos[0] < 0) &&
456 !(try_pos[0] >= game.map_size[0])
457 && !(try_pos[1] >= game.map_size[1])) {
458 this.position = try_pos;
463 update_info_db: function(yx, str) {
464 this.info_db[yx] = str;
465 if (tui.mode == mode_study) {
469 empty_info_db: function() {
471 if (tui.mode == mode_study) {
475 query_info: function() {
476 server.send(["GET_ANNOTATION", unparser.to_yx(explorer.position)]);
478 get_info: function() {
479 if (this.position in this.info_db) {
480 return this.info_db[this.position];
485 annotate: function(msg) {
486 if (msg.length == 0) {
487 msg = " "; // triggers annotation deletion
489 server.send(["ANNOTATE", unparser.to_yx(explorer.position), msg]);
493 document.addEventListener('keydown', (event) => {
494 if (tui.mode.has_input_prompt && event.key.length === 1) {
495 tui.add_to_input(event.key);
496 } else if (tui.mode.has_input_prompt && event.key == 'Backspace') {
498 } else if (tui.mode == mode_login && event.key == 'Enter') {
499 server.send(['LOGIN', tui.input]);
500 tui.switch_mode(mode_login);
501 } else if (tui.mode == mode_annotate && event.key == 'Enter') {
502 explorer.annotate(tui.input);
503 tui.switch_mode(mode_study, true);
504 } else if (tui.mode == mode_chat && event.key == 'Enter') {
505 let [tokens, token_starts] = parser.tokenize(tui.input);
506 if (tokens.length > 0 && tokens[0].length > 0) {
507 if (tokens[0][0] == ':') {
508 if (tokens[0] == ':play' || tokens[0] == ':p') {
509 tui.switch_mode(mode_play);
510 } else if (tokens[0] == ':study' || tokens[0] == ':?') {
511 tui.switch_mode(mode_study);
512 } else if (tokens[0] == ':help') {
514 } else if (tokens[0] == ':nick') {
515 if (tokens.length > 1) {
516 server.send(['LOGIN', tokens[1]]);
518 tui.log_msg('? need login name');
520 } else if (tokens[0] == ':msg') {
521 if (tokens.length > 2) {
522 let msg = tui.input.slice(token_starts[2]);
523 server.send(['QUERY', tokens[1], msg]);
525 tui.log_msg('? need message target and message');
528 tui.log_msg('? unknown command');
531 server.send(['ALL', tui.input]);
533 } else if (tui.input.length > 0) {
534 server.send(['ALL', tui.input]);
538 } else if (tui.mode == mode_play) {
539 if (event.key === 'c') {
540 tui.switch_mode(mode_chat);
541 } else if (event.key === 'e') {
542 tui.switch_mode(mode_edit);
543 } else if (event.key === '?') {
544 tui.switch_mode(mode_study);
545 } else if (event.key === 'F1') {
547 } else if (event.key === 'f') {
548 server.send(["TASK:FLATTEN_SURROUNDINGS"]);
549 } else if (event.key === tui.key_left) {
550 server.send(['TASK:MOVE', 'LEFT']);
551 } else if (event.key === tui.key_right) {
552 server.send(['TASK:MOVE', 'RIGHT']);
553 } else if (event.key === tui.key_up) {
554 server.send(['TASK:MOVE', 'UP']);
555 } else if (event.key === tui.key_down) {
556 server.send(['TASK:MOVE', 'DOWN']);
558 } else if (tui.mode == mode_edit) {
559 if (event.key != "Shift" && event.key.length == 1) {
560 server.send(["TASK:WRITE", event.key]);
561 tui.switch_mode(mode_play);
563 } else if (tui.mode == mode_study) {
564 if (event.key === 'c') {
565 tui.switch_mode(mode_chat);
566 } else if (event.key == 'p') {
567 tui.switch_mode(mode_play);
568 } else if (event.key === tui.key_left) {
569 explorer.move('left');
570 } else if (event.key === tui.key_right) {
571 explorer.move('right');
572 } else if (event.key === tui.key_up) {
574 } else if (event.key === tui.key_down) {
575 explorer.move('down');
576 } else if (event.key === 'e') {
577 tui.switch_mode(mode_annotate);
582 let wasd_selector = document.getElementById("WASD_selector");
583 wasd_selector.addEventListener('input', function() {
584 if (wasd_selector.value == 'w, a, s, d') {
589 } else if (wasd_selector.value == 'arrow keys') {
590 tui.key_up = 'ArrowUp';
591 tui.key_down = 'ArrowDown';
592 tui.key_left = 'ArrowLeft';
593 tui.key_right = 'ArrowRight';
595 tui.movement_keys_desc = wasd_selector.value;
597 let rows_selector = document.getElementById("n_rows");
598 rows_selector.addEventListener('input', function() {
599 terminal.rows = rows_selector.value;
600 terminal.initialize();
603 let cols_selector = document.getElementById("n_cols");
604 cols_selector.addEventListener('input', function() {
605 terminal.cols = cols_selector.value;
606 terminal.initialize();
607 tui.window_width = terminal.cols / 2,