<commit_before>import ProtoBuf from 'protobufjs';
import * as PIXI from "pixi.js";
import * as Constants from './constants.js';
import * as Utils from './utils.js';
import Spaceship from './spaceship.js';
import { renderer, stage } from './globals.js';

// TODO: Use config for this
const ws = new WebSocket("ws://" + window.location.hostname + ":8080/superstellar");

const spaceMessageHandler = (space) => {
  const ships = space.spaceships;

  for (var i in ships) {
    let shipId = ships[i].id;

    if (!shipIds.has(shipId)) {
      const newSpaceship = new Spaceship(shipTexture, shipThrustFrames, ships[i]);

      shipIds.set(shipId, newSpaceship);
    } else {
      shipIds.get(shipId).updateData(ships[i]);
    }

    if (myID == 0) {
      for (var i in ships) {
	if (ships[i].id > myID) {
	  myID = ships[i].id
	}
      }
    }
  }
};

const helloMessageHandler = (message) => {
  myID = message.myId;
};

const playerLeftHandler = (message) => {
  const playerId = message.id;

  let spaceship = shipIds.get(playerId);

  spaceship.remove();

  shipIds.delete(playerId);
};

const messageHandlers = new Map();

messageHandlers.set("hello", helloMessageHandler);
messageHandlers.set("space", spaceMessageHandler);
messageHandlers.set("playerLeft", playerLeftHandler);

const webSocketMessageReceived = (e) => {
  var fileReader = new FileReader();

  fileReader.onload = function() {
    const message = Message.decode(this.result);

    messageHandlers.get(message.content)(message[message.content]);
  };

  fileReader.readAsArrayBuffer(e.data);
};


const KEY_UP = 38;
const KEY_LEFT = 37;
const KEY_RIGHT = 39;

const shipIds = new Map();

document.body.appendChild(renderer.view);

const builder = ProtoBuf.loadJsonFile(Constants.PROTOBUF_DEFINITION);
const Message = builder.build(Constants.MESSAGE_DEFINITION);
const Space = builder.build(Constants.SPACE_DEFINITION);
const UserInput = builder.build(Constants.USER_INPUT_DEFINITION);
const PlayerLeft = builder.build(Constants.PLAYER_LEFT_DEFINITION);


const loadProgressHandler = (loader, resource) => {
  console.log(`progress: ${loader.progress}%`);
};

PIXI.loader.
  add([Constants.SHIP_TEXTURE, Constants.BACKGROUND_TEXTURE, Constants.FLAME_SPRITESHEET]).
  on("progress", loadProgressHandler).
  load(setup);

let shipTexture;
let shipThrustTexture;
let bgTexture;

let tilingSprite;

const hudTextStyle = {
  fontFamily: 'Helvetica',
  fontSize: '24px',
  fill: '#FFFFFF',
  align: 'left',
  textBaseline: 'top'
};

const buildHudText = (shipCount, fps, x, y) => {
  let text = "Ships: " + shipCount + "\n";
  text += "FPS: " + fps + "\n";
  text += "X: " + x + "\n";
  text += "Y: " + y + "\n";

  return text;
}

let hudText;
let shipThrustFrames = [];
let thrustAnim;

function setup() {
  shipTexture = PIXI.loader.resources[Constants.SHIP_TEXTURE].texture;

  Constants.FLAME_SPRITESHEET_FRAME_NAMES.forEach((frameName) =>  {
    shipThrustFrames.push(PIXI.Texture.fromFrame(frameName));
  });

  ws.onmessage = webSocketMessageReceived;

  bgTexture = PIXI.loader.resources[Constants.BACKGROUND_TEXTURE].texture;

  tilingSprite = new PIXI.extras.TilingSprite(bgTexture, renderer.width, renderer.height);
  stage.addChild(tilingSprite);

  hudText = new PIXI.Text('', hudTextStyle);
  hudText.x = 580;
  hudText.y = 0;
  stage.addChild(hudText);

  // Let's play this game!
  var then = Date.now();
  main();
}

var myID = 0;

var viewport = {vx: 0, vy: 0, width: 800, height: 600}

var frameCounter = 0;
var lastTime = Date.now();
var fps = 0;

// Handle keyboard controls
var keysDown = {};

addEventListener("keydown", function (e) {
  keysDown[e.keyCode] = true;
}, false);

addEventListener("keyup", function (e) {
  delete keysDown[e.keyCode];
}, false);

var sendInput = function() {
  var thrust = KEY_UP in keysDown;

  var direction = "NONE";
  if (KEY_LEFT in keysDown) {
    direction = "LEFT";
  } else if (KEY_RIGHT in keysDown) {
    direction = "RIGHT";
  }

  var userInput = new UserInput(thrust, direction);
  var buffer = userInput.encode();

  if (ws.readyState == WebSocket.OPEN) {
    ws.send(buffer.toArrayBuffer());
  }
}

