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 < end_y; x++) {
48 this.content[y][x] = ' ';
58 tokenize: function(str) {
63 for (let i = 0; i < str.length; i++) {
69 } else if (c == '\\') {
71 } else if (c == '"') {
76 } else if (c == '"') {
78 } else if (c === ' ') {
79 if (token.length > 0) {
87 if (token.length > 0) {
92 parse_yx(position_string) {
93 let coordinate_strings = position_string.split(',')
94 let position = [0, 0];
95 position[0] = parseInt(coordinate_strings[0].slice(2));
96 position[1] = parseInt(coordinate_strings[1].slice(2));
102 draw_history: function() {
103 terminal.drawBox(1, terminal.cols / 2, terminal.rows - 2, terminal.cols / 2);
105 for (let line of chat.history) {
106 terminal.write(terminal.rows - 2 - i, terminal.cols / 2, line);
110 draw_map: function() {
111 terminal.drawBox(0, 0, terminal.rows, terminal.cols / 2);
114 for (let i = 0, j = 0; i < game.map.length; i++, j++) {
115 if (j == game.map_size[1]) {
116 map_lines.push(line);
120 line.push(game.map[i]);
122 map_lines.push(line);
123 let player_position = [0,0];
124 for (const thing_id in game.things) {
125 let t = game.things[thing_id];
126 map_lines[t[0]][t[1]] = '@';
129 let offset = [(terminal.rows / 2) - player_position[0],
130 terminal.cols / 4 - player_position[1]];
131 for (let term_y = offset[0], map_y = 0;
132 term_y < terminal.rows && map_y < game.map_size[0];
135 let to_draw = map_lines[map_y].join('').slice(0, terminal.cols / 2 - offset[1]);
136 terminal.write(term_y, offset[1], to_draw);
140 draw_turn_line: function(n) {
141 terminal.drawBox(0, terminal.cols / 2, 1, terminal.cols / 2);
142 terminal.write(0, terminal.cols / 2, 'turn: ' + game.turn);
144 draw_input_line: function() {
145 terminal.drawBox(terminal.rows - 1, terminal.cols / 2, 1, terminal.cols / 2);
146 terminal.write(terminal.rows - 1, terminal.cols / 2, chat.input_line);
148 log_msg: function(msg, indent=0) {
149 let line_length = (terminal.cols / 2) - indent;
151 for (let i = 0, x = 0; i < msg.length; i++, x++) {
152 if (x >= line_length) {
153 chat.history.unshift(' '.repeat(indent) + chunk);
159 chat.history.unshift(' '.repeat(indent) + chunk);
160 while (chat.history.length > terminal.rows - 2) {
165 refresh: function() {
182 terminal.initialize();
185 tui.draw_turn_line();
187 tui.draw_input_line();
190 tui.log_msg("basic commands:", 1);
191 tui.log_msg("LOGIN USER - register as USER", 3);
192 tui.log_msg("ALL TEXT - send TEXT to all users", 3);
193 tui.log_msg("QUERY USER TEXT - send TEXT to USER", 3);
195 tui.log_msg("Use arrow keys to move your avatar. You can only move over \".\" map cells.", 1);
197 tui.log_msg("Use double quotes for strings that contain whitespace, escape them with \\.", 1);
199 tui.log_msg("To change the map cell you are standing on, type the desired ASCII character into the prompt and hit Return.", 1);
201 tui.log_msg("more commands:", 1);
202 tui.log_msg("FLATTEN - transform surrounding map cells to \".\" ones", 3);
205 let websocket = new WebSocket(websocket_location);
206 websocket.onmessage = function (event) {
207 let tokens = parser.tokenize(event.data);
208 if (tokens[0] === 'TURN') {
210 game.turn = parseInt(tokens[1]);
211 } else if (tokens[0] === 'THING_POS') {
212 game.things[tokens[1]] = parser.parse_yx(tokens[2]);
213 } else if (tokens[0] === 'MAP') {
214 game.map_size = parser.parse_yx(tokens[1]);
216 } else if (tokens[0] === 'GAME_STATE_COMPLETE') {
217 tui.draw_turn_line();
220 } else if (tokens[0] === 'LOG') {
221 tui.log_msg(tokens[1], 1);
223 } else if (tokens[0] === 'META') {
224 tui.log_msg(tokens[1]);
226 } else if (tokens[0] === 'UNHANDLED_INPUT') {
227 tui.log_msg('unknown command');
229 } else if (tokens[0] === 'ARGUMENT_ERROR') {
230 tui.log_msg('syntax error: ' + tokens[1]);
232 } else if (tokens[0] === 'GAME_ERROR') {
233 tui.log_msg('game error: ' + tokens[1]);
235 } else if (tokens[0] === 'PONG') {
238 tui.log_msg('unhandled input: ' + event.data);
243 document.addEventListener('keydown', (event) => {
244 if (chat.input_line === '') {
245 tui.draw_input_line();
248 if (event.key && event.key.length === 1) {
249 chat.input_line += event.key;
250 tui.draw_input_line();
252 } else if (event.key === 'Backspace') {
253 chat.input_line = chat.input_line.slice(0, -1);
254 tui.draw_input_line();
256 } else if (event.key === 'Enter') {
257 if (chat.input_line.length === 1) {
258 websocket.send("TASK:WRITE " + chat.input_line);
259 } else if (chat.input_line.trimEnd() === 'FLATTEN') {
260 websocket.send("TASK:FLATTEN_SURROUNDINGS");
262 websocket.send(chat.input_line);
264 chat.input_line = '';
265 tui.draw_input_line();
266 } else if (event.key === 'ArrowLeft') {
267 websocket.send('TASK:MOVE LEFT');
268 } else if (event.key === 'ArrowRight') {
269 websocket.send('TASK:MOVE RIGHT');
270 } else if (event.key === 'ArrowUp') {
271 websocket.send('TASK:MOVE UP');
272 } else if (event.key === 'ArrowDown') {
273 websocket.send('TASK:MOVE DOWN');
277 window.setInterval(function() { websocket.send('PING') }, 30000);