4 canvas { border: 1px solid black; }
7 <canvas id="terminal" />
10 let websocket_location = "ws://localhost:8000"
16 initialize: function() {
17 this.ctx = document.getElementById("terminal").getContext("2d"),
19 this.charWidth = this.ctx.measureText("M").width;
20 this.ctx.canvas.height = this.charHeight * this.rows;
21 this.ctx.canvas.width = this.charWidth * this.cols;
22 this.set_font(); // ctx.font gets reset to default on canvas size change, so we have to re-set our own
23 this.ctx.textBaseline = "top";
25 set_font: function(type='normal') {
26 this.ctx.font = type + ' ' + this.charHeight + 'px monospace';
28 write: function(start_y, start_x, msg, foreground_color='black') {
29 this.ctx.fillStyle = foreground_color;
30 this.ctx.fillRect(start_x*this.charWidth, start_y*this.charHeight,
31 this.charWidth*msg.length, this.charHeight);
32 if (foreground_color === 'black') {
33 this.ctx.fillStyle = 'white';
35 this.ctx.fillStyle = 'black';
37 this.ctx.fillText(msg, start_x*this.charWidth, start_y*this.charHeight);
39 drawBox: function (start_y, start_x, height, width, color='white') {
40 this.ctx.fillStyle = color;
41 this.ctx.fillRect(start_x*this.charWidth, start_y*this.charHeight,
42 this.charWidth*width, this.charHeight*height);
47 tokenize: function(str) {
52 for (let i = 0; i < str.length; i++) {
58 } else if (c == '\\') {
60 } else if (c == '"') {
65 } else if (c == '"') {
67 } else if (c === ' ') {
68 if (token.length > 0) {
76 if (token.length > 0) {
81 parse_yx(position_string) {
82 let coordinate_strings = position_string.split(',')
83 let position = [0, 0];
84 position[0] = coordinate_strings[0].slice(2);
85 position[1] = coordinate_strings[1].slice(2);
91 draw_history: function() {
92 terminal.drawBox(1, terminal.cols / 2, terminal.rows - 2, terminal.cols, 'black');
94 for (let line of chat.history) {
95 terminal.write(terminal.rows - 2 - i, terminal.cols / 2, line);
97 // if (i > terminal.rows - 3) {
102 draw_map: function() {
103 terminal.drawBox(0, 0, terminal.rows, terminal.cols / 2);
106 for (let i = 0, x = 0; i < game.map.length; i++, x++) {
107 if (x >= game.map_size[1]) {
108 terminal.write(y, 0, map_line);
113 map_line += game.map[i];
115 terminal.write(y, 0, map_line);
116 for (const t in game.things) {
117 terminal.write(game.things[t][0], game.things[t][1], '@');
120 draw_tick_line: function(n) {
121 terminal.drawBox(0, terminal.cols / 2, 1, terminal.cols / 2, 'black');
122 terminal.write(0, terminal.cols / 2, 'tick: ' + game.tick);
124 draw_input_line: function() {
125 terminal.drawBox(terminal.rows - 1, terminal.cols / 2, 1, terminal.cols / 2, 'black');
126 terminal.write(terminal.rows - 1, terminal.cols / 2, chat.input_line);
128 log_msg: function(msg) {
129 chat.history.unshift(msg);
130 if (chat.history.length > terminal.rows - 2) {
146 history: ["visible ASCII char in the input prompt.",
147 "To write on the map, enter on a single",
148 "contain whitespace, escape them with \\.",
149 "Use double quotes for strings that",
150 "Use arrow keys to move your avatar.",
151 " QUERY USER TEXT - send TEXT to USER",
152 " ALL TEXT - send TEXT to all users",
153 " LOGIN USER - register as USER",
157 terminal.initialize()
159 tui.draw_tick_line();
161 tui.draw_input_line();
163 document.addEventListener('keydown', (event) => {
164 if (chat.input_line === '') {
165 terminal.drawBox(terminal.rows - 1, terminal.cols / 2, 1, terminal.rows, 'black');
167 if (event.key && event.key.length === 1) {
168 chat.input_line += event.key;
169 tui.draw_input_line();
170 } else if (event.key === 'Backspace') {
171 chat.input_line = chat.input_line.slice(0, -1);
172 tui.draw_input_line();
173 } else if (event.key === 'Enter') {
174 if (chat.input_line.length === 1) {
175 websocket.send("TASK:WRITE " + chat.input_line);
177 websocket.send(chat.input_line);
179 chat.input_line = '';
180 tui.draw_input_line();
181 } else if (event.key === 'ArrowLeft') {
182 websocket.send('TASK:MOVE LEFT');
183 } else if (event.key === 'ArrowRight') {
184 websocket.send('TASK:MOVE RIGHT');
185 } else if (event.key === 'ArrowUp') {
186 websocket.send('TASK:MOVE UP');
187 } else if (event.key === 'ArrowDown') {
188 websocket.send('TASK:MOVE DOWN');
192 let websocket = new WebSocket(websocket_location);
193 websocket.onmessage = function (event) {
194 let tokens = parser.tokenize(event.data);
195 if (tokens[0] === 'TURN') {
197 game.tick = parseInt(tokens[1]);
198 } else if (tokens[0] === 'THING_POS') {
199 game.things[tokens[1]] = parser.parse_yx(tokens[2]);
200 } else if (tokens[0] === 'MAP') {
201 game.map_size = parser.parse_yx(tokens[1]);
203 } else if (tokens[0] === 'GAME_STATE_COMPLETE') {
204 tui.draw_tick_line();
207 } else if (tokens[0] === 'LOG') {
208 tui.log_msg(' ' + tokens[1]);
209 } else if (tokens[0] === 'UNHANDLED_INPUT') {
210 tui.log_msg('unknown command');
211 } else if (tokens[0] === 'GAME_ERROR') {
212 tui.log_msg('game error: ' + tokens[1]);
213 } else if (tokens[0] === 'GAME_ERROR') {
214 tui.log_msg('game error: ' + tokens[1]);
216 tui.log_msg('unhandled input: ' + event.data);