server update

This commit is contained in:
Killian Marty 2023-12-06 08:52:39 +01:00
parent 335c1e87e1
commit 032dfc9aae
5 changed files with 135 additions and 16 deletions

View file

@ -616,6 +616,11 @@
"node": ">= 0.10" "node": ">= 0.10"
} }
}, },
"node_modules/punycode": {
"version": "1.4.1",
"resolved": "https://registry.npmjs.org/punycode/-/punycode-1.4.1.tgz",
"integrity": "sha512-jmYNElW7yvO7TV33CjSmvSiE2yco3bV2czu/OzDKdMNVZQWfxCblURLhf+47syQRBntjfLdd/H0egrzIG+oaFQ=="
},
"node_modules/qs": { "node_modules/qs": {
"version": "6.11.0", "version": "6.11.0",
"resolved": "https://registry.npmjs.org/qs/-/qs-6.11.0.tgz", "resolved": "https://registry.npmjs.org/qs/-/qs-6.11.0.tgz",
@ -861,6 +866,29 @@
"node": ">= 0.8" "node": ">= 0.8"
} }
}, },
"node_modules/url": {
"version": "0.11.3",
"resolved": "https://registry.npmjs.org/url/-/url-0.11.3.tgz",
"integrity": "sha512-6hxOLGfZASQK/cijlZnZJTq8OXAkt/3YGfQX45vvMYXpZoo8NdWZcY73K108Jf759lS1Bv/8wXnHDTSz17dSRw==",
"dependencies": {
"punycode": "^1.4.1",
"qs": "^6.11.2"
}
},
"node_modules/url/node_modules/qs": {
"version": "6.11.2",
"resolved": "https://registry.npmjs.org/qs/-/qs-6.11.2.tgz",
"integrity": "sha512-tDNIz22aBzCDxLtVH++VnTfzxlfeK5CbqohpSqpJgj1Wg/cQbStNAz3NuqCs5vV+pjBsK4x4pN9HlVh7rcYRiA==",
"dependencies": {
"side-channel": "^1.0.4"
},
"engines": {
"node": ">=0.6"
},
"funding": {
"url": "https://github.com/sponsors/ljharb"
}
},
"node_modules/utils-merge": { "node_modules/utils-merge": {
"version": "1.0.1", "version": "1.0.1",
"resolved": "https://registry.npmjs.org/utils-merge/-/utils-merge-1.0.1.tgz", "resolved": "https://registry.npmjs.org/utils-merge/-/utils-merge-1.0.1.tgz",

View file

@ -11,6 +11,7 @@
"dependencies": { "dependencies": {
"express": "^4.18.2", "express": "^4.18.2",
"socket.io": "^4.7.2", "socket.io": "^4.7.2",
"url": "^0.11.3",
"ws": "^8.14.2" "ws": "^8.14.2"
} }
}, },
@ -626,6 +627,11 @@
"node": ">= 0.10" "node": ">= 0.10"
} }
}, },
"node_modules/punycode": {
"version": "1.4.1",
"resolved": "https://registry.npmjs.org/punycode/-/punycode-1.4.1.tgz",
"integrity": "sha512-jmYNElW7yvO7TV33CjSmvSiE2yco3bV2czu/OzDKdMNVZQWfxCblURLhf+47syQRBntjfLdd/H0egrzIG+oaFQ=="
},
"node_modules/qs": { "node_modules/qs": {
"version": "6.11.0", "version": "6.11.0",
"resolved": "https://registry.npmjs.org/qs/-/qs-6.11.0.tgz", "resolved": "https://registry.npmjs.org/qs/-/qs-6.11.0.tgz",
@ -871,6 +877,29 @@
"node": ">= 0.8" "node": ">= 0.8"
} }
}, },
"node_modules/url": {
"version": "0.11.3",
"resolved": "https://registry.npmjs.org/url/-/url-0.11.3.tgz",
"integrity": "sha512-6hxOLGfZASQK/cijlZnZJTq8OXAkt/3YGfQX45vvMYXpZoo8NdWZcY73K108Jf759lS1Bv/8wXnHDTSz17dSRw==",
"dependencies": {
"punycode": "^1.4.1",
"qs": "^6.11.2"
}
},
"node_modules/url/node_modules/qs": {
"version": "6.11.2",
"resolved": "https://registry.npmjs.org/qs/-/qs-6.11.2.tgz",
"integrity": "sha512-tDNIz22aBzCDxLtVH++VnTfzxlfeK5CbqohpSqpJgj1Wg/cQbStNAz3NuqCs5vV+pjBsK4x4pN9HlVh7rcYRiA==",
"dependencies": {
"side-channel": "^1.0.4"
},
"engines": {
"node": ">=0.6"
},
"funding": {
"url": "https://github.com/sponsors/ljharb"
}
},
"node_modules/utils-merge": { "node_modules/utils-merge": {
"version": "1.0.1", "version": "1.0.1",
"resolved": "https://registry.npmjs.org/utils-merge/-/utils-merge-1.0.1.tgz", "resolved": "https://registry.npmjs.org/utils-merge/-/utils-merge-1.0.1.tgz",
@ -1370,6 +1399,11 @@
"ipaddr.js": "1.9.1" "ipaddr.js": "1.9.1"
} }
}, },
"punycode": {
"version": "1.4.1",
"resolved": "https://registry.npmjs.org/punycode/-/punycode-1.4.1.tgz",
"integrity": "sha512-jmYNElW7yvO7TV33CjSmvSiE2yco3bV2czu/OzDKdMNVZQWfxCblURLhf+47syQRBntjfLdd/H0egrzIG+oaFQ=="
},
"qs": { "qs": {
"version": "6.11.0", "version": "6.11.0",
"resolved": "https://registry.npmjs.org/qs/-/qs-6.11.0.tgz", "resolved": "https://registry.npmjs.org/qs/-/qs-6.11.0.tgz",
@ -1551,6 +1585,25 @@
"resolved": "https://registry.npmjs.org/unpipe/-/unpipe-1.0.0.tgz", "resolved": "https://registry.npmjs.org/unpipe/-/unpipe-1.0.0.tgz",
"integrity": "sha512-pjy2bYhSsufwWlKwPc+l3cN7+wuJlK6uz0YdJEOlQDbl6jo/YlPi4mb8agUkVC8BF7V8NuzeyPNqRksA3hztKQ==" "integrity": "sha512-pjy2bYhSsufwWlKwPc+l3cN7+wuJlK6uz0YdJEOlQDbl6jo/YlPi4mb8agUkVC8BF7V8NuzeyPNqRksA3hztKQ=="
}, },
"url": {
"version": "0.11.3",
"resolved": "https://registry.npmjs.org/url/-/url-0.11.3.tgz",
"integrity": "sha512-6hxOLGfZASQK/cijlZnZJTq8OXAkt/3YGfQX45vvMYXpZoo8NdWZcY73K108Jf759lS1Bv/8wXnHDTSz17dSRw==",
"requires": {
"punycode": "^1.4.1",
"qs": "^6.11.2"
},
"dependencies": {
"qs": {
"version": "6.11.2",
"resolved": "https://registry.npmjs.org/qs/-/qs-6.11.2.tgz",
"integrity": "sha512-tDNIz22aBzCDxLtVH++VnTfzxlfeK5CbqohpSqpJgj1Wg/cQbStNAz3NuqCs5vV+pjBsK4x4pN9HlVh7rcYRiA==",
"requires": {
"side-channel": "^1.0.4"
}
}
}
},
"utils-merge": { "utils-merge": {
"version": "1.0.1", "version": "1.0.1",
"resolved": "https://registry.npmjs.org/utils-merge/-/utils-merge-1.0.1.tgz", "resolved": "https://registry.npmjs.org/utils-merge/-/utils-merge-1.0.1.tgz",

View file

@ -12,6 +12,7 @@
"dependencies": { "dependencies": {
"express": "^4.18.2", "express": "^4.18.2",
"socket.io": "^4.7.2", "socket.io": "^4.7.2",
"url": "^0.11.3",
"ws": "^8.14.2" "ws": "^8.14.2"
} }
} }

