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