};
return [tokens, token_starts];
},
- parse_yx(position_string) {
+ parse_yx: function(position_string) {
let coordinate_strings = position_string.split(',')
let position = [0, 0];
position[0] = parseInt(coordinate_strings[0].slice(2));
},
}
-function quote(str) {
- let quoted = ['"'];
- for (let i = 0; i < str.length; i++) {
- let c = str[i];
- if (c in ['"', '\\']) {
- quoted.push('\\');
+let server = {
+ init: function(url) {
+ this.websocket = new WebSocket(url);
+ this.websocket.onopen = function(event) {
+ window.setInterval(function() { server.send(['PING']) }, 30000);
+ server.send(['GET_GAMESTATE']);
};
- quoted.push(c);
+ },
+ send: function(tokens) {
+ this.websocket.send(unparser.untokenize(tokens));
+ }
+}
+
+let unparser = {
+ quote: function(str) {
+ let quoted = ['"'];
+ for (let i = 0; i < str.length; i++) {
+ let c = str[i];
+ if (c in ['"', '\\']) {
+ quoted.push('\\');
+ };
+ quoted.push(c);
+ }
+ quoted.push('"');
+ return quoted.join('');
+ },
+ to_yx: function(yx_coordinate) {
+ return "Y:" + yx_coordinate[0] + ",X:" + yx_coordinate[1];
+ },
+ untokenize: function(tokens) {
+ let quoted_tokens = [];
+ for (let token of tokens) {
+ quoted_tokens.push(this.quote(token));
+ }
+ return quoted_tokens.join(" ");
}
- quoted.push('"');
- return quoted.join('');
}
let tui = {
tui.log_help();
tui.full_refresh();
-let websocket = new WebSocket(websocket_location);
-websocket.onopen = function (event) {
- window.setInterval(function() { websocket.send('PING') }, 30000);
- websocket.send('GET_GAMESTATE');
-}
-websocket.onmessage = function (event) {
+server.init(websocket_location);
+server.websocket.onmessage = function (event) {
let tokens = parser.tokenize(event.data)[0];
if (tokens[0] === 'TURN') {
game.things = {}
}
},
query_info: function() {
- websocket.send("GET_ANNOTATION Y:" + explorer.position[0] + ",X:" + explorer.position[1]);
+ server.send(["GET_ANNOTATION", unparser.to_yx(explorer.position)]);
},
get_info: function() {
if (this.position in this.info_db) {
if (msg.length == 0) {
msg = " "; // triggers annotation deletion
}
- websocket.send("ANNOTATE Y:" + explorer.position[0] + ",X:" + explorer.position[1] + " " + quote(msg));
+ server.send(["ANNOTATE", unparser.to_yx(explorer.position), msg]);
}
}
tui.refresh();
} else if (tokens[0] == ':login') {
if (tokens.length > 1) {
- websocket.send('LOGIN ' + quote(tokens[1]));
+ server.send(['LOGIN', tokens[1]]);
} else {
tui.log_msg('? need login name');
}
} else if (tokens[0] == ':msg') {
if (tokens.length > 2) {
let msg = tui.input_line.slice(token_starts[2]);
- websocket.send('QUERY ' + quote(tokens[1]) + ' ' + quote(msg));
+ server.send(['QUERY', tokens[1], msg]);
} else {
tui.log_msg('? need message target and message');
}
tui.log_msg('? unknown command');
}
} else {
- websocket.send('ALL ' + quote(tui.input_line));
+ server.send(['ALL', tui.input_line]);
}
}
tui.input_line = '';
tui.log_help();
tui.refresh();
} else if (event.key === 'f') {
- websocket.send("TASK:FLATTEN_SURROUNDINGS");
+ server.send(["TASK:FLATTEN_SURROUNDINGS"]);
} else if (event.key === 'a') {
- websocket.send('TASK:MOVE LEFT');
+ server.send(['TASK:MOVE', 'LEFT']);
} else if (event.key === 'd') {
- websocket.send('TASK:MOVE RIGHT');
+ server.send(['TASK:MOVE', 'RIGHT']);
} else if (event.key === 'w') {
- websocket.send('TASK:MOVE UP');
+ server.send(['TASK:MOVE', 'UP']);
} else if (event.key === 's') {
- websocket.send('TASK:MOVE DOWN');
+ server.send(['TASK:MOVE', 'DOWN']);
};
} else if (tui.mode == 'edit') {
if (event.key != "Shift" && event.key.length == 1) {
- websocket.send("TASK:WRITE " + quote(event.key));
+ server.send(["TASK:WRITE", event.key]);
tui.switch_mode('play');
}
} else if (tui.mode == 'study') {