6 <pre id="terminal" style="display: inline-block; color: white; background-color: black;"></pre>
9 let websocket_location = "ws://localhost:8000";
14 initialize: function() {
15 this.pre_el = document.getElementById("terminal");
18 for (let y = 0, x = 0; y <= this.rows; x++) {
22 this.content.push(line);
33 for (let y = 0; y < this.rows; y++) {
34 let line = this.content[y].join('');
35 pre_string += line + '\n';
37 this.pre_el.textContent = pre_string;
39 write: function(start_y, start_x, msg) {
40 for (let x = start_x, i = 0; x < this.cols && i < msg.length; x++, i++) {
41 this.content[start_y][x] = msg[i];
44 drawBox: function(start_y, start_x, height, width) {
45 let end_y = start_y + height;
46 let end_x = start_x + width;
47 for (let y = start_y, x = start_x; y < this.rows; x++) {
55 this.content[y][x] = ' ';
61 tokenize: function(str) {
67 for (let i = 0; i < str.length; i++) {
73 } else if (c == '\\') {
75 } else if (c == '"') {
80 } else if (c == '"') {
82 } else if (c === ' ') {
83 if (token.length > 0) {
92 if (token.length > 0) {
95 let token_starts = [];
96 for (let i = 0; i < token_ends.length; i++) {
97 token_starts.push(token_ends[i] - tokens[i].length);
99 return [tokens, token_starts];
101 parse_yx: function(position_string) {
102 let coordinate_strings = position_string.split(',')
103 let position = [0, 0];
104 position[0] = parseInt(coordinate_strings[0].slice(2));
105 position[1] = parseInt(coordinate_strings[1].slice(2));
111 init: function(url) {
112 this.websocket = new WebSocket(url);
113 this.websocket.onopen = function(event) {
114 window.setInterval(function() { server.send(['PING']) }, 30000);
115 server.send(['GET_GAMESTATE']);
118 send: function(tokens) {
119 this.websocket.send(unparser.untokenize(tokens));
124 quote: function(str) {
126 for (let i = 0; i < str.length; i++) {
128 if (c in ['"', '\\']) {
134 return quoted.join('');
136 to_yx: function(yx_coordinate) {
137 return "Y:" + yx_coordinate[0] + ",X:" + yx_coordinate[1];
139 untokenize: function(tokens) {
140 let quoted_tokens = [];
141 for (let token of tokens) {
142 quoted_tokens.push(this.quote(token));
144 return quoted_tokens.join(" ");
153 window_width: terminal.cols / 2,
158 this.recalc_input_lines();
159 this.height_header = this.height_turn_line + this.height_mode_line;
161 switch_mode: function(mode_name, keep_pos=false) {
162 if (mode_name == 'study' && !keep_pos) {
163 explorer.position = game.things[game.player_id];
165 this.mode = mode_name;
169 draw_mode_line: function() {
170 terminal.drawBox(1, this.window_width, this.height_mode_line, this.window_width);
171 terminal.write(1, this.window_width, 'MODE ' + this.mode);
173 draw_history: function() {
174 if (terminal.rows <= this.height_header + this.height_input) {
177 terminal.drawBox(this.height_header, this.window_width, terminal.rows - this.height_header - this.height_input, this.window_width);
178 for (let y = terminal.rows - 1 - this.height_input,
179 i = this.log.length - 1;
180 y >= this.height_header && i >= 0;
182 terminal.write(y, this.window_width, this.log[i]);
185 draw_map: function() {
186 terminal.drawBox(0, 0, terminal.rows, this.window_width);
189 for (let i = 0, j = 0; i < game.map.length; i++, j++) {
190 if (j == game.map_size[1]) {
191 map_lines.push(line);
195 line.push(game.map[i]);
197 map_lines.push(line);
198 let player_position = [0,0];
199 let center_pos = [Math.floor(game.map_size[0] / 2),
200 Math.floor(game.map_size[1] / 2)];
201 for (const thing_id in game.things) {
202 let t = game.things[thing_id];
203 map_lines[t[0]][t[1]] = '@';
204 if (game.player_id == thing_id) {
208 if (tui.mode == 'study' || tui.mode == 'annotate') {
209 map_lines[explorer.position[0]][explorer.position[1]] = '?';
210 center_pos = explorer.position;
212 let offset = [(terminal.rows / 2) - center_pos[0],
213 this.window_width / 2 - center_pos[1]];
214 for (let term_y = offset[0], map_y = 0;
215 term_y < terminal.rows && map_y < game.map_size[0];
218 let to_draw = map_lines[map_y].join('').slice(0, this.window_width - offset[1]);
219 terminal.write(term_y, offset[1], to_draw);
223 draw_turn_line: function(n) {
224 terminal.drawBox(0, this.window_width, 1, this.window_width);
225 terminal.write(0, this.window_width, 'turn: ' + game.turn);
227 empty_input: function(str) {
229 if (this.mode == 'annotate' || this.mode == 'chat') {
230 this.recalc_input_lines();
232 this.height_input = 0;
235 add_to_input: function(str) {
236 if (this.input.length + str.length > this.window_width * terminal.rows) {
240 this.recalc_input_lines();
242 recalc_input_lines: function() {
243 this.input_lines = this.msg_into_lines_of_width("> " + this.input, this.window_width);
244 this.height_input = this.input_lines.length;
246 shorten_input: function() {
247 this.input = tui.input.slice(0, -1);
248 this.recalc_input_lines();
250 draw_input: function() {
251 terminal.drawBox(terminal.rows - this.height_input, this.window_width, this.height_input, this.window_width);
252 if (this.mode == 'chat' || this.mode == 'annotate') {
253 for (let y = terminal.rows - this.height_input, i = 0; y < terminal.rows && i < this.input_lines.length; y++, i++) {
254 terminal.write(y, this.window_width, this.input_lines[i]);
258 msg_into_lines_of_width: function(msg, width) {
261 for (let i = 0, x = 0; i < msg.length; i++, x++) {
272 log_msg: function(msg) {
273 let lines = this.msg_into_lines_of_width(msg, this.window_width);
274 this.log = this.log.concat(lines);
275 while (this.log.length > terminal.rows) {
280 refresh: function() {
283 log_help: function() {
287 tui.log_msg("chat mode commands:");
288 tui.log_msg(":login USER - register as USER");
289 tui.log_msg(":msg USER TEXT - send TEXT to USER");
290 tui.log_msg(":help - show this help");
291 tui.log_msg(":play or :p - switch to play mode");
292 tui.log_msg(":study or :s - switch to study mode");
294 tui.log_msg("play mode commands:");
295 tui.log_msg("w, a, s, d - move avatar");
296 tui.log_msg("f - flatten surroundings");
297 tui.log_msg("e - write following ASCII character");
298 tui.log_msg("c - switch to chat mode");
299 tui.log_msg("? - switch to study mode");
301 tui.log_msg("study mode commands:");
302 tui.log_msg("w, a, s, d - move question mark");
303 tui.log_msg("A - annotate terrain");
304 tui.log_msg("c - switch to chat mode");
305 tui.log_msg("p - switch to play mode");
308 draw_info: function() {
309 terminal.drawBox(this.height_header, this.window_width, terminal.rows - this.height_header - this.height_input, this.window_width);
310 let lines = this.msg_into_lines_of_width(explorer.get_info(), this.window_width);
311 for (let y = this.height_header, i = 0; y < terminal.rows && i < lines.length; y++, i++) {
312 terminal.write(y, this.window_width, lines[i]);
315 full_refresh: function() {
317 this.draw_turn_line();
318 this.draw_mode_line();
319 if (this.mode == 'study' || this.mode == 'annotate') {
337 terminal.initialize();
342 server.init(websocket_location);
343 server.websocket.onmessage = function (event) {
344 let tokens = parser.tokenize(event.data)[0];
345 if (tokens[0] === 'TURN') {
347 game.turn = parseInt(tokens[1]);
348 } else if (tokens[0] === 'THING_POS') {
349 game.things[tokens[1]] = parser.parse_yx(tokens[2]);
350 } else if (tokens[0] === 'MAP') {
351 game.map_size = parser.parse_yx(tokens[1]);
353 } else if (tokens[0] === 'GAME_STATE_COMPLETE') {
354 explorer.empty_info_db();
355 if (tui.mode == 'study') {
356 explorer.query_info();
358 tui.draw_turn_line();
361 } else if (tokens[0] === 'CHAT') {
362 tui.log_msg('# ' + tokens[1], 1);
364 } else if (tokens[0] === 'PLAYER_ID') {
365 game.player_id = parseInt(tokens[1]);
366 } else if (tokens[0] === 'META') {
367 tui.log_msg('@ ' + tokens[1]);
369 } else if (tokens[0] === 'ANNOTATION') {
370 let position = parser.parse_yx(tokens[1]);
371 explorer.update_info_db(position, tokens[2]);
372 } else if (tokens[0] === 'UNHANDLED_INPUT') {
373 tui.log_msg('? unknown command');
375 } else if (tokens[0] === 'ARGUMENT_ERROR') {
376 tui.log_msg('? syntax error: ' + tokens[1]);
378 } else if (tokens[0] === 'GAME_ERROR') {
379 tui.log_msg('? game error: ' + tokens[1]);
381 } else if (tokens[0] === 'PONG') {
384 tui.log_msg('? unhandled input: ' + event.data);
392 move: function(direction) {
394 try_pos[0] = this.position[0];
395 try_pos[1] = this.position[1];
396 if (direction == 'left') {
398 } else if (direction == 'right') {
400 } else if (direction == 'up') {
402 } else if (direction == 'down') {
405 if (!(try_pos[0] < 0) &&
407 !(try_pos[0] >= game.map_size[0])
408 && !(try_pos[1] >= game.map_size[1])) {
409 this.position = try_pos;
416 update_info_db: function(yx, str) {
417 this.info_db[yx] = str;
418 if (tui.mode == 'study') {
423 empty_info_db: function() {
425 if (tui.mode == 'study') {
430 query_info: function() {
431 server.send(["GET_ANNOTATION", unparser.to_yx(explorer.position)]);
433 get_info: function() {
434 if (this.position in this.info_db) {
435 return this.info_db[this.position];
440 annotate: function(msg) {
441 if (msg.length == 0) {
442 msg = " "; // triggers annotation deletion
444 server.send(["ANNOTATE", unparser.to_yx(explorer.position), msg]);
448 document.addEventListener('keydown', (event) => {
449 if (tui.mode == 'chat') {
450 if (event.key.length === 1) {
451 tui.add_to_input(event.key);
453 } else if (event.key == 'Backspace') {
456 } else if (event.key == 'Enter') {
457 let [tokens, token_starts] = parser.tokenize(tui.input);
458 if (tokens.length > 0 && tokens[0].length > 0) {
459 if (tokens[0][0] == ':') {
460 if (tokens[0] == ':play' || tokens[0] == ':p') {
461 tui.switch_mode('play');
462 } else if (tokens[0] == ':study' || tokens[0] == ':s') {
463 tui.switch_mode('study');
464 } else if (tokens[0] == ':help') {
467 } else if (tokens[0] == ':login') {
468 if (tokens.length > 1) {
469 server.send(['LOGIN', tokens[1]]);
471 tui.log_msg('? need login name');
473 } else if (tokens[0] == ':msg') {
474 if (tokens.length > 2) {
475 let msg = tui.input.slice(token_starts[2]);
476 server.send(['QUERY', tokens[1], msg]);
478 tui.log_msg('? need message target and message');
481 tui.log_msg('? unknown command');
484 server.send(['ALL', tui.input]);
490 } else if (tui.mode == 'play') {
491 if (event.key === 'c') {
492 tui.switch_mode('chat');
493 } else if (event.key === 'e') {
494 tui.switch_mode('edit');
495 } else if (event.key === '?') {
496 tui.switch_mode('study');
497 } else if (event.key === 'F1') {
500 } else if (event.key === 'f') {
501 server.send(["TASK:FLATTEN_SURROUNDINGS"]);
502 } else if (event.key === 'a') {
503 server.send(['TASK:MOVE', 'LEFT']);
504 } else if (event.key === 'd') {
505 server.send(['TASK:MOVE', 'RIGHT']);
506 } else if (event.key === 'w') {
507 server.send(['TASK:MOVE', 'UP']);
508 } else if (event.key === 's') {
509 server.send(['TASK:MOVE', 'DOWN']);
511 } else if (tui.mode == 'edit') {
512 if (event.key != "Shift" && event.key.length == 1) {
513 server.send(["TASK:WRITE", event.key]);
514 tui.switch_mode('play');
516 } else if (tui.mode == 'study') {
517 if (event.key === 'c') {
518 tui.switch_mode('chat');
519 } else if (event.key == 'p') {
520 tui.switch_mode('play');
521 } else if (event.key === 'a') {
522 explorer.move('left');
523 } else if (event.key === 'd') {
524 explorer.move('right');
525 } else if (event.key === 'w') {
527 } else if (event.key === 's') {
528 explorer.move('down');
529 } else if (event.key === 'A') {
530 tui.switch_mode('annotate');
534 } else if (tui.mode == 'annotate') {
535 if (event.key.length === 1) {
536 tui.add_to_input(event.key);
538 } else if (event.key == 'Backspace') {
541 } else if (event.key == 'Enter') {
542 explorer.annotate(tui.input);
543 tui.switch_mode('study', true);