import Entity from "./out/Entity"

var decoder = new TextDecoder("utf-8");
var port = 8089;
var ws = require("ws");
var wss = new ws.WebSocketServer({ port: port });
console.log("WebSocket server listening on ws://localhost:" + port + "/");
wss.on("connection", function (ws) {
  console.log("Client connected!");
  ws.on("message", function (message, isBinary) {
    if (!isBinary) {
      var text = decoder.decode(new Uint8Array(message).buffer);
      console.log("received TEXT: " + text.length + " characters:");
      console.log('  "' + text + '"');
    } else {
      const entity = new Entity({}, new Uint8Array(message));
      console.log(entity.Camera.position.x, entity.Camera.position.y, entity.Camera.position.z);
      console.log(entity.Name);
      console.log(entity.Luck);
      console.log(entity.bytes);

      ws.send(entity.bytes, { binary: true }); // Echo back the received message
    }
  });
});
