GrandTabernacleAutoVI/server/server.js

182 lines
3.9 KiB
JavaScript
Raw Normal View History

2023-12-08 13:53:44 +01:00
const https = require('https').createServer;
2023-11-14 17:35:47 +01:00
const WebSocket = require('ws');
2023-12-06 08:52:39 +01:00
const url = require('url');
2023-12-08 13:53:44 +01:00
const fs = require('fs');
2023-12-08 17:35:49 +01:00
const objectsModule = require('./objects');
2023-11-14 17:35:47 +01:00
2023-12-08 14:17:32 +01:00
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")
2023-12-08 13:53:44 +01:00
});
2023-12-08 17:35:49 +01:00
2023-12-07 11:20:48 +01:00
const wss = new WebSocket.Server({
server
});
2023-11-14 17:35:47 +01:00
2023-12-08 17:35:49 +01:00
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}]
2023-11-14 17:45:09 +01:00
const connections = new Set();
2023-11-14 17:35:47 +01:00
var playerCount = 0;
2023-11-18 13:38:29 +01:00
var players = []
2023-11-14 17:35:47 +01:00
2023-12-06 09:52:34 +01:00
const NAME_MAXLEN = 25;
2023-12-06 08:52:39 +01:00
2023-12-08 17:35:49 +01:00
function generatePosition() {
let index = Math.floor(Math.random()*spawnPoints.length);
return [spawnPoints[index].x, spawnPoints[index].y];
2023-12-06 08:52:39 +01:00
}
2023-12-08 17:35:49 +01:00
function getUsername(req){
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);
2023-12-06 08:52:39 +01:00
}
2023-12-08 17:35:49 +01:00
return username;
2023-12-06 08:52:39 +01:00
}
2023-11-19 18:23:15 +01:00
2023-12-07 11:20:48 +01:00
function createNewPlayer(socket, name) {
2023-12-06 09:16:05 +01:00
let pos = generatePosition();
2023-11-18 20:17:47 +01:00
var obj = {
id: playerCount,
2023-12-06 09:17:24 +01:00
x: pos[0],
y: pos[1],
2023-11-19 18:23:15 +01:00
dir: 0,
visibleDir: 0,
2023-12-06 08:52:39 +01:00
name: name
2023-11-18 20:17:47 +01:00
}
2023-11-19 18:23:15 +01:00
players.push(obj);
playerCount++;
2023-11-18 20:17:47 +01:00
socket.send(JSON.stringify({
type: "connect",
data: {
playerId: obj.id,
players: players
}
}))
broadcast(JSON.stringify({
type: "newplayer",
data: obj
}), obj.id);
socket.id = obj.id;
2023-11-14 17:45:09 +01:00
connections.add(socket);
2023-11-18 20:17:47 +01:00
}
2023-12-08 17:35:49 +01:00
function update(message, socket){
for (var i = players.length - 1; i >= 0; i--) {
if (players[i].id == message.data.id) {
players[i] = message.data;
}
2023-12-06 09:16:05 +01:00
}
2023-12-08 17:35:49 +01:00
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];
broadcast(JSON.stringify({
type: "update",
data: players[i]
}))
}
}
}
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);
}
2023-12-06 09:46:36 +01:00
}
2023-12-08 17:35:49 +01:00
connections.delete(socket);
}
function broadcast(message, exceptId = -1) {
connections.forEach((socket) => {
if (socket.readyState === WebSocket.OPEN && socket.id != exceptId) {
socket.send(message);
}
});
}
wss.on('connection', (socket, req) => {
//create new player, send informations to new player and broadcast new player for all
let username = getUsername(req);
2023-12-06 08:52:39 +01:00
createNewPlayer(socket, username);
2023-11-14 17:45:09 +01:00
2023-12-08 17:35:49 +01:00
//handle client's messages
2023-11-14 17:45:09 +01:00
socket.on('message', (message) => {
message = JSON.parse(message);
2023-12-07 11:20:48 +01:00
switch (message.type) {
case 'ping':
socket.send("pong");
break;
case "update":
2023-12-08 17:42:21 +01:00
update(message, socket);
2023-12-07 11:20:48 +01:00
break;
case "newBullet":
2023-12-08 17:35:49 +01:00
newBullet(message, socket);
2023-12-07 11:20:48 +01:00
break;
case "died":
2023-12-08 17:35:49 +01:00
died(message, socket);
2023-12-07 11:20:48 +01:00
break;
default:
break;
2023-11-14 17:45:09 +01:00
}
});
2023-12-08 17:35:49 +01:00
//handle disconnecting
2023-11-14 17:35:47 +01:00
socket.on('close', () => {
2023-12-08 17:43:38 +01:00
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);
2023-11-14 17:35:47 +01:00
});
2023-11-20 11:01:11 +01:00
const PORT = 8080;
2023-11-14 17:45:09 +01:00
server.listen(PORT, () => {
2023-12-08 17:35:49 +01:00
console.log(`WebSocket listening on port ${PORT}.`);
2023-12-07 11:20:48 +01:00
});