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 game mode", 3);
209 tui.log_msg("map 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);
216 full_refresh: function() {
218 this.draw_turn_line();
220 this.draw_input_line();
238 terminal.initialize();
242 let websocket = new WebSocket(websocket_location);
243 websocket.onmessage = function (event) {
244 let tokens = parser.tokenize(event.data);
245 if (tokens[0] === 'TURN') {
247 game.turn = parseInt(tokens[1]);
248 } else if (tokens[0] === 'THING_POS') {
249 game.things[tokens[1]] = parser.parse_yx(tokens[2]);
250 } else if (tokens[0] === 'MAP') {
251 game.map_size = parser.parse_yx(tokens[1]);
253 } else if (tokens[0] === 'GAME_STATE_COMPLETE') {
254 tui.draw_turn_line();
257 } else if (tokens[0] === 'LOG') {
258 tui.log_msg(tokens[1], 1);
260 } else if (tokens[0] === 'PLAYER_ID') {
261 game.player_id = parseInt(tokens[1]);
262 } else if (tokens[0] === 'META') {
263 tui.log_msg(tokens[1]);
265 } else if (tokens[0] === 'UNHANDLED_INPUT') {
266 tui.log_msg('unknown command');
268 } else if (tokens[0] === 'ARGUMENT_ERROR') {
269 tui.log_msg('syntax error: ' + tokens[1]);
271 } else if (tokens[0] === 'GAME_ERROR') {
272 tui.log_msg('game error: ' + tokens[1]);
274 } else if (tokens[0] === 'PONG') {
277 tui.log_msg('unhandled input: ' + event.data);
284 move: function(direction) {
286 try_pos[0] = this.position[0];
287 try_pos[1] = this.position[1];
288 if (direction == 'left') {
290 } else if (direction == 'right') {
292 } else if (direction == 'up') {
294 } else if (direction == 'down') {
297 if (!(try_pos[0] < 0) &&
299 !(try_pos[0] >= game.map_size[0])
300 && !(try_pos[1] >= game.map_size[1])) {
301 this.position = try_pos;
308 document.addEventListener('keydown', (event) => {
309 if (tui.mode == 'chat') {
310 if (event.key.length === 1) {
311 chat.input_line += event.key;
312 tui.draw_input_line();
314 } else if (event.key == 'Backspace') {
315 chat.input_line = chat.input_line.slice(0, -1);
316 tui.draw_input_line();
318 } else if (event.key == 'Enter') {
319 let tokens = parser.tokenize(chat.input_line);
320 if (tokens.length > 0 && tokens[0].length > 0) {
321 if (tokens[0][0] == '/') {
322 if (tokens[0] == '/play') {
323 tui.switch_mode('play');
324 } else if (tokens[0] == '/?') {
327 } else if (tokens[0] == '/login') {
328 if (tokens.length > 1) {
329 websocket.send('LOGIN ' + quote(tokens[1]));
331 tui.log_msg('need login name');
333 } else if (tokens[0] == '/msg') {
334 if (tokens.length > 2) {
335 websocket.send('QUERY ' + quote(tokens[1]) + ' ' + quote(tokens[2]));
337 tui.log_msg('need message target and message');
340 tui.log_msg('unknown command');
343 websocket.send('ALL ' + quote(chat.input_line));
346 chat.input_line = '';
347 tui.draw_input_line();
350 } else if (tui.mode == 'play') {
351 if (event.key === 'c') {
352 tui.switch_mode('chat');
353 } else if (event.key === 'e') {
354 tui.switch_mode('edit');
355 } else if (event.key === '?') {
356 tui.switch_mode('explore');
357 } else if (event.key === 'F1') {
360 } else if (event.key === 'f') {
361 websocket.send("TASK:FLATTEN_SURROUNDINGS");
362 } else if (event.key === 'a') {
363 websocket.send('TASK:MOVE LEFT');
364 } else if (event.key === 'd') {
365 websocket.send('TASK:MOVE RIGHT');
366 } else if (event.key === 'w') {
367 websocket.send('TASK:MOVE UP');
368 } else if (event.key === 's') {
369 websocket.send('TASK:MOVE DOWN');
371 } else if (tui.mode == 'edit') {
372 if (event.key.length === 1) {
373 websocket.send("TASK:WRITE " + quote(event.key));
375 tui.switch_mode('play');
376 } else if (tui.mode == 'explore') {
377 if (event.key === 'c') {
378 tui.switch_mode('chat');
379 } else if (event.key == 'p') {
380 tui.switch_mode('play');
381 } else if (event.key === 'a') {
382 explorer.move('left');
383 } else if (event.key === 'd') {
384 explorer.move('right');
385 } else if (event.key === 'w') {
387 } else if (event.key === 's') {
388 explorer.move('down');
393 window.setInterval(function() { websocket.send('PING') }, 30000);