home · contact · privacy
Don't use LOG for meta messages, errors.
[plomrogue2-experiments] / new2 / rogue_chat.html
1 <!DOCTYPE HTML>
2 <html>
3 <style>
4 canvas { border: 1px solid black; }
5 </style>
6 <body>
7 <canvas id="terminal" />
8 <script>
9 "use strict";
10 let websocket_location = "ws://localhost:8000"
11
12 let terminal = {
13   rows: 24,
14   cols: 80,
15   charHeight: 24,
16   initialize: function() {
17     this.ctx = document.getElementById("terminal").getContext("2d"),
18     this.set_font();
19     this.charWidth = this.ctx.measureText("M").width;
20     this.ctx.canvas.height = this.charHeight * this.rows;
21     this.ctx.canvas.width = this.charWidth * this.cols;
22     this.set_font();  // ctx.font gets reset to default on canvas size change, so we have to re-set our own
23     this.ctx.textBaseline = "top";
24   },
25   set_font: function(type='normal') {
26     this.ctx.font = type + ' ' + this.charHeight + 'px monospace';
27   },
28   write: function(start_y, start_x, msg, foreground_color='black') {
29     this.ctx.fillStyle = foreground_color; 
30     this.ctx.fillRect(start_x*this.charWidth, start_y*this.charHeight,
31                       this.charWidth*msg.length, this.charHeight);
32     if (foreground_color === 'black') {
33       this.ctx.fillStyle = 'white';
34     } else {
35       this.ctx.fillStyle = 'black';
36     }
37     this.ctx.fillText(msg, start_x*this.charWidth, start_y*this.charHeight); 
38   },
39   drawBox: function (start_y, start_x, height, width, color='white') {
40     this.ctx.fillStyle = color;
41     this.ctx.fillRect(start_x*this.charWidth, start_y*this.charHeight,
42                       this.charWidth*width, this.charHeight*height);
43   }
44 }
45
46 let parser = {
47   tokenize: function(str) {
48     let tokens = [];
49     let token = ''
50     let quoted = false;
51     let escaped = false;
52     for (let i = 0; i < str.length; i++) {
53       let c = str[i];
54       if (quoted) {
55         if (escaped) {
56           token += c;
57           escaped = false;
58         } else if (c == '\\') { 
59           escaped = true;
60         } else if (c == '"') { 
61             quoted = false
62         } else {
63           token += c;
64         }
65       } else if (c == '"') {
66         quoted = true
67       } else if (c === ' ') {
68         if (token.length > 0) {
69           tokens.push(token);
70           token = '';
71         }
72       } else {
73         token += c;
74       }
75     }
76     if (token.length > 0) {
77       tokens.push(token);
78     }
79     return tokens;
80   },
81   parse_yx(position_string) {
82     let coordinate_strings = position_string.split(',')
83     let position = [0, 0];
84     position[0] = coordinate_strings[0].slice(2);
85     position[1] = coordinate_strings[1].slice(2);
86     return position;
87   }
88 }
89
90 let tui = {
91   draw_history: function() {
92     terminal.drawBox(1, terminal.cols / 2, terminal.rows - 2, terminal.cols, 'black');
93     let i = 0;
94     for (let line of chat.history) {
95       terminal.write(terminal.rows - 2 - i, terminal.cols / 2, line);
96       i += 1;
97       // if (i > terminal.rows - 3) {
98       //   break;
99       // }
100     }
101   },
102   draw_map: function() {
103     terminal.drawBox(0, 0, terminal.rows, terminal.cols / 2);
104     let map_line = "";
105     let y = 0;
106     for (let i = 0, x = 0; i < game.map.length; i++, x++) {
107       if (x >= game.map_size[1]) {
108         terminal.write(y, 0, map_line);
109         map_line = "";
110         x = 0;
111         y += 1;
112       };
113       map_line += game.map[i];
114     }
115     terminal.write(y, 0, map_line);
116     for (const t in game.things) {
117       terminal.write(game.things[t][0], game.things[t][1], '@');
118     }
119   },
120   draw_tick_line: function(n) {
121     terminal.drawBox(0, terminal.cols / 2, 1, terminal.cols / 2, 'black');
122     terminal.write(0, terminal.cols / 2, 'tick: ' + game.tick);
123   },
124   draw_input_line: function() {
125     terminal.drawBox(terminal.rows - 1, terminal.cols / 2, 1, terminal.cols / 2, 'black');
126     terminal.write(terminal.rows - 1, terminal.cols / 2, chat.input_line);
127   },
128   log_msg: function(msg, indent=0) {
129     let line_length = (terminal.cols / 2) - indent;
130     let chunk = "";
131     for (let i = 0, x = 0; i < msg.length; i++, x++) {
132       if (x >= line_length) {
133         chat.history.unshift(' '.repeat(indent) + chunk);
134         chunk = "";
135         x = 0;
136       };
137       chunk += msg[i];
138     }
139     chat.history.unshift(' '.repeat(indent) + chunk);
140     if (chat.history.length > terminal.rows - 2) {
141
142       chat.history.pop();
143     };
144     this.draw_history();
145   }
146 }
147
148 let game = {
149   things: {},
150   tick: 0,
151   map: "",
152   map_size: [1,1]
153 }
154
155 let chat = {
156   input_line:"",
157   history: [" visible ASCII char in the input prompt.",
158             " To write on the map, enter on a single",
159             " contain whitespace, escape them with \\.",
160             " Use double quotes for strings that",
161             " Use arrow keys to move your avatar.",
162             "   QUERY USER TEXT - send TEXT to USER",
163             "   ALL TEXT - send TEXT to all users",
164             "   LOGIN USER - register as USER",
165             " commands:"]
166 }
167
168 terminal.initialize()
169 tui.draw_map();
170 tui.draw_tick_line();
171 tui.draw_history();
172 tui.draw_input_line();
173
174 document.addEventListener('keydown', (event) => {
175   if (chat.input_line === '') {
176     terminal.drawBox(terminal.rows - 1, terminal.cols / 2, 1, terminal.rows, 'black');
177   }
178   if (event.key && event.key.length === 1) {
179     chat.input_line += event.key;
180     tui.draw_input_line();
181   } else if (event.key === 'Backspace') {
182     chat.input_line = chat.input_line.slice(0, -1);
183     tui.draw_input_line();
184   } else if (event.key === 'Enter') {
185     if (chat.input_line.length === 1) {
186       websocket.send("TASK:WRITE " + chat.input_line);
187     } else {
188       websocket.send(chat.input_line);
189     }
190     chat.input_line = '';
191     tui.draw_input_line();
192   } else if (event.key === 'ArrowLeft') {
193     websocket.send('TASK:MOVE LEFT');
194   } else if (event.key === 'ArrowRight') {
195     websocket.send('TASK:MOVE RIGHT');
196   } else if (event.key === 'ArrowUp') {
197     websocket.send('TASK:MOVE UP');
198   } else if (event.key === 'ArrowDown') {
199     websocket.send('TASK:MOVE DOWN');
200   };
201 }, false);
202
203 let websocket = new WebSocket(websocket_location);
204 websocket.onmessage = function (event) {
205   let tokens = parser.tokenize(event.data);
206   if (tokens[0] === 'TURN') {
207     game.things = {}
208     game.tick = parseInt(tokens[1]);
209   } else if (tokens[0] === 'THING_POS') {
210     game.things[tokens[1]] = parser.parse_yx(tokens[2]);
211   } else if (tokens[0] === 'MAP') {
212     game.map_size = parser.parse_yx(tokens[1]);
213     game.map = tokens[2]
214   } else if (tokens[0] === 'GAME_STATE_COMPLETE') {
215     tui.draw_tick_line();
216     tui.draw_map();
217     tui.draw_map();
218   } else if (tokens[0] === 'LOG') {
219      tui.log_msg(tokens[1], 1);
220   } else if (tokens[0] === 'META') {
221      tui.log_msg(tokens[1]);
222   } else if (tokens[0] === 'UNHANDLED_INPUT') {
223      tui.log_msg('unknown command');
224   } else if (tokens[0] === 'GAME_ERROR') {
225      tui.log_msg('game error: ' + tokens[1]);
226   } else if (tokens[0] === 'GAME_ERROR') {
227      tui.log_msg('game error: ' + tokens[1]);
228   } else {
229      tui.log_msg('unhandled input: ' + event.data);
230   }
231 }
232 </script>
233 </body>
234 </html>