GrandTabernacleAutoVI/js/network.js

75 lines
2 KiB
JavaScript
Raw Normal View History

2023-11-17 19:25:26 +01:00
class Network{
2023-11-29 16:12:00 +01:00
constructor(adress){
2023-11-17 19:25:26 +01:00
this.adress = adress;
this.connected = false;
2023-11-29 16:12:00 +01:00
2023-11-29 16:20:47 +01:00
this.playerId = null;
2023-11-29 17:25:40 +01:00
this.clientPlayer=null;
2023-11-29 16:12:00 +01:00
this.playersToAdd = [];
this.playersToRemove = [];
this.playersToUpdate = [];
2023-11-17 19:25:26 +01:00
}
message(data){
switch(data.type){
case 'connect':
2023-11-29 16:12:00 +01:00
this.playerId = data.data.playerId;
2023-11-19 18:38:27 +01:00
for (var i = data.data.players.length - 1; i >= 0; i--) {
2023-11-29 17:25:40 +01:00
if(data.data.players[i].id==this.playerId)
2023-11-29 17:50:21 +01:00
this.clientPlayer=new Player(data.data.players[i].id, data.data.players[i].x, data.data.players[i].y, data.data.players[i].name, data.data.players[i].dir);
2023-11-29 17:25:40 +01:00
else
this.playersToAdd.push(new Player(data.data.players[i].id, data.data.players[i].x, data.data.players[i].y, data.data.players[i].name, data.data.players[i].dir))
2023-11-18 13:38:29 +01:00
}
2023-11-17 19:25:26 +01:00
break;
2023-12-02 17:40:38 +01:00
2023-11-17 19:25:26 +01:00
case 'update':
2023-11-29 16:12:00 +01:00
this.playersToUpdate.push(data.data);
2023-11-18 13:38:29 +01:00
break;
2023-12-02 17:40:38 +01:00
2023-11-18 13:38:29 +01:00
case "newplayer":
2023-11-29 16:12:00 +01:00
this.playersToAdd.push(new Player(data.data.id, data.data.x, data.data.y, data.data.name, data.data.dir));
2023-11-17 19:25:26 +01:00
break;
2023-12-02 17:40:38 +01:00
2023-11-29 18:00:22 +01:00
case "removePlayer":
this.playersToRemove.push(data.data.id);
break;
2023-12-02 17:40:38 +01:00
default:
break;
2023-11-17 19:25:26 +01:00
}
}
2023-12-02 17:40:38 +01:00
connect(){ //create the WebSocket, initialize it and connect to the server
2023-11-17 19:25:26 +01:00
this.socket = new WebSocket(this.adress);
2023-12-02 17:40:38 +01:00
this.socket.addEventListener('open', (e)=>{
this.connected = true; //connected to server
2023-11-17 19:25:26 +01:00
});
this.socket.addEventListener('message', (e)=>{
this.message(JSON.parse(e.data));
})
}
2023-11-19 19:56:29 +01:00
2023-12-02 17:40:38 +01:00
update(obj){ //send data to server in order to broadcast
2023-11-19 19:56:29 +01:00
this.socket.send(JSON.stringify({
type: "update",
data: obj
}))
}
2023-11-29 16:12:00 +01:00
2023-12-02 17:40:38 +01:00
getPlayersToAdd(){ //returns the list of new players
2023-11-29 16:12:00 +01:00
let tmp = this.playersToAdd;
this.playersToAdd = [];
return tmp;
}
2023-12-02 17:40:38 +01:00
getPlayersToRemove(){ //returns the list of player who have left the game
2023-11-29 16:12:00 +01:00
let tmp = this.playersToRemove;
this.playersToRemove = [];
return tmp;
}
2023-12-02 17:40:38 +01:00
getPlayersToUpdate(){ //return a list of all updates recieved from the server
2023-11-29 16:12:00 +01:00
let tmp = this.playersToUpdate;
this.playersToUpdate = [];
return tmp;
}
2023-11-17 19:25:26 +01:00
}