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(" ");
149 constructor(name, has_input_prompt=false, shows_annotations=false) {
151 this.has_input_prompt = has_input_prompt;
152 this.shows_annotations = shows_annotations;
155 let mode_chat = new Mode('chat', true, false);
156 let mode_annotate = new Mode('annotate', true, true);
157 let mode_play = new Mode('play', false, false);
158 let mode_study = new Mode('study', false, true);
159 let mode_edit = new Mode('edit', false, false);
166 window_width: terminal.cols / 2,
171 this.recalc_input_lines();
172 this.height_header = this.height_turn_line + this.height_mode_line;
174 switch_mode: function(mode, keep_pos=false) {
175 if (mode == mode_study && !keep_pos) {
176 explorer.position = game.things[game.player_id];
182 draw_mode_line: function() {
183 terminal.drawBox(1, this.window_width, this.height_mode_line, this.window_width);
184 terminal.write(1, this.window_width, 'MODE ' + this.mode.name);
186 draw_history: function() {
187 if (terminal.rows <= this.height_header + this.height_input) {
190 terminal.drawBox(this.height_header, this.window_width, terminal.rows - this.height_header - this.height_input, this.window_width);
191 for (let y = terminal.rows - 1 - this.height_input,
192 i = this.log.length - 1;
193 y >= this.height_header && i >= 0;
195 terminal.write(y, this.window_width, this.log[i]);
198 draw_map: function() {
199 terminal.drawBox(0, 0, terminal.rows, this.window_width);
202 for (let i = 0, j = 0; i < game.map.length; i++, j++) {
203 if (j == game.map_size[1]) {
204 map_lines.push(line);
208 line.push(game.map[i]);
210 map_lines.push(line);
211 let player_position = [0,0];
212 let center_pos = [Math.floor(game.map_size[0] / 2),
213 Math.floor(game.map_size[1] / 2)];
214 for (const thing_id in game.things) {
215 let t = game.things[thing_id];
216 map_lines[t[0]][t[1]] = '@';
217 if (game.player_id == thing_id) {
221 if (tui.mode.shows_annotations) {
222 map_lines[explorer.position[0]][explorer.position[1]] = '?';
223 center_pos = explorer.position;
225 let offset = [(terminal.rows / 2) - center_pos[0],
226 this.window_width / 2 - center_pos[1]];
227 for (let term_y = offset[0], map_y = 0;
228 term_y < terminal.rows && map_y < game.map_size[0];
231 let to_draw = map_lines[map_y].join('').slice(0, this.window_width - offset[1]);
232 terminal.write(term_y, offset[1], to_draw);
236 draw_turn_line: function(n) {
237 terminal.drawBox(0, this.window_width, 1, this.window_width);
238 terminal.write(0, this.window_width, 'turn: ' + game.turn);
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.length + str.length > this.window_width * terminal.rows) {
253 this.recalc_input_lines();
255 recalc_input_lines: function() {
256 this.input_lines = this.msg_into_lines_of_width("> " + this.input, this.window_width);
257 this.height_input = this.input_lines.length;
259 shorten_input: function() {
260 this.input = tui.input.slice(0, -1);
261 this.recalc_input_lines();
263 draw_input: function() {
264 terminal.drawBox(terminal.rows - this.height_input, this.window_width, this.height_input, this.window_width);
265 if (this.mode.has_input_prompt) {
266 for (let y = terminal.rows - this.height_input, i = 0; y < terminal.rows && i < this.input_lines.length; y++, i++) {
267 terminal.write(y, this.window_width, this.input_lines[i]);
271 msg_into_lines_of_width: function(msg, width) {
274 for (let i = 0, x = 0; i < msg.length; i++, x++) {
285 log_msg: function(msg) {
286 let lines = this.msg_into_lines_of_width(msg, this.window_width);
287 this.log = this.log.concat(lines);
288 while (this.log.length > terminal.rows) {
293 refresh: function() {
296 log_help: function() {
300 tui.log_msg("chat mode commands:");
301 tui.log_msg(":login USER - register as USER");
302 tui.log_msg(":msg USER TEXT - send TEXT to USER");
303 tui.log_msg(":help - show this help");
304 tui.log_msg(":play or :p - switch to play mode");
305 tui.log_msg(":study or :s - switch to study mode");
307 tui.log_msg("play mode commands:");
308 tui.log_msg("w, a, s, d - move avatar");
309 tui.log_msg("f - flatten surroundings");
310 tui.log_msg("e - write following ASCII character");
311 tui.log_msg("c - switch to chat mode");
312 tui.log_msg("? - switch to study mode");
314 tui.log_msg("study mode commands:");
315 tui.log_msg("w, a, s, d - move question mark");
316 tui.log_msg("A - annotate terrain");
317 tui.log_msg("c - switch to chat mode");
318 tui.log_msg("p - switch to play mode");
321 draw_info: function() {
322 terminal.drawBox(this.height_header, this.window_width, terminal.rows - this.height_header - this.height_input, this.window_width);
323 let lines = this.msg_into_lines_of_width(explorer.get_info(), this.window_width);
324 for (let y = this.height_header, i = 0; y < terminal.rows && i < lines.length; y++, i++) {
325 terminal.write(y, this.window_width, lines[i]);
328 full_refresh: function() {
330 this.draw_turn_line();
331 this.draw_mode_line();
332 if (this.mode.shows_annotations) {
350 terminal.initialize();
355 server.init(websocket_location);
356 server.websocket.onmessage = function (event) {
357 let tokens = parser.tokenize(event.data)[0];
358 if (tokens[0] === 'TURN') {
360 game.turn = parseInt(tokens[1]);
361 } else if (tokens[0] === 'THING_POS') {
362 game.things[tokens[1]] = parser.parse_yx(tokens[2]);
363 } else if (tokens[0] === 'MAP') {
364 game.map_size = parser.parse_yx(tokens[1]);
366 } else if (tokens[0] === 'GAME_STATE_COMPLETE') {
367 explorer.empty_info_db();
368 if (tui.mode == mode_study) {
369 explorer.query_info();
371 tui.draw_turn_line();
374 } else if (tokens[0] === 'CHAT') {
375 tui.log_msg('# ' + tokens[1], 1);
377 } else if (tokens[0] === 'PLAYER_ID') {
378 game.player_id = parseInt(tokens[1]);
379 } else if (tokens[0] === 'META') {
380 tui.log_msg('@ ' + tokens[1]);
382 } else if (tokens[0] === 'ANNOTATION') {
383 let position = parser.parse_yx(tokens[1]);
384 explorer.update_info_db(position, tokens[2]);
385 } else if (tokens[0] === 'UNHANDLED_INPUT') {
386 tui.log_msg('? unknown command');
388 } else if (tokens[0] === 'ARGUMENT_ERROR') {
389 tui.log_msg('? syntax error: ' + tokens[1]);
391 } else if (tokens[0] === 'GAME_ERROR') {
392 tui.log_msg('? game error: ' + tokens[1]);
394 } else if (tokens[0] === 'PONG') {
397 tui.log_msg('? unhandled input: ' + event.data);
405 move: function(direction) {
407 try_pos[0] = this.position[0];
408 try_pos[1] = this.position[1];
409 if (direction == 'left') {
411 } else if (direction == 'right') {
413 } else if (direction == 'up') {
415 } else if (direction == 'down') {
418 if (!(try_pos[0] < 0) &&
420 !(try_pos[0] >= game.map_size[0])
421 && !(try_pos[1] >= game.map_size[1])) {
422 this.position = try_pos;
429 update_info_db: function(yx, str) {
430 this.info_db[yx] = str;
431 if (tui.mode == mode_study) {
436 empty_info_db: function() {
438 if (tui.mode == mode_study) {
443 query_info: function() {
444 server.send(["GET_ANNOTATION", unparser.to_yx(explorer.position)]);
446 get_info: function() {
447 if (this.position in this.info_db) {
448 return this.info_db[this.position];
453 annotate: function(msg) {
454 if (msg.length == 0) {
455 msg = " "; // triggers annotation deletion
457 server.send(["ANNOTATE", unparser.to_yx(explorer.position), msg]);
461 document.addEventListener('keydown', (event) => {
462 if (tui.mode.has_input_prompt && event.key.length === 1) {
463 tui.add_to_input(event.key);
465 } else if (tui.mode.has_input_prompt && event.key == 'Backspace') {
468 } else if (tui.mode == mode_annotate && event.key == 'Enter') {
469 explorer.annotate(tui.input);
470 tui.switch_mode(mode_study, true);
471 } else if (tui.mode == mode_chat && event.key == 'Enter') {
472 let [tokens, token_starts] = parser.tokenize(tui.input);
473 if (tokens.length > 0 && tokens[0].length > 0) {
474 if (tokens[0][0] == ':') {
475 if (tokens[0] == ':play' || tokens[0] == ':p') {
476 tui.switch_mode(mode_play);
477 } else if (tokens[0] == ':study' || tokens[0] == ':s') {
478 tui.switch_mode(mode_study);
479 } else if (tokens[0] == ':help') {
482 } else if (tokens[0] == ':login') {
483 if (tokens.length > 1) {
484 server.send(['LOGIN', tokens[1]]);
486 tui.log_msg('? need login name');
488 } else if (tokens[0] == ':msg') {
489 if (tokens.length > 2) {
490 let msg = tui.input.slice(token_starts[2]);
491 server.send(['QUERY', tokens[1], msg]);
493 tui.log_msg('? need message target and message');
496 tui.log_msg('? unknown command');
499 server.send(['ALL', tui.input]);
504 } else if (tui.mode == mode_play) {
505 if (event.key === 'c') {
506 tui.switch_mode(mode_chat);
507 } else if (event.key === 'e') {
508 tui.switch_mode(mode_edit);
509 } else if (event.key === '?') {
510 tui.switch_mode(mode_study);
511 } else if (event.key === 'F1') {
514 } else if (event.key === 'f') {
515 server.send(["TASK:FLATTEN_SURROUNDINGS"]);
516 } else if (event.key === 'a') {
517 server.send(['TASK:MOVE', 'LEFT']);
518 } else if (event.key === 'd') {
519 server.send(['TASK:MOVE', 'RIGHT']);
520 } else if (event.key === 'w') {
521 server.send(['TASK:MOVE', 'UP']);
522 } else if (event.key === 's') {
523 server.send(['TASK:MOVE', 'DOWN']);
525 } else if (tui.mode == mode_edit) {
526 if (event.key != "Shift" && event.key.length == 1) {
527 server.send(["TASK:WRITE", event.key]);
528 tui.switch_mode(mode_play);
530 } else if (tui.mode == mode_study) {
531 if (event.key === 'c') {
532 tui.switch_mode(mode_chat);
533 } else if (event.key == 'p') {
534 tui.switch_mode(mode_play);
535 } else if (event.key === 'a') {
536 explorer.move('left');
537 } else if (event.key === 'd') {
538 explorer.move('right');
539 } else if (event.key === 'w') {
541 } else if (event.key === 's') {
542 explorer.move('down');
543 } else if (event.key === 'A') {
544 tui.switch_mode(mode_annotate);