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