home · contact · privacy
Improve client control.
[plomrogue2-experiments] / new2 / rogue_chat_nocanvas_monochrome.html
index 97c0f0879d8176baacf145a454b3fa6cb1b6b31c..7f852bc92139ad714ec36d96879b7694cdc4dc67 100644 (file)
@@ -44,12 +44,15 @@ let terminal = {
   drawBox: function(start_y, start_x, height, width) {
     let end_y = start_y + height;
     let end_x = start_x + width;
-    for (let y = start_y, x = start_x; y < end_y; x++) {
-        this.content[y][x] = ' ';
+      for (let y = start_y, x = start_x;; x++) {
         if (x == end_x) {
-            x = start_x - 1;
-            y += 1;
-        }
+          x = start_x;
+          y += 1;
+          if (y == end_y) {
+              break;
+          }
+        };
+        this.content[y][x] = ' ';
     }
   },
 }
@@ -95,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 = {
@@ -124,7 +140,9 @@ let tui = {
     for (const thing_id in game.things) {
         let t = game.things[thing_id];
         map_lines[t[0]][t[1]] = '@';
-        player_position = t;
+        if (game.player_id == thing_id) {
+            player_position = t;
+        }
     }
     let offset = [(terminal.rows / 2) - player_position[0],
                   terminal.cols / 4 - player_position[1]];
@@ -164,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("");
   }
 }
 
@@ -171,37 +203,23 @@ let game = {
   things: {},
   turn: 0,
   map: "",
-  map_size: [0,0]
+  map_size: [0,0],
+  player_id: 0
 }
 
 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);
@@ -220,6 +238,8 @@ websocket.onmessage = function (event) {
   } else if (tokens[0] === 'LOG') {
      tui.log_msg(tokens[1], 1);
      tui.refresh();
+  } else if (tokens[0] === 'PLAYER_ID') {
+      game.player_id = parseInt(tokens[1]);
   } else if (tokens[0] === 'META') {
      tui.log_msg(tokens[1]);
      tui.refresh();
@@ -240,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);