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