6 <pre id="terminal" style="display: inline-block; color: white; background-color: black;"></pre>
8 movement: <select id="WASD_selector" name="WASD_selector" >
9 <option value="w, a, s, d" selected>w, a, s, d</option>
10 <option value="arrow keys">arrow keys</option>
15 let websocket_location = "ws://localhost:8000";
22 initialize: function() {
23 this.pre_el = document.getElementById("terminal");
24 this.pre_el.style.color = this.foreground;
25 this.pre_el.style.backgroundColor = this.background;
28 for (let y = 0, x = 0; y <= this.rows; x++) {
32 this.content.push(line);
41 blink_screen: function() {
42 this.pre_el.style.color = this.background;
43 this.pre_el.style.backgroundColor = this.foreground;
45 this.pre_el.style.color = this.foreground;
46 this.pre_el.style.backgroundColor = this.background;
51 for (let y = 0; y < this.rows; y++) {
52 let line = this.content[y].join('');
53 pre_string += line + '\n';
55 this.pre_el.textContent = pre_string;
57 write: function(start_y, start_x, msg) {
58 for (let x = start_x, i = 0; x < this.cols && i < msg.length; x++, i++) {
59 this.content[start_y][x] = msg[i];
62 drawBox: function(start_y, start_x, height, width) {
63 let end_y = start_y + height;
64 let end_x = start_x + width;
65 for (let y = start_y, x = start_x; y < this.rows; x++) {
73 this.content[y][x] = ' ';
79 tokenize: function(str) {
85 for (let i = 0; i < str.length; i++) {
91 } else if (c == '\\') {
93 } else if (c == '"') {
98 } else if (c == '"') {
100 } else if (c === ' ') {
101 if (token.length > 0) {
110 if (token.length > 0) {
113 let token_starts = [];
114 for (let i = 0; i < token_ends.length; i++) {
115 token_starts.push(token_ends[i] - tokens[i].length);
117 return [tokens, token_starts];
119 parse_yx: function(position_string) {
120 let coordinate_strings = position_string.split(',')
121 let position = [0, 0];
122 position[0] = parseInt(coordinate_strings[0].slice(2));
123 position[1] = parseInt(coordinate_strings[1].slice(2));
129 init: function(url) {
130 this.websocket = new WebSocket(url);
131 this.websocket.onopen = function(event) {
132 window.setInterval(function() { server.send(['PING']) }, 30000);
133 tui.log_msg("@ server connected!");
137 send: function(tokens) {
138 if (this.websocket.readyState !== WebSocket.OPEN) {
139 tui.log_msg('server disconnected :(');
141 this.websocket.send(unparser.untokenize(tokens));
147 quote: function(str) {
149 for (let i = 0; i < str.length; i++) {
151 if (c in ['"', '\\']) {
157 return quoted.join('');
159 to_yx: function(yx_coordinate) {
160 return "Y:" + yx_coordinate[0] + ",X:" + yx_coordinate[1];
162 untokenize: function(tokens) {
163 let quoted_tokens = [];
164 for (let token of tokens) {
165 quoted_tokens.push(this.quote(token));
167 return quoted_tokens.join(" ");
172 constructor(name, has_input_prompt=false, shows_annotations=false, is_intro=false) {
174 this.has_input_prompt = has_input_prompt;
175 this.shows_annotations = shows_annotations;
176 this.is_intro = is_intro;
179 let mode_waiting_for_server = new Mode('waiting_for_server', false, false, true);
180 let mode_login = new Mode('login', true, false, true);
181 let mode_chat = new Mode('chat / write messages to players', true, false);
182 let mode_annotate = new Mode('add message to map tile', true, true);
183 let mode_play = new Mode('play / move around', false, false);
184 let mode_study = new Mode('check map tiles for messages', false, true);
185 let mode_edit = new Mode('write ASCII char to map tile', false, false);
188 mode: mode_waiting_for_server,
193 window_width: terminal.cols / 2,
201 movement_keys_desc: 'w, a, s, d',
203 this.recalc_input_lines();
204 this.height_header = this.height_turn_line + this.height_mode_line;
205 this.log_msg("@ waiting for server connection ...");
206 this.wasd_selector = document.getElementById("WASD_selector");
207 this.wasd_selector.addEventListener('input', function() {
208 if (tui.wasd_selector.value == 'w, a, s, d') {
213 } else if (tui.wasd_selector.value == 'arrow keys') {
214 tui.key_up = 'ArrowUp';
215 tui.key_down = 'ArrowDown';
216 tui.key_left = 'ArrowLeft';
217 tui.key_right = 'ArrowRight';
219 tui.movement_keys_desc = tui.wasd_selector.value;
222 init_login: function() {
223 this.log_msg("@ please enter your username:");
224 this.switch_mode(mode_login);
226 switch_mode: function(mode, keep_pos=false) {
227 if (mode == mode_study && !keep_pos) {
228 explorer.position = game.things[game.player_id];
232 if (mode == mode_annotate && explorer.position in explorer.info_db) {
233 let info = explorer.info_db[explorer.position];
234 if (info != "(none)") {
235 this.add_to_input(explorer.info_db[explorer.position]);
240 empty_input: function(str) {
242 if (this.mode.has_input_prompt) {
243 this.recalc_input_lines();
245 this.height_input = 0;
248 add_to_input: function(str) {
249 if (this.input_prompt.length + this.input.length + str.length > this.window_width * terminal.rows) {
253 this.recalc_input_lines();
256 recalc_input_lines: function() {
257 this.input_lines = this.msg_into_lines_of_width(this.input_prompt + this.input, this.window_width);
258 this.height_input = this.input_lines.length;
260 shorten_input: function() {
261 if (this.input.length == 0) {
262 terminal.blink_screen();
264 this.input = tui.input.slice(0, -1);
265 this.recalc_input_lines();
269 msg_into_lines_of_width: function(msg, width) {
272 for (let i = 0, x = 0; i < msg.length; i++, x++) {
283 log_msg: function(msg) {
284 let lines = this.msg_into_lines_of_width(msg, this.window_width);
285 this.log = this.log.concat(lines);
286 while (this.log.length > terminal.rows) {
291 log_help: function() {
292 this.log_msg("HELP:");
293 this.log_msg("chat mode commands:");
294 this.log_msg(" :nick NAME - re-name yourself to NAME");
295 this.log_msg(" :msg USER TEXT - send TEXT to USER");
296 this.log_msg(" :help - show this help");
297 this.log_msg(" :p or :play - switch to play mode");
298 this.log_msg(" :? or :study - switch to study mode");
299 this.log_msg("commands common to study and play mode:");
300 this.log_msg(" " + this.movement_keys_desc + " - move");
301 this.log_msg(" c - switch to chat mode");
302 this.log_msg("commands specific to play mode:");
303 this.log_msg(" e - write following ASCII character");
304 this.log_msg(" f - flatten surroundings");
305 this.log_msg(" ? - switch to study mode");
306 this.log_msg("commands specific to study mode:");
307 this.log_msg(" e - annotate terrain");
308 this.log_msg(" p - switch to play mode");
310 draw_map: function() {
313 for (let i = 0, j = 0; i < game.map.length; i++, j++) {
314 if (j == game.map_size[1]) {
315 map_lines.push(line);
319 line.push(game.map[i]);
321 map_lines.push(line);
322 let player_position = [0,0];
323 let center_pos = [Math.floor(game.map_size[0] / 2),
324 Math.floor(game.map_size[1] / 2)];
325 for (const thing_id in game.things) {
326 let t = game.things[thing_id];
327 map_lines[t[0]][t[1]] = '@';
328 if (game.player_id == thing_id) {
332 if (tui.mode.shows_annotations) {
333 map_lines[explorer.position[0]][explorer.position[1]] = '?';
334 center_pos = explorer.position;
336 let offset = [(terminal.rows / 2) - center_pos[0],
337 this.window_width / 2 - center_pos[1]];
338 for (let term_y = offset[0], map_y = 0;
339 term_y < terminal.rows && map_y < game.map_size[0];
342 let to_draw = map_lines[map_y].join('').slice(0, this.window_width - offset[1]);
343 terminal.write(term_y, offset[1], to_draw);
347 draw_mode_line: function() {
348 terminal.write(0, this.window_width, 'MODE: ' + this.mode.name);
350 draw_turn_line: function(n) {
351 terminal.write(1, this.window_width, 'TURN: ' + game.turn);
353 draw_history: function() {
354 if (terminal.rows <= this.height_header + this.height_input) {
357 for (let y = terminal.rows - 1 - this.height_input,
358 i = this.log.length - 1;
359 y >= this.height_header && i >= 0;
361 terminal.write(y, this.window_width, this.log[i]);
364 draw_info: function() {
365 let lines = this.msg_into_lines_of_width(explorer.get_info(), this.window_width);
366 for (let y = this.height_header, i = 0; y < terminal.rows && i < lines.length; y++, i++) {
367 terminal.write(y, this.window_width, lines[i]);
370 draw_input: function() {
371 if (this.mode.has_input_prompt) {
372 for (let y = terminal.rows - this.height_input, i = 0; y < terminal.rows && i < this.input_lines.length; y++, i++) {
373 terminal.write(y, this.window_width, this.input_lines[i]);
377 full_refresh: function() {
378 terminal.drawBox(0, 0, terminal.rows, terminal.cols);
379 if (this.mode.is_intro) {
384 this.draw_turn_line();
385 this.draw_mode_line();
386 if (this.mode.shows_annotations) {
405 terminal.initialize();
409 server.init(websocket_location);
410 server.websocket.onmessage = function (event) {
411 let tokens = parser.tokenize(event.data)[0];
412 if (tokens[0] === 'TURN') {
414 game.turn = parseInt(tokens[1]);
415 } else if (tokens[0] === 'THING_POS') {
416 game.things[tokens[1]] = parser.parse_yx(tokens[2]);
417 } else if (tokens[0] === 'MAP') {
418 game.map_size = parser.parse_yx(tokens[1]);
420 } else if (tokens[0] === 'GAME_STATE_COMPLETE') {
421 explorer.empty_info_db();
422 if (tui.mode == mode_study) {
423 explorer.query_info();
426 } else if (tokens[0] === 'CHAT') {
427 tui.log_msg('# ' + tokens[1], 1);
428 } else if (tokens[0] === 'PLAYER_ID') {
429 game.player_id = parseInt(tokens[1]);
430 } else if (tokens[0] === 'META') {
431 tui.log_msg('@ ' + tokens[1]);
432 } else if (tokens[0] === 'LOGIN_OK') {
433 server.send(['GET_GAMESTATE']);
434 tui.log_msg('@ ' + tokens[1]);
436 tui.switch_mode(mode_chat);
437 } else if (tokens[0] === 'ANNOTATION') {
438 let position = parser.parse_yx(tokens[1]);
439 explorer.update_info_db(position, tokens[2]);
440 } else if (tokens[0] === 'UNHANDLED_INPUT') {
441 tui.log_msg('? unknown command');
442 } else if (tokens[0] === 'PLAY_ERROR') {
443 terminal.blink_screen();
444 } else if (tokens[0] === 'ARGUMENT_ERROR') {
445 tui.log_msg('? syntax error: ' + tokens[1]);
446 } else if (tokens[0] === 'GAME_ERROR') {
447 tui.log_msg('? game error: ' + tokens[1]);
448 } else if (tokens[0] === 'PONG') {
451 tui.log_msg('? unhandled input: ' + event.data);
458 move: function(direction) {
460 try_pos[0] = this.position[0];
461 try_pos[1] = this.position[1];
462 if (direction == 'left') {
464 } else if (direction == 'right') {
466 } else if (direction == 'up') {
468 } else if (direction == 'down') {
471 if (!(try_pos[0] < 0) &&
473 !(try_pos[0] >= game.map_size[0])
474 && !(try_pos[1] >= game.map_size[1])) {
475 this.position = try_pos;
480 update_info_db: function(yx, str) {
481 this.info_db[yx] = str;
482 if (tui.mode == mode_study) {
486 empty_info_db: function() {
488 if (tui.mode == mode_study) {
492 query_info: function() {
493 server.send(["GET_ANNOTATION", unparser.to_yx(explorer.position)]);
495 get_info: function() {
496 if (this.position in this.info_db) {
497 return this.info_db[this.position];
502 annotate: function(msg) {
503 if (msg.length == 0) {
504 msg = " "; // triggers annotation deletion
506 server.send(["ANNOTATE", unparser.to_yx(explorer.position), msg]);
510 document.addEventListener('keydown', (event) => {
511 if (tui.mode.has_input_prompt && event.key.length === 1) {
512 tui.add_to_input(event.key);
513 } else if (tui.mode.has_input_prompt && event.key == 'Backspace') {
515 } else if (tui.mode == mode_login && event.key == 'Enter') {
516 server.send(['LOGIN', tui.input]);
517 tui.switch_mode(mode_login);
518 } else if (tui.mode == mode_annotate && event.key == 'Enter') {
519 explorer.annotate(tui.input);
520 tui.switch_mode(mode_study, true);
521 } else if (tui.mode == mode_chat && event.key == 'Enter') {
522 let [tokens, token_starts] = parser.tokenize(tui.input);
523 if (tokens.length > 0 && tokens[0].length > 0) {
524 if (tokens[0][0] == ':') {
525 if (tokens[0] == ':play' || tokens[0] == ':p') {
526 tui.switch_mode(mode_play);
527 } else if (tokens[0] == ':study' || tokens[0] == ':?') {
528 tui.switch_mode(mode_study);
529 } else if (tokens[0] == ':help') {
531 } else if (tokens[0] == ':nick') {
532 if (tokens.length > 1) {
533 server.send(['LOGIN', tokens[1]]);
535 tui.log_msg('? need login name');
537 } else if (tokens[0] == ':msg') {
538 if (tokens.length > 2) {
539 let msg = tui.input.slice(token_starts[2]);
540 server.send(['QUERY', tokens[1], msg]);
542 tui.log_msg('? need message target and message');
545 tui.log_msg('? unknown command');
548 server.send(['ALL', tui.input]);
553 } else if (tui.mode == mode_play) {
554 if (event.key === 'c') {
555 tui.switch_mode(mode_chat);
556 } else if (event.key === 'e') {
557 tui.switch_mode(mode_edit);
558 } else if (event.key === '?') {
559 tui.switch_mode(mode_study);
560 } else if (event.key === 'F1') {
562 } else if (event.key === 'f') {
563 server.send(["TASK:FLATTEN_SURROUNDINGS"]);
564 } else if (event.key === tui.key_left) {
565 server.send(['TASK:MOVE', 'LEFT']);
566 } else if (event.key === tui.key_right) {
567 server.send(['TASK:MOVE', 'RIGHT']);
568 } else if (event.key === tui.key_up) {
569 server.send(['TASK:MOVE', 'UP']);
570 } else if (event.key === tui.key_down) {
571 server.send(['TASK:MOVE', 'DOWN']);
573 } else if (tui.mode == mode_edit) {
574 if (event.key != "Shift" && event.key.length == 1) {
575 server.send(["TASK:WRITE", event.key]);
576 tui.switch_mode(mode_play);
578 } else if (tui.mode == mode_study) {
579 if (event.key === 'c') {
580 tui.switch_mode(mode_chat);
581 } else if (event.key == 'p') {
582 tui.switch_mode(mode_play);
583 } else if (event.key === tui.key_left) {
584 explorer.move('left');
585 } else if (event.key === tui.key_right) {
586 explorer.move('right');
587 } else if (event.key === tui.key_up) {
589 } else if (event.key === tui.key_down) {
590 explorer.move('down');
591 } else if (event.key === 'e') {
592 tui.switch_mode(mode_annotate);