import { Logger } from './Logger.js'; export class GameWebsocketCommandHandler { parse(commandString) { let dataArray = commandString.split(" "); switch (dataArray[0]) { case 'connect': return (state) => { this.connect(state, dataArray.slice(1)); }; case 'join': return (state) => { this.join(state, dataArray.slice(1)); }; case 'move': return (state) => { this.move(state, dataArray.slice(1)); }; case 'leave': return (state) => { this.leave(state, dataArray.slice(1)); } default: return () => { console.warn("recieved message without handler", commandString); }; } } connect(state, dataArray) { state.connectPlayer(dataArray[0]); } join(state, dataArray) { let data = {}; data.id = dataArray[0]; data.position = {}; data.position.x = parseFloat(dataArray[1]); data.position.y = parseFloat(dataArray[2]); data.angle = parseFloat(dataArray[3]); data.velocity = {}; data.velocity.x = parseFloat(dataArray[4]); data.velocity.y = parseFloat(dataArray[5]); data.acceleration = {}; data.acceleration.x = parseFloat(dataArray[6]); data.acceleration.y = parseFloat(dataArray[7]); data.isThrusting = dataArray[8] == "True"; state.addOtherShip(data); } move(state, dataArray) { let data = {}; data.id = dataArray[0]; data.position = {}; data.position.x = parseFloat(dataArray[1]); data.position.y = parseFloat(dataArray[2]); data.angle = parseFloat(dataArray[3]); data.velocity = {}; data.velocity.x = parseFloat(dataArray[4]); data.velocity.y = parseFloat(dataArray[5]); data.acceleration = {}; data.acceleration.x = parseFloat(dataArray[6]); data.acceleration.y = parseFloat(dataArray[7]); data.isThrusting = dataArray[8] == "True"; state.updateOtherShip(data); } leave(state, dataArray) { state.removeOtherShip( dataArray[0], ); } }