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-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-12-26 18:09:04 +01:00
|
|
|
let playerCount = 0;
|
|
|
|
let players = []
|
|
|
|
const MAX_HEALTH = 10;
|
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-26 17:51:50 +01:00
|
|
|
function kick_player(player_id, socket, reason){
|
|
|
|
//send cheating info to all players
|
|
|
|
broadcast(JSON.stringify({
|
|
|
|
type: "kick",
|
|
|
|
data: {
|
|
|
|
player_id: player_id,
|
|
|
|
reason: reason
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
deletePlayer(socket); //delete player of players list
|
|
|
|
socket.terminate(); //disconnect cheating player
|
|
|
|
}
|
|
|
|
|
2023-12-08 17:35:49 +01:00
|
|
|
function update(message, socket){
|
|
|
|
for (var i = players.length - 1; i >= 0; i--) {
|
2023-12-26 17:51:50 +01:00
|
|
|
if(message.data.health <= MAX_HEALTH) {
|
|
|
|
if (players[i].id == message.data.id) {
|
|
|
|
players[i] = message.data;
|
|
|
|
}
|
|
|
|
}else{
|
|
|
|
//cheat detected
|
|
|
|
kick_player(message.data.id, socket, "health cheat");
|
2023-12-08 17:35:49 +01:00
|
|
|
}
|
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);
|
|
|
|
}
|
|
|
|
|
2023-12-11 15:21:12 +01:00
|
|
|
function recievemessage(msg, socket){
|
2023-12-11 15:19:06 +01:00
|
|
|
broadcast(JSON.stringify(msg), socket.id);
|
2023-12-11 15:10:17 +01:00
|
|
|
}
|
2023-11-14 17:35:47 +01:00
|
|
|
|
2023-12-08 17:35:49 +01:00
|
|
|
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) => {
|
2023-12-08 19:16:43 +01:00
|
|
|
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;
|
|
|
|
|
2023-12-11 15:10:17 +01:00
|
|
|
case "message":
|
2023-12-11 15:21:12 +01:00
|
|
|
recievemessage(message, socket);
|
2023-12-11 15:10:17 +01:00
|
|
|
break;
|
|
|
|
|
2023-12-08 19:16:43 +01:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2023-12-08 19:18:36 +01:00
|
|
|
}catch(e){
|
|
|
|
console.log('error');
|
2023-11-14 17:45:09 +01:00
|
|
|
}
|
2023-12-08 19:16:43 +01:00
|
|
|
|
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:46:12 +01:00
|
|
|
deletePlayer(socket);
|
2023-12-08 17:45:22 +01:00
|
|
|
});
|
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-18 13:33:08 +01:00
|
|
|
});
|