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;; 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(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));
110 function quote(str) {
112 for (let i = 0; i < str.length; i++) {
114 if (c in ['"', '\\']) {
120 return quoted.join('');
127 switch_mode: function(mode_name, keep_pos=false) {
128 if (mode_name == 'study' && !keep_pos) {
129 explorer.position = game.things[game.player_id];
131 this.mode = mode_name;
134 draw_history: function() {
135 terminal.drawBox(1, terminal.cols / 2, terminal.rows - 2, terminal.cols / 2);
137 for (let line of this.log) {
138 terminal.write(terminal.rows - 2 - i, terminal.cols / 2, line);
142 draw_map: function() {
143 terminal.drawBox(0, 0, terminal.rows, terminal.cols / 2);
146 for (let i = 0, j = 0; i < game.map.length; i++, j++) {
147 if (j == game.map_size[1]) {
148 map_lines.push(line);
152 line.push(game.map[i]);
154 map_lines.push(line);
155 let player_position = [0,0];
156 for (const thing_id in game.things) {
157 let t = game.things[thing_id];
158 map_lines[t[0]][t[1]] = '@';
159 if (game.player_id == thing_id) {
163 let center_pos = player_position;
164 if (tui.mode == 'study' || tui.mode == 'annotate') {
165 map_lines[explorer.position[0]][explorer.position[1]] = '?';
166 center_pos = explorer.position;
168 let offset = [(terminal.rows / 2) - center_pos[0],
169 terminal.cols / 4 - center_pos[1]];
170 for (let term_y = offset[0], map_y = 0;
171 term_y < terminal.rows && map_y < game.map_size[0];
174 let to_draw = map_lines[map_y].join('').slice(0, terminal.cols / 2 - offset[1]);
175 terminal.write(term_y, offset[1], to_draw);
179 draw_turn_line: function(n) {
180 terminal.drawBox(0, terminal.cols / 2, 1, terminal.cols / 2);
181 terminal.write(0, terminal.cols / 2, 'turn: ' + game.turn);
183 draw_input_line: function() {
184 terminal.drawBox(terminal.rows - 1, terminal.cols / 2, 1, terminal.cols / 2);
185 if (this.mode == 'chat' || this.mode == 'annotate') {
186 terminal.write(terminal.rows - 1, terminal.cols / 2, '> ' + this.input_line);
189 msg_into_lines_of_width: function(msg, width) {
192 for (let i = 0, x = 0; i < msg.length; i++, x++) {
194 lines.unshift(chunk);
200 lines.unshift(chunk);
203 log_msg: function(msg) {
204 let line_length = (terminal.cols / 2);
206 this.log = this.msg_into_lines_of_width(msg, terminal.cols / 2).concat(this.log);
207 while (this.log.length > terminal.rows - 2) {
212 refresh: function() {
215 log_help: function() {
218 tui.log_msg("chat mode commands:");
220 tui.log_msg("/login USER - register as USER");
221 tui.log_msg("/msg USER TEXT - send TEXT to USER");
222 tui.log_msg("/help - show this help");
223 tui.log_msg("/play - switch to play mode");
225 tui.log_msg("play mode commands:");
226 tui.log_msg("w, a, s, d - move avatar");
227 tui.log_msg("f - flatten surroundings");
228 tui.log_msg("e - write following ASCII character");
229 tui.log_msg("c - switch to chat mode");
230 tui.log_msg("? - switch to investigation mode");
232 tui.log_msg("investigation mode commands:");
233 tui.log_msg("w, a, s, d - move question mark");
234 tui.log_msg("A - annotate terrain");
235 tui.log_msg("c - switch to chat mode");
236 tui.log_msg("p - switch to play mode");
239 draw_info: function() {
240 terminal.drawBox(1, terminal.cols / 2, terminal.rows - 2, terminal.cols / 2);
241 let lines = this.msg_into_lines_of_width(explorer.get_info(), terminal.cols / 2);
243 for (let y = 1, i = 0; y < terminal.rows && i < lines.length; y++, i++) {
244 terminal.write(y, terminal.cols / 2, lines[i]);
247 full_refresh: function() {
249 this.draw_turn_line();
250 if (this.mode == 'study' || this.mode == 'annotate') {
255 this.draw_input_line();
268 terminal.initialize();
272 let websocket = new WebSocket(websocket_location);
273 websocket.onmessage = function (event) {
274 let tokens = parser.tokenize(event.data)[0];
275 if (tokens[0] === 'TURN') {
277 game.turn = parseInt(tokens[1]);
278 } else if (tokens[0] === 'THING_POS') {
279 game.things[tokens[1]] = parser.parse_yx(tokens[2]);
280 } else if (tokens[0] === 'MAP') {
281 game.map_size = parser.parse_yx(tokens[1]);
283 } else if (tokens[0] === 'GAME_STATE_COMPLETE') {
284 explorer.empty_info_db();
285 if (tui.mode == 'study') {
286 explorer.query_info();
288 tui.draw_turn_line();
291 } else if (tokens[0] === 'CHAT') {
292 tui.log_msg('# ' + tokens[1], 1);
294 } else if (tokens[0] === 'PLAYER_ID') {
295 game.player_id = parseInt(tokens[1]);
296 } else if (tokens[0] === 'META') {
297 tui.log_msg('@ ' + tokens[1]);
299 } else if (tokens[0] === 'ANNOTATION') {
300 let position = parser.parse_yx(tokens[1]);
301 explorer.update_info_db(position, tokens[2]);
302 } else if (tokens[0] === 'UNHANDLED_INPUT') {
303 tui.log_msg('? unknown command');
305 } else if (tokens[0] === 'ARGUMENT_ERROR') {
306 tui.log_msg('? syntax error: ' + tokens[1]);
308 } else if (tokens[0] === 'GAME_ERROR') {
309 tui.log_msg('? game error: ' + tokens[1]);
311 } else if (tokens[0] === 'PONG') {
314 tui.log_msg('? unhandled input: ' + event.data);
322 move: function(direction) {
324 try_pos[0] = this.position[0];
325 try_pos[1] = this.position[1];
326 if (direction == 'left') {
328 } else if (direction == 'right') {
330 } else if (direction == 'up') {
332 } else if (direction == 'down') {
335 if (!(try_pos[0] < 0) &&
337 !(try_pos[0] >= game.map_size[0])
338 && !(try_pos[1] >= game.map_size[1])) {
339 this.position = try_pos;
346 update_info_db: function(yx, str) {
347 this.info_db[yx] = str;
348 if (tui.mode == 'study') {
353 empty_info_db: function() {
355 if (tui.mode == 'study') {
360 query_info: function() {
361 websocket.send("GET_ANNOTATION Y:" + explorer.position[0] + ",X:" + explorer.position[1]);
363 get_info: function() {
364 if (this.position in this.info_db) {
365 return this.info_db[this.position];
370 annotate: function(msg) {
371 if (msg.length == 0) {
372 msg = " "; // triggers annotation deletion
374 websocket.send("ANNOTATE Y:" + explorer.position[0] + ",X:" + explorer.position[1] + " " + quote(msg));
378 document.addEventListener('keydown', (event) => {
379 if (tui.mode == 'chat') {
380 if (event.key.length === 1) {
381 tui.input_line += event.key;
382 tui.draw_input_line();
384 } else if (event.key == 'Backspace') {
385 tui.input_line = tui.input_line.slice(0, -1);
386 tui.draw_input_line();
388 } else if (event.key == 'Enter') {
389 let [tokens, token_starts] = parser.tokenize(tui.input_line);
390 if (tokens.length > 0 && tokens[0].length > 0) {
391 if (tokens[0][0] == '/') {
392 if (tokens[0] == '/play') {
393 tui.switch_mode('play');
394 } else if (tokens[0] == '/study') {
395 tui.switch_mode('study');
396 } else if (tokens[0] == '/help') {
399 } else if (tokens[0] == '/login') {
400 if (tokens.length > 1) {
401 websocket.send('LOGIN ' + quote(tokens[1]));
403 tui.log_msg('? need login name');
405 } else if (tokens[0] == '/msg') {
406 if (tokens.length > 2) {
407 let msg = tui.input_line.slice(token_starts[2]);
408 websocket.send('QUERY ' + quote(tokens[1]) + ' ' + quote(msg));
410 tui.log_msg('? need message target and message');
413 tui.log_msg('? unknown command');
416 websocket.send('ALL ' + quote(tui.input_line));
420 tui.draw_input_line();
423 } else if (tui.mode == 'play') {
424 if (event.key === 'c') {
425 tui.switch_mode('chat');
426 } else if (event.key === 'e') {
427 tui.switch_mode('edit');
428 } else if (event.key === '?') {
429 tui.switch_mode('study');
430 } else if (event.key === 'F1') {
433 } else if (event.key === 'f') {
434 websocket.send("TASK:FLATTEN_SURROUNDINGS");
435 } else if (event.key === 'a') {
436 websocket.send('TASK:MOVE LEFT');
437 } else if (event.key === 'd') {
438 websocket.send('TASK:MOVE RIGHT');
439 } else if (event.key === 'w') {
440 websocket.send('TASK:MOVE UP');
441 } else if (event.key === 's') {
442 websocket.send('TASK:MOVE DOWN');
444 } else if (tui.mode == 'edit') {
445 if (event.key.length === 1) {
446 websocket.send("TASK:WRITE " + quote(event.key));
448 tui.switch_mode('play');
449 } else if (tui.mode == 'study') {
450 if (event.key === 'c') {
451 tui.switch_mode('chat');
452 } else if (event.key == 'p') {
453 tui.switch_mode('play');
454 } else if (event.key === 'a') {
455 explorer.move('left');
456 } else if (event.key === 'd') {
457 explorer.move('right');
458 } else if (event.key === 'w') {
460 } else if (event.key === 's') {
461 explorer.move('down');
462 } else if (event.key === 'A') {
463 tui.switch_mode('annotate');
467 } else if (tui.mode == 'annotate') {
468 if (event.key.length === 1) {
469 tui.input_line += event.key;
470 tui.draw_input_line();
472 } else if (event.key == 'Backspace') {
473 tui.input_line = tui.input_line.slice(0, -1);
474 tui.draw_input_line();
476 } else if (event.key == 'Enter') {
477 explorer.annotate(tui.input_line);
479 tui.switch_mode('study', true);
484 window.setInterval(function() { websocket.send('PING') }, 30000);