diff --git a/.gitignore b/.gitignore index 6704566..2a99ec5 100644 --- a/.gitignore +++ b/.gitignore @@ -1,104 +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 +# 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 diff --git a/README.md b/README.md index b4e48d1..9477354 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# CupidServer -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 +# CupidServer +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 diff --git a/app.js b/app.js index 6e19a30..a50074a 100644 --- a/app.js +++ b/app.js @@ -1,148 +1,146 @@ -console.log("Starting Cupid v1") -console.log("") -const { response } = require('express'); -const { exec, execFile } = require('child_process'); -var spawn = require('child_process').spawn; -const express = require('express') -const app = express() -require('./settings')(); -const Helpers = require('./helpers'); -const { start } = require('repl'); - - -//Read settings -var settings = ReadSettings(); -if (settings == null) { return; } - -console.log(settings); - -var Rooms = []; - -// Rooms.push({Port:"2002",Players:["Player1","Player2"]}) -app.get('/settings',(req,res)=>{ - if(req.query.password != settings.password){ - res.send("403 Unauthorized") - return; - } - var m_settings = {minimum_players:settings.minimum_players, maximum_players:settings.maximum_players, waiting_time:settings.waiting_time} - res.send(m_settings); -}) -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 = []; - - //TODO: Make this loop go backwards and remove the extra loop below - 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); - } - } - } - //Purge expired rooms - 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; - } - } - - - for (var i = 0; i < Rooms.length; i++) { - 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; - } - } - } - - 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; - } 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); - //Spawn server instance - var arguments = ["-port", Rooms[selectedRoomId].Port]; - console.log(arguments); - // execFile(settings.game_exe,arguments, {maxBuffer:1024 * 1000}, (err, stdout, stderr) => { - // if (err) { - // // node couldn't execute the command - // console.log(err); - // return; - // } - // console.log("game started correctly?"); - // // the *entire* stdout and stderr (buffered) - // console.log(`stdout: ${stdout}`); - // console.log(`stderr: ${stderr}`); - // }); - var child = spawn(settings.game_exe, arguments); - - child.stdout.on('data', function (data) { - // console.log('stdout: ' + data); - }); - - child.stderr.on('data', function (data) { - console.log('stderr: ' + data); - }); - - child.on('close', function (code) { - console.log('child process exited with code ' + code); - }); - } else { - res.send("0," + Rooms[selectedRoomId].Port); - } - } - console.log(Rooms); - -}) - +console.log("Starting Cupid v1") +console.log("") +const { response } = require('express'); +const { exec, execFile } = require('child_process'); +var spawn = require('child_process').spawn; +const express = require('express') +const app = express() +require('./settings')(); +const Helpers = require('./helpers'); +const { start } = require('repl'); + + +//Read settings +var settings = ReadSettings(); +if (settings == null) { return; } + +console.log(settings); + +var Rooms = []; + +// Rooms.push({Port:"2002",Players:["Player1","Player2"]}) +app.get('/settings',(req,res)=>{ + if(req.query.password != settings.password){ + res.send("403 Unauthorized") + return; + } + var m_settings = {minimum_players:settings.minimum_players, maximum_players:settings.maximum_players, waiting_time:settings.waiting_time} + res.send(m_settings); +}) +app.get('/', (req, res) => { + //Validate request + 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; + } + //Phase 1 : Select expired rooms + var myRoomId = -1; + var possibleRoomId = -1; + var expiredRooms = []; + for (var i = 0; i < Rooms.length; i++) { + + if(true){ + // if(Rooms[i].Players.length >= settings.minimum_players){ + var expireDate = Rooms[i].Time + settings.waiting_time; + var timeToLive = expireDate - Date.now(); + console.log(timeToLive); + 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; + } + } + } + //Purge expired rooms + 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; + } else { + // var newUser = {name:username, time:Date.now()}; + 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); + //Spawn server instance + var arguments = ["-port", Rooms[selectedRoomId].Port]; + console.log(arguments); + // execFile(settings.game_exe,arguments, {maxBuffer:1024 * 1000}, (err, stdout, stderr) => { + // if (err) { + // // node couldn't execute the command + // console.log(err); + // return; + // } + // console.log("game started correctly?"); + // // the *entire* stdout and stderr (buffered) + // console.log(`stdout: ${stdout}`); + // console.log(`stderr: ${stderr}`); + // }); + var child = spawn(settings.game_exe, arguments); + + child.stdout.on('data', function (data) { + // console.log('stdout: ' + data); + }); + + child.stderr.on('data', function (data) { + console.log('stderr: ' + data); + }); + + child.on('close', function (code) { + console.log('child process exited with code ' + code); + }); + } else { + res.send("0," + Rooms[selectedRoomId].Port); + } + } + console.log(Date.now()); + console.log(Rooms); + +}) + app.listen(settings.port); \ No newline at end of file diff --git a/helpers.js b/helpers.js index 7c63598..a2bc15c 100644 --- a/helpers.js +++ b/helpers.js @@ -1,10 +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) +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) } \ No newline at end of file diff --git a/settings.js b/settings.js index 1abbed5..276f201 100644 --- a/settings.js +++ b/settings.js @@ -1,18 +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; - } +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; + } } \ No newline at end of file diff --git a/settings.json b/settings.json index 451d735..09efb29 100644 --- a/settings.json +++ b/settings.json @@ -1,10 +1,10 @@ -{ - "port":1601, - "password":"xyz@123", - "game_exe":"/root/Unity/CupidSample/Builds/Linux/Cupid.x86_64", - "minimum_players": 2, - "maximum_players": 5, - "waiting_time": 60000, - "port_range_min":2000, - "port_range_max":3000 -} +{ + "port":1601, + "password":"xyz@123", + "game_exe":"/root/Unity/CupidSample/Builds/Linux/Cupid.x86_64", + "minimum_players": 2, + "maximum_players": 5, + "waiting_time": 2000, + "port_range_min":2000, + "port_range_max":3000 +}