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){ | ||||
| 		this.socket.send(JSON.stringify({type: "sendMessage", data: {title: title, content: content}})); | ||||
| 		this.socket.send(JSON.stringify({type: "message", data: {title: title, content: content}})); | ||||
| 	} | ||||
| } | ||||
							
								
								
									
										160
									
								
								server/server.js
									
									
									
									
									
								
							
							
						
						
									
										160
									
								
								server/server.js
									
									
									
									
									
								
							|  | @ -1,66 +1,49 @@ | |||
| const express = require('express'); | ||||
| const http = require('http'); | ||||
| const https = require('https').createServer; | ||||
| const WebSocket = require('ws'); | ||||
| const url = require('url'); | ||||
| const fs = require('fs'); | ||||
| 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({ | ||||
|   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(); | ||||
| var playerCount = 0; | ||||
| var players = [] | ||||
| 
 | ||||
| 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() { | ||||
|   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; | ||||
|   let index = Math.floor(Math.random()*spawnPoints.length); | ||||
|   return [spawnPoints[index].x, spawnPoints[index].y]; | ||||
| } | ||||
| 
 | ||||
| function getUsername(req){ | ||||
|   let username = url.parse(req.url, true).query.name; | ||||
|   if (username === undefined || username == null || username == "null") { | ||||
|     username = "Soldat Inconnu" | ||||
|   } | ||||
|   return [x, y]; | ||||
|   if (username.length > NAME_MAXLEN) { | ||||
|     username = username.substring(0, NAME_MAXLEN); | ||||
|   } | ||||
|   return username; | ||||
| } | ||||
| 
 | ||||
| function createNewPlayer(socket, name) { | ||||
|  | @ -93,38 +76,20 @@ function createNewPlayer(socket, name) { | |||
|   connections.add(socket); | ||||
| } | ||||
| 
 | ||||
| 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 || username == "null") { | ||||
|     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": | ||||
| function update(message, socket){ | ||||
|   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": | ||||
| function newBullet(message, socket){ | ||||
|   broadcast(JSON.stringify(message), socket.id); | ||||
|         break; | ||||
| } | ||||
| 
 | ||||
|       case "died": | ||||
| function died(message, socket){ | ||||
|   broadcast(JSON.stringify(message), socket.id); | ||||
|   for (var i = players.length - 1; i >= 0; i--) { | ||||
| 
 | ||||
|  | @ -139,14 +104,9 @@ wss.on('connection', (socket, req) => { | |||
|       })) | ||||
|     } | ||||
|   } | ||||
|         break; | ||||
| 
 | ||||
|       default: | ||||
|         break; | ||||
| } | ||||
|   }); | ||||
| 
 | ||||
|   socket.on('close', () => { | ||||
| function deletePlayer(socket){ | ||||
|   for (var i = players.length - 1; i >= 0; i--) { | ||||
|     broadcast(JSON.stringify({ | ||||
|       type: "removePlayer", | ||||
|  | @ -159,8 +119,11 @@ wss.on('connection', (socket, req) => { | |||
|     } | ||||
|   } | ||||
|   connections.delete(socket); | ||||
|   }); | ||||
| }); | ||||
| } | ||||
| 
 | ||||
| function message(socket, data){ | ||||
|   broadcast(JSON.stringify(data), socket.id); | ||||
| } | ||||
| 
 | ||||
| function broadcast(message, exceptId = -1) { | ||||
|   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; | ||||
| 
 | ||||
| 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