diff --git a/server/node_modules/.package-lock.json b/server/node_modules/.package-lock.json index 1b909ed..7f2718c 100644 --- a/server/node_modules/.package-lock.json +++ b/server/node_modules/.package-lock.json @@ -616,6 +616,11 @@ "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": { "version": "6.11.0", "resolved": "https://registry.npmjs.org/qs/-/qs-6.11.0.tgz", @@ -861,6 +866,29 @@ "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": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/utils-merge/-/utils-merge-1.0.1.tgz", diff --git a/server/package-lock.json b/server/package-lock.json index 27a3594..f507e4f 100644 --- a/server/package-lock.json +++ b/server/package-lock.json @@ -11,6 +11,7 @@ "dependencies": { "express": "^4.18.2", "socket.io": "^4.7.2", + "url": "^0.11.3", "ws": "^8.14.2" } }, @@ -626,6 +627,11 @@ "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": { "version": "6.11.0", "resolved": "https://registry.npmjs.org/qs/-/qs-6.11.0.tgz", @@ -871,6 +877,29 @@ "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": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/utils-merge/-/utils-merge-1.0.1.tgz", @@ -1370,6 +1399,11 @@ "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": { "version": "6.11.0", "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", "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": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/utils-merge/-/utils-merge-1.0.1.tgz", diff --git a/server/package.json b/server/package.json index e4a2154..04ec167 100644 --- a/server/package.json +++ b/server/package.json @@ -12,6 +12,7 @@ "dependencies": { "express": "^4.18.2", "socket.io": "^4.7.2", + "url": "^0.11.3", "ws": "^8.14.2" } } diff --git a/server/server.js b/server/server.js index 9681c0c..7f40c2e 100644 --- a/server/server.js +++ b/server/server.js @@ -1,6 +1,8 @@ const express = require('express'); const http = require('http'); const WebSocket = require('ws'); +const url = require('url'); +const objectsModule = require('./objects'); const app = express(); const server = http.createServer(app); @@ -14,24 +16,57 @@ app.get('/', (req, res) => { res.send('Grand Tabernacle Auto VI'); }); -players.push({ - id: playerCount, - x: 100, - y: 100, - dir: 4, - visibleDir: 4, - name: playerCount.toString() + "joueur" - }) -playerCount++; +function norm(x1, x2, y1, y2){ + return Math.sqrt((x1-x2)**2 + (y1+y2)**2); +} -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 = { id: playerCount, - x: Math.floor(Math.random()*1000), - y: Math.floor(Math.random()*1000), + x: x, + y: y, dir: 0, visibleDir: 0, - name: playerCount.toString() + "joueur" + name: name } players.push(obj); playerCount++; @@ -53,9 +88,11 @@ function createNewPlayer(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 - createNewPlayer(socket); + let username = url.parse(req.url, true).query.name; + console.log(username); + createNewPlayer(socket, username); socket.on('message', (message) => { message = JSON.parse(message); diff --git a/server/testsite/app.js b/server/testsite/app.js index 29b14c2..1454825 100644 --- a/server/testsite/app.js +++ b/server/testsite/app.js @@ -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(); socket.addEventListener('open', (event) => { console.log('connecté');