home · contact · privacy
Refactor player session code.
[plomrogue2] / rogue_chat_nocanvas_monochrome.html
index 7a479d0b0cd85730a941f88d9668b2826210eb61..b8d7b986d11c1350bf203f5a8f380bc4b1298816 100644 (file)
@@ -4,8 +4,8 @@
 </style>
 </head><body>
 <div>
-terminal rows: <input id="n_rows" type="number" step=4 min=8 value=24 />
-terminal columns: <input id="n_cols" type="number" step=4 min=20 value=80 />
+terminal rows: <input id="n_rows" type="number" step=4 min=24 value=24 />
+terminal columns: <input id="n_cols" type="number" step=4 min=80 value=80 />
 </div>
 <pre id="terminal" style="display: inline-block;"></pre>
 <textarea id="input" style="opacity: 0; width: 0px;"></textarea>
@@ -29,7 +29,7 @@ terminal columns: <input id="n_cols" type="number" step=4 min=20 value=80 />
 <button id="switch_to_password">change tile editing password</button>
 <button id="switch_to_annotate">annotate tile</button>
 <button id="switch_to_portal">edit portal link</button>
-<button id="toggle_map_mode">toggle terrain/control view</button>
+<button id="toggle_map_mode">toggle terrain/annotations/control view</button>
 </div>
 <h3>edit keybindings</h3> (see <a href="https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key/Key_Values">here</a> for non-obvious available values):<br />
 <ul>
@@ -55,12 +55,13 @@ terminal columns: <input id="n_cols" type="number" step=4 min=20 value=80 />
 <li>enter tile password (from play mode): <input id="key_switch_to_password" type="text" value="P" />
 <li>annotate tile (from play mode): <input id="key_switch_to_annotate" type="text" value="M" />
 <li>annotate portal (from play mode): <input id="key_switch_to_portal" type="text" value="T" />
-<li>toggle terrain/control view (from study mode): <input id="key_toggle_map_mode" type="text" value="M" />
+<li>toggle terrain/annotations/control view (from study mode): <input id="key_toggle_map_mode" type="text" value="M" />
 </ul>
 </div>
 <script>
 "use strict";
 let websocket_location = "wss://plomlompom.com/rogue_chat/";
+//let websocket_location = "ws://localhost:8000/";
 
 let rows_selector = document.getElementById("n_rows");
 let cols_selector = document.getElementById("n_cols");
