phone
This commit is contained in:
		
							parent
							
								
									539ccd2213
								
							
						
					
					
						commit
						cc5d19ea91
					
				
					 2 changed files with 118 additions and 110 deletions
				
			
		|  | @ -75,6 +75,6 @@ class Network{ | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	sendMessage(title, content){ | 	sendMessage(title, content){ | ||||||
| 		this.socket.send(JSON.stringify({type: "sendMessage", data: {title: title, content: content}})); | 		this.socket.send(JSON.stringify({type: "message", data: {title: title, content: content}})); | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
							
								
								
									
										226
									
								
								server/server.js
									
									
									
									
									
								
							
							
						
						
									
										226
									
								
								server/server.js
									
									
									
									
									
								
							|  | @ -1,66 +1,49 @@ | ||||||
| const express = require('express'); | const https = require('https').createServer; | ||||||
| const http = require('http'); |  | ||||||
| const WebSocket = require('ws'); | const WebSocket = require('ws'); | ||||||
| const url = require('url'); | const url = require('url'); | ||||||
|  | const fs = require('fs'); | ||||||
| const objectsModule = require('./objects'); | const objectsModule = require('./objects'); | ||||||
| 
 | 
 | ||||||
| const app = express(); | 
 | ||||||
| const server = http.createServer(app); | 
 | ||||||
|  | const options = { | ||||||
|  |     cert: fs.readFileSync("/home/ubuntu/servers/keys/gta6/cert.pem"), | ||||||
|  |     key: fs.readFileSync("/home/ubuntu/servers/keys/gta6/privkey.pem") | ||||||
|  | }; | ||||||
|  | 
 | ||||||
|  | const server = https(options, (req, res)=>{ | ||||||
|  |     res.end("Grand Tabernacle Auto 6") | ||||||
|  | }); | ||||||
|  | 
 | ||||||
| const wss = new WebSocket.Server({ | const wss = new WebSocket.Server({ | ||||||
|   server |   server | ||||||
| }); | }); | ||||||
| 
 | 
 | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | const spawnPoints = [{"x":218,"y":193},{"x":601,"y":715},{"x":1264,"y":57},{"x":274,"y":506},{"x":921,"y":854},{"x":1041,"y":442},{"x":638,"y":124},{"x":483,"y":436}] | ||||||
| const connections = new Set(); | const connections = new Set(); | ||||||
| var playerCount = 0; | var playerCount = 0; | ||||||
| var players = [] | var players = [] | ||||||
| 
 | 
 | ||||||
| const NAME_MAXLEN = 25; | const NAME_MAXLEN = 25; | ||||||
| 
 | 
 | ||||||
| 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 circleCollide(x, y) { |  | ||||||
|   let colliding = false; |  | ||||||
|   objectsModule.objects.circles.forEach((circle) => { |  | ||||||
|     if (norm(x, circle.x, y, circle.y) <= circle.r) { |  | ||||||
|       colliding = true; |  | ||||||
|     } |  | ||||||
|   }) |  | ||||||
|   return colliding; |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| 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) { |  | ||||||
|       colliding = true; |  | ||||||
|     } |  | ||||||
|   }) |  | ||||||
|   return colliding; |  | ||||||
| } |  | ||||||
| 
 | 
 | ||||||
