home · contact · privacy
Re-do and extrend color handling.
[plomrogue2-experiments] / new2 / rogue_chat.html
index 6fec9085a43d82bf095cabb89f8ee8f1013041ad..3d8e4c3850fa373bdb8dae0e2823cef6c50729be 100644 (file)
@@ -4,7 +4,8 @@
 canvas { border: 1px solid black; }
 </style>
 <body>
-<canvas id="terminal" />
+<p><canvas id="terminal" /></p>
+<p>(running against <a href="https://plomlompom.com/repos/?p=plomrogue2-experiments;a=blob;f=new2/rogue_chat.py">some plomrogue engine experiment</a>)</p>
 <script>
 "use strict";
 let websocket_location = "ws://localhost:8000"
@@ -25,16 +26,17 @@ let terminal = {
   set_font: function(type='normal') {
     this.ctx.font = type + ' ' + this.charHeight + 'px monospace';
   },
-  write: function(start_y, start_x, msg, foreground_color='black') {
-    this.ctx.fillStyle = foreground_color; 
-    this.ctx.fillRect(start_x*this.charWidth, start_y*this.charHeight,
-                     this.charWidth*msg.length, this.charHeight);
+  write: function(start_y, start_x, msg, foreground_color='white') {
     if (foreground_color === 'black') {
       this.ctx.fillStyle = 'white';
     } else {
       this.ctx.fillStyle = 'black';
     }
     this.ctx.fillText(msg, start_x*this.charWidth, start_y*this.charHeight); 
+    this.ctx.fillRect(start_x*this.charWidth, start_y*this.charHeight,
+                      this.charWidth*msg.length, this.charHeight);
+    this.ctx.fillStyle = foreground_color;
+    this.ctx.fillText(msg, start_x*this.charWidth, start_y*this.charHeight);
   },
   drawBox: function (start_y, start_x, height, width, color='white') {
     this.ctx.fillStyle = color;
@@ -78,7 +80,7 @@ let parser = {
     }
     return tokens;
   },
-  parse_position(position_string) {
+  parse_yx(position_string) {
     let coordinate_strings = position_string.split(',')
     let position = [0, 0];
     position[0] = coordinate_strings[0].slice(2);
@@ -88,40 +90,69 @@ let parser = {
 }
 
 let tui = {
-  log_msg: function(msg) {
-    chat.history.unshift(msg);
-    if (chat.history.length > terminal.rows - 2) {
-      chat.history.pop();
-    }
-    terminal.drawBox(1, terminal.cols / 2, terminal.rows - 2, terminal.cols);
+  draw_history: function() {
+    terminal.drawBox(1, terminal.cols / 2, terminal.rows - 2, terminal.cols, 'black');
     let i = 0;
     for (let line of chat.history) {
       terminal.write(terminal.rows - 2 - i, terminal.cols / 2, line);
       i += 1;
-      // if (i > terminal.rows - 3) {
-      //   break;
-      // }
     }
   },
   draw_map: function() {
     terminal.drawBox(0, 0, terminal.rows, terminal.cols / 2);
+    for (let i = 0, y = 0, x = 0; i < game.map.length; i++, x++) {
+      if (x >= game.map_size[1]) {
+        x = 0;
+       y += 1;
+      };
+      let c = game.map[i];
+      let color = 'white';
+      if (c == '.') {
+        color = '#ffaa00';
+      } else if (c == '~') {
+        color = '#5555ff';
+      } else if (c == 'X') {
+        color = '#55ff00';
+      }
+      terminal.write(y, x, game.map[i], color);
+    }
     for (const t in game.things) {
       terminal.write(game.things[t][0], game.things[t][1], '@');
     }
   },
-  draw_tick_line: function(n) {
-    terminal.drawBox(0, 0, terminal.rows, terminal.cols / 2);
-    terminal.write(0, terminal.cols / 2, 'tick: ' + game.tick);
+  draw_turn_line: function(n) {
+    terminal.drawBox(0, terminal.cols / 2, 1, terminal.cols / 2, 'black');
+    terminal.write(0, terminal.cols / 2, 'turn: ' + game.turn);
   },
   draw_input_line: function() {
     terminal.drawBox(terminal.rows - 1, terminal.cols / 2, 1, terminal.cols / 2, 'black');
     terminal.write(terminal.rows - 1, terminal.cols / 2, chat.input_line);
+  },
+  log_msg: function(msg, indent=0) {
+    let line_length = (terminal.cols / 2) - indent;
+    let chunk = "";
+    for (let i = 0, x = 0; i < msg.length; i++, x++) {
+      if (x >= line_length) {
+       chat.history.unshift(' '.repeat(indent) + chunk);
+       chunk = "";
+        x = 0;
+      };
+      chunk += msg[i];
+    }
+    chat.history.unshift(' '.repeat(indent) + chunk);
+    if (chat.history.length > terminal.rows - 2) {
+
+      chat.history.pop();
+    };
+    this.draw_history();
   }
 }
 
 let game = {
   things: {},
-  tick: 0
+  turn: 0,
+  map: "",
+  map_size: [1,1]
 }
 
 let chat = {
@@ -130,7 +161,25 @@ let chat = {
 }
 
 terminal.initialize()
-terminal.drawBox(terminal.rows - 1, terminal.cols / 2, 1, terminal.cols, 'black');
+tui.draw_map();
+tui.draw_turn_line();
+tui.draw_history();
+tui.draw_input_line();
+
+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("");
 
 document.addEventListener('keydown', (event) => {
   if (chat.input_line === '') {
@@ -143,8 +192,14 @@ document.addEventListener('keydown', (event) => {
     chat.input_line = chat.input_line.slice(0, -1);
     tui.draw_input_line();
   } else if (event.key === 'Enter') {
-    websocket.send(chat.input_line);
-    chat.input_line = ''
+    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);
+    }
+    chat.input_line = '';
     tui.draw_input_line();
   } else if (event.key === 'ArrowLeft') {
     websocket.send('TASK:MOVE LEFT');
@@ -155,7 +210,6 @@ document.addEventListener('keydown', (event) => {
   } else if (event.key === 'ArrowDown') {
     websocket.send('TASK:MOVE DOWN');
   };
-  console.log(event.key);
 }, false);
 
 let websocket = new WebSocket(websocket_location);
@@ -163,21 +217,34 @@ websocket.onmessage = function (event) {
   let tokens = parser.tokenize(event.data);
   if (tokens[0] === 'TURN') {
     game.things = {}
-    game.tick = parseInt(tokens[1]);
-    tui.draw_tick_line();
+    game.turn = parseInt(tokens[1]);
   } else if (tokens[0] === 'THING_POS') {
-    game.things[tokens[1]] = parser.parse_position(tokens[2]); 
+    game.things[tokens[1]] = parser.parse_yx(tokens[2]);
+  } else if (tokens[0] === 'MAP') {
+    game.map_size = parser.parse_yx(tokens[1]);
+    game.map = tokens[2]
+  } else if (tokens[0] === 'GAME_STATE_COMPLETE') {
+    tui.draw_turn_line();
+    tui.draw_map();
     tui.draw_map();
   } else if (tokens[0] === 'LOG') {
-     tui.log_msg(' ' + tokens[1]);
+     tui.log_msg(tokens[1], 1);
+  } else if (tokens[0] === 'META') {
+     tui.log_msg(tokens[1]);
+  } else if (tokens[0] === 'UNHANDLED_INPUT') {
+     tui.log_msg('unknown command');
   } else if (tokens[0] === 'ARGUMENT_ERROR') {
      tui.log_msg('syntax error: ' + tokens[1]);
   } else if (tokens[0] === 'GAME_ERROR') {
      tui.log_msg('game error: ' + tokens[1]);
+  } else if (tokens[0] === 'PONG') {
+    console.log('PONG');
   } else {
      tui.log_msg('unhandled input: ' + event.data);
   }
 }
+
+window.setInterval(function() { websocket.send('PING') }, 30000);
 </script>
 </body>
 </html>