@@ -142,7 +143,6 @@ terminal.initialize();
 
 let parser = {
   tokenize: function(str) {
-    let token_ends = [];
     let tokens = [];
     let token = ''
     let quoted = false;
@@ -164,7 +164,6 @@ let parser = {
         quoted = true
       } else if (c === ' ') {
         if (token.length > 0) {
-          token_ends.push(i);
           tokens.push(token);
           token = '';
         }
@@ -175,11 +174,7 @@ let parser = {
     if (token.length > 0) {
       tokens.push(token);
     }
-    let token_starts = [];
-    for (let i = 0; i < token_ends.length; i++) {
-      token_starts.push(token_ends[i] - tokens[i].length);
-    };
-    return [tokens, token_starts];
+    return tokens;
   },
   parse_yx: function(position_string) {
     let coordinate_strings = position_string.split(',')
@@ -225,9 +220,10 @@ let server = {
         this.websocket.send(unparser.untokenize(tokens));
     },
     handle_event: function(event) {
-        let tokens = parser.tokenize(event.data)[0];
+        let tokens = parser.tokenize(event.data);
         if (tokens[0] === 'TURN') {
             game.turn_complete = false;
+            explorer.empty_info_db();
             game.things = {};
             game.portals = {};
             game.turn = parseInt(tokens[1]);
@@ -262,7 +258,6 @@ let server = {
             game.map_control = tokens[1]
         } else if (tokens[0] === 'GAME_STATE_COMPLETE') {
             game.turn_complete = true;
-            explorer.empty_info_db();
             if (tui.mode == mode_post_login_wait) {
                 tui.switch_mode(mode_play);
             } else if (tui.mode == mode_study) {
@@ -279,12 +274,18 @@ let server = {
         } else if (tokens[0] === 'PORTAL') {
             let position = parser.parse_yx(tokens[1]);
             game.portals[position] = tokens[2];
+        } else if (tokens[0] === 'ANNOTATION_HINT') {
+            let position = parser.parse_yx(tokens[1]);
+            explorer.info_hints = explorer.info_hints.concat([position]);
         } else if (tokens[0] === 'ANNOTATION') {
             let position = parser.parse_yx(tokens[1]);
             explorer.update_info_db(position, tokens[2]);
+            tui.restore_input_values();
+            tui.full_refresh();
         } else if (tokens[0] === 'UNHANDLED_INPUT') {
             tui.log_msg('? unknown command');
         } else if (tokens[0] === 'PLAY_ERROR') {
+            tui.log_msg('? ' + tokens[1]);
             terminal.blink_screen();
         } else if (tokens[0] === 'ARGUMENT_ERROR') {
             tui.log_msg('? syntax error: ' + tokens[1]);
@@ -385,10 +386,12 @@ let tui = {
     };
   },
   switch_mode: function(mode) {
+    this.inputEl.focus();
     this.show_help = false;
     this.map_mode = 'terrain';
     if (mode.shows_info && game.player_id in game.things) {
       explorer.position = game.things[game.player_id].position;
+      explorer.query_info();
     }
     this.mode = mode;
     this.empty_input();
@@ -532,7 +535,11 @@ let tui = {
         line.push(map_content[i] + ' ');
     };
     map_lines_split.push(line);
-    if (this.map_mode == 'terrain') {
+    if (this.map_mode == 'annotations') {
+        for (const coordinate of explorer.info_hints) {
+            map_lines_split[coordinate[0]][coordinate[1]] = 'A ';
+        }
+    } else if (this.map_mode == 'terrain') {
         for (const p in game.portals) {
             let coordinate = p.split(',')
             map_lines_split[coordinate[0]][coordinate[1]] = 'P ';
@@ -655,13 +662,12 @@ let tui = {
       } else if (this.mode == mode_study) {
           content += "Available actions:\n";
           content += '[' + movement_keys_desc + '] – move question mark\n';
-          content += '[' + this.keys.toggle_map_mode + '] – toggle view between terrain, and password protection areas\n';
+          content += '[' + this.keys.toggle_map_mode + '] – toggle view between terrain, annotations, and password protection areas\n';
           content += '\nOther modes available from here:\n';
           content += '[' + this.keys.switch_to_chat + '] – chat mode\n';
           content += '[' + this.keys.switch_to_play + '] – play mode\n';
       } else if (this.mode == mode_chat) {
           content += '/nick NAME – re-name yourself to NAME\n';
-          //content += '/msg USER TEXT – send TEXT to USER\n';
           content += '/' + this.keys.switch_to_play + ' or /play – switch to play mode\n';
           content += '/' + this.keys.switch_to_study + ' or /study – switch to study mode\n';
       }
@@ -781,6 +787,7 @@ server.init(websocket_location);
 let explorer = {
     position: [0,0],
     info_db: {},
+    info_hints: [],
     move: function(direction) {
         let target = game.move(this.position, direction);
         if (target) {
@@ -798,6 +805,7 @@ let explorer = {
     },
     empty_info_db: function() {
         this.info_db = {};
+        this.info_hints = [];
         if (tui.mode == mode_study) {
             tui.full_refresh();
         }
@@ -817,6 +825,11 @@ let explorer = {
             terrain_desc = game.terrains[terrain_char];
         };
         info += 'TERRAIN: "' + terrain_char + '" / ' + terrain_desc + "\n";
+        let protection = game.map_control[position_i];
+        if (protection == '.') {
+            protection = 'unprotected';
+        };
+        info += 'PROTECTION: ' + protection + '\n';
         for (let t_id in game.things) {
              let t = game.things[t_id];
              if (t.position[0] == this.position[0] && t.position[1] == this.position[1]) {
@@ -868,7 +881,6 @@ tui.inputEl.addEventListener('input', (event) => {
     }
     tui.full_refresh();
 }, false);
-
 tui.inputEl.addEventListener('keydown', (event) => {
     tui.show_help = false;
     if (event.key == 'Enter') {
@@ -897,7 +909,7 @@ tui.inputEl.addEventListener('keydown', (event) => {
         tui.password = tui.inputEl.value
         tui.switch_mode(mode_play);
     } else if (tui.mode == mode_chat && event.key == 'Enter') {
-        let [tokens, token_starts] = parser.tokenize(tui.inputEl.value);
+        let tokens = parser.tokenize(tui.inputEl.value);
         if (tokens.length > 0 && tokens[0].length > 0) {
             if (tui.inputEl.value[0][0] == '/') {
                 if (tokens[0].slice(1) == 'play' || tokens[0][1] == tui.keys.switch_to_play) {
@@ -910,13 +922,6 @@ tui.inputEl.addEventListener('keydown', (event) => {
                     } else {
                         tui.log_msg('? need new name');
                     }
-                //} else if (tokens[0].slice(1) == 'msg') {
-                //    if (tokens.length > 2) {
-                //        let msg = tui.inputEl.value.slice(token_starts[2]);
-                //        server.send(['QUERY', tokens[1], msg]);
-                //    } else {
-                //        tui.log_msg('? need message target and message');
-                //    }
                 } else {
                     tui.log_msg('? unknown command');
                 }
@@ -971,6 +976,8 @@ tui.inputEl.addEventListener('keydown', (event) => {
             explorer.move(tui.movement_keys[event.key]);
         } else if (event.key == tui.keys.toggle_map_mode) {
             if (tui.map_mode == 'terrain') {
+                tui.map_mode = 'annotations';
+            } else if (tui.map_mode == 'annotations') {
                 tui.map_mode = 'control';
             } else {
                 tui.map_mode = 'terrain';
@@ -981,7 +988,7 @@ tui.inputEl.addEventListener('keydown', (event) => {
 }, false);
 
 rows_selector.addEventListener('input', function() {
-    if (rows_selector.value % 4 != 0) {
+    if (rows_selector.value % 4 != 0 || rows_selector.value < 24) {
         return;
     }
     window.localStorage.setItem(rows_selector.id, rows_selector.value);
@@ -989,7 +996,7 @@ rows_selector.addEventListener('input', function() {
     tui.full_refresh();
 }, false);
 cols_selector.addEventListener('input', function() {
-    if (cols_selector.value % 4 != 0) {
+    if (cols_selector.value % 4 != 0 || cols_selector.value < 80) {
         return;
     }
     window.localStorage.setItem(cols_selector.id, cols_selector.value);
@@ -1003,12 +1010,6 @@ for (let key_selector of key_selectors) {
         tui.init_keys();
     }, false);
 }
-window.setInterval(function() {
-    if (!(['input', 'n_cols', 'n_rows'].includes(document.activeElement.id)
-          || document.activeElement.id.startsWith('key_'))) {
-        tui.inputEl.focus();
-    }
-}, 100);
 window.setInterval(function() {
     if (server.connected) {
         server.send(['PING']);
@@ -1017,7 +1018,9 @@ window.setInterval(function() {
         tui.log_msg('@ attempting reconnect …')
     }
 }, 5000);
-
+document.getElementById("terminal").onclick = function() {
+    tui.inputEl.focus();
+};
 document.getElementById("help").onclick = function() {
     tui.show_help = true;
     tui.full_refresh();
@@ -1052,6 +1055,8 @@ document.getElementById("switch_to_portal").onclick = function() {
 };
 document.getElementById("toggle_map_mode").onclick = function() {
     if (tui.map_mode == 'terrain') {
+        tui.map_mode = 'annotations';
+    } else if (tui.map_mode == 'annotations') {
         tui.map_mode = 'control';
     } else {
         tui.map_mode = 'terrain';