home · contact · privacy
Make map rendering slightly more efficient.
[plomrogue2-experiments] / new2 / rogue_chat.html
1 <!DOCTYPE HTML>
2 <html>
3 <style>
4 canvas { border: 1px solid black; }
5 </style>
6 <body>
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>
9 <script>
10 "use strict";
11 let websocket_location = "ws://localhost:8000"
12
13 let terminal = {
14   rows: 24,
15   cols: 80,
16   charHeight: 24,
17   initialize: function() {
18     this.ctx = document.getElementById("terminal").getContext("2d"),
19     this.set_font();
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";
25   },
26   set_font: function(type='normal') {
27     this.ctx.font = type + ' ' + this.charHeight + 'px monospace';
28   },
29   write_cheap: function(start_y, start_x, msg) {
30     this.ctx.fillText(msg, start_x*this.charWidth, start_y*this.charHeight);
31   },
32   set_color: function(color) {
33     this.ctx.fillStyle = color;
34   },
35   write: function(start_y, start_x, msg, foreground_color='white') {
36     this.ctx.fillStyle = 'black';
37     this.ctx.fillRect(start_x*this.charWidth, start_y*this.charHeight,
38                       this.charWidth*msg.length, this.charHeight);
39     this.ctx.fillStyle = foreground_color;
40     this.ctx.fillText(msg, start_x*this.charWidth, start_y*this.charHeight);
41   },
42   drawBox: function (start_y, start_x, height, width, color='white') {
43     this.ctx.fillStyle = color;
44     this.ctx.fillRect(start_x*this.charWidth, start_y*this.charHeight,
45                       this.charWidth*width, this.charHeight*height);
46   }
47 }
48
49 let parser = {
50   tokenize: function(str) {
51     let tokens = [];
52     let token = ''
53     let quoted = false;
54     let escaped = false;
55     for (let i = 0; i < str.length; i++) {
56       let c = str[i];
57       if (quoted) {
58         if (escaped) {
59           token += c;
60           escaped = false;
61         } else if (c == '\\') { 
62           escaped = true;
63         } else if (c == '"') { 
64             quoted = false
65         } else {
66           token += c;
67         }
68       } else if (c == '"') {
69         quoted = true
70       } else if (c === ' ') {
71         if (token.length > 0) {
72           tokens.push(token);
73           token = '';
74         }
75       } else {
76         token += c;
77       }
78     }
79     if (token.length > 0) {
80       tokens.push(token);
81     }
82     return tokens;
83   },
84   parse_yx(position_string) {
85     let coordinate_strings = position_string.split(',')
86     let position = [0, 0];
87     position[0] = coordinate_strings[0].slice(2);
88     position[1] = coordinate_strings[1].slice(2);
89     return position;
90   }
91 }
92
93 let tui = {
94   draw_history: function() {
95     terminal.drawBox(1, terminal.cols / 2, terminal.rows - 2, terminal.cols, 'black');
96     let i = 0;
97     for (let line of chat.history) {
98       terminal.write(terminal.rows - 2 - i, terminal.cols / 2, line);
99       i += 1;
100     }
101   },
102   draw_map: function() {
103     terminal.drawBox(0, 0, terminal.rows, terminal.cols / 2, 'black');
104     let last_c = '';
105     let color = 'white';
106     for (let i = 0, y = 0, x = 0; i < game.map.length; i++, x++) {
107       if (x >= game.map_size[1]) {
108         x = 0;
109         y += 1;
110       };
111       let c = game.map[i];
112       if (c != last_c) {
113         last_c = c;
114         color = 'white';
115         if (c == '.') {
116           color = '#ffaa00';
117         } else if (c == '~') {
118           color = '#5555ff';
119         } else if (c == 'X') {
120           color = '#55ff00';
121         }
122         terminal.set_color(color);
123       }
124       terminal.write_cheap(y, x, c);
125     }
126     for (const t in game.things) {
127       terminal.write(game.things[t][0], game.things[t][1], '@');
128     }
129   },
130   draw_turn_line: function(n) {
131     terminal.drawBox(0, terminal.cols / 2, 1, terminal.cols / 2, 'black');
132     terminal.write(0, terminal.cols / 2, 'turn: ' + game.turn);
133   },
134   draw_input_line: function() {
135     terminal.drawBox(terminal.rows - 1, terminal.cols / 2, 1, terminal.cols / 2, 'black');
136     terminal.write(terminal.rows - 1, terminal.cols / 2, chat.input_line);
137   },
138   log_msg: function(msg, indent=0) {
139     let line_length = (terminal.cols / 2) - indent;
140     let chunk = "";
141     for (let i = 0, x = 0; i < msg.length; i++, x++) {
142       if (x >= line_length) {
143         chat.history.unshift(' '.repeat(indent) + chunk);
144         chunk = "";
145         x = 0;
146       };
147       chunk += msg[i];
148     }
149     chat.history.unshift(' '.repeat(indent) + chunk);
150     if (chat.history.length > terminal.rows - 2) {
151
152       chat.history.pop();
153     };
154     this.draw_history();
155   }
156 }
157
158 let game = {
159   things: {},
160   turn: 0,
161   map: "",
162   map_size: [1,1]
163 }
164
165 let chat = {
166   input_line: "",
167   history: []
168 }
169
170 terminal.initialize()
171 tui.draw_map();
172 tui.draw_turn_line();
173 tui.draw_history();
174 tui.draw_input_line();
175
176 tui.log_msg("basic commands:", 1);
177 tui.log_msg("LOGIN USER - register as USER", 3);
178 tui.log_msg("ALL TEXT - send TEXT to all users", 3);
179 tui.log_msg("QUERY USER TEXT - send TEXT to USER", 3);
180 tui.log_msg("");
181 tui.log_msg("Use arrow keys to move your avatar. You can only move over \".\" map cells.", 1);
182 tui.log_msg("");
183 tui.log_msg("Use double quotes for strings that contain whitespace, escape them with \\.", 1);
184 tui.log_msg("");
185 tui.log_msg("To change the map cell you are standing on, type the desired ASCII character into the prompt and hit Return.", 1);
186 tui.log_msg("");
187 tui.log_msg("more commands:", 1);
188 tui.log_msg("FLATTEN - transform surrounding map cells to \".\" ones", 3);
189 tui.log_msg("");
190
191 document.addEventListener('keydown', (event) => {
192   if (chat.input_line === '') {
193     terminal.drawBox(terminal.rows - 1, terminal.cols / 2, 1, terminal.rows, 'black');
194   }
195   if (event.key && event.key.length === 1) {
196     chat.input_line += event.key;
197     tui.draw_input_line();
198   } else if (event.key === 'Backspace') {
199     chat.input_line = chat.input_line.slice(0, -1);
200     tui.draw_input_line();
201   } else if (event.key === 'Enter') {
202     if (chat.input_line.length === 1) {
203       websocket.send("TASK:WRITE " + chat.input_line);
204     } else if (chat.input_line.trimEnd() === 'FLATTEN') {
205       websocket.send("TASK:FLATTEN_SURROUNDINGS");
206     } else {
207       websocket.send(chat.input_line);
208     }
209     chat.input_line = '';
210     tui.draw_input_line();
211   } else if (event.key === 'ArrowLeft') {
212     websocket.send('TASK:MOVE LEFT');
213   } else if (event.key === 'ArrowRight') {
214     websocket.send('TASK:MOVE RIGHT');
215   } else if (event.key === 'ArrowUp') {
216     websocket.send('TASK:MOVE UP');
217   } else if (event.key === 'ArrowDown') {
218     websocket.send('TASK:MOVE DOWN');
219   };
220 }, false);
221
222 let websocket = new WebSocket(websocket_location);
223 websocket.onmessage = function (event) {
224   let tokens = parser.tokenize(event.data);
225   if (tokens[0] === 'TURN') {
226     game.things = {}
227     game.turn = parseInt(tokens[1]);
228   } else if (tokens[0] === 'THING_POS') {
229     game.things[tokens[1]] = parser.parse_yx(tokens[2]);
230   } else if (tokens[0] === 'MAP') {
231     game.map_size = parser.parse_yx(tokens[1]);
232     game.map = tokens[2]
233   } else if (tokens[0] === 'GAME_STATE_COMPLETE') {
234     tui.draw_turn_line();
235     tui.draw_map();
236     tui.draw_map();
237   } else if (tokens[0] === 'LOG') {
238      tui.log_msg(tokens[1], 1);
239   } else if (tokens[0] === 'META') {
240      tui.log_msg(tokens[1]);
241   } else if (tokens[0] === 'UNHANDLED_INPUT') {
242      tui.log_msg('unknown command');
243   } else if (tokens[0] === 'ARGUMENT_ERROR') {
244      tui.log_msg('syntax error: ' + tokens[1]);
245   } else if (tokens[0] === 'GAME_ERROR') {
246      tui.log_msg('game error: ' + tokens[1]);
247   } else if (tokens[0] === 'PONG') {
248     console.log('PONG');
249   } else {
250      tui.log_msg('unhandled input: ' + event.data);
251   }
252 }
253
254 window.setInterval(function() { websocket.send('PING') }, 30000);
255 </script>
256 </body>
257 </html>