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,
157 this.recalc_input_lines();
159 switch_mode: function(mode_name, keep_pos=false) {
160 if (mode_name == 'study' && !keep_pos) {
161 explorer.position = game.things[game.player_id];
163 this.mode = mode_name;
166 draw_history: function() {
167 if (terminal.rows <= this.height_turn_line + this.height_input) {
170 terminal.drawBox(this.height_turn_line, this.window_width, terminal.rows - this.height_turn_line - this.height_input, this.window_width);
171 console.log(this.log);
172 for (let y = terminal.rows - this.height_input - this.height_turn_line,
173 i = this.log.length - 1;
174 y >= this.height_turn_line && i >= 0;
176 terminal.write(y, this.window_width, this.log[i]);
179 draw_map: function() {
180 terminal.drawBox(0, 0, terminal.rows, this.window_width);
183 for (let i = 0, j = 0; i < game.map.length; i++, j++) {
184 if (j == game.map_size[1]) {
185 map_lines.push(line);
189 line.push(game.map[i]);
191 map_lines.push(line);
192 let player_position = [0,0];
193 let center_pos = [Math.floor(game.map_size[0] / 2),
194 Math.floor(game.map_size[1] / 2)];
195 for (const thing_id in game.things) {
196 let t = game.things[thing_id];
197 map_lines[t[0]][t[1]] = '@';
198 if (game.player_id == thing_id) {
202 if (tui.mode == 'study' || tui.mode == 'annotate') {
203 map_lines[explorer.position[0]][explorer.position[1]] = '?';
204 center_pos = explorer.position;
206 let offset = [(terminal.rows / 2) - center_pos[0],
207 this.window_width / 2 - center_pos[1]];
208 for (let term_y = offset[0], map_y = 0;
209 term_y < terminal.rows && map_y < game.map_size[0];
212 let to_draw = map_lines[map_y].join('').slice(0, this.window_width - offset[1]);
213 terminal.write(term_y, offset[1], to_draw);
217 draw_turn_line: function(n) {
218 terminal.drawBox(0, this.window_width, 1, this.window_width);
219 terminal.write(0, this.window_width, 'turn: ' + game.turn);
221 empty_input: function(str) {
223 this.recalc_input_lines();
225 add_to_input: function(str) {
226 if (this.input.length + str.length > this.window_width * terminal.rows) {
230 this.recalc_input_lines();
232 recalc_input_lines: function() {
233 this.input_lines = this.msg_into_lines_of_width("> " + this.input, this.window_width);
234 this.height_input = this.input_lines.length;
236 shorten_input: function() {
237 this.input = tui.input.slice(0, -1);
238 this.recalc_input_lines();
240 draw_input: function() {
241 terminal.drawBox(terminal.rows - this.height_input, this.window_width, this.height_input, this.window_width);
242 if (this.mode == 'chat' || this.mode == 'annotate') {
243 for (let y = terminal.rows - this.height_input, i = 0; y < terminal.rows && i < this.input_lines.length; y++, i++) {
244 terminal.write(y, this.window_width, this.input_lines[i]);
248 msg_into_lines_of_width: function(msg, width) {
251 for (let i = 0, x = 0; i < msg.length; i++, x++) {
262 log_msg: function(msg) {
263 let lines = this.msg_into_lines_of_width(msg, this.window_width);
264 this.log = this.log.concat(lines);
265 while (this.log.length > terminal.rows - 2) {
270 refresh: function() {
273 log_help: function() {
277 tui.log_msg("chat mode commands:");
278 tui.log_msg(":login USER - register as USER");
279 tui.log_msg(":msg USER TEXT - send TEXT to USER");
280 tui.log_msg(":help - show this help");
281 tui.log_msg(":play or :p - switch to play mode");
282 tui.log_msg(":study or :s - switch to study mode");
284 tui.log_msg("play mode commands:");
285 tui.log_msg("w, a, s, d - move avatar");
286 tui.log_msg("f - flatten surroundings");
287 tui.log_msg("e - write following ASCII character");
288 tui.log_msg("c - switch to chat mode");
289 tui.log_msg("? - switch to study mode");
291 tui.log_msg("study mode commands:");
292 tui.log_msg("w, a, s, d - move question mark");
293 tui.log_msg("A - annotate terrain");
294 tui.log_msg("c - switch to chat mode");
295 tui.log_msg("p - switch to play mode");
298 draw_info: function() {
299 terminal.drawBox(this.height_turn_line, this.window_width, terminal.rows - this.height_turn_line - this.height_input, this.window_width);
300 let lines = this.msg_into_lines_of_width(explorer.get_info(), this.window_width);
301 for (let y = this.height_turn_line, i = 0; y < terminal.rows && i < lines.length; y++, i++) {
302 terminal.write(y, this.window_width, lines[i]);
305 full_refresh: function() {
307 this.draw_turn_line();
308 if (this.mode == 'study' || this.mode == 'annotate') {
326 terminal.initialize();
331 server.init(websocket_location);
332 server.websocket.onmessage = function (event) {
333 let tokens = parser.tokenize(event.data)[0];
334 if (tokens[0] === 'TURN') {
336 game.turn = parseInt(tokens[1]);
337 } else if (tokens[0] === 'THING_POS') {
338 game.things[tokens[1]] = parser.parse_yx(tokens[2]);
339 } else if (tokens[0] === 'MAP') {
340 game.map_size = parser.parse_yx(tokens[1]);
342 } else if (tokens[0] === 'GAME_STATE_COMPLETE') {
343 explorer.empty_info_db();
344 if (tui.mode == 'study') {
345 explorer.query_info();
347 tui.draw_turn_line();
350 } else if (tokens[0] === 'CHAT') {
351 tui.log_msg('# ' + tokens[1], 1);
353 } else if (tokens[0] === 'PLAYER_ID') {
354 game.player_id = parseInt(tokens[1]);
355 } else if (tokens[0] === 'META') {
356 tui.log_msg('@ ' + tokens[1]);
358 } else if (tokens[0] === 'ANNOTATION') {
359 let position = parser.parse_yx(tokens[1]);
360 explorer.update_info_db(position, tokens[2]);
361 } else if (tokens[0] === 'UNHANDLED_INPUT') {
362 tui.log_msg('? unknown command');
364 } else if (tokens[0] === 'ARGUMENT_ERROR') {
365 tui.log_msg('? syntax error: ' + tokens[1]);
367 } else if (tokens[0] === 'GAME_ERROR') {
368 tui.log_msg('? game error: ' + tokens[1]);
370 } else if (tokens[0] === 'PONG') {
373 tui.log_msg('? unhandled input: ' + event.data);
381 move: function(direction) {
383 try_pos[0] = this.position[0];
384 try_pos[1] = this.position[1];
385 if (direction == 'left') {
387 } else if (direction == 'right') {
389 } else if (direction == 'up') {
391 } else if (direction == 'down') {
394 if (!(try_pos[0] < 0) &&
396 !(try_pos[0] >= game.map_size[0])
397 && !(try_pos[1] >= game.map_size[1])) {
398 this.position = try_pos;
405 update_info_db: function(yx, str) {
406 this.info_db[yx] = str;
407 if (tui.mode == 'study') {
412 empty_info_db: function() {
414 if (tui.mode == 'study') {
419 query_info: function() {
420 server.send(["GET_ANNOTATION", unparser.to_yx(explorer.position)]);
422 get_info: function() {
423 if (this.position in this.info_db) {
424 return this.info_db[this.position];
429 annotate: function(msg) {
430 if (msg.length == 0) {
431 msg = " "; // triggers annotation deletion
433 server.send(["ANNOTATE", unparser.to_yx(explorer.position), msg]);
437 document.addEventListener('keydown', (event) => {
438 if (tui.mode == 'chat') {
439 if (event.key.length === 1) {
440 tui.add_to_input(event.key);
442 } else if (event.key == 'Backspace') {
445 } else if (event.key == 'Enter') {
446 let [tokens, token_starts] = parser.tokenize(tui.input);
447 if (tokens.length > 0 && tokens[0].length > 0) {
448 if (tokens[0][0] == ':') {
449 if (tokens[0] == ':play' || tokens[0] == ':p') {
450 tui.switch_mode('play');
451 } else if (tokens[0] == ':study' || tokens[0] == ':s') {
452 tui.switch_mode('study');
453 } else if (tokens[0] == ':help') {
456 } else if (tokens[0] == ':login') {
457 if (tokens.length > 1) {
458 server.send(['LOGIN', tokens[1]]);
460 tui.log_msg('? need login name');
462 } else if (tokens[0] == ':msg') {
463 if (tokens.length > 2) {
464 let msg = tui.input.slice(token_starts[2]);
465 server.send(['QUERY', tokens[1], msg]);
467 tui.log_msg('? need message target and message');
470 tui.log_msg('? unknown command');
473 server.send(['ALL', tui.input]);
479 } else if (tui.mode == 'play') {
480 if (event.key === 'c') {
481 tui.switch_mode('chat');
482 } else if (event.key === 'e') {
483 tui.switch_mode('edit');
484 } else if (event.key === '?') {
485 tui.switch_mode('study');
486 } else if (event.key === 'F1') {
489 } else if (event.key === 'f') {
490 server.send(["TASK:FLATTEN_SURROUNDINGS"]);
491 } else if (event.key === 'a') {
492 server.send(['TASK:MOVE', 'LEFT']);
493 } else if (event.key === 'd') {
494 server.send(['TASK:MOVE', 'RIGHT']);
495 } else if (event.key === 'w') {
496 server.send(['TASK:MOVE', 'UP']);
497 } else if (event.key === 's') {
498 server.send(['TASK:MOVE', 'DOWN']);
500 } else if (tui.mode == 'edit') {
501 if (event.key != "Shift" && event.key.length == 1) {
502 server.send(["TASK:WRITE", event.key]);
503 tui.switch_mode('play');
505 } else if (tui.mode == 'study') {
506 if (event.key === 'c') {
507 tui.switch_mode('chat');
508 } else if (event.key == 'p') {
509 tui.switch_mode('play');
510 } else if (event.key === 'a') {
511 explorer.move('left');
512 } else if (event.key === 'd') {
513 explorer.move('right');
514 } else if (event.key === 'w') {
516 } else if (event.key === 's') {
517 explorer.move('down');
518 } else if (event.key === 'A') {
519 tui.switch_mode('annotate');
523 } else if (tui.mode == 'annotate') {
524 if (event.key.length === 1) {
525 tui.add_to_input(event.key);
527 } else if (event.key == 'Backspace') {
530 } else if (event.key == 'Enter') {
531 explorer.annotate(tui.input);
533 tui.switch_mode('study', true);