GameWebsocketCommandHandler.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import { Logger } from './Logger.js';
  2. export class GameWebsocketCommandHandler {
  3. parse(commandString) {
  4. let dataArray = commandString.split(" ");
  5. switch (dataArray[0]) {
  6. case 'connect':
  7. return (state) => { this.connect(state, dataArray.slice(1)); };
  8. case 'join':
  9. return (state) => { this.join(state, dataArray.slice(1)); };
  10. case 'move':
  11. return (state) => { this.move(state, dataArray.slice(1)); };
  12. case 'leave':
  13. return (state) => { this.leave(state, dataArray.slice(1)); }
  14. default:
  15. return () => {
  16. console.warn("recieved message without handler", commandString);
  17. };
  18. }
  19. }
  20. connect(state, dataArray) {
  21. state.connectPlayer(dataArray[0]);
  22. }
  23. join(state, dataArray) {
  24. let data = {};
  25. data.id = dataArray[0];
  26. data.position = {};
  27. data.position.x = parseFloat(dataArray[1]);
  28. data.position.y = parseFloat(dataArray[2]);
  29. data.angle = parseFloat(dataArray[3]);
  30. data.velocity = {};
  31. data.velocity.x = parseFloat(dataArray[4]);
  32. data.velocity.y = parseFloat(dataArray[5]);
  33. data.acceleration = {};
  34. data.acceleration.x = parseFloat(dataArray[6]);
  35. data.acceleration.y = parseFloat(dataArray[7]);
  36. data.isThrusting = dataArray[8] == "True";
  37. state.addOtherShip(data);
  38. }
  39. move(state, dataArray) {
  40. let data = {};
  41. data.id = dataArray[0];
  42. data.position = {};
  43. data.position.x = parseFloat(dataArray[1]);
  44. data.position.y = parseFloat(dataArray[2]);
  45. data.angle = parseFloat(dataArray[3]);
  46. data.velocity = {};
  47. data.velocity.x = parseFloat(dataArray[4]);
  48. data.velocity.y = parseFloat(dataArray[5]);
  49. data.acceleration = {};
  50. data.acceleration.x = parseFloat(dataArray[6]);
  51. data.acceleration.y = parseFloat(dataArray[7]);
  52. data.isThrusting = dataArray[8] == "True";
  53. state.updateOtherShip(data);
  54. }
  55. leave(state, dataArray) {
  56. state.removeOtherShip(
  57. dataArray[0],
  58. );
  59. }
  60. }