From: Christian Heller Date: Wed, 28 Oct 2020 21:23:57 +0000 (+0100) Subject: Some client refactoring. X-Git-Url: https://plomlompom.com/repos/task?a=commitdiff_plain;h=d111426abddb8b84eb609cfa8916d036b05a5d52;p=plomrogue2-experiments Some client refactoring. --- diff --git a/new2/rogue_chat_nocanvas_monochrome.html b/new2/rogue_chat_nocanvas_monochrome.html index 72ac10c..a3f81f2 100644 --- a/new2/rogue_chat_nocanvas_monochrome.html +++ b/new2/rogue_chat_nocanvas_monochrome.html @@ -98,7 +98,7 @@ let parser = { }; 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)); @@ -107,17 +107,42 @@ let parser = { }, } -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 = { @@ -271,12 +296,8 @@ terminal.initialize(); 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 = {} @@ -364,7 +385,7 @@ let explorer = { } }, 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) { @@ -377,7 +398,7 @@ let explorer = { 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]); } } @@ -404,14 +425,14 @@ document.addEventListener('keydown', (event) => { 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'); } @@ -419,7 +440,7 @@ document.addEventListener('keydown', (event) => { tui.log_msg('? unknown command'); } } else { - websocket.send('ALL ' + quote(tui.input_line)); + server.send(['ALL', tui.input_line]); } } tui.input_line = ''; @@ -437,19 +458,19 @@ document.addEventListener('keydown', (event) => { 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') {