home · contact · privacy
Add basic multiplayer roguelike chat example.
[plomrogue2-experiments] / new2 / rogue_chat.html
diff --git a/new2/rogue_chat.html b/new2/rogue_chat.html
new file mode 100644 (file)
index 0000000..6fec908
--- /dev/null
@@ -0,0 +1,183 @@
+<!DOCTYPE HTML>
+<html>
+<style>
+canvas { border: 1px solid black; }
+</style>
+<body>
+<canvas id="terminal" />
+<script>
+"use strict";
+let websocket_location = "ws://localhost:8000"
+
+let terminal = {
+  rows: 24,
+  cols: 80,
+  charHeight: 24,
+  initialize: function() {
+    this.ctx = document.getElementById("terminal").getContext("2d"),
+    this.set_font();
+    this.charWidth = this.ctx.measureText("M").width;
+    this.ctx.canvas.height = this.charHeight * this.rows;
+    this.ctx.canvas.width = this.charWidth * this.cols;
+    this.set_font();  // ctx.font gets reset to default on canvas size change, so we have to re-set our own
+    this.ctx.textBaseline = "top";
+  },
+  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);
+    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); 
+  },
+  drawBox: function (start_y, start_x, height, width, color='white') {
+    this.ctx.fillStyle = color;
+    this.ctx.fillRect(start_x*this.charWidth, start_y*this.charHeight,
+                     this.charWidth*width, this.charHeight*height);
+  }
+}
+
+let parser = {
+  tokenize: function(str) {
+    let tokens = [];
+    let token = ''
+    let quoted = false;
+    let escaped = false;
+    for (let i = 0; i < str.length; i++) {
+      let c = str[i];
+      if (quoted) {
+        if (escaped) {
+          token += c;
+          escaped = false;
+        } else if (c == '\\') { 
+          escaped = true;
+        } else if (c == '"') { 
+           quoted = false
+        } else {
+          token += c;
+        }
+      } else if (c == '"') {
+        quoted = true
+      } else if (c === ' ') {
+        if (token.length > 0) {
+          tokens.push(token);
+          token = '';
+        }
+      } else {
+        token += c;
+      }
+    }
+    if (token.length > 0) {
+      tokens.push(token);
+    }
+    return tokens;
+  },
+  parse_position(position_string) {
+    let coordinate_strings = position_string.split(',')
+    let position = [0, 0];
+    position[0] = coordinate_strings[0].slice(2);
+    position[1] = coordinate_strings[1].slice(2);
+    return position;
+  }
+}
+
+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);
+    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 (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_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);
+  }
+}
+
+let game = {
+  things: {},
+  tick: 0
+}
+
+let chat = {
+  input_line: "",
+  history: []
+}
+
+terminal.initialize()
+terminal.drawBox(terminal.rows - 1, terminal.cols / 2, 1, terminal.cols, 'black');
+
+document.addEventListener('keydown', (event) => {
+  if (chat.input_line === '') {
+    terminal.drawBox(terminal.rows - 1, terminal.cols / 2, 1, terminal.rows, 'black');
+  }
+  if (event.key && event.key.length === 1) {
+    chat.input_line += event.key;
+    tui.draw_input_line();
+  } else if (event.key === 'Backspace') {
+    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 = ''
+    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');
+  };
+  console.log(event.key);
+}, false);
+
+let websocket = new WebSocket(websocket_location);
+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();
+  } else if (tokens[0] === 'THING_POS') {
+    game.things[tokens[1]] = parser.parse_position(tokens[2]); 
+    tui.draw_map();
+  } else if (tokens[0] === 'LOG') {
+     tui.log_msg(' ' + tokens[1]);
+  } 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 {
+     tui.log_msg('unhandled input: ' + event.data);
+  }
+}
+</script>
+</body>
+</html>