From: Christian Heller Date: Tue, 27 Oct 2020 00:21:48 +0000 (+0100) Subject: Improve client control. X-Git-Url: https://plomlompom.com/repos/te"st.html?a=commitdiff_plain;h=460e83248e8fb477f18c1a09d49c7b2bdcea9046;p=plomrogue2-experiments Improve client control. --- diff --git a/new2/rogue_chat_nocanvas_monochrome.html b/new2/rogue_chat_nocanvas_monochrome.html index d873b37..7f852bc 100644 --- a/new2/rogue_chat_nocanvas_monochrome.html +++ b/new2/rogue_chat_nocanvas_monochrome.html @@ -98,7 +98,20 @@ let parser = { position[0] = parseInt(coordinate_strings[0].slice(2)); position[1] = parseInt(coordinate_strings[1].slice(2)); return position; - } + }, +} + +function quote(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(''); } let tui = { @@ -169,6 +182,20 @@ let tui = { }, refresh: function() { terminal.refresh(); + }, + log_help: function() { + tui.log_msg("HELP", 1); + tui.log_msg(""); + tui.log_msg("/login USER - register as USER", 3); + tui.log_msg("/msg USER TEXT - send TEXT to USER", 3); + tui.log_msg("/game - switch to game mode", 3); + tui.log_msg(""); + tui.log_msg("map mode commands:", 1); + tui.log_msg("w, a, s, d - move avatar", 3); + tui.log_msg("f - flatten surroundings", 3); + tui.log_msg("e - write following ASCII character", 3); + tui.log_msg("c - switch to chat mode", 3); + tui.log_msg(""); } } @@ -182,32 +209,17 @@ let game = { let chat = { input_line: "", - history: [] + history: [], } terminal.initialize(); - +tui.log_help(); tui.draw_map(); tui.draw_turn_line(); tui.draw_history(); tui.draw_input_line(); tui.refresh(); -tui.log_msg("basic commands:", 1); -tui.log_msg("LOGIN USER - register as USER", 3); -tui.log_msg("ALL TEXT - send TEXT to all users", 3); -tui.log_msg("QUERY USER TEXT - send TEXT to USER", 3); -tui.log_msg(""); -tui.log_msg("Use arrow keys to move your avatar. You can only move over \".\" map cells.", 1); -tui.log_msg(""); -tui.log_msg("Use double quotes for strings that contain whitespace, escape them with \\.", 1); -tui.log_msg(""); -tui.log_msg("To change the map cell you are standing on, type the desired ASCII character into the prompt and hit Return.", 1); -tui.log_msg(""); -tui.log_msg("more commands:", 1); -tui.log_msg("FLATTEN - transform surrounding map cells to \".\" ones", 3); -tui.log_msg(""); - let websocket = new WebSocket(websocket_location); websocket.onmessage = function (event) { let tokens = parser.tokenize(event.data); @@ -248,38 +260,75 @@ websocket.onmessage = function (event) { } } +let mode = 'chat'; document.addEventListener('keydown', (event) => { - if (chat.input_line === '') { - tui.draw_input_line(); - tui.refresh(); - } - if (event.key && event.key.length === 1) { - chat.input_line += event.key; - tui.draw_input_line(); - tui.refresh(); - } else if (event.key === 'Backspace') { - chat.input_line = chat.input_line.slice(0, -1); - tui.draw_input_line(); - tui.refresh(); - } else if (event.key === 'Enter') { - if (chat.input_line.length === 1) { - websocket.send("TASK:WRITE " + chat.input_line); - } else if (chat.input_line.trimEnd() === 'FLATTEN') { - websocket.send("TASK:FLATTEN_SURROUNDINGS"); - } else { - websocket.send(chat.input_line); + if (mode == 'chat') { + if (event.key.length === 1) { + chat.input_line += event.key; + tui.draw_input_line(); + tui.refresh(); + } else if (event.key == 'Backspace') { + chat.input_line = chat.input_line.slice(0, -1); + tui.draw_input_line(); + tui.refresh(); + } else if (event.key == 'Enter') { + let tokens = parser.tokenize(chat.input_line); + console.log(tokens); + if (tokens.length > 0 && tokens[0].length > 0) { + if (tokens[0][0] == '/') { + if (tokens[0] == '/game') { + mode = 'game'; + } else if (tokens[0] == '/?') { + tui.log_help(); + tui.refresh(); + } else if (tokens[0] == '/login') { + if (tokens.length > 1) { + websocket.send('LOGIN ' + quote(tokens[1])); + } else { + tui.log_msg('need login name'); + } + } else if (tokens[0] == '/msg') { + if (tokens.length > 2) { + websocket.send('QUERY ' + quote(tokens[1]) + ' ' + quote(tokens[2])); + } else { + tui.log_msg('need message target and message'); + } + } else { + tui.log_msg('unknown command'); + } + } else { + websocket.send('ALL ' + quote(chat.input_line)); + } + } + chat.input_line = ''; + tui.draw_input_line(); + tui.refresh(); + } + } else if (mode == 'game') { + if (event.key === 'c') { + mode = 'chat'; + } else if (event.key === 'e') { + mode = 'edit'; + } else if (event.key === '?') { + tui.log_help(); + tui.refresh(); + } else if (event.key === 'f') { + websocket.send("TASK:FLATTEN_SURROUNDINGS"); + } else if (event.key === 'a') { + websocket.send('TASK:MOVE LEFT'); + } else if (event.key === 'd') { + websocket.send('TASK:MOVE RIGHT'); + } else if (event.key === 'w') { + websocket.send('TASK:MOVE UP'); + } else if (event.key === 's') { + websocket.send('TASK:MOVE DOWN'); + }; + } else if (mode == 'edit') { + if (event.key.length === 1) { + websocket.send("TASK:WRITE " + event.key); + } + mode = 'game'; } - chat.input_line = ''; - tui.draw_input_line(); - } else if (event.key === 'ArrowLeft') { - websocket.send('TASK:MOVE LEFT'); - } else if (event.key === 'ArrowRight') { - websocket.send('TASK:MOVE RIGHT'); - } else if (event.key === 'ArrowUp') { - websocket.send('TASK:MOVE UP'); - } else if (event.key === 'ArrowDown') { - websocket.send('TASK:MOVE DOWN'); - }; }, false); window.setInterval(function() { websocket.send('PING') }, 30000);