| function generatePosition() { | function generatePosition() { | ||||||
|   let mapWidth = 1000; |   let index = Math.floor(Math.random()*spawnPoints.length); | ||||||
|   let mapHeight = 1000; |   return [spawnPoints[index].x, spawnPoints[index].y]; | ||||||
|   let count = 0; | } | ||||||
| 
 | 
 | ||||||
|   let x = Math.floor(Math.random() * mapWidth); | function getUsername(req){ | ||||||
|   let y = Math.floor(Math.random() * mapHeight); |   let username = url.parse(req.url, true).query.name; | ||||||
| 
 |   if (username === undefined || username == null || username == "null") { | ||||||
|   while ((circleCollide(x, y) || squareCollide(x, y))) { |     username = "Soldat Inconnu" | ||||||
|     x = Math.floor(Math.random() * mapWidth); |  | ||||||
|     y = Math.floor(Math.random() * mapHeight); |  | ||||||
|     count++; |  | ||||||
|     if (count >= 1000) { |  | ||||||
|       break; |  | ||||||
|     } |  | ||||||
|   } |   } | ||||||
|   return [x, y]; |   if (username.length > NAME_MAXLEN) { | ||||||
|  |     username = username.substring(0, NAME_MAXLEN); | ||||||
|  |   } | ||||||
|  |   return username; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| function createNewPlayer(socket, name) { | function createNewPlayer(socket, name) { | ||||||
|  | @ -93,74 +76,54 @@ function createNewPlayer(socket, name) { | ||||||
|   connections.add(socket); |   connections.add(socket); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| wss.on('connection', (socket, req) => { | function update(message, socket){ | ||||||
|   //create new player, send informations to new player and broadcast new player for all
 |   for (var i = players.length - 1; i >= 0; i--) { | ||||||
|   let username = url.parse(req.url, true).query.name; |     if (players[i].id == message.data.id) { | ||||||
|   if (username === undefined || username == null || username == "null") { |       players[i] = message.data; | ||||||
|     username = "Soldat Inconnu" |  | ||||||
|   } |  | ||||||
|   if (username.length > NAME_MAXLEN) { |  | ||||||
|     username = username.substring(0, NAME_MAXLEN); |  | ||||||
|   } |  | ||||||
|   createNewPlayer(socket, username); |  | ||||||
| 
 |  | ||||||
|   socket.on('message', (message) => { |  | ||||||
|     message = JSON.parse(message); |  | ||||||
|     switch (message.type) { |  | ||||||
|       case 'ping': |  | ||||||
|         socket.send("pong"); |  | ||||||
|         break; |  | ||||||
| 
 |  | ||||||
|       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; |  | ||||||
| 
 |  | ||||||
|       case "newBullet": |  | ||||||
|         broadcast(JSON.stringify(message), socket.id); |  | ||||||
|         break; |  | ||||||
| 
 |  | ||||||
|       case "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] |  | ||||||
|             })) |  | ||||||
|           } |  | ||||||
|         } |  | ||||||
|         break; |  | ||||||
| 
 |  | ||||||
|       default: |  | ||||||
|         break; |  | ||||||
|     } |     } | ||||||
|   }); |   } | ||||||
|  |   broadcast(JSON.stringify(message), socket.id); | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | function newBullet(message, socket){ | ||||||
|  |   broadcast(JSON.stringify(message), socket.id); | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | function died(message, socket){ | ||||||
|  |   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]; | ||||||
| 
 | 
 | ||||||
|   socket.on('close', () => { |  | ||||||
|     for (var i = players.length - 1; i >= 0; i--) { |  | ||||||
|       broadcast(JSON.stringify({ |       broadcast(JSON.stringify({ | ||||||
|         type: "removePlayer", |         type: "update", | ||||||
|         data: { |         data: players[i] | ||||||
|           id: socket.id |       })) | ||||||
|         } |  | ||||||
|       })); |  | ||||||
|       if (players[i].id == socket.id) { |  | ||||||
|         players.splice(i, 1); |  | ||||||
|       } |  | ||||||
|     } |     } | ||||||
|     connections.delete(socket); |   } | ||||||
|   }); | } | ||||||
| }); | 
 | ||||||
|  | function deletePlayer(socket){ | ||||||
|  |   for (var i = players.length - 1; i >= 0; i--) { | ||||||
|  |     broadcast(JSON.stringify({ | ||||||
|  |       type: "removePlayer", | ||||||
|  |       data: { | ||||||
|  |         id: socket.id | ||||||
|  |       } | ||||||
|  |     })); | ||||||
|  |     if (players[i].id == socket.id) { | ||||||
|  |       players.splice(i, 1); | ||||||
|  |     } | ||||||
|  |   } | ||||||
|  |   connections.delete(socket); | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | function message(socket, data){ | ||||||
|  |   broadcast(JSON.stringify(data), socket.id); | ||||||
|  | } | ||||||
| 
 | 
 | ||||||
| function broadcast(message, exceptId = -1) { | function broadcast(message, exceptId = -1) { | ||||||
|   connections.forEach((socket) => { |   connections.forEach((socket) => { | ||||||
|  | @ -170,8 +133,53 @@ function broadcast(message, exceptId = -1) { | ||||||
|   }); |   }); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | wss.on('connection', (socket, req) => { | ||||||
|  |   //create new player, send informations to new player and broadcast new player for all
 | ||||||
|  |   let username = getUsername(req); | ||||||
|  |   createNewPlayer(socket, username); | ||||||
|  | 
 | ||||||
|  |   //handle client's messages
 | ||||||
|  |   socket.on('message', (message) => { | ||||||
|  |     try{ | ||||||
|  |       message = JSON.parse(message); | ||||||
|  |       switch (message.type) { | ||||||
|  |         case 'ping': | ||||||
|  |           socket.send("pong"); | ||||||
|  |           break; | ||||||
|  | 
 | ||||||
|  |         case "update": | ||||||
|  |           update(message, socket); | ||||||
|  |           break; | ||||||
|  | 
 | ||||||
|  |         case "newBullet": | ||||||
|  |           newBullet(message, socket); | ||||||
|  |           break; | ||||||
|  | 
 | ||||||
|  |         case "died": | ||||||
|  |           died(message, socket); | ||||||
|  |           break; | ||||||
|  | 
 | ||||||
|  |         case "message": | ||||||
|  |           message(message, socket); | ||||||
|  |           break; | ||||||
|  | 
 | ||||||
|  |         default: | ||||||
|  |           break; | ||||||
|  |       } | ||||||
|  |     }catch(e){ | ||||||
|  |       console.log('error'); | ||||||
|  |     } | ||||||
|  |      | ||||||
|  |   }); | ||||||
|  | 
 | ||||||
|  |   //handle disconnecting
 | ||||||
|  |   socket.on('close', () => { | ||||||
|  |     deletePlayer(socket); | ||||||
|  |   }); | ||||||
|  | }); | ||||||
|  | 
 | ||||||
| const PORT = 8080; | const PORT = 8080; | ||||||
| 
 | 
 | ||||||
| server.listen(PORT, () => { | server.listen(PORT, () => { | ||||||
|   console.log(`Serveur WebSocket écoutant sur le port ${PORT}`); |   console.log(`WebSocket listening on port ${PORT}.`); | ||||||
| }); | }); | ||||||
		Loading…
	
		Reference in a new issue