server update
This commit is contained in:
parent
d05feafb86
commit
ee621bb431
1 changed files with 59 additions and 50 deletions
|
@ -6,7 +6,9 @@ const objectsModule = require('./objects');
|
||||||
|
|
||||||
const app = express();
|
const app = express();
|
||||||
const server = http.createServer(app);
|
const server = http.createServer(app);
|
||||||
const wss = new WebSocket.Server({ server });
|
const wss = new WebSocket.Server({
|
||||||
|
server
|
||||||
|
});
|
||||||
|
|
||||||
const connections = new Set();
|
const connections = new Set();
|
||||||
var playerCount = 0;
|
var playerCount = 0;
|
||||||
|
@ -18,50 +20,50 @@ app.get('/', (req, res) => {
|
||||||
res.send('Grand Tabernacle Auto VI');
|
res.send('Grand Tabernacle Auto VI');
|
||||||
});
|
});
|
||||||
|
|
||||||
function norm(x1, x2, y1, y2){
|
function norm(x1, x2, y1, y2) {
|
||||||
return Math.sqrt((x1-x2)**2 + (y1+y2)**2);
|
return Math.sqrt((x1 - x2) ** 2 + (y1 + y2) ** 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
function circleCollide(x, y){
|
function circleCollide(x, y) {
|
||||||
let colliding = false;
|
let colliding = false;
|
||||||
objectsModule.objects.circles.forEach((circle)=>{
|
objectsModule.objects.circles.forEach((circle) => {
|
||||||
if(norm(x, circle.x, y, circle.y)<=circle.r){
|
if (norm(x, circle.x, y, circle.y) <= circle.r) {
|
||||||
colliding = true;
|
colliding = true;
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
return colliding;
|
return colliding;
|
||||||
}
|
}
|
||||||
|
|
||||||
function squareCollide(x, y){
|
function squareCollide(x, y) {
|
||||||
let colliding = false;
|
let colliding = false;
|
||||||
objectsModule.objects.squares.forEach((square)=>{
|
objectsModule.objects.squares.forEach((square) => {
|
||||||
if(square.x <= x && x<=square.x+square.w && square.y<=y && y<=square.y+square.h){
|
if (square.x <= x && x <= square.x + square.w && square.y <= y && y <= square.y + square.h) {
|
||||||
colliding = true;
|
colliding = true;
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
return colliding;
|
return colliding;
|
||||||
}
|
}
|
||||||
|
|
||||||
function generatePosition(){
|
function generatePosition() {
|
||||||
let mapWidth = 1000;
|
let mapWidth = 1000;
|
||||||
let mapHeight = 1000;
|
let mapHeight = 1000;
|
||||||
let count = 0;
|
let count = 0;
|
||||||
|
|
||||||
let x = Math.floor(Math.random()*mapWidth);
|
let x = Math.floor(Math.random() * mapWidth);
|
||||||
let y = Math.floor(Math.random()*mapHeight);
|
let y = Math.floor(Math.random() * mapHeight);
|
||||||
|
|
||||||
while((circleCollide(x,y) || squareCollide(x,y))){
|
while ((circleCollide(x, y) || squareCollide(x, y))) {
|
||||||
x = Math.floor(Math.random()*mapWidth);
|
x = Math.floor(Math.random() * mapWidth);
|
||||||
y = Math.floor(Math.random()*mapHeight);
|
y = Math.floor(Math.random() * mapHeight);
|
||||||
count++;
|
count++;
|
||||||
if(count >= 1000){
|
if (count >= 1000) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return [x, y];
|
return [x, y];
|
||||||
}
|
}
|
||||||
|
|
||||||
function createNewPlayer(socket, name){
|
function createNewPlayer(socket, name) {
|
||||||
let pos = generatePosition();
|
let pos = generatePosition();
|
||||||
var obj = {
|
var obj = {
|
||||||
id: playerCount,
|
id: playerCount,
|
||||||
|
@ -94,48 +96,53 @@ function createNewPlayer(socket, name){
|
||||||
wss.on('connection', (socket, req) => {
|
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
|
||||||
let username = url.parse(req.url, true).query.name;
|
let username = url.parse(req.url, true).query.name;
|
||||||
if(username===undefined || username == null){
|
if (username === undefined || username == null || username == "null") {
|
||||||
username = "Soldat Inconnu"
|
username = "Soldat Inconnu"
|
||||||
}
|
}
|
||||||
if(username.length > NAME_MAXLEN){
|
if (username.length > NAME_MAXLEN) {
|
||||||
username=username.substring(0, NAME_MAXLEN);
|
username = username.substring(0, NAME_MAXLEN);
|
||||||
}
|
}
|
||||||
createNewPlayer(socket, username);
|
createNewPlayer(socket, username);
|
||||||
|
|
||||||
socket.on('message', (message) => {
|
socket.on('message', (message) => {
|
||||||
message = JSON.parse(message);
|
message = JSON.parse(message);
|
||||||
if (message.type == "ping") {
|
switch (message.type) {
|
||||||
|
case 'ping':
|
||||||
socket.send("pong");
|
socket.send("pong");
|
||||||
|
break;
|
||||||
|
|
||||||
}else if(message.type=="update") {
|
case "update":
|
||||||
|
|
||||||
for (var i = players.length - 1; i >= 0; i--) {
|
for (var i = players.length - 1; i >= 0; i--) {
|
||||||
if(players[i].id == message.data.id){
|
if (players[i].id == message.data.id) {
|
||||||
players[i]=message.data;
|
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);
|
broadcast(JSON.stringify(message), socket.id);
|
||||||
|
break;
|
||||||
|
|
||||||
} else if(message.type=="died"){
|
case "died":
|
||||||
broadcast(JSON.stringify(message), socket.id);
|
broadcast(JSON.stringify(message), socket.id);
|
||||||
for (var i = players.length - 1; i >= 0; i--) {
|
for (var i = players.length - 1; i >= 0; i--) {
|
||||||
|
|
||||||
if(players[i].id == message.data.id){
|
if (players[i].id == message.data.id) {
|
||||||
let pos = generatePosition();
|
let pos = generatePosition();
|
||||||
players[i].x=pos[0];
|
players[i].x = pos[0];
|
||||||
players[i].y=pos[1];
|
players[i].y = pos[1];
|
||||||
|
|
||||||
broadcast(JSON.stringify({
|
broadcast(JSON.stringify({
|
||||||
type:"update",
|
type: "update",
|
||||||
data: players[i]
|
data: players[i]
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -143,9 +150,11 @@ wss.on('connection', (socket, req) => {
|
||||||
for (var i = players.length - 1; i >= 0; i--) {
|
for (var i = players.length - 1; i >= 0; i--) {
|
||||||
broadcast(JSON.stringify({
|
broadcast(JSON.stringify({
|
||||||
type: "removePlayer",
|
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);
|
players.splice(i, 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -155,7 +164,7 @@ wss.on('connection', (socket, req) => {
|
||||||
|
|
||||||
function broadcast(message, exceptId = -1) {
|
function broadcast(message, exceptId = -1) {
|
||||||
connections.forEach((socket) => {
|
connections.forEach((socket) => {
|
||||||
if (socket.readyState === WebSocket.OPEN && socket.id!=exceptId) {
|
if (socket.readyState === WebSocket.OPEN && socket.id != exceptId) {
|
||||||
socket.send(message);
|
socket.send(message);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in a new issue