Gotta change system

This commit is contained in:
root
2022-11-16 11:19:39 +08:00
parent 2a5761a7a4
commit 03c5d79c5e
6 changed files with 289 additions and 291 deletions
+104 -104
View File
@@ -1,104 +1,104 @@
# Logs # Logs
logs logs
*.log *.log
npm-debug.log* npm-debug.log*
yarn-debug.log* yarn-debug.log*
yarn-error.log* yarn-error.log*
lerna-debug.log* lerna-debug.log*
# Diagnostic reports (https://nodejs.org/api/report.html) # Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
# Runtime data # Runtime data
pids pids
*.pid *.pid
*.seed *.seed
*.pid.lock *.pid.lock
# Directory for instrumented libs generated by jscoverage/JSCover # Directory for instrumented libs generated by jscoverage/JSCover
lib-cov lib-cov
# Coverage directory used by tools like istanbul # Coverage directory used by tools like istanbul
coverage coverage
*.lcov *.lcov
# nyc test coverage # nyc test coverage
.nyc_output .nyc_output
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) # Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt .grunt
# Bower dependency directory (https://bower.io/) # Bower dependency directory (https://bower.io/)
bower_components bower_components
# node-waf configuration # node-waf configuration
.lock-wscript .lock-wscript
# Compiled binary addons (https://nodejs.org/api/addons.html) # Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release build/Release
# Dependency directories # Dependency directories
node_modules/ node_modules/
jspm_packages/ jspm_packages/
# TypeScript v1 declaration files # TypeScript v1 declaration files
typings/ typings/
# TypeScript cache # TypeScript cache
*.tsbuildinfo *.tsbuildinfo
# Optional npm cache directory # Optional npm cache directory
.npm .npm
# Optional eslint cache # Optional eslint cache
.eslintcache .eslintcache
# Microbundle cache # Microbundle cache
.rpt2_cache/ .rpt2_cache/
.rts2_cache_cjs/ .rts2_cache_cjs/
.rts2_cache_es/ .rts2_cache_es/
.rts2_cache_umd/ .rts2_cache_umd/
# Optional REPL history # Optional REPL history
.node_repl_history .node_repl_history
# Output of 'npm pack' # Output of 'npm pack'
*.tgz *.tgz
# Yarn Integrity file # Yarn Integrity file
.yarn-integrity .yarn-integrity
# dotenv environment variables file # dotenv environment variables file
.env .env
.env.test .env.test
# parcel-bundler cache (https://parceljs.org/) # parcel-bundler cache (https://parceljs.org/)
.cache .cache
# Next.js build output # Next.js build output
.next .next
# Nuxt.js build / generate output # Nuxt.js build / generate output
.nuxt .nuxt
dist dist
# Gatsby files # Gatsby files
.cache/ .cache/
# Comment in the public line in if your project uses Gatsby and *not* Next.js # 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 # https://nextjs.org/blog/next-9-1#public-directory-support
# public # public
# vuepress build output # vuepress build output
.vuepress/dist .vuepress/dist
# Serverless directories # Serverless directories
.serverless/ .serverless/
# FuseBox cache # FuseBox cache
.fusebox/ .fusebox/
# DynamoDB Local files # DynamoDB Local files
.dynamodb/ .dynamodb/
# TernJS port file # TernJS port file
.tern-port .tern-port
+4 -4
View File
@@ -1,4 +1,4 @@
# CupidServer # CupidServer
This is the Server daemon for Cupid Matchmaking system for Mirror unity. This is the Server daemon for Cupid Matchmaking system for Mirror unity.
You have to run this program on the same server where you'll be hosting your game You have to run this program on the same server where you'll be hosting your game
+145 -147
View File
@@ -1,148 +1,146 @@
console.log("Starting Cupid v1") console.log("Starting Cupid v1")
console.log("") console.log("")
const { response } = require('express'); const { response } = require('express');
const { exec, execFile } = require('child_process'); const { exec, execFile } = require('child_process');
var spawn = require('child_process').spawn; var spawn = require('child_process').spawn;
const express = require('express') const express = require('express')
const app = express() const app = express()
require('./settings')(); require('./settings')();
const Helpers = require('./helpers'); const Helpers = require('./helpers');
const { start } = require('repl'); const { start } = require('repl');
//Read settings //Read settings
var settings = ReadSettings(); var settings = ReadSettings();
if (settings == null) { return; } if (settings == null) { return; }
console.log(settings); console.log(settings);
var Rooms = []; var Rooms = [];
// Rooms.push({Port:"2002",Players:["Player1","Player2"]}) // Rooms.push({Port:"2002",Players:["Player1","Player2"]})
app.get('/settings',(req,res)=>{ app.get('/settings',(req,res)=>{
if(req.query.password != settings.password){ if(req.query.password != settings.password){
res.send("403 Unauthorized") res.send("403 Unauthorized")
return; return;
} }
var m_settings = {minimum_players:settings.minimum_players, maximum_players:settings.maximum_players, waiting_time:settings.waiting_time} var m_settings = {minimum_players:settings.minimum_players, maximum_players:settings.maximum_players, waiting_time:settings.waiting_time}
res.send(m_settings); res.send(m_settings);
}) })
app.get('/', (req, res) => { app.get('/', (req, res) => {
//Validate request
var username = req.query.username ?? ""; var username = req.query.username ?? "";
var password = req.query.password ?? ""; var password = req.query.password ?? "";
if (password != settings.password) { if (password != settings.password) {
res.send("403 Unauthorized"); res.send("403 Unauthorized");
console.log("Unauthorized call " + password + ":" + settings.password); console.log("Unauthorized call " + password + ":" + settings.password);
return; return;
} }
if (username.length < 2) { if (username.length < 2) {
res.send("Bad credentials"); res.send("Bad credentials");
return; return;
} }
// res.send('1') //Phase 1 : Select expired rooms
var myRoomId = -1; var myRoomId = -1;
var possibleRoomId = -1; var possibleRoomId = -1;
var expiredRooms = []; var expiredRooms = [];
for (var i = 0; i < Rooms.length; i++) {
//TODO: Make this loop go backwards and remove the extra loop below
for (var i = 0; i < Rooms.length; i++) { if(true){
// if(Rooms[i].Players.length >= settings.minimum_players){
if(Rooms[i].Players.length >= settings.minimum_players){ var expireDate = Rooms[i].Time + settings.waiting_time;
var expireDate = Rooms[i].Time + settings.waiting_time; var timeToLive = expireDate - Date.now();
var timeToLive = expireDate - Date.now(); console.log(timeToLive);
if(timeToLive <0){
if(timeToLive <0){ //expired
//expired console.log(Rooms[i]);
console.log(Rooms[i]); console.log(" is expired.");
console.log(" is expired."); expiredRooms.push(i);
expiredRooms.push(i); }else{
}else{ if(timeToLive < 4){
if(timeToLive < 4){ console.log(Rooms[i].Port + " Port room will be expired in " + timeToLive);
console.log(Rooms[i].Port + " Port room will be expired in " + timeToLive); }
} // console.log("Room " + i + " will be expired in " + expireDate + "-" + Date.now() + "=" + timeToLive);
// console.log("Room " + i + " will be expired in " + expireDate + "-" + Date.now() + "=" + timeToLive); }
} }
}
} if(myRoomId > -1){continue;}
//Purge expired rooms if (Rooms[i].Players.indexOf(username) > -1) {
for(var i=expiredRooms.length-1; i>=0; i--){ myRoomId = i;
Rooms.pop(expiredRooms[i]); } else {
if(possibleRoomId == expiredRooms[i]){ if (Rooms[i].Players.length < settings.maximum_players) {
possibleRoomId =-1; console.log("found possible room " + i);
} possibleRoomId = i;
if(myRoomId == expiredRooms[i]){ }
myRoomId =-1; }
} }
} //Purge expired rooms
for(var i=expiredRooms.length-1; i>=0; i--){
Rooms.pop(expiredRooms[i]);
for (var i = 0; i < Rooms.length; i++) { if(possibleRoomId == expiredRooms[i]){
if(myRoomId > -1){continue;} possibleRoomId =-1;
if (Rooms[i].Players.indexOf(username) > -1) { }
myRoomId = i; if(myRoomId == expiredRooms[i]){
} else { myRoomId =-1;
if (Rooms[i].Players.length < settings.maximum_players) { }
console.log("found possible room " + i); }
possibleRoomId = i;
} 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;
if (myRoomId == -1) { } else {
if (possibleRoomId == -1) { // var newUser = {name:username, time:Date.now()};
Rooms.push({ Port: Helpers.GetRandomPort(settings.port_range_min, settings.port_range_max), Players: [username], Time: Date.now() }) Rooms[possibleRoomId].Players.push(username);
myRoomId= Rooms.length-1; }
} else { }
Rooms[possibleRoomId].Players.push(username); var selectedRoomId = -1;
} if (myRoomId > -1) {
} //M
var selectedRoomId = -1; selectedRoomId = myRoomId;
if (myRoomId > -1) { } else if (possibleRoomId > -1) {
//M selectedRoomId = possibleRoomId;
selectedRoomId = myRoomId; } else {
} else if (possibleRoomId > -1) { res.send("-1, Error!");
selectedRoomId = possibleRoomId; }
} else {
res.send("-1, Error!"); if (selectedRoomId > -1) {
} if (Rooms[selectedRoomId].Players.length >= settings.minimum_players) {
res.send("1," + Rooms[selectedRoomId].Port);
if (selectedRoomId > -1) { //Spawn server instance
if (Rooms[selectedRoomId].Players.length >= settings.minimum_players) { var arguments = ["-port", Rooms[selectedRoomId].Port];
res.send("1," + Rooms[selectedRoomId].Port); console.log(arguments);
//Spawn server instance // execFile(settings.game_exe,arguments, {maxBuffer:1024 * 1000}, (err, stdout, stderr) => {
var arguments = ["-port", Rooms[selectedRoomId].Port]; // if (err) {
console.log(arguments); // // node couldn't execute the command
// execFile(settings.game_exe,arguments, {maxBuffer:1024 * 1000}, (err, stdout, stderr) => { // console.log(err);
// if (err) { // return;
// // node couldn't execute the command // }
// console.log(err); // console.log("game started correctly?");
// return; // // the *entire* stdout and stderr (buffered)
// } // console.log(`stdout: ${stdout}`);
// console.log("game started correctly?"); // console.log(`stderr: ${stderr}`);
// // the *entire* stdout and stderr (buffered) // });
// console.log(`stdout: ${stdout}`); var child = spawn(settings.game_exe, arguments);
// console.log(`stderr: ${stderr}`);
// }); child.stdout.on('data', function (data) {
var child = spawn(settings.game_exe, arguments); // console.log('stdout: ' + data);
});
child.stdout.on('data', function (data) {
// console.log('stdout: ' + data); child.stderr.on('data', function (data) {
}); console.log('stderr: ' + data);
});
child.stderr.on('data', function (data) {
console.log('stderr: ' + data); child.on('close', function (code) {
}); console.log('child process exited with code ' + code);
});
child.on('close', function (code) { } else {
console.log('child process exited with code ' + code); res.send("0," + Rooms[selectedRoomId].Port);
}); }
} else { }
res.send("0," + Rooms[selectedRoomId].Port); console.log(Date.now());
} console.log(Rooms);
}
console.log(Rooms); })
})
app.listen(settings.port); app.listen(settings.port);
+9 -9
View File
@@ -1,10 +1,10 @@
exports.GetRandomPort =function(min,max){ exports.GetRandomPort =function(min,max){
var port = randomIntFromInterval(min,max); var port = randomIntFromInterval(min,max);
// console.log("min:"+min+", max:"+max+" = "+port); // console.log("min:"+min+", max:"+max+" = "+port);
return port; return port;
}; };
function randomIntFromInterval(min, max) { // min and max included function randomIntFromInterval(min, max) { // min and max included
return Math.floor(Math.random() * (max - min + 1) + min) return Math.floor(Math.random() * (max - min + 1) + min)
} }
+17 -17
View File
@@ -1,18 +1,18 @@
module.exports = function(){ module.exports = function(){
this.ReadSettings = Read; this.ReadSettings = Read;
} }
function Read(){ function Read(){
try{ try{
console.log("Reading settings.json") console.log("Reading settings.json")
var fs = require("fs"); var fs = require("fs");
var settings_txt = fs.readFileSync("settings.json"); var settings_txt = fs.readFileSync("settings.json");
var settings = JSON.parse(settings_txt); var settings = JSON.parse(settings_txt);
console.log("Done") console.log("Done")
return settings; return settings;
}catch(err){ }catch(err){
console.log("Error: couldn't read settings.json. make sure its there and valid"); console.log("Error: couldn't read settings.json. make sure its there and valid");
return null; return null;
} }
} }
+10 -10
View File
@@ -1,10 +1,10 @@
{ {
"port":1601, "port":1601,
"password":"xyz@123", "password":"xyz@123",
"game_exe":"/root/Unity/CupidSample/Builds/Linux/Cupid.x86_64", "game_exe":"/root/Unity/CupidSample/Builds/Linux/Cupid.x86_64",
"minimum_players": 2, "minimum_players": 2,
"maximum_players": 5, "maximum_players": 5,
"waiting_time": 60000, "waiting_time": 2000,
"port_range_min":2000, "port_range_min":2000,
"port_range_max":3000 "port_range_max":3000
} }