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 (c in ['"', '\\']) {
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']);
418 tui.log_msg('@ ' + tokens[1]);
420 tui.switch_mode(mode_play);
421 } else if (tokens[0] === 'ANNOTATION') {
422 let position = parser.parse_yx(tokens[1]);
423 explorer.update_info_db(position, tokens[2]);
424 } else if (tokens[0] === 'UNHANDLED_INPUT') {
425 tui.log_msg('? unknown command');
426 } else if (tokens[0] === 'PLAY_ERROR') {
427 terminal.blink_screen();
428 } else if (tokens[0] === 'ARGUMENT_ERROR') {
429 tui.log_msg('? syntax error: ' + tokens[1]);
430 } else if (tokens[0] === 'GAME_ERROR') {
431 tui.log_msg('? game error: ' + tokens[1]);
432 } else if (tokens[0] === 'PONG') {
435 tui.log_msg('? unhandled input: ' + event.data);
442 move: function(direction) {
444 try_pos[0] = this.position[0];
445 try_pos[1] = this.position[1];
446 if (direction == 'left') {
448 } else if (direction == 'right') {
450 } else if (direction == 'up') {
452 } else if (direction == 'down') {
455 if (!(try_pos[0] < 0) &&
457 !(try_pos[0] >= game.map_size[0])
458 && !(try_pos[1] >= game.map_size[1])) {
459 this.position = try_pos;
464 update_info_db: function(yx, str) {
465 this.info_db[yx] = str;
466 if (tui.mode == mode_study) {
470 empty_info_db: function() {
472 if (tui.mode == mode_study) {
476 query_info: function() {
477 server.send(["GET_ANNOTATION", unparser.to_yx(explorer.position)]);
479 get_info: function() {
480 if (this.position in this.info_db) {
481 return this.info_db[this.position];
486 annotate: function(msg) {
487 if (msg.length == 0) {
488 msg = " "; // triggers annotation deletion
490 server.send(["ANNOTATE", unparser.to_yx(explorer.position), msg]);
494 document.addEventListener('keydown', (event) => {
495 if (tui.mode.has_input_prompt && event.key.length === 1) {
496 tui.add_to_input(event.key);
497 } else if (tui.mode.has_input_prompt && event.key == 'Backspace') {
499 } else if (tui.mode == mode_login && event.key == 'Enter') {
500 server.send(['LOGIN', tui.input]);
501 tui.switch_mode(mode_login);
502 } else if (tui.mode == mode_annotate && event.key == 'Enter') {
503 explorer.annotate(tui.input);
504 tui.switch_mode(mode_study, true);
505 } else if (tui.mode == mode_chat && event.key == 'Enter') {
506 let [tokens, token_starts] = parser.tokenize(tui.input);
507 if (tokens.length > 0 && tokens[0].length > 0) {
508 if (tokens[0][0] == ':') {
509 if (tokens[0] == ':play' || tokens[0] == ':p') {
510 tui.switch_mode(mode_play);
511 } else if (tokens[0] == ':study' || tokens[0] == ':?') {
512 tui.switch_mode(mode_study);
513 } else if (tokens[0] == ':help') {
515 } else if (tokens[0] == ':nick') {
516 if (tokens.length > 1) {
517 server.send(['LOGIN', tokens[1]]);
519 tui.log_msg('? need login name');
521 } else if (tokens[0] == ':msg') {
522 if (tokens.length > 2) {
523 let msg = tui.input.slice(token_starts[2]);
524 server.send(['QUERY', tokens[1], msg]);
526 tui.log_msg('? need message target and message');
529 tui.log_msg('? unknown command');
532 server.send(['ALL', tui.input]);
537 } else if (tui.mode == mode_play) {
538 if (event.key === 'c') {
539 tui.switch_mode(mode_chat);
540 } else if (event.key === 'e') {
541 tui.switch_mode(mode_edit);
542 } else if (event.key === '?') {
543 tui.switch_mode(mode_study);
544 } else if (event.key === 'F1') {
546 } else if (event.key === 'f') {
547 server.send(["TASK:FLATTEN_SURROUNDINGS"]);
548 } else if (event.key === tui.key_left) {
549 server.send(['TASK:MOVE', 'LEFT']);
550 } else if (event.key === tui.key_right) {
551 server.send(['TASK:MOVE', 'RIGHT']);
552 } else if (event.key === tui.key_up) {
553 server.send(['TASK:MOVE', 'UP']);
554 } else if (event.key === tui.key_down) {
555 server.send(['TASK:MOVE', 'DOWN']);
557 } else if (tui.mode == mode_edit) {
558 if (event.key != "Shift" && event.key.length == 1) {
559 server.send(["TASK:WRITE", event.key]);
560 tui.switch_mode(mode_play);
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 tui.switch_mode(mode_annotate);
581 let wasd_selector = document.getElementById("WASD_selector");
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 let rows_selector = document.getElementById("n_rows");
597 rows_selector.addEventListener('input', function() {
598 terminal.rows = rows_selector.value;
599 terminal.initialize();
602 let cols_selector = document.getElementById("n_cols");
603 cols_selector.addEventListener('input', function() {
604 terminal.cols = cols_selector.value;
605 terminal.initialize();
606 tui.window_width = terminal.cols / 2,