server update

This commit is contained in:
Killian Marty 2023-12-07 11:20:48 +01:00
parent d05feafb86
commit ee621bb431

View file

@ -6,7 +6,9 @@ const objectsModule = require('./objects');
const app = express();
const server = http.createServer(app);
const wss = new WebSocket.Server({ server });
const wss = new WebSocket.Server({
server
});
const connections = new Set();
var playerCount = 0;
@ -94,7 +96,7 @@ function createNewPlayer(socket, name){
wss.on('connection', (socket, req) => {
//create new player, send informations to new player and broadcast new player for all
let username = url.parse(req.url, true).query.name;
if(username===undefined || username == null){
if (username === undefined || username == null || username == "null") {
username = "Soldat Inconnu"
}
if (username.length > NAME_MAXLEN) {
@ -104,24 +106,25 @@ wss.on('connection', (socket, req) => {
socket.on('message', (message) => {
message = JSON.parse(message);
if (message.type == "ping") {
switch (message.type) {
case 'ping':
socket.send("pong");
break;
}else if(message.type=="update") {
case "update":
for (var i = players.length - 1; i >= 0; i--) {
if (players[i].id == message.data.id) {
players[i] = message.data;
}
}
broadcast(JSON.stringify(message), socket.id);
break;
} else if(message.type=="newBullet"){
case "newBullet":
broadcast(JSON.stringify(message), socket.id);
break;
} else if(message.type=="died"){
case "died":
broadcast(JSON.stringify(message), socket.id);
for (var i = players.length - 1; i >= 0; i--) {
@ -136,6 +139,10 @@ wss.on('connection', (socket, req) => {
}))
}
}
break;
default:
break;
}
});
@ -143,7 +150,9 @@ wss.on('connection', (socket, req) => {
for (var i = players.length - 1; i >= 0; i--) {
broadcast(JSON.stringify({
type: "removePlayer",
data: {id: socket.id}
data: {
id: socket.id
}
}));
if (players[i].id == socket.id) {
players.splice(i, 1);