6 <pre id="terminal" style="display: inline-block; color: white; background-color: black;"></pre>
9 let websocket_location = "ws://localhost:8000";
16 initialize: function() {
17 this.pre_el = document.getElementById("terminal");
18 this.pre_el.style.color = this.foreground;
19 this.pre_el.style.backgroundColor = this.background;
22 for (let y = 0, x = 0; y <= this.rows; x++) {
26 this.content.push(line);
35 blink_screen: function() {
36 this.pre_el.style.color = this.background;
37 this.pre_el.style.backgroundColor = this.foreground;
39 this.pre_el.style.color = this.foreground;
40 this.pre_el.style.backgroundColor = this.background;
45 for (let y = 0; y < this.rows; y++) {
46 let line = this.content[y].join('');
47 pre_string += line + '\n';
49 this.pre_el.textContent = pre_string;
51 write: function(start_y, start_x, msg) {
52 for (let x = start_x, i = 0; x < this.cols && i < msg.length; x++, i++) {
53 this.content[start_y][x] = msg[i];
56 drawBox: function(start_y, start_x, height, width) {
57 let end_y = start_y + height;
58 let end_x = start_x + width;
59 for (let y = start_y, x = start_x; y < this.rows; x++) {
67 this.content[y][x] = ' ';
73 tokenize: function(str) {
79 for (let i = 0; i < str.length; i++) {
85 } else if (c == '\\') {
87 } else if (c == '"') {
92 } else if (c == '"') {
94 } else if (c === ' ') {
95 if (token.length > 0) {
104 if (token.length > 0) {
107 let token_starts = [];
108 for (let i = 0; i < token_ends.length; i++) {
109 token_starts.push(token_ends[i] - tokens[i].length);
111 return [tokens, token_starts];
113 parse_yx: function(position_string) {
114 let coordinate_strings = position_string.split(',')
115 let position = [0, 0];
116 position[0] = parseInt(coordinate_strings[0].slice(2));
117 position[1] = parseInt(coordinate_strings[1].slice(2));
123 init: function(url) {
124 this.websocket = new WebSocket(url);
125 this.websocket.onopen = function(event) {
126 window.setInterval(function() { server.send(['PING']) }, 30000);
127 tui.log_msg("@ server connected!");
131 send: function(tokens) {
132 if (this.websocket.readyState !== WebSocket.OPEN) {
133 tui.log_msg('server disconnected :(');
135 this.websocket.send(unparser.untokenize(tokens));
141 quote: function(str) {
143 for (let i = 0; i < str.length; i++) {
145 if (c in ['"', '\\']) {
151 return quoted.join('');
153 to_yx: function(yx_coordinate) {
154 return "Y:" + yx_coordinate[0] + ",X:" + yx_coordinate[1];
156 untokenize: function(tokens) {
157 let quoted_tokens = [];
158 for (let token of tokens) {
159 quoted_tokens.push(this.quote(token));
161 return quoted_tokens.join(" ");
166 constructor(name, has_input_prompt=false, shows_annotations=false, is_intro=false) {
168 this.has_input_prompt = has_input_prompt;
169 this.shows_annotations = shows_annotations;
170 this.is_intro = is_intro;
173 let mode_waiting_for_server = new Mode('waiting_for_server', false, false, true);
174 let mode_login = new Mode('login', true, false, true);
175 let mode_chat = new Mode('chat / write messages to players', true, false);
176 let mode_annotate = new Mode('add message to map tile', true, true);
177 let mode_play = new Mode('play / move around', false, false);
178 let mode_study = new Mode('check map tiles for messages', false, true);
179 let mode_edit = new Mode('write ASCII char to map tile', false, false);
182 mode: mode_waiting_for_server,
187 window_width: terminal.cols / 2,
192 this.recalc_input_lines();
193 this.height_header = this.height_turn_line + this.height_mode_line;
194 this.log_msg("@ waiting for server connection ...");
196 init_login: function() {
197 this.log_msg("@ please enter your username:");
198 this.switch_mode(mode_login);
200 switch_mode: function(mode, keep_pos=false) {
201 if (mode == mode_study && !keep_pos) {
202 explorer.position = game.things[game.player_id];
206 if (mode == mode_annotate && explorer.position in explorer.info_db) {
207 let info = explorer.info_db[explorer.position];
208 if (info != "(none)") {
209 this.add_to_input(explorer.info_db[explorer.position]);
214 empty_input: function(str) {
216 if (this.mode.has_input_prompt) {
217 this.recalc_input_lines();
219 this.height_input = 0;
222 add_to_input: function(str) {
223 if (this.input_prompt.length + this.input.length + str.length > this.window_width * terminal.rows) {
227 this.recalc_input_lines();
230 recalc_input_lines: function() {
231 this.input_lines = this.msg_into_lines_of_width(this.input_prompt + this.input, this.window_width);
232 this.height_input = this.input_lines.length;
234 shorten_input: function() {
235 if (this.input.length == 0) {
236 terminal.blink_screen();
238 this.input = tui.input.slice(0, -1);
239 this.recalc_input_lines();
243 msg_into_lines_of_width: function(msg, width) {
246 for (let i = 0, x = 0; i < msg.length; i++, x++) {
257 log_msg: function(msg) {
258 let lines = this.msg_into_lines_of_width(msg, this.window_width);
259 this.log = this.log.concat(lines);
260 while (this.log.length > terminal.rows) {
265 log_help: function() {
267 this.log_msg("HELP:");
268 this.log_msg("chat mode commands:");
269 this.log_msg(" :nick NAME - re-name yourself to NAME");
270 this.log_msg(" :msg USER TEXT - send TEXT to USER");
271 this.log_msg(" :help - show this help");
272 this.log_msg(" :p or :play - switch to play mode");
273 this.log_msg(" :? or :study - switch to study mode");
274 this.log_msg("commands common to study and play mode:");
275 this.log_msg(" w, a, s, d - move");
276 this.log_msg(" c - switch to chat mode");
277 this.log_msg("commands specific to play mode:");
278 this.log_msg(" e - write following ASCII character");
279 this.log_msg(" f - flatten surroundings");
280 this.log_msg(" ? - switch to study mode");
281 this.log_msg("commands specific to study mode:");
282 this.log_msg(" e - annotate terrain");
283 this.log_msg(" p - switch to play mode");
286 draw_map: function() {
289 for (let i = 0, j = 0; i < game.map.length; i++, j++) {
290 if (j == game.map_size[1]) {
291 map_lines.push(line);
295 line.push(game.map[i]);
297 map_lines.push(line);
298 let player_position = [0,0];
299 let center_pos = [Math.floor(game.map_size[0] / 2),
300 Math.floor(game.map_size[1] / 2)];
301 for (const thing_id in game.things) {
302 let t = game.things[thing_id];
303 map_lines[t[0]][t[1]] = '@';
304 if (game.player_id == thing_id) {
308 if (tui.mode.shows_annotations) {
309 map_lines[explorer.position[0]][explorer.position[1]] = '?';
310 center_pos = explorer.position;
312 let offset = [(terminal.rows / 2) - center_pos[0],
313 this.window_width / 2 - center_pos[1]];
314 for (let term_y = offset[0], map_y = 0;
315 term_y < terminal.rows && map_y < game.map_size[0];
318 let to_draw = map_lines[map_y].join('').slice(0, this.window_width - offset[1]);
319 terminal.write(term_y, offset[1], to_draw);
323 draw_mode_line: function() {
324 terminal.write(0, this.window_width, 'MODE: ' + this.mode.name);
326 draw_turn_line: function(n) {
327 terminal.write(1, this.window_width, 'TURN: ' + game.turn);
329 draw_history: function() {
330 if (terminal.rows <= this.height_header + this.height_input) {
333 for (let y = terminal.rows - 1 - this.height_input,
334 i = this.log.length - 1;
335 y >= this.height_header && i >= 0;
337 terminal.write(y, this.window_width, this.log[i]);
340 draw_info: function() {
341 let lines = this.msg_into_lines_of_width(explorer.get_info(), this.window_width);
342 for (let y = this.height_header, i = 0; y < terminal.rows && i < lines.length; y++, i++) {
343 terminal.write(y, this.window_width, lines[i]);
346 draw_input: function() {
347 if (this.mode.has_input_prompt) {
348 for (let y = terminal.rows - this.height_input, i = 0; y < terminal.rows && i < this.input_lines.length; y++, i++) {
349 terminal.write(y, this.window_width, this.input_lines[i]);
353 full_refresh: function() {
354 terminal.drawBox(0, 0, terminal.rows, terminal.cols);
355 if (this.mode.is_intro) {
360 this.draw_turn_line();
361 this.draw_mode_line();
362 if (this.mode.shows_annotations) {
381 terminal.initialize();
385 server.init(websocket_location);
386 server.websocket.onmessage = function (event) {
387 let tokens = parser.tokenize(event.data)[0];
388 if (tokens[0] === 'TURN') {
390 game.turn = parseInt(tokens[1]);
391 } else if (tokens[0] === 'THING_POS') {
392 game.things[tokens[1]] = parser.parse_yx(tokens[2]);
393 } else if (tokens[0] === 'MAP') {
394 game.map_size = parser.parse_yx(tokens[1]);
396 } else if (tokens[0] === 'GAME_STATE_COMPLETE') {
397 explorer.empty_info_db();
398 if (tui.mode == mode_study) {
399 explorer.query_info();
402 } else if (tokens[0] === 'CHAT') {
403 tui.log_msg('# ' + tokens[1], 1);
404 } else if (tokens[0] === 'PLAYER_ID') {
405 game.player_id = parseInt(tokens[1]);
406 } else if (tokens[0] === 'META') {
407 tui.log_msg('@ ' + tokens[1]);
408 } else if (tokens[0] === 'LOGIN_OK') {
409 server.send(['GET_GAMESTATE']);
410 tui.log_msg('@ ' + tokens[1]);
412 tui.switch_mode(mode_chat);
413 } else if (tokens[0] === 'ANNOTATION') {
414 let position = parser.parse_yx(tokens[1]);
415 explorer.update_info_db(position, tokens[2]);
416 } else if (tokens[0] === 'UNHANDLED_INPUT') {
417 tui.log_msg('? unknown command');
418 } else if (tokens[0] === 'PLAY_ERROR') {
419 terminal.blink_screen();
420 } else if (tokens[0] === 'ARGUMENT_ERROR') {
421 tui.log_msg('? syntax error: ' + tokens[1]);
422 } else if (tokens[0] === 'GAME_ERROR') {
423 tui.log_msg('? game error: ' + tokens[1]);
424 } else if (tokens[0] === 'PONG') {
427 tui.log_msg('? unhandled input: ' + event.data);
434 move: function(direction) {
436 try_pos[0] = this.position[0];
437 try_pos[1] = this.position[1];
438 if (direction == 'left') {
440 } else if (direction == 'right') {
442 } else if (direction == 'up') {
444 } else if (direction == 'down') {
447 if (!(try_pos[0] < 0) &&
449 !(try_pos[0] >= game.map_size[0])
450 && !(try_pos[1] >= game.map_size[1])) {
451 this.position = try_pos;
456 update_info_db: function(yx, str) {
457 this.info_db[yx] = str;
458 if (tui.mode == mode_study) {
462 empty_info_db: function() {
464 if (tui.mode == mode_study) {
468 query_info: function() {
469 server.send(["GET_ANNOTATION", unparser.to_yx(explorer.position)]);
471 get_info: function() {
472 if (this.position in this.info_db) {
473 return this.info_db[this.position];
478 annotate: function(msg) {
479 if (msg.length == 0) {
480 msg = " "; // triggers annotation deletion
482 server.send(["ANNOTATE", unparser.to_yx(explorer.position), msg]);
486 document.addEventListener('keydown', (event) => {
487 if (tui.mode.has_input_prompt && event.key.length === 1) {
488 tui.add_to_input(event.key);
489 } else if (tui.mode.has_input_prompt && event.key == 'Backspace') {
491 } else if (tui.mode == mode_login && event.key == 'Enter') {
492 server.send(['LOGIN', tui.input]);
493 tui.switch_mode(mode_login);
494 } else if (tui.mode == mode_annotate && event.key == 'Enter') {
495 explorer.annotate(tui.input);
496 tui.switch_mode(mode_study, true);
497 } else if (tui.mode == mode_chat && event.key == 'Enter') {
498 let [tokens, token_starts] = parser.tokenize(tui.input);
499 if (tokens.length > 0 && tokens[0].length > 0) {
500 if (tokens[0][0] == ':') {
501 if (tokens[0] == ':play' || tokens[0] == ':p') {
502 tui.switch_mode(mode_play);
503 } else if (tokens[0] == ':study' || tokens[0] == ':?') {
504 tui.switch_mode(mode_study);
505 } else if (tokens[0] == ':help') {
507 } else if (tokens[0] == ':nick') {
508 if (tokens.length > 1) {
509 server.send(['LOGIN', tokens[1]]);
511 tui.log_msg('? need login name');
513 } else if (tokens[0] == ':msg') {
514 if (tokens.length > 2) {
515 let msg = tui.input.slice(token_starts[2]);
516 server.send(['QUERY', tokens[1], msg]);
518 tui.log_msg('? need message target and message');
521 tui.log_msg('? unknown command');
524 server.send(['ALL', tui.input]);
529 } else if (tui.mode == mode_play) {
530 if (event.key === 'c') {
531 tui.switch_mode(mode_chat);
532 } else if (event.key === 'e') {
533 tui.switch_mode(mode_edit);
534 } else if (event.key === '?') {
535 tui.switch_mode(mode_study);
536 } else if (event.key === 'F1') {
538 } else if (event.key === 'f') {
539 server.send(["TASK:FLATTEN_SURROUNDINGS"]);
540 } else if (event.key === 'a') {
541 server.send(['TASK:MOVE', 'LEFT']);
542 } else if (event.key === 'd') {
543 server.send(['TASK:MOVE', 'RIGHT']);
544 } else if (event.key === 'w') {
545 server.send(['TASK:MOVE', 'UP']);
546 } else if (event.key === 's') {
547 server.send(['TASK:MOVE', 'DOWN']);
549 } else if (tui.mode == mode_edit) {
550 if (event.key != "Shift" && event.key.length == 1) {
551 server.send(["TASK:WRITE", event.key]);
552 tui.switch_mode(mode_play);
554 } else if (tui.mode == mode_study) {
555 if (event.key === 'c') {
556 tui.switch_mode(mode_chat);
557 } else if (event.key == 'p') {
558 tui.switch_mode(mode_play);
559 } else if (event.key === 'a') {
560 explorer.move('left');
561 } else if (event.key === 'd') {
562 explorer.move('right');
563 } else if (event.key === 'w') {
565 } else if (event.key === 's') {
566 explorer.move('down');
567 } else if (event.key === 'e') {
568 tui.switch_mode(mode_annotate);