View file

@ -1,6 +1,8 @@
const express = require('express'); const express = require('express');
const http = require('http'); const http = require('http');
const WebSocket = require('ws'); const WebSocket = require('ws');
const url = require('url');
const objectsModule = require('./objects');
const app = express(); const app = express();
const server = http.createServer(app); const server = http.createServer(app);
@ -14,24 +16,57 @@ app.get('/', (req, res) => {
res.send('Grand Tabernacle Auto VI'); res.send('Grand Tabernacle Auto VI');
}); });
players.push({ function norm(x1, x2, y1, y2){
id: playerCount, return Math.sqrt((x1-x2)**2 + (y1+y2)**2);
x: 100, }
y: 100,
dir: 4,
visibleDir: 4,
name: playerCount.toString() + "joueur"
})
playerCount++;
function createNewPlayer(socket){ function circleCollide(x, y){
objectsModule.objects.circles.forEach((circle)=>{
if(norm(x, circle.x, y, circle.y)<=circle.radius){
return true;
}
})
return false;
}
function squareCollide(x, y){
objectsModule.objects.squares.forEach((square)=>{
if(square.x <= x && x<=square.x+square.w && square.y<=y && y<=square.y+square.h){
return true;
}
})
return false;
}
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;
}
}
return x, y;
}
function createNewPlayer(socket, name){
let x, y = generatePosition();
var obj = { var obj = {
id: playerCount, id: playerCount,
x: Math.floor(Math.random()*1000), x: x,
y: Math.floor(Math.random()*1000), y: y,
dir: 0, dir: 0,
visibleDir: 0, visibleDir: 0,
name: playerCount.toString() + "joueur" name: name
} }
players.push(obj); players.push(obj);
playerCount++; playerCount++;
@ -53,9 +88,11 @@ function createNewPlayer(socket){
connections.add(socket); connections.add(socket);
} }
wss.on('connection', (socket) => { wss.on('connection', (socket, req) => {
//create new player, send informations to new player and broadcast new player for all //create new player, send informations to new player and broadcast new player for all
createNewPlayer(socket); let username = url.parse(req.url, true).query.name;
console.log(username);
createNewPlayer(socket, username);
socket.on('message', (message) => { socket.on('message', (message) => {
message = JSON.parse(message); message = JSON.parse(message);

View file

@ -1,4 +1,4 @@
const socket = new WebSocket('ws://localhost:8080'); //ws://129.151.227.50:9600 const socket = new WebSocket('ws://localhost:8080?name=coucou'); //ws://129.151.227.50:9600
var time = new Date(); var time = new Date();
socket.addEventListener('open', (event) => { socket.addEventListener('open', (event) => {
console.log('connecté'); console.log('connecté');