// Draw everything
var render = function () {
  var myShip;

  let backgroundPos = Utils.translateToViewport(0, 0, viewport);
  tilingSprite.tilePosition.set(backgroundPos.x, backgroundPos.y);

  if (shipIds.size > 0) {
    myShip = shipIds.get(myID);
    viewport = myShip.viewport();
  }

  shipIds.forEach((spaceship) => spaceship.update(viewport));
  frameCounter++;
<commit_msg>Fix Hello message not being received<commit_after>import ProtoBuf from 'protobufjs';
import * as PIXI from "pixi.js";
import * as Constants from './constants.js';
import * as Utils from './utils.js';
import Spaceship from './spaceship.js';
import { renderer, stage } from './globals.js';

// TODO: Use config for this
let ws; 

const spaceMessageHandler = (space) => {
  const ships = space.spaceships;

  for (var i in ships) {
    let shipId = ships[i].id;

    if (!shipIds.has(shipId)) {
      const newSpaceship = new Spaceship(shipTexture, shipThrustFrames, ships[i]);

      shipIds.set(shipId, newSpaceship);
    } else {
      shipIds.get(shipId).updateData(ships[i]);
    }
  }
};

const helloMessageHandler = (message) => {
  myID = message.myId;
};

const playerLeftHandler = (message) => {
  const playerId = message.id;

  let spaceship = shipIds.get(playerId);

  spaceship.remove();

  shipIds.delete(playerId);
};

const messageHandlers = new Map();

messageHandlers.set("hello", helloMessageHandler);
messageHandlers.set("space", spaceMessageHandler);
messageHandlers.set("playerLeft", playerLeftHandler);

const webSocketMessageReceived = (e) => {
  var fileReader = new FileReader();

  fileReader.onload = function() {
    const message = Message.decode(this.result);

    messageHandlers.get(message.content)(message[message.content]);
  };

  fileReader.readAsArrayBuffer(e.data);
};


const KEY_UP = 38;
const KEY_LEFT = 37;
const KEY_RIGHT = 39;

const shipIds = new Map();

document.body.appendChild(renderer.view);

const builder = ProtoBuf.loadJsonFile(Constants.PROTOBUF_DEFINITION);
const Message = builder.build(Constants.MESSAGE_DEFINITION);
const Space = builder.build(Constants.SPACE_DEFINITION);
const UserInput = builder.build(Constants.USER_INPUT_DEFINITION);
const PlayerLeft = builder.build(Constants.PLAYER_LEFT_DEFINITION);


const loadProgressHandler = (loader, resource) => {
  console.log(`progress: ${loader.progress}%`);
};

PIXI.loader.
  add([Constants.SHIP_TEXTURE, Constants.BACKGROUND_TEXTURE, Constants.FLAME_SPRITESHEET]).
  on("progress", loadProgressHandler).
  load(setup);

let shipTexture;
let shipThrustTexture;
let bgTexture;

let tilingSprite;

const hudTextStyle = {
  fontFamily: 'Helvetica',
  fontSize: '24px',
  fill: '#FFFFFF',
  align: 'left',
  textBaseline: 'top'
};

const buildHudText = (shipCount, fps, x, y) => {
  let text = "Ships: " + shipCount + "\n";
  text += "FPS: " + fps + "\n";
  text += "X: " + x + "\n";
  text += "Y: " + y + "\n";

  return text;
}

let hudText;
let shipThrustFrames = [];
let thrustAnim;

function setup() {
  shipTexture = PIXI.loader.resources[Constants.SHIP_TEXTURE].texture;

  Constants.FLAME_SPRITESHEET_FRAME_NAMES.forEach((frameName) =>  {
    shipThrustFrames.push(PIXI.Texture.fromFrame(frameName));
  });

  ws = new WebSocket("ws://" + window.location.hostname + ":8080/superstellar");
  ws.onmessage = webSocketMessageReceived;

  bgTexture = PIXI.loader.resources[Constants.BACKGROUND_TEXTURE].texture;

  tilingSprite = new PIXI.extras.TilingSprite(bgTexture, renderer.width, renderer.height);
  stage.addChild(tilingSprite);

  hudText = new PIXI.Text('', hudTextStyle);
  hudText.x = 580;
  hudText.y = 0;
  stage.addChild(hudText);

  // Let's play this game!
  var then = Date.now();
  main();
}

var myID = 0;

var viewport = {vx: 0, vy: 0, width: 800, height: 600}

var frameCounter = 0;
var lastTime = Date.now();
var fps = 0;

// Handle keyboard controls
var keysDown = {};

addEventListener("keydown", function (e) {
  keysDown[e.keyCode] = true;
}, false);

addEventListener("keyup", function (e) {
  delete keysDown[e.keyCode];
}, false);

var sendInput = function() {
  var thrust = KEY_UP in keysDown;

  var direction = "NONE";
  if (KEY_LEFT in keysDown) {
    direction = "LEFT";
  } else if (KEY_RIGHT in keysDown) {
    direction = "RIGHT";
  }

  var userInput = new UserInput(thrust, direction);
  var buffer = userInput.encode();

  if (ws.readyState == WebSocket.OPEN) {
    ws.send(buffer.toArrayBuffer());
  }
}

// Draw everything
var render = function () {
  if (!myID) { return }
  let myShip;

  let backgroundPos = Utils.translateToViewport(0, 0, viewport);
  tilingSprite.tilePosition.set(backgroundPos.x, backgroundPos.y);

  if (shipIds.size > 0) {
    myShip = shipIds.get(myID);
    viewport = myShip.viewport();
  }

  shipIds.forEach((spaceship) => spaceship.update(viewport));
  frameCounter++;

{'fraction_non_alphanumeric': 0.1449436949492697, 'fraction_numerical': 0.006801204147619579, 'mean_word_length': 4.2857984678845025, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 22, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3054743', 'n_tokens_mistral': 3174, 'n_tokens_neox': 3028, 'n_words': 682}