4 canvas { border: 1px solid black; }
7 <p><canvas id="terminal" /></p>
8 <p>(running against <a href="https://plomlompom.com/repos/?p=plomrogue2-experiments;a=blob;f=new2/rogue_chat.py">some plomrogue engine experiment</a>)</p>
11 let websocket_location = "ws://localhost:8000"
17 initialize: function() {
18 this.ctx = document.getElementById("terminal").getContext("2d"),
20 this.charWidth = this.ctx.measureText("M").width;
21 this.ctx.canvas.height = this.charHeight * this.rows;
22 this.ctx.canvas.width = this.charWidth * this.cols;
23 this.set_font(); // ctx.font gets reset to default on canvas size change, so we have to re-set our own
24 this.ctx.textBaseline = "top";
26 set_font: function(type='normal') {
27 this.ctx.font = type + ' ' + this.charHeight + 'px monospace';
29 write: function(start_y, start_x, msg, foreground_color='black') {
30 this.ctx.fillStyle = foreground_color;
31 this.ctx.fillRect(start_x*this.charWidth, start_y*this.charHeight,
32 this.charWidth*msg.length, this.charHeight);
33 if (foreground_color === 'black') {
34 this.ctx.fillStyle = 'white';
36 this.ctx.fillStyle = 'black';
38 this.ctx.fillText(msg, start_x*this.charWidth, start_y*this.charHeight);
40 drawBox: function (start_y, start_x, height, width, color='white') {
41 this.ctx.fillStyle = color;
42 this.ctx.fillRect(start_x*this.charWidth, start_y*this.charHeight,
43 this.charWidth*width, this.charHeight*height);
48 tokenize: function(str) {
53 for (let i = 0; i < str.length; i++) {
59 } else if (c == '\\') {
61 } else if (c == '"') {
66 } else if (c == '"') {
68 } else if (c === ' ') {
69 if (token.length > 0) {
77 if (token.length > 0) {
82 parse_yx(position_string) {
83 let coordinate_strings = position_string.split(',')
84 let position = [0, 0];
85 position[0] = coordinate_strings[0].slice(2);
86 position[1] = coordinate_strings[1].slice(2);
92 draw_history: function() {
93 terminal.drawBox(1, terminal.cols / 2, terminal.rows - 2, terminal.cols, 'black');
95 for (let line of chat.history) {
96 terminal.write(terminal.rows - 2 - i, terminal.cols / 2, line);
98 // if (i > terminal.rows - 3) {
103 draw_map: function() {
104 terminal.drawBox(0, 0, terminal.rows, terminal.cols / 2);
107 for (let i = 0, x = 0; i < game.map.length; i++, x++) {
108 if (x >= game.map_size[1]) {
109 terminal.write(y, 0, map_line);
114 map_line += game.map[i];
116 terminal.write(y, 0, map_line);
117 for (const t in game.things) {
118 terminal.write(game.things[t][0], game.things[t][1], '@');
121 draw_tick_line: function(n) {
122 terminal.drawBox(0, terminal.cols / 2, 1, terminal.cols / 2, 'black');
123 terminal.write(0, terminal.cols / 2, 'tick: ' + game.tick);
125 draw_input_line: function() {
126 terminal.drawBox(terminal.rows - 1, terminal.cols / 2, 1, terminal.cols / 2, 'black');
127 terminal.write(terminal.rows - 1, terminal.cols / 2, chat.input_line);
129 log_msg: function(msg, indent=0) {
130 let line_length = (terminal.cols / 2) - indent;
132 for (let i = 0, x = 0; i < msg.length; i++, x++) {
133 if (x >= line_length) {
134 chat.history.unshift(' '.repeat(indent) + chunk);
140 chat.history.unshift(' '.repeat(indent) + chunk);
141 if (chat.history.length > terminal.rows - 2) {
159 " visible ASCII char in the input prompt.",
160 " To write on the map, enter on a single",
162 " contain whitespace, escape them with \\.",
163 " Use double quotes for strings that",
165 " Use arrow keys to move your avatar.",
167 " QUERY USER TEXT - send TEXT to USER",
168 " ALL TEXT - send TEXT to all users",
169 " LOGIN USER - register as USER",
173 terminal.initialize()
175 tui.draw_tick_line();
177 tui.draw_input_line();
179 document.addEventListener('keydown', (event) => {
180 if (chat.input_line === '') {
181 terminal.drawBox(terminal.rows - 1, terminal.cols / 2, 1, terminal.rows, 'black');
183 if (event.key && event.key.length === 1) {
184 chat.input_line += event.key;
185 tui.draw_input_line();
186 } else if (event.key === 'Backspace') {
187 chat.input_line = chat.input_line.slice(0, -1);
188 tui.draw_input_line();
189 } else if (event.key === 'Enter') {
190 if (chat.input_line.length === 1) {
191 websocket.send("TASK:WRITE " + chat.input_line);
193 websocket.send(chat.input_line);
195 chat.input_line = '';
196 tui.draw_input_line();
197 } else if (event.key === 'ArrowLeft') {
198 websocket.send('TASK:MOVE LEFT');
199 } else if (event.key === 'ArrowRight') {
200 websocket.send('TASK:MOVE RIGHT');
201 } else if (event.key === 'ArrowUp') {
202 websocket.send('TASK:MOVE UP');
203 } else if (event.key === 'ArrowDown') {
204 websocket.send('TASK:MOVE DOWN');
208 let websocket = new WebSocket(websocket_location);
209 websocket.onmessage = function (event) {
210 let tokens = parser.tokenize(event.data);
211 if (tokens[0] === 'TURN') {
213 game.tick = parseInt(tokens[1]);
214 } else if (tokens[0] === 'THING_POS') {
215 game.things[tokens[1]] = parser.parse_yx(tokens[2]);
216 } else if (tokens[0] === 'MAP') {
217 game.map_size = parser.parse_yx(tokens[1]);
219 } else if (tokens[0] === 'GAME_STATE_COMPLETE') {
220 tui.draw_tick_line();
223 } else if (tokens[0] === 'LOG') {
224 tui.log_msg(tokens[1], 1);
225 } else if (tokens[0] === 'META') {
226 tui.log_msg(tokens[1]);
227 } else if (tokens[0] === 'UNHANDLED_INPUT') {
228 tui.log_msg('unknown command');
229 } else if (tokens[0] === 'ARGUMENT_ERROR') {
230 tui.log_msg('syntax error: ' + tokens[1]);
231 } else if (tokens[0] === 'GAME_ERROR') {
232 tui.log_msg('game error: ' + tokens[1]);
233 } else if (tokens[0] === 'PONG') {
236 tui.log_msg('unhandled input: ' + event.data);
240 window.setInterval(function() { websocket.send('PING') }, 30000);