GrandTabernacleAutoVI/js/network.js

40 lines
943 B
JavaScript
Raw Normal View History

2023-11-17 19:25:26 +01:00
class Network{
constructor(adress){
this.adress = adress;
this.connected = false;
}
message(data){
switch(data.type){
case 'connect':
2023-11-18 13:38:29 +01:00
playerId = data.data.playerId;
for (var i = data.data.length - 1; i >= 0; i--) {
players.push(new Player(data.data.players[i].id, data.data.players[i].x, data.data.players[i].y, data.data.players[i].name))
}
2023-11-17 19:25:26 +01:00
break;
case 'update':
2023-11-18 13:38:29 +01:00
for (var i = players.length - 1; i >= 0; i--) {
if(players[i].id==data.data.id){
players[i]=data.data;
break;
}
}
break;
case "newplayer":
players.push(new Player(data.data.id, data.data.x, data.data.y, data.data.name));
2023-11-17 19:25:26 +01:00
break;
}
}
connect(){
this.socket = new WebSocket(this.adress);
this.socket.addEventListener('open', (e)=>{
//connected to server
this.connected = true;
});
this.socket.addEventListener('message', (e)=>{
this.message(JSON.parse(e.data));
})
}
}