diff --git a/client.js b/client.js index e829a8b7..255fa4e3 100644 --- a/client.js +++ b/client.js @@ -25,6 +25,7 @@ if (!fs.existsSync("./sharedMods/")) { const instance = process.argv[3]; const instancedirectory = './instances/' + instance; const command = process.argv[2]; +var instanceInfo = {}; // Functions function deleteFolderRecursive(path) { @@ -58,70 +59,6 @@ function messageInterface(command, callback) { } } -// load plugins and execute onLoad event -let pluginDirectories = getDirectories("./sharedPlugins/"); -let plugins = []; -for(i=0; i { - // log("Stdout: " + data); - messageInterface(data.toString('utf8')); - }); - plugins[i].stderr.on("data", (data) => { - log("STDERR: " + data); - }) - plugins[i].on('close', (code) => { - log(`child process exited with code ${code}`); - }); -} - - // handle commandline parameters if (!command || command == "help" || command == "--help") { console.error("Usage: ") @@ -315,30 +252,116 @@ write-data=__PATH__executable__/../../../instances/" + instance + "\r\n\ } function instanceManagement() { + // load plugins and execute onLoad event + let pluginDirectories = getDirectories("./sharedPlugins/"); + let plugins = []; + for(i=0; i { + // log("Stdout: " + data); + messageInterface(data.toString('utf8')); + }); + plugins[i].stderr.on("data", (data) => { + log("STDERR: " + data); + }) + plugins[i].on('close', (code) => { + log(`child process exited with code ${code}`); + }); + } + // world IDs ------------------------------------------------------------------ hashMods(instance, function(modHashes){ setInterval(getID, 10000); getID() function getID() { - var payload = { - time: Date.now(), - rconPort: instanceconfig.clientPort, - rconPassword: instanceconfig.clientPassword, - serverPort: instanceconfig.factorioPort, - unique: instanceconfig.clientPassword.hashCode(), - publicIP: config.publicIP, // IP of the server should be global for all instances, so we pull that straight from the config - mods:modHashes - } - require('getmac').getMac(function (err, mac) { - if (err) throw err - payload.mac = mac - console.log(payload) - needle.post(config.masterIP + ":" + config.masterPort + '/getID', payload, function (err, response, body) { - if (response && response.body) { - // In the future we might be interested in whether or not we actually manage to send it, but honestly I don't care. - console.log(response.body) + messageInterface("/silent-command game.write_file('tempfile.txt', 'connected_players ' .. #game.connected_players .. '\\n', true, 0)", function(err) {setTimeout(function(){ + // get array of lines in file + if(fs.existsSync(instancedirectory + "/script-output/tempfile.txt")) { + var data = fs.readFileSync(instancedirectory + "/script-output/tempfile.txt", "utf8").split("\n"); + // delete when we are done + fs.unlink(instancedirectory + "/script-output/tempfile.txt"); + } + // if we actually got anything from the file, proceed to categorize it + if (data && data[0]) { + while (data[0]) { + let q = data[0].split(" "); + // delete array element + data.splice(0,1) + if(q[0] == "connected_players" && Number(q[1]) != NaN) { + instanceInfo.playerCount = q[1]; + } } - }); + } else { + instanceInfo.playerCount = 0; + } + var payload = { + time: Date.now(), + rconPort: instanceconfig.clientPort, + rconPassword: instanceconfig.clientPassword, + serverPort: instanceconfig.factorioPort, + unique: instanceconfig.clientPassword.hashCode(), + publicIP: config.publicIP, // IP of the server should be global for all instances, so we pull that straight from the config + mods:modHashes, + playerCount: instanceInfo.playerCount || 0, + } + require('getmac').getMac(function (err, mac) { + if (err) throw err + payload.mac = mac + console.log(payload) + needle.post(config.masterIP + ":" + config.masterPort + '/getID', payload, function (err, response, body) { + if (response && response.body) { + // In the future we might be interested in whether or not we actually manage to send it, but honestly I don't care. + console.log(response.body) + } + }); + }) + },1000) }) } }) @@ -498,7 +521,7 @@ function instanceManagement() { } // console.log(frameset); // Send all our compressed frames - client.exec("/silent-command remote.call('clusterio', 'receiveMany', '" + JSON.stringify(frameset) + "')"); + messageInterface("/silent-command remote.call('clusterio', 'receiveMany', '" + JSON.stringify(frameset) + "')"); } }); // after fetching all the latest frames, we take a timestamp. During the next iteration, we fetch all frames submitted after this.