server update
This commit is contained in:
		
							parent
							
								
									d05feafb86
								
							
						
					
					
						commit
						ee621bb431
					
				
					 1 changed files with 59 additions and 50 deletions
				
			
		
							
								
								
									
										109
									
								
								server/server.js
									
									
									
									
									
								
							
							
						
						
									
										109
									
								
								server/server.js
									
									
									
									
									
								
							|  | @ -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; | ||||
|  | @ -18,50 +20,50 @@ app.get('/', (req, res) => { | |||
|   res.send('Grand Tabernacle Auto VI'); | ||||
| }); | ||||
| 
 | ||||
| function norm(x1, x2, y1, y2){ | ||||
|   return Math.sqrt((x1-x2)**2 + (y1+y2)**2); | ||||
| function norm(x1, x2, y1, y2) { | ||||
|   return Math.sqrt((x1 - x2) ** 2 + (y1 + y2) ** 2); | ||||
| } | ||||
| 
 | ||||
| function circleCollide(x, y){ | ||||
| function circleCollide(x, y) { | ||||
|   let colliding = false; | ||||
|   objectsModule.objects.circles.forEach((circle)=>{ | ||||
|     if(norm(x, circle.x, y, circle.y)<=circle.r){ | ||||
|   objectsModule.objects.circles.forEach((circle) => { | ||||
|     if (norm(x, circle.x, y, circle.y) <= circle.r) { | ||||
|       colliding = true; | ||||
|     } | ||||
|   }) | ||||
|   return colliding; | ||||
| } | ||||
| 
 | ||||
| function squareCollide(x, y){ | ||||
| function squareCollide(x, y) { | ||||
|   let colliding = false; | ||||
|   objectsModule.objects.squares.forEach((square)=>{ | ||||
|     if(square.x <= x && x<=square.x+square.w && square.y<=y && y<=square.y+square.h){ | ||||
|   objectsModule.objects.squares.forEach((square) => { | ||||
|     if (square.x <= x && x <= square.x + square.w && square.y <= y && y <= square.y + square.h) { | ||||
|       colliding = true; | ||||
|     } | ||||
|   }) | ||||
|   return colliding; | ||||
| } | ||||
| 
 | ||||
| function generatePosition(){ | ||||
| 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); | ||||
|   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){ | ||||
|     if (count >= 1000) { | ||||
|       break; | ||||
|     } | ||||
|   } | ||||
|   return [x, y]; | ||||
| } | ||||
| 
 | ||||
| function createNewPlayer(socket, name){ | ||||
| function createNewPlayer(socket, name) { | ||||
|   let pos = generatePosition(); | ||||
|   var obj = { | ||||
|     id: playerCount, | ||||
|  | @ -94,48 +96,53 @@ 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){ | ||||
|     username=username.substring(0, NAME_MAXLEN); | ||||
|   if (username.length > NAME_MAXLEN) { | ||||
|     username = username.substring(0, NAME_MAXLEN); | ||||
|   } | ||||
|   createNewPlayer(socket, username); | ||||
| 
 | ||||
|   socket.on('message', (message) => { | ||||
|     message = JSON.parse(message); | ||||
|     if (message.type == "ping") { | ||||
|     switch (message.type) { | ||||
|       case 'ping': | ||||
|         socket.send("pong"); | ||||
|         break; | ||||
| 
 | ||||
|       socket.send("pong"); | ||||
| 
 | ||||
|     }else if(message.type=="update") { | ||||
| 
 | ||||
|       for (var i = players.length - 1; i >= 0; i--) { | ||||
|         if(players[i].id == message.data.id){ | ||||
|           players[i]=message.data; | ||||
|       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); | ||||
|         broadcast(JSON.stringify(message), socket.id); | ||||
|         break; | ||||
| 
 | ||||
|     } else if(message.type=="newBullet"){ | ||||
|       case "newBullet": | ||||
|         broadcast(JSON.stringify(message), socket.id); | ||||
|         break; | ||||
| 
 | ||||
|       broadcast(JSON.stringify(message), socket.id); | ||||
|       case "died": | ||||
|         broadcast(JSON.stringify(message), socket.id); | ||||
|         for (var i = players.length - 1; i >= 0; i--) { | ||||
| 
 | ||||
|     } 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]; | ||||
| 
 | ||||
|         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] | ||||
|           })) | ||||
|             broadcast(JSON.stringify({ | ||||
|               type: "update", | ||||
|               data: players[i] | ||||
|             })) | ||||
|           } | ||||
|         } | ||||
|       } | ||||
|         break; | ||||
| 
 | ||||
|       default: | ||||
|         break; | ||||
|     } | ||||
|   }); | ||||
| 
 | ||||
|  | @ -143,9 +150,11 @@ 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){ | ||||
|       if (players[i].id == socket.id) { | ||||
|         players.splice(i, 1); | ||||
|       } | ||||
|     } | ||||
|  | @ -155,7 +164,7 @@ wss.on('connection', (socket, req) => { | |||
| 
 | ||||
| function broadcast(message, exceptId = -1) { | ||||
|   connections.forEach((socket) => { | ||||
|     if (socket.readyState === WebSocket.OPEN && socket.id!=exceptId) { | ||||
|     if (socket.readyState === WebSocket.OPEN && socket.id != exceptId) { | ||||
|       socket.send(message); | ||||
|     } | ||||
|   }); | ||||
|  | @ -165,4 +174,4 @@ const PORT = 8080; | |||
| 
 | ||||
| server.listen(PORT, () => { | ||||
|   console.log(`Serveur WebSocket écoutant sur le port ${PORT}`); | ||||
| }); | ||||
| }); | ||||
		Loading…
	
		Reference in a new issue