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) {
66 for (let i = 0; i < str.length; i++) {
72 } else if (c == '\\') {
74 } else if (c == '"') {
79 } else if (c == '"') {
81 } else if (c === ' ') {
82 if (token.length > 0) {
90 if (token.length > 0) {
95 parse_yx(position_string) {
96 let coordinate_strings = position_string.split(',')
97 let position = [0, 0];
98 position[0] = parseInt(coordinate_strings[0].slice(2));
99 position[1] = parseInt(coordinate_strings[1].slice(2));
104 function quote(str) {
106 for (let i = 0; i < str.length; i++) {
108 if (c in ['"', '\\']) {
114 return quoted.join('');
119 switch_mode: function(mode_name) {
120 if (mode_name == 'explore') {
121 explorer.position = game.things[game.player_id];
123 this.mode = mode_name;
126 draw_history: function() {
127 terminal.drawBox(1, terminal.cols / 2, terminal.rows - 2, terminal.cols / 2);
129 for (let line of chat.history) {
130 terminal.write(terminal.rows - 2 - i, terminal.cols / 2, line);
134 draw_map: function() {
135 terminal.drawBox(0, 0, terminal.rows, terminal.cols / 2);
138 for (let i = 0, j = 0; i < game.map.length; i++, j++) {
139 if (j == game.map_size[1]) {
140 map_lines.push(line);
144 line.push(game.map[i]);
146 map_lines.push(line);
147 let player_position = [0,0];
148 for (const thing_id in game.things) {
149 let t = game.things[thing_id];
150 map_lines[t[0]][t[1]] = '@';
151 if (game.player_id == thing_id) {
155 let center_pos = player_position;
156 if (tui.mode == 'explore') {
157 map_lines[explorer.position[0]][explorer.position[1]] = '?';
158 center_pos = explorer.position;
160 let offset = [(terminal.rows / 2) - center_pos[0],
161 terminal.cols / 4 - center_pos[1]];
162 for (let term_y = offset[0], map_y = 0;
163 term_y < terminal.rows && map_y < game.map_size[0];
166 let to_draw = map_lines[map_y].join('').slice(0, terminal.cols / 2 - offset[1]);
167 terminal.write(term_y, offset[1], to_draw);
171 draw_turn_line: function(n) {
172 terminal.drawBox(0, terminal.cols / 2, 1, terminal.cols / 2);
173 terminal.write(0, terminal.cols / 2, 'turn: ' + game.turn);
175 draw_input_line: function() {
176 terminal.drawBox(terminal.rows - 1, terminal.cols / 2, 1, terminal.cols / 2);
177 terminal.write(terminal.rows - 1, terminal.cols / 2, chat.input_line);
179 log_msg: function(msg, indent=0) {
180 let line_length = (terminal.cols / 2) - indent;
182 for (let i = 0, x = 0; i < msg.length; i++, x++) {
183 if (x >= line_length) {
184 chat.history.unshift(' '.repeat(indent) + chunk);
190 chat.history.unshift(' '.repeat(indent) + chunk);
191 while (chat.history.length > terminal.rows - 2) {
196 refresh: function() {
199 log_help: function() {
201 tui.log_msg("HELP", 1);
202 tui.log_msg("chat mode commands:", 1);
204 tui.log_msg("/login USER - register as USER", 3);
205 tui.log_msg("/msg USER TEXT - send TEXT to USER", 3);
206 tui.log_msg("/help - show this help", 3);
207 tui.log_msg("/play - switch to play mode", 3);
209 tui.log_msg("play mode commands:", 1);
210 tui.log_msg("w, a, s, d - move avatar", 3);
211 tui.log_msg("f - flatten surroundings", 3);
212 tui.log_msg("e - write following ASCII character", 3);
213 tui.log_msg("c - switch to chat mode", 3);
214 tui.log_msg("? - switch to explore mode", 3);
216 tui.log_msg("explore mode commands:", 1);
217 tui.log_msg("w, a, s, d - move question mark", 3);
218 tui.log_msg("c - switch to chat mode", 3);
219 tui.log_msg("p - switch to play mode", 3);
222 draw_info: function() {
223 terminal.drawBox(0, terminal.cols / 2, terminal.rows, terminal.cols / 2);
224 let lines = ['unfinished info screen'];
225 for (let y = 0, i = 0; y < terminal.rows && i < lines.length; y++, i++) {
226 terminal.write(y, terminal.cols / 2, lines[i]);
229 full_refresh: function() {
231 this.draw_turn_line();
233 this.draw_input_line();
251 terminal.initialize();
255 let websocket = new WebSocket(websocket_location);
256 websocket.onmessage = function (event) {
257 let tokens = parser.tokenize(event.data);
258 if (tokens[0] === 'TURN') {
260 game.turn = parseInt(tokens[1]);
261 } else if (tokens[0] === 'THING_POS') {
262 game.things[tokens[1]] = parser.parse_yx(tokens[2]);
263 } else if (tokens[0] === 'MAP') {
264 game.map_size = parser.parse_yx(tokens[1]);
266 } else if (tokens[0] === 'GAME_STATE_COMPLETE') {
267 tui.draw_turn_line();
270 } else if (tokens[0] === 'LOG') {
271 tui.log_msg(tokens[1], 1);
273 } else if (tokens[0] === 'PLAYER_ID') {
274 game.player_id = parseInt(tokens[1]);
275 } else if (tokens[0] === 'META') {
276 tui.log_msg(tokens[1]);
278 } else if (tokens[0] === 'UNHANDLED_INPUT') {
279 tui.log_msg('unknown command');
281 } else if (tokens[0] === 'ARGUMENT_ERROR') {
282 tui.log_msg('syntax error: ' + tokens[1]);
284 } else if (tokens[0] === 'GAME_ERROR') {
285 tui.log_msg('game error: ' + tokens[1]);
287 } else if (tokens[0] === 'PONG') {
290 tui.log_msg('unhandled input: ' + event.data);
297 move: function(direction) {
299 try_pos[0] = this.position[0];
300 try_pos[1] = this.position[1];
301 if (direction == 'left') {
303 } else if (direction == 'right') {
305 } else if (direction == 'up') {
307 } else if (direction == 'down') {
310 if (!(try_pos[0] < 0) &&
312 !(try_pos[0] >= game.map_size[0])
313 && !(try_pos[1] >= game.map_size[1])) {
314 this.position = try_pos;
322 document.addEventListener('keydown', (event) => {
323 if (tui.mode == 'chat') {
324 if (event.key.length === 1) {
325 chat.input_line += event.key;
326 tui.draw_input_line();
328 } else if (event.key == 'Backspace') {
329 chat.input_line = chat.input_line.slice(0, -1);
330 tui.draw_input_line();
332 } else if (event.key == 'Enter') {
333 let tokens = parser.tokenize(chat.input_line);
334 if (tokens.length > 0 && tokens[0].length > 0) {
335 if (tokens[0][0] == '/') {
336 if (tokens[0] == '/play') {
337 tui.switch_mode('play');
338 } else if (tokens[0] == '/?') {
341 } else if (tokens[0] == '/login') {
342 if (tokens.length > 1) {
343 websocket.send('LOGIN ' + quote(tokens[1]));
345 tui.log_msg('need login name');
347 } else if (tokens[0] == '/msg') {
348 if (tokens.length > 2) {
349 websocket.send('QUERY ' + quote(tokens[1]) + ' ' + quote(tokens[2]));
351 tui.log_msg('need message target and message');
354 tui.log_msg('unknown command');
357 websocket.send('ALL ' + quote(chat.input_line));
360 chat.input_line = '';
361 tui.draw_input_line();
364 } else if (tui.mode == 'play') {
365 if (event.key === 'c') {
366 tui.switch_mode('chat');
367 } else if (event.key === 'e') {
368 tui.switch_mode('edit');
369 } else if (event.key === '?') {
370 tui.switch_mode('explore');
371 } else if (event.key === 'F1') {
374 } else if (event.key === 'f') {
375 websocket.send("TASK:FLATTEN_SURROUNDINGS");
376 } else if (event.key === 'a') {
377 websocket.send('TASK:MOVE LEFT');
378 } else if (event.key === 'd') {
379 websocket.send('TASK:MOVE RIGHT');
380 } else if (event.key === 'w') {
381 websocket.send('TASK:MOVE UP');
382 } else if (event.key === 's') {
383 websocket.send('TASK:MOVE DOWN');
385 } else if (tui.mode == 'edit') {
386 if (event.key.length === 1) {
387 websocket.send("TASK:WRITE " + quote(event.key));
389 tui.switch_mode('play');
390 } else if (tui.mode == 'explore') {
391 if (event.key === 'c') {
392 tui.switch_mode('chat');
393 } else if (event.key == 'p') {
394 tui.switch_mode('play');
395 } else if (event.key === 'a') {
396 explorer.move('left');
397 } else if (event.key === 'd') {
398 explorer.move('right');
399 } else if (event.key === 'w') {
401 } else if (event.key === 's') {
402 explorer.move('down');
407 window.setInterval(function() { websocket.send('PING') }, 30000);