This commit is contained in:
2022-11-09 16:28:24 +05:30
commit 3eb1ef47a7
7 changed files with 1292 additions and 0 deletions
+104
View File
@@ -0,0 +1,104 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
# Runtime data
pids
*.pid
*.seed
*.pid.lock
# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov
# Coverage directory used by tools like istanbul
coverage
*.lcov
# nyc test coverage
.nyc_output
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt
# Bower dependency directory (https://bower.io/)
bower_components
# node-waf configuration
.lock-wscript
# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release
# Dependency directories
node_modules/
jspm_packages/
# TypeScript v1 declaration files
typings/
# TypeScript cache
*.tsbuildinfo
# Optional npm cache directory
.npm
# Optional eslint cache
.eslintcache
# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/
# Optional REPL history
.node_repl_history
# Output of 'npm pack'
*.tgz
# Yarn Integrity file
.yarn-integrity
# dotenv environment variables file
.env
.env.test
# parcel-bundler cache (https://parceljs.org/)
.cache
# Next.js build output
.next
# Nuxt.js build / generate output
.nuxt
dist
# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and *not* Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public
# vuepress build output
.vuepress/dist
# Serverless directories
.serverless/
# FuseBox cache
.fusebox/
# DynamoDB Local files
.dynamodb/
# TernJS port file
.tern-port
+118
View File
@@ -0,0 +1,118 @@
console.log("Starting Cupid v1")
console.log("")
const { response } = require('express');
const { exec } = require('child_process');
const express = require('express')
const app = express()
require('./settings')();
const Helpers = require('./helpers');
//Read settings
var settings = ReadSettings();
if (settings == null) { return; }
console.log(settings);
var Rooms = [];
// Rooms.push({Port:"2002",Players:["Player1","Player2"]})
app.get('/', (req, res) => {
var username = req.query.username ?? "";
var password = req.query.password ?? "";
if (password != settings.password) {
res.send("403 Unauthorized");
console.log("Unauthorized call " + password + ":" + settings.password);
return;
}
if (username.length < 2) {
res.send("Bad credentials");
return;
}
// res.send('1')
var myRoomId = -1;
var possibleRoomId = -1;
var expiredRooms = [];
for (var i = 0; i < Rooms.length; i++) {
if(Rooms[i].Players.length >= settings.minimum_players){
var expireDate = Rooms[i].Time + settings.waiting_time;
var timeToLive = expireDate - Date.now();
if(timeToLive <0){
//expired
console.log(Rooms[i]);
console.log(" is expired.");
expiredRooms.push(i);
}else{
if(timeToLive < 4){
console.log(Rooms[i].Port + " Port room will be expired in " + timeToLive);
}
// console.log("Room " + i + " will be expired in " + expireDate + "-" + Date.now() + "=" + timeToLive);
}
}
if(myRoomId > -1){continue;}
if (Rooms[i].Players.indexOf(username) > -1) {
myRoomId = i;
} else {
if (Rooms[i].Players.length < settings.maximum_players) {
console.log("found possible room " + i);
possibleRoomId = i;
}
}
}
for(var i=expiredRooms.length-1; i>=0; i--){
Rooms.pop(expiredRooms[i]);
if(possibleRoomId == expiredRooms[i]){
possibleRoomId =-1;
}
if(myRoomId == expiredRooms[i]){
myRoomId =-1;
}
}
if (myRoomId == -1) {
if (possibleRoomId == -1) {
Rooms.push({ Port: Helpers.GetRandomPort(settings.port_range_min, settings.port_range_max), Players: [username], Time: Date.now() })
myRoomId= Rooms.length-1;
//Spawn server instance
exec(settings.game_exe, (err, stdout, stderr) => {
if (err) {
// node couldn't execute the command
return;
}
// the *entire* stdout and stderr (buffered)
console.log(`stdout: ${stdout}`);
console.log(`stderr: ${stderr}`);
});
} else {
Rooms[possibleRoomId].Players.push(username);
}
}
var selectedRoomId = -1;
if (myRoomId > -1) {
//M
selectedRoomId = myRoomId;
} else if (possibleRoomId > -1) {
selectedRoomId = possibleRoomId;
} else {
res.send("-1, Error!");
}
if (selectedRoomId > -1) {
if (Rooms[selectedRoomId].Players.length >= settings.minimum_players) {
res.send("1," + Rooms[selectedRoomId].Port);
} else {
res.send("0," + Rooms[selectedRoomId].Port);
}
}
console.log(Rooms);
})
app.listen(settings.port);
+10
View File
@@ -0,0 +1,10 @@
exports.GetRandomPort =function(min,max){
var port = randomIntFromInterval(min,max);
// console.log("min:"+min+", max:"+max+" = "+port);
return port;
};
function randomIntFromInterval(min, max) { // min and max included
return Math.floor(Math.random() * (max - min + 1) + min)
}
+1018
View File
File diff suppressed because it is too large Load Diff
+14
View File
@@ -0,0 +1,14 @@
{
"name": "cupidserver",
"version": "1.0.0",
"description": "Matchmaking daemon for Cupid mirror",
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.18.2"
}
}
+18
View File
@@ -0,0 +1,18 @@
module.exports = function(){
this.ReadSettings = Read;
}
function Read(){
try{
console.log("Reading settings.json")
var fs = require("fs");
var settings_txt = fs.readFileSync("settings.json");
var settings = JSON.parse(settings_txt);
console.log("Done")
return settings;
}catch(err){
console.log("Error: couldn't read settings.json. make sure its there and valid");
return null;
}
}
+10
View File
@@ -0,0 +1,10 @@
{
"port":1601,
"password":"xyz@123",
"game_exe":"/home/game/game.x86_64",
"minimum_players": 2,
"maximum_players": 5,
"waiting_time": 60000,
"port_range_min":2000,
"port_range_max":3000
}