network retrieving

This commit is contained in:
Killian Marty 2023-11-18 13:38:29 +01:00
parent 11e518c2eb
commit 80a7c81911
2 changed files with 32 additions and 4 deletions

View file

@ -7,10 +7,21 @@ class Network{
message(data){ message(data){
switch(data.type){ switch(data.type){
case 'connect': case 'connect':
console.log("connected"); 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))
}
break; break;
case 'update': case 'update':
console.log("updated"); 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));
break; break;
} }

View file

@ -8,6 +8,7 @@ const wss = new WebSocket.Server({ server });
const connections = new Set(); const connections = new Set();
var playerCount = 0; var playerCount = 0;
var players = []
app.get('/', (req, res) => { app.get('/', (req, res) => {
res.send('Grand Tabernacle Auto VI'); res.send('Grand Tabernacle Auto VI');
@ -22,10 +23,26 @@ wss.on('connection', (socket) => {
var obj = { var obj = {
id: playerCount, id: playerCount,
x: 0, x: 0,
y: 0 y: 0,
name: playerCount.toString() + "joueur"
} }
socket.send(JSON.stringify(obj)); players.push(obj)
playerCount++; playerCount++;
socket.send(JSON.stringify({
type: "connect",
data: {
playerId: obj.id,
players: players
}
}))
broadcast(JSON.stringify({
type: "newplayer",
data: obj
}));
} else if (message.type == "ping") { } else if (message.type == "ping") {
socket.send("pong"); socket.send("pong");
} else { } else {