GrandTabernacleAutoVI/server/server.js

169 lines
3.7 KiB
JavaScript
Raw Normal View History

2023-11-14 17:45:09 +01:00
const express = require('express');
const http = require('http');
2023-11-14 17:35:47 +01:00
const WebSocket = require('ws');
2023-12-06 08:52:39 +01:00
const url = require('url');
const objectsModule = require('./objects');
2023-11-14 17:35:47 +01:00
2023-11-14 17:45:09 +01:00
const app = express();
const server = http.createServer(app);
const wss = new WebSocket.Server({ server });
2023-11-14 17:35:47 +01:00
2023-11-14 17:45:09 +01:00
const connections = new Set();
2023-11-14 17:35:47 +01:00
var playerCount = 0;
2023-11-18 13:38:29 +01:00
var players = []
2023-11-14 17:35:47 +01:00
2023-12-06 09:52:34 +01:00
const NAME_MAXLEN = 25;
2023-11-14 17:45:09 +01:00
app.get('/', (req, res) => {
2023-11-14 18:07:51 +01:00
res.send('Grand Tabernacle Auto VI');
2023-11-14 17:45:09 +01:00
});
2023-12-06 08:52:39 +01:00
function norm(x1, x2, y1, y2){
return Math.sqrt((x1-x2)**2 + (y1+y2)**2);
}
function circleCollide(x, y){
2023-12-06 10:09:16 +01:00
let colliding = false;
2023-12-06 08:52:39 +01:00
objectsModule.objects.circles.forEach((circle)=>{
2023-12-06 10:09:16 +01:00
if(norm(x, circle.x, y, circle.y)<=circle.r){
colliding = true;
2023-12-06 08:52:39 +01:00
}
})
2023-12-06 10:09:16 +01:00
return colliding;
2023-12-06 08:52:39 +01:00
}
function squareCollide(x, y){
2023-12-06 10:09:16 +01:00
let colliding = false;
2023-12-06 08:52:39 +01:00
objectsModule.objects.squares.forEach((square)=>{
if(square.x <= x && x<=square.x+square.w && square.y<=y && y<=square.y+square.h){
2023-12-06 10:09:16 +01:00
colliding = true;
2023-12-06 08:52:39 +01:00
}
2023-11-19 18:23:15 +01:00
})
2023-12-06 10:09:16 +01:00
return colliding;
2023-12-06 08:52:39 +01:00
}
function generatePosition(){
let mapWidth = 1000;
let mapHeight = 1000;
let count = 0;
let x = Math.floor(Math.random()*mapWidth);
let y = Math.floor(Math.random()*mapHeight);
while((circleCollide(x,y) || squareCollide(x,y))){
x = Math.floor(Math.random()*mapWidth);
y = Math.floor(Math.random()*mapHeight);
count++;
if(count >= 1000){
break;
}
}
2023-12-06 09:45:08 +01:00
return [x, y];
2023-12-06 08:52:39 +01:00
}
2023-11-19 18:23:15 +01:00
2023-12-06 08:52:39 +01:00
function createNewPlayer(socket, name){
2023-12-06 09:16:05 +01:00
let pos = generatePosition();
2023-11-18 20:17:47 +01:00
var obj = {
id: playerCount,
2023-12-06 09:17:24 +01:00
x: pos[0],
y: pos[1],
2023-11-19 18:23:15 +01:00
dir: 0,
visibleDir: 0,
2023-12-06 08:52:39 +01:00
name: name
2023-11-18 20:17:47 +01:00
}
2023-11-19 18:23:15 +01:00
players.push(obj);
playerCount++;
2023-11-18 20:17:47 +01:00
socket.send(JSON.stringify({
type: "connect",
data: {
playerId: obj.id,
players: players
}
}))
broadcast(JSON.stringify({
type: "newplayer",
data: obj
}), obj.id);
socket.id = obj.id;
2023-11-14 17:45:09 +01:00
connections.add(socket);
2023-11-18 20:17:47 +01:00
}
2023-12-06 08:52:39 +01:00
wss.on('connection', (socket, req) => {
2023-11-18 20:17:47 +01:00
//create new player, send informations to new player and broadcast new player for all
2023-12-06 08:52:39 +01:00
let username = url.parse(req.url, true).query.name;
2023-12-06 09:49:25 +01:00
if(username===undefined || username == null){
2023-12-06 09:40:15 +01:00
username = "Soldat Inconnu"
2023-12-06 09:16:05 +01:00
}
2023-12-06 09:52:34 +01:00
if(username.length > NAME_MAXLEN){
username=username.substring(0, NAME_MAXLEN);
2023-12-06 09:46:36 +01:00
}
2023-12-06 08:52:39 +01:00
createNewPlayer(socket, username);
2023-11-14 17:45:09 +01:00
socket.on('message', (message) => {
message = JSON.parse(message);
2023-11-18 20:17:47 +01:00
if (message.type == "ping") {
2023-12-06 10:42:53 +01:00
2023-11-14 17:45:09 +01:00
socket.send("pong");
2023-12-06 10:42:53 +01:00
}else if(message.type=="update") {
2023-12-06 09:40:15 +01:00
for (var i = players.length - 1; i >= 0; i--) {
2023-12-06 09:41:08 +01:00
if(players[i].id == message.data.id){
2023-12-06 09:40:15 +01:00
players[i]=message.data;
2023-12-06 09:35:51 +01:00
}
2023-12-06 09:40:15 +01:00
}
2023-11-18 20:17:47 +01:00
broadcast(JSON.stringify(message), socket.id);
2023-12-06 10:42:53 +01:00
2023-12-06 10:20:31 +01:00
} else if(message.type=="newBullet"){
2023-12-06 10:42:53 +01:00
2023-12-06 10:25:42 +01:00
broadcast(JSON.stringify(message), socket.id);
2023-12-06 10:42:53 +01:00
} else if(message.type=="died"){
broadcast(JSON.stringify(message), socket.id);
for (var i = players.length - 1; i >= 0; i--) {
if(players[i].id == message.data.id){
let pos = generatePosition();
players[i].x=pos[0];
players[i].y=pos[1];
broadcast(JSON.stringify({
type:"update",
data: players[i]
}))
}
}
2023-11-14 17:45:09 +01:00
}
});
2023-11-14 17:35:47 +01:00
socket.on('close', () => {
2023-11-18 20:17:47 +01:00
for (var i = players.length - 1; i >= 0; i--) {
2023-12-06 09:21:43 +01:00
broadcast(JSON.stringify({
type: "removePlayer",
2023-12-06 09:22:16 +01:00
data: {id: socket.id}
2023-12-06 09:21:43 +01:00
}));
2023-11-18 20:17:47 +01:00
if(players[i].id==socket.id){
players.splice(i, 1);
}
}
2023-11-14 17:35:47 +01:00
connections.delete(socket);
});
});
2023-11-18 20:17:47 +01:00
function broadcast(message, exceptId = -1) {
2023-11-14 17:45:09 +01:00
connections.forEach((socket) => {
2023-11-18 20:17:47 +01:00
if (socket.readyState === WebSocket.OPEN && socket.id!=exceptId) {
2023-11-14 17:45:09 +01:00
socket.send(message);
}
});
2023-11-14 17:35:47 +01:00
}
2023-11-14 17:45:09 +01:00
2023-11-20 11:01:11 +01:00
const PORT = 8080;
2023-11-14 17:45:09 +01:00
server.listen(PORT, () => {
console.log(`Serveur WebSocket écoutant sur le port ${PORT}`);
});