From 85d7246bcada053db8572b5dab3a959ca8e68951 Mon Sep 17 00:00:00 2001 From: Danielv123 Date: Thu, 18 May 2017 21:13:13 +0200 Subject: [PATCH 01/29] Add semicolons because they should be there --- client.js | 126 +++++++++++++++++++++++++++--------------------------- 1 file changed, 63 insertions(+), 63 deletions(-) diff --git a/client.js b/client.js index 1ebe80fd..8645fb03 100644 --- a/client.js +++ b/client.js @@ -86,27 +86,27 @@ function messageInterface(command, callback) { // handle commandline parameters if (!command || command == "help" || command == "--help") { - console.error("Usage: ") - console.error("node client.js start [instance name]") - console.error("node client.js list") - console.error("node client.js delete [instance]") - console.error("To download the latest version of the Clusterio lua mod, do") - console.error("node client.js download") - process.exit(1) + console.error("Usage: "); + console.error("node client.js start [instance name]"); + console.error("node client.js list"); + console.error("node client.js delete [instance]"); + console.error("To download the latest version of the Clusterio lua mod, do"); + console.error("node client.js download"); + process.exit(1); } else if (command == "list") { console.dir(getDirectories("./instances/")); - process.exit(1) + process.exit(1); } else if (command == "delete") { if (!process.argv[3]) { console.error("Usage: node client.js delete [instance]"); - process.exit(1) + process.exit(1); } else if (typeof process.argv[3] == "string" && fs.existsSync("./instances/" + process.argv[3]) && process.argv[3] != "/" && process.argv[3] != "") { deleteFolderRecursive("./instances/" + process.argv[3]); - console.log("Deleted instance " + process.argv[3]) - process.exit(1) + console.log("Deleted instance " + process.argv[3]); + process.exit(1); } else { console.error("Instance not found: " + process.argv[3]); - process.exit(1) + process.exit(1); } } else if (command == "download") { console.log("Downloading mods..."); @@ -119,7 +119,7 @@ if (!command || command == "help" || command == "--help") { var file = fs.createWriteStream("sharedMods/"+name); var request = https.get(url, function(response) { response.pipe(file); - console.log("Downloaded "+name) + console.log("Downloaded "+name); }); } } else if (command == "start" && typeof instance == "string" && instance != "/" && !fs.existsSync(instancedirectory)) { @@ -138,14 +138,14 @@ read-data=__PATH__executable__/../../data\r\n\ write-data=__PATH__executable__/../../../instances/" + instance + "\r\n\ "); - fs.copySync('sharedMods', instancedirectory + "/mods") + fs.copySync('sharedMods', instancedirectory + "/mods"); let instconf = { "factorioPort": process.env.FACTORIOPORT | Math.floor(Math.random() * 65535), "clientPort": process.env.RCONPORT | Math.floor(Math.random() * 65535), "clientPassword": Math.random().toString(36).replace(/[^a-z]+/g, '').substr(0, 8), } - console.log("Clusterio | Moving shared mods...") - console.log(instconf) + console.log("Clusterio | Moving shared mods..."); + console.log(instconf); // create instance config fs.writeFileSync(instancedirectory + "/config.json", JSON.stringify(instconf, null, 4)); @@ -178,10 +178,10 @@ write-data=__PATH__executable__/../../../instances/" + instance + "\r\n\ '-c', instancedirectory + '/config.ini', '--create', instancedirectory + '/saves/save.zip', ] - ) - console.log("Instance created!") + ); + console.log("Instance created!"); } else if (command == "start" && typeof instance == "string" && instance != "/" && fs.existsSync(instancedirectory)) { - // Exit if no instance specified (it should be, just a safeguard) + // Exit if no instance specified (it should be, just a safeguard); if(instancedirectory != "./instances/undefined"){ var instanceconfig = require(instancedirectory + '/config'); instanceconfig.unique = instanceconfig.clientPassword.hashCode(); @@ -192,7 +192,7 @@ write-data=__PATH__executable__/../../../instances/" + instance + "\r\n\ instanceconfig.rconPort = process.env.RCONPORT; } } else { - process.exit(1) + process.exit(1); } console.log("Deleting .tmp.zip files"); let savefiles = fs.readdirSync(instancedirectory + "/saves/"); @@ -208,12 +208,12 @@ write-data=__PATH__executable__/../../../instances/" + instance + "\r\n\ // clean old log file to avoid crash // file exists, delete so we don't get in trouble try { - fs.unlinkSync(instancedirectory+'/factorio-current.log') + fs.unlinkSync(instancedirectory+'/factorio-current.log'); } catch (err){ if(err){ console.log(err); } else { - console.log("Clusterio | Deleting old logs...") + console.log("Clusterio | Deleting old logs..."); } } @@ -227,7 +227,7 @@ write-data=__PATH__executable__/../../../instances/" + instance + "\r\n\ }); // Spawn factorio server - //var serverprocess = child_process.exec(commandline) + //var serverprocess = child_process.exec(commandline); getNewestFile(instancedirectory + "/saves/", fs.readdirSync(instancedirectory + "/saves/"),function(latestSave) { // implicit global serverprocess = child_process.spawn( @@ -241,7 +241,7 @@ write-data=__PATH__executable__/../../../instances/" + instance + "\r\n\ ], { 'stdio': ['pipe', 'pipe', 'pipe'] } - ) + ); serverprocess.on('close', (code) => { console.log(`child process exited with code ${code}`); @@ -260,11 +260,11 @@ write-data=__PATH__executable__/../../../instances/" + instance + "\r\n\ if(process.platform == "linux"){ console.log('Factorio: ' + chunk); } - }) + }); serverprocess.stderr.on('data', (chunk) => { console.log('ERR: ' + chunk); - }) + }); // connect to the server with rcon // IP, port, password @@ -305,21 +305,21 @@ write-data=__PATH__executable__/../../../instances/" + instance + "\r\n\ // set some globals confirmedOrders = []; lastSignalCheck = Date.now(); - }) + }); } _.once(instanceManagement); function instanceManagement() { - console.log("Started instanceManagement();") + console.log("Started instanceManagement();"); // load plugins and execute onLoad event let pluginDirectories = getDirectories("./sharedPlugins/"); let plugins = []; for(i=0; i { @@ -367,7 +367,7 @@ function instanceManagement() { }); plugins[i].stderr.on("data", (data) => { log("STDERR: " + data); - }) + }); plugins[i].on('close', (code) => { log(`child process exited with code ${code}`); }); @@ -376,7 +376,7 @@ function instanceManagement() { // world IDs ------------------------------------------------------------------ hashMods(instance, function(modHashes){ setInterval(getID, 10000); - getID() + getID(); function getID() { 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 @@ -390,7 +390,7 @@ function instanceManagement() { while (data[0]) { let q = data[0].split(" "); // delete array element - data.splice(0,1) + data.splice(0,1); if(q[0] == "connected_players" && Number(q[1]) != NaN) { instanceInfo.playerCount = q[1]; } @@ -419,14 +419,14 @@ function instanceManagement() { console.error(err); } else 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) + console.log(response.body); } }); - }) - },1000) - }) + }); + },1000); + }); } - }) + }); // Mod uploading and management ----------------------------------------------- @@ -444,10 +444,10 @@ function instanceManagement() { needle.post(config.masterIP + ":" + config.masterPort + '/api/checkMod', payload, function (err, response, body) { if(err) throw err // Unable to contact master server! Please check your config.json. if(response && body && body == "found") { - console.log("master has mod") + console.log("master has mod"); } else if (response && body && typeof body == "string") { let mod = response.body; - console.log("Sending mod: " + mod) + console.log("Sending mod: " + mod); // Send mods master says it wants // response.body is a string which is a modName.zip var req = request.post("http://"+config.masterIP + ":" + config.masterPort + '/api/uploadMod', function (err, resp, body) { @@ -503,15 +503,15 @@ function instanceManagement() { console.log("Recorded flows, copper plate since last time: " + payload["copper-plate"]); needle.post(config.masterIP + ":" + config.masterPort + '/api/logStats', {timestamp: timestamp, slaveID: instanceconfig.unique,data: payload}, function (err, response, body) { // we did it, keep going - }) + }); } oldTimestamp = timestamp; oldFlowStats = totalFlows; fs.writeFileSync(instancedirectory + "/script-output/flows.txt", ""); } - }) + }); // we don't need to update stats quickly as that could be expensive - }, 60000) + }, 60000); // provide items -------------------------------------------------------------- // trigger when something happens to output.txt @@ -520,7 +520,7 @@ function instanceManagement() { let items = fs.readFileSync(instancedirectory + "/script-output/output.txt", "utf8").split("\n"); // if you found anything, reset the file if (items[0]) { - fs.writeFileSync(instancedirectory + "/script-output/output.txt", "") + fs.writeFileSync(instancedirectory + "/script-output/output.txt", ""); } for (let i = 0; i < items.length; i++) { if (items[i]) { @@ -536,7 +536,7 @@ function instanceManagement() { }); } } - }) + }); // request items -------------------------------------------------------------- setInterval(function () { // get array of lines in file @@ -574,7 +574,7 @@ function instanceManagement() { } else if (typeof Number(items[i][1]) == "number") { preparedPackage[items[i][0]] = { "name": items[i][0], - "count": Number(items[i][1]) + "count": Number(items[i][1]); }; } } @@ -582,7 +582,7 @@ function instanceManagement() { } // request our items, one item at a time for (let i = 0; i < Object.keys(preparedPackage).length; i++) { - console.log(preparedPackage[Object.keys(preparedPackage)[i]]) + console.log(preparedPackage[Object.keys(preparedPackage)[i]]); needle.post(config.masterIP + ":" + config.masterPort + '/api/remove', preparedPackage[Object.keys(preparedPackage)[i]], function (err, response, body) { if (response && response.body && typeof response.body == "object") { // buffer confirmed orders @@ -594,20 +594,20 @@ function instanceManagement() { } // if we got some confirmed orders // console.log("Importing " + confirmedOrders.length + " items! " + JSON.stringify(confirmedOrders)); - sadas = JSON.stringify(confirmedOrders) + sadas = JSON.stringify(confirmedOrders); confirmedOrders = []; // send our RCON command with whatever we got messageInterface("/silent-command remote.call('clusterio', 'importMany', '" + sadas + "')"); sadas = null; } - }, 3000) + }, 3000); // COMBINATOR SIGNALS --------------------------------------------------------- // get inventory from Master and RCON it to our slave setInterval(function () { needle.get(config.masterIP + ":" + config.masterPort + '/api/inventory', function (err, response, body) { if (response && response.body) { // Take the inventory we (hopefully) got and turn it into the format LUA accepts - // console.log(response.body) + // console.log(response.body); var inventory = response.body; var inventoryFrame = {}; for (let i = 0; i < inventory.length; i++) { @@ -618,7 +618,7 @@ function instanceManagement() { messageInterface("/silent-command remote.call('clusterio', 'receiveInventory', '" + JSON.stringify(inventoryFrame) + "')"); } }); - }, 1000) + }, 1000); // REMOTE SIGNALLING // send any signals the slave has been told to send setInterval(function () { @@ -652,12 +652,12 @@ function instanceManagement() { if (signals[i]) { // signals[i] is a JSON array called a "frame" of signals. We timestamp it for storage on master // then we unpack and RCON in this.frame to the game later. - let framepart = JSON.parse(signals[i]) + let framepart = JSON.parse(signals[i]); let doneframe = { time: Date.now(), - frame: framepart, // thats our array of objects(single signals) + frame: framepart, // thats our array of objects(single signals); } - // console.log(doneframe) + // console.log(doneframe); needle.post(config.masterIP + ":" + config.masterPort + '/api/setSignal', doneframe, 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. @@ -667,7 +667,7 @@ function instanceManagement() { })(i); } } - }, 1000) + }, 1000); } // END OF INSTANCE START --------------------------------------------------------------------- // get all directories in folder @@ -692,12 +692,12 @@ function hashMods(instanceName, callback) { if(hashedMods.length == /*mods.length*/ + instanceMods.length) { callback(hashedMods); } - //console.log(modname) + //console.log(modname); } let hashedMods = []; var i = 0; /*let mods = fs.readdirSync("./sharedMods/")*/ - let instanceMods = fs.readdirSync("./instances/"+instanceName+"/mods/") + let instanceMods = fs.readdirSync("./instances/"+instanceName+"/mods/"); for(o=0;o Date: Thu, 18 May 2017 21:16:11 +0200 Subject: [PATCH 02/29] Semicolons for consistency --- master.js | 52 ++++++++++++++++++++++++++-------------------------- 1 file changed, 26 insertions(+), 26 deletions(-) diff --git a/master.js b/master.js index d6713f59..e4e50684 100644 --- a/master.js +++ b/master.js @@ -5,9 +5,9 @@ const masterModFolder = "./database/masterMods/" const mkdirp = require("mkdirp"); mkdirp.sync("./database"); mkdirp.sync(masterModFolder); -const path = require("path") -const fs = require("fs") -var nedb = require("nedb") +const path = require("path"); +const fs = require("fs"); +var nedb = require("nedb"); // require config.json var config = require('./config'); @@ -47,7 +47,7 @@ db.signals = new Datastore({ filename: 'database/signals.db', autoload: true, in db.signals.ensureIndex({ fieldName: 'time', expireAfterSeconds: 3600 }, function (err) {}); // production chart database -db.flows = new Datastore({ filename: "database/flows.db", autoload: true}) +db.flows = new Datastore({ filename: "database/flows.db", autoload: true}); db.flows.ensureIndex({ fieldName: "slaveID", expireAfterSeconds: 2592000}); // expire after 30 days // db.slaves = new Datastore({ filename: 'database/slaves.db', autoload: true, inMemoryOnly: false}); @@ -64,7 +64,7 @@ db.items.additem = function(object) { db.items.insert(object); console.log('Item created!'); } - }) + }); } var slaves = {} // world ID management @@ -82,7 +82,7 @@ app.post("/api/getID", function(req,res) { // mod management // should handle uploading and checking if mods are uploaded app.post("/api/checkMod", function(req,res) { - let files = fs.readdirSync(masterModFolder) + let files = fs.readdirSync(masterModFolder); let found = false; files.forEach(file => { if(file == req.body.modName) { @@ -91,24 +91,24 @@ app.post("/api/checkMod", function(req,res) { }); if(!found) { // we don't have mod, plz send - res.send(req.body.modName) + res.send(req.body.modName); } else { - res.send("found") + res.send("found"); } - res.end() + res.end(); }); app.post("/api/uploadMod", function(req,res) { if (!req.files) { res.send('No files were uploaded.'); return; } else { - console.log(req.files.file) + console.log(req.files.file); req.files.file.mv('./database/masterMods/'+req.files.file.name, function(err) { if (err) { res.status(500).send(err); } else { res.send('File uploaded!'); - console.log("Uploaded mod: " + req.files.file.name) + console.log("Uploaded mod: " + req.files.file.name); } }); } @@ -122,7 +122,7 @@ app.get("/api/slaves", function(req, res) { for(key in copyOfSlaves) { copyOfSlaves[key].rconPassword = "hidden"; } - res.send(copyOfSlaves) + res.send(copyOfSlaves); }); // endpoint to send items to @@ -131,7 +131,7 @@ app.post("/api/place", function(req, res) { res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept"); console.log("added: " + req.body.name + " " + req.body.count); // save items we get - db.items.additem(req.body) + db.items.additem(req.body); // Attempt confirming res.end("success"); }); @@ -172,14 +172,14 @@ app.post("/api/remove", function(req, res) { console.log('failure ' + object.name); } } - }) + }); }); // circuit stuff app.post("/api/setSignal", function(req,res) { if(typeof req.body == "object" && req.body.time){ db.signals.insert(req.body); - // console.log("signal set") + // console.log("signal set"); } }); @@ -190,7 +190,7 @@ app.post("/api/readSignal", function(req,res) { db.signals.find({time:{$gte: req.body.since}}, function (err, docs) { // $gte means greater than or equal to, meaning we only get entries newer than the timestamp res.send(docs); - // console.log(docs) + // console.log(docs); }); }); @@ -215,18 +215,18 @@ app.post("/api/logStats", function(req,res) { timestamp: req.body.timestamp, data: req.body.data, }); - console.log("inserted: " + req.body.slaveID + " | " + req.body.timestamp) + console.log("inserted: " + req.body.slaveID + " | " + req.body.timestamp); } else { console.log("error invalid timestamp " + req.body.timestamp); - res.send(failure) + res.send(failure); } } else { - res.send("failure") + res.send("failure"); } -}) +}); // {slaveID: string, fromTime: Date, toTime, Date} app.post("/api/getStats", function(req,res) { - console.log(req.body) + console.log(req.body); if(typeof req.body == "object" && req.body.slaveID) { // if not specified, get stats for last 24 hours if(!req.body.fromTime) { @@ -235,18 +235,18 @@ app.post("/api/getStats", function(req,res) { if(!req.body.toTime) { req.body.toTime = Date.now(); } - console.log("Looking... " + JSON.stringify(req.body)) + console.log("Looking... " + JSON.stringify(req.body)); db.flows.find({ slaveID: req.body.slaveID, }, function(err, docs) { let entries = docs.filter(function (el) { return el.timestamp <= req.body.toTime && el.timestamp >= req.body.fromTime; }); - // console.log(entries) - res.send(entries) - }) + // console.log(entries); + res.send(entries); + }); } -}) +}); // endpoint for getting the chartjs library app.get("/chart.js", function(req, res) { From 924edc7af7d0ce340ecc1f6dcfd63f6dcfa387d5 Mon Sep 17 00:00:00 2001 From: Danielv123 Date: Fri, 19 May 2017 07:22:36 +0200 Subject: [PATCH 03/29] Allow starting plugins with arguments --- client.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/client.js b/client.js index 8645fb03..e964e168 100644 --- a/client.js +++ b/client.js @@ -318,9 +318,10 @@ function instanceManagement() { let log = function(t) { console.log("Clusterio | "+ pluginDirectories[I] + " | " + t); } + let arguments = pluginConfig.arguments || []; let pluginConfig = require("./sharedPlugins/" + pluginDirectories[i] + "/config.js"); - plugins.push(child_process.spawn(pluginConfig.binary, [], { + plugins.push(child_process.spawn(pluginConfig.binary, arguments, { cwd: "./sharedPlugins/"+pluginDirectories[i], stdio: ['pipe', 'pipe', 'pipe'], })); From 930d3352cc8118666c45ac848f0e7d42b67e1989 Mon Sep 17 00:00:00 2001 From: Danielv123 Date: Fri, 19 May 2017 07:24:09 +0200 Subject: [PATCH 04/29] Fixed problem --- client.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client.js b/client.js index e964e168..d6234fe1 100644 --- a/client.js +++ b/client.js @@ -575,7 +575,7 @@ function instanceManagement() { } else if (typeof Number(items[i][1]) == "number") { preparedPackage[items[i][0]] = { "name": items[i][0], - "count": Number(items[i][1]); + "count": Number(items[i][1]), }; } } From ba626295a5d187e82a7d4f47d02b730fa6a278f0 Mon Sep 17 00:00:00 2001 From: Danielv123 Date: Fri, 19 May 2017 09:07:58 +0200 Subject: [PATCH 05/29] Only pipe plugin stdout if it starts with /, else log to console --- client.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/client.js b/client.js index d6234fe1..a3b950c0 100644 --- a/client.js +++ b/client.js @@ -318,9 +318,9 @@ function instanceManagement() { let log = function(t) { console.log("Clusterio | "+ pluginDirectories[I] + " | " + t); } - let arguments = pluginConfig.arguments || []; let pluginConfig = require("./sharedPlugins/" + pluginDirectories[i] + "/config.js"); + let arguments = pluginConfig.arguments || []; plugins.push(child_process.spawn(pluginConfig.binary, arguments, { cwd: "./sharedPlugins/"+pluginDirectories[i], stdio: ['pipe', 'pipe', 'pipe'], @@ -363,8 +363,11 @@ function instanceManagement() { } console.log("Clusterio | Loaded plugin " + pluginDirectories[i]); plugins[i].stdout.on("data", (data) => { - // log("Stdout: " + data); - messageInterface(data.toString('utf8')); + if(data.toString('utf8')[0] != "/") { + log("Stdout: " + data.toString('utf8')) + } else { + messageInterface(data.toString('utf8')); + } }); plugins[i].stderr.on("data", (data) => { log("STDERR: " + data); From 336eb07cabc540cc102cbc40eeb430904536ccb2 Mon Sep 17 00:00:00 2001 From: Danielv123 Date: Fri, 19 May 2017 11:05:11 +0200 Subject: [PATCH 06/29] /api/editSlaveMeta allows you to attach arbitrary metadata to slaves You will need its unique ID and rcon password though. --- master.js | 36 ++++++++++++++++++++++++++++++++---- package.json | 1 + 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/master.js b/master.js index e4e50684..e08f7977 100644 --- a/master.js +++ b/master.js @@ -5,6 +5,7 @@ const masterModFolder = "./database/masterMods/" const mkdirp = require("mkdirp"); mkdirp.sync("./database"); mkdirp.sync(masterModFolder); +const deepmerge = require("deepmerge"); const path = require("path"); const fs = require("fs"); var nedb = require("nedb"); @@ -75,8 +76,35 @@ app.post("/api/getID", function(req,res) { // time us a unix timestamp we can use to check for how long the server has been unresponsive // we should save that somewhere and give appropriate response if(req.body){ - slaves[req.body.unique] = req.body; - console.log("Slave: " + req.body.mac + " : " + req.body.serverPort+" at " + req.body.publicIP); + console.log(req.body) + if(!slaves[req.body.unique]){ + slaves[req.body.unique] = {}; + } + for(k in req.body){ + if(k != "meta" && req.body.hasOwnProperty(k)){ + slaves[req.body.unique][k] = req.body[k]; + } + } + console.log("Slave registered: " + slaves[req.body.unique].mac + " : " + slaves[req.body.unique].serverPort+" at " + slaves[req.body.unique].publicIP + " with name " + slaves[req.body.unique].instanceName); + } +}); +// todo: Write docs for this +app.post("/api/editSlaveMeta", function(req,res) { + // request.body should be an object + // {slaveID, pass, meta:{x,y,z}} + + if(req.body && req.body.slaveID && req.body.password && req.body.meta){ + // check for editing permissions + if(slaves[req.body.slaveID] && slaves[req.body.slaveID].rconPassword == req.body.password){ + if(!slaves[req.body.slaveID].meta){ + slaves[req.body.slaveID].meta = {}; + slaves[req.body.slaveID].meta = deepmerge(slaves[req.body.slaveID].meta, req.body.meta, {clone:true}) + } + console.log("Updating slave: " + req.body.mac + " : " + req.body.serverPort+" at " + req.body.publicIP); + } else { + res.send("ERROR: Invalid slaveID or password") + } + slaves[req.body.slaveID] = req.body; } }); // mod management @@ -117,9 +145,9 @@ app.post("/api/uploadMod", function(req,res) { app.get("/api/slaves", function(req, res) { res.header("Access-Control-Allow-Origin", "*"); res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept"); - let copyOfSlaves = slaves + let copyOfSlaves = JSON.parse(JSON.stringify(slaves)); // filter out the rcon password because thats kindof not a safe thing to share - for(key in copyOfSlaves) { + for(key in copyOfSlaves){ copyOfSlaves[key].rconPassword = "hidden"; } res.send(copyOfSlaves); diff --git a/package.json b/package.json index 46f5a301..05107905 100644 --- a/package.json +++ b/package.json @@ -16,6 +16,7 @@ "body-parser": "^1.15.2", "canvasjs": "^1.8.1", "child_process": "^1.0.2", + "deepmerge": "^1.3.2", "ejs": "^2.5.6", "express": "^4.14.0", "express-fileupload": "0.0.6", From cd44b3572b20605192a26f5044784170875f16c9 Mon Sep 17 00:00:00 2001 From: Danielv123 Date: Sat, 20 May 2017 19:40:47 +0200 Subject: [PATCH 07/29] Added tests using mocha --- libomega.js | 5 ++++ libomega.spec.js | 62 ++++++++++++++++++++++++++++++++++++++++++++++++ package.json | 6 ++++- 3 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 libomega.spec.js diff --git a/libomega.js b/libomega.js index e7ef0a36..d60ace92 100644 --- a/libomega.js +++ b/libomega.js @@ -38,6 +38,11 @@ var clone = function(obj) { return copy; } +var deepclone = function(obj){ + return JSON.parse(JSON.stringify(obj)); +} + module.exports = { clone:clone, + deepclone:deepclone, }; \ No newline at end of file diff --git a/libomega.spec.js b/libomega.spec.js new file mode 100644 index 00000000..694395e0 --- /dev/null +++ b/libomega.spec.js @@ -0,0 +1,62 @@ +const sinon = require("sinon"); +var assert = require('assert'); +const libomega = require('./libomega'); +/* +describe('The webpage module', function () { + it('saves the content', function * () { + const url = 'google.com' + const content = '

title

' + const writeFileStub = this.sandbox.stub(fs, 'writeFile', function (filePath, fileContent, cb) { + cb(null) + }) + + const requestStub = this.sandbox.stub(request, 'get', function (url, cb) { + cb(null, null, content) + }) + + const result = yield webpage.saveWebpage(url) + + expect(writeFileStub).to.be.calledWith() + expect(requestStub).to.be.calledWith(url) + expect(result).to.eql('page') + }) +}) +*/ +describe("libomega.js", function(){ + describe("libomega.clone", function(){ + it("clones javascript objects", function(){ + var object1 = {hello:"world", cat:{legs:4, name:"Kitty", colors:["brown", "yellow", "purple"]}}; + var object2 = {}; + object2 = libomega.clone(object1); + + assert.equal(object2.cat.name, object1.cat.name) + assert.equal(object1.hello, object2.hello) + }); + it("does not deep clone objects", function(){ + var obj1 = {hello:"world", cat:{legs:4, name:"Kitty", colors:["brown", "yellow", "purple"]}}; + var obj2 = {}; + obj2 = libomega.clone(obj1); + + obj1.cat.colors = "black"; + assert.equal(obj1.cat.colors, "black"); + assert.notEqual(obj2.cat.colors[1], "yellow"); + }); + }); + describe("libomega.deepclone", function(){ + it("deep clones javascript objects", function(){ + var obj1 = {hello:"world", cat:{legs:4, name:"Kitty", colors:["brown", "yellow", "purple"]}}; + var obj2 = {}; + obj2 = libomega.deepclone(obj1); + + obj1.cat.colors = "black"; + assert.equal(obj1.cat.colors, "black"); + assert.equal(obj2.cat.colors[1], "yellow"); + }); + it("throws on non JSON parameters", function(){ + x = function(){}; + assert.throws(function(){ + y = libomega.deepclone(x) + }); + }); + }); +}); \ No newline at end of file diff --git a/package.json b/package.json index 05107905..c39f336a 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,8 @@ "description": "Factorio cluster???", "main": "master.js", "scripts": { - "test": "nodemon master.js", + "test": "npm run test-unit", + "test-unit": "SET NODE_ENV=test && mocha *.spec.js", "start": "echo Run node client.js start [instance] to start slaves && node master.js" }, "keywords": [ @@ -35,5 +36,8 @@ "simple-rcon": "^0.3.1", "sync-request": "^4.0.1", "underscore": "^1.8.3" + }, + "devDependencies": { + "mocha": "^3.4.1" } } From 6e95c86fb17f2615f5def8ab235c3735982ca428 Mon Sep 17 00:00:00 2001 From: Danielv123 Date: Sat, 20 May 2017 20:17:02 +0200 Subject: [PATCH 08/29] moved libs to ./lib/ --- client.js | 2 +- libomega.js => lib/libomega.js | 0 libomega.spec.js => lib/libomega.spec.js | 0 3 files changed, 1 insertion(+), 1 deletion(-) rename libomega.js => lib/libomega.js (100%) rename libomega.spec.js => lib/libomega.spec.js (100%) diff --git a/client.js b/client.js index a3b950c0..deebed95 100644 --- a/client.js +++ b/client.js @@ -10,7 +10,7 @@ var Rcon = require('simple-rcon'); var hashFiles = require('hash-files'); var _ = require('underscore'); -var libomega = require("./libomega.js"); +var libomega = require("./lib/libomega.js"); // require config.json var config = require('./config'); var global = {}; diff --git a/libomega.js b/lib/libomega.js similarity index 100% rename from libomega.js rename to lib/libomega.js diff --git a/libomega.spec.js b/lib/libomega.spec.js similarity index 100% rename from libomega.spec.js rename to lib/libomega.spec.js From d9e897836b23733035cecd77d0716fb8301e937f Mon Sep 17 00:00:00 2001 From: Danielv123 Date: Sat, 20 May 2017 20:30:25 +0200 Subject: [PATCH 09/29] test cleanup --- lib/libomega.spec.js | 21 --------------------- package.json | 2 +- 2 files changed, 1 insertion(+), 22 deletions(-) diff --git a/lib/libomega.spec.js b/lib/libomega.spec.js index 694395e0..5306c580 100644 --- a/lib/libomega.spec.js +++ b/lib/libomega.spec.js @@ -1,27 +1,6 @@ -const sinon = require("sinon"); var assert = require('assert'); const libomega = require('./libomega'); -/* -describe('The webpage module', function () { - it('saves the content', function * () { - const url = 'google.com' - const content = '

title

' - const writeFileStub = this.sandbox.stub(fs, 'writeFile', function (filePath, fileContent, cb) { - cb(null) - }) - const requestStub = this.sandbox.stub(request, 'get', function (url, cb) { - cb(null, null, content) - }) - - const result = yield webpage.saveWebpage(url) - - expect(writeFileStub).to.be.calledWith() - expect(requestStub).to.be.calledWith(url) - expect(result).to.eql('page') - }) -}) -*/ describe("libomega.js", function(){ describe("libomega.clone", function(){ it("clones javascript objects", function(){ diff --git a/package.json b/package.json index c39f336a..6c076c1a 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "main": "master.js", "scripts": { "test": "npm run test-unit", - "test-unit": "SET NODE_ENV=test && mocha *.spec.js", + "test-unit": "SET NODE_ENV=test && mocha --recursive ./**/*.spec.js", "start": "echo Run node client.js start [instance] to start slaves && node master.js" }, "keywords": [ From 5f3a160b75e9709a597cba4506774148bedf1d01 Mon Sep 17 00:00:00 2001 From: Danielv123 Date: Sun, 21 May 2017 22:00:30 +0200 Subject: [PATCH 10/29] Moved file functions to internal lib with some tests as well --- client.js | 65 ++++----------------------- database/items.db | 94 +++++++++++++++++++++++++++++++++++++++ lib/fileOps.js | 65 +++++++++++++++++++++++++++ lib/fileOps.spec.js | 43 ++++++++++++++++++ lib/test/another file.txt | 0 lib/test/file.txt | 0 package.json | 11 +++-- 7 files changed, 218 insertions(+), 60 deletions(-) create mode 100644 lib/fileOps.js create mode 100644 lib/fileOps.spec.js create mode 100644 lib/test/another file.txt create mode 100644 lib/test/file.txt diff --git a/client.js b/client.js index deebed95..2292dc2a 100644 --- a/client.js +++ b/client.js @@ -10,7 +10,10 @@ var Rcon = require('simple-rcon'); var hashFiles = require('hash-files'); var _ = require('underscore'); +// internal libraries var libomega = require("./lib/libomega.js"); +var fileOps = require("./lib/fileOps.js"); + // require config.json var config = require('./config'); var global = {}; @@ -29,21 +32,6 @@ const instancedirectory = './instances/' + instance; const command = process.argv[2]; var instanceInfo = {}; -// Functions -function deleteFolderRecursive(path) { - if (fs.existsSync(path)) { - fs.readdirSync(path).forEach(function (file, index) { - var curPath = path + "/" + file; - if (fs.lstatSync(curPath).isDirectory()) { // recurse - deleteFolderRecursive(curPath); - } else { // delete file - fs.unlinkSync(curPath); - } - }); - fs.rmdirSync(path); - } -}; - // simple string hasher String.prototype.hashCode = function () { var hash = 0; @@ -94,14 +82,14 @@ if (!command || command == "help" || command == "--help") { console.error("node client.js download"); process.exit(1); } else if (command == "list") { - console.dir(getDirectories("./instances/")); + console.dir(fileOps.getDirectories("./instances/")); process.exit(1); } else if (command == "delete") { if (!process.argv[3]) { console.error("Usage: node client.js delete [instance]"); process.exit(1); } else if (typeof process.argv[3] == "string" && fs.existsSync("./instances/" + process.argv[3]) && process.argv[3] != "/" && process.argv[3] != "") { - deleteFolderRecursive("./instances/" + process.argv[3]); + fileOps.deleteFolderRecursive("./instances/" + process.argv[3]); console.log("Deleted instance " + process.argv[3]); process.exit(1); } else { @@ -228,7 +216,7 @@ write-data=__PATH__executable__/../../../instances/" + instance + "\r\n\ // Spawn factorio server //var serverprocess = child_process.exec(commandline); - getNewestFile(instancedirectory + "/saves/", fs.readdirSync(instancedirectory + "/saves/"),function(latestSave) { + fileOps.getNewestFile(instancedirectory + "/saves/", fs.readdirSync(instancedirectory + "/saves/"),function(latestSave) { // implicit global serverprocess = child_process.spawn( './' + config.factorioDirectory + '/bin/x64/factorio', [ @@ -307,11 +295,13 @@ write-data=__PATH__executable__/../../../instances/" + instance + "\r\n\ lastSignalCheck = Date.now(); }); } + +// ensure instancemanagement only runs once _.once(instanceManagement); function instanceManagement() { console.log("Started instanceManagement();"); // load plugins and execute onLoad event - let pluginDirectories = getDirectories("./sharedPlugins/"); + let pluginDirectories = fileOps.getDirectories("./sharedPlugins/"); let plugins = []; for(i=0; i newest.mtime.getTime()) { - newest = { file : file, mtime : stats.mtime }; - } - if (checked == files.length) { - callback(newest); - } - }); - })(dir + file); - } - }); - } diff --git a/database/items.db b/database/items.db index e69de29b..4355854e 100644 --- a/database/items.db +++ b/database/items.db @@ -0,0 +1,94 @@ +{"name":"combat-shotgun","count":"1","_id":"01jLIbbQ2ZXo8xet"} +{"name":"crude-oil","count":3399414,"_id":"0z6ahVSklQcCuOmf"} +{"name":"sulfur","count":1032,"_id":"23SHYRpD6xVRNhaO"} +{"name":"science-pack-3","count":5002,"_id":"25wXED3PWYD9qk2r"} +{"name":"copper-plate","count":6144494,"_id":"2CawU9P5jgZHHiUU"} +{"name":"copper-ore","count":"102","_id":"2DYQES3PaVPuYyal"} +{"name":"stone","count":28,"_id":"2aCh9XADK403IjMu"} +{"name":"battery","count":53293,"_id":"4wzpuWq4UJGgGB0w"} +{"name":"substation","count":1035,"_id":"6EvWa5sr7aOfsOmh"} +{"name":"storage-tank","count":"4","_id":"6L8D2UUQGpqoeSyV"} +{"name":"small-pump","count":250,"_id":"6Y5Gdm7fANYWCK4S"} +{"name":"energy-shield-mk2-equipment","count":26,"_id":"7uV1wOd6YpP6YBg6"} +{"name":"pipe-to-ground","count":100,"_id":"9QP4hAy3sJkpDCrZ"} +{"name":"pipe","count":1004,"_id":"AHkpkp67PHoT2Ip8"} +{"name":"fast-inserter","count":"132","_id":"ALVxvbF1YpOzGQSI"} +{"name":"solid-fuel","count":70,"_id":"APqiAHT3a3sJRF7U"} +{"name":"chemical-plant","count":501,"_id":"CGwD3PkkPMqsCQBJ"} +{"name":"splitter","count":1008,"_id":"D6pu2w2C3tEpJSrY"} +{"name":"rail","count":2504,"_id":"DOIN6qxET53zLjAR"} +{"name":"small-electric-pole","count":55,"_id":"Dy4cKwfZtwB2pjeF"} +{"name":"battery-mk2-equipment","count":27,"_id":"E84qUcptl6eH4D14"} +{"name":"cargo-wagon","count":21,"_id":"EWkaGca01NYDnyrt"} +{"name":"steel-plate","count":1594404,"_id":"F5wIdk2J2O5Jfp64"} +{"name":"science-pack-2","count":108672,"_id":"HkKc8AFK9INvlxnD"} +{"name":"green-wire","count":"36","_id":"HoIHkVMIMSMXlfPW"} +{"name":"beacon","count":"10","_id":"ImP3SlS0bfO8kaip"} +{"name":"fusion-reactor-equipment","count":7,"_id":"JynGGRC3wgbjG1xK"} +{"name":"big-electric-pole","count":1005,"_id":"KvOb3foDB5Px2jCL"} +{"name":"solar-panel","count":1023,"_id":"LQ70r6rrvPQ1twsB"} +{"name":"productivity-module-2","count":254,"_id":"LWmYiJ4zAOcdEgRY"} +{"name":"science-pack-1","count":139816,"_id":"LXBJ6Ns3IvDtBq5Y"} +{"name":"advanced-circuit","count":645071,"_id":"LdCXjtKXYPpmmLJf"} +{"name":"electric-mining-drill","count":509,"_id":"MC0sUIbd4bNPyiWV"} +{"name":"iron-ore","count":"52","_id":"MQZOQJ132g92DlEg"} +{"name":"water","count":50058,"_id":"MTEnmbNGHKSE8Gy3"} +{"name":"destroyer-capsule","count":"100","_id":"MlXqRhCD4UZETrdK"} +{"name":"iron-gear-wheel","count":"180","_id":"NsPHgS3B2qaKmyap"} +{"name":"speed-module-2","count":254,"_id":"Nxz2ZKMbcyiqUWd1"} +{"name":"burner-inserter","count":94,"_id":"Ouwv7ZlKhaz00FAt"} +{"name":"logistic-chest-requester","count":257,"_id":"PNLjyvBGrb4Gci5G"} +{"name":"express-splitter","count":1010,"_id":"PT7b3VbWa47lJJNI"} +{"name":"piercing-shotgun-shell","count":"70","_id":"PfsRTwLQLwsnZK6F"} +{"name":"solar-panel-equipment","count":18,"_id":"S29hMsueEXwICCvp"} +{"name":"personal-roboport-equipment","count":26,"_id":"STElkhgN3HjQ9LKj"} +{"name":"logistic-chest-storage","count":250,"_id":"SscVzS8jf9xk6Eat"} +{"name":"stone-furnace","count":1001,"_id":"TIONNgEB1OcvuR4m"} +{"name":"iron-plate","count":25036,"_id":"TKUNsKLhzi9wbXLk"} +{"name":"assembling-machine-1","count":"50","_id":"VAjgjWvWSDLzJhcz"} +{"name":"rail-chain-signal","count":"97","_id":"W8KAv7VO9viuCuLO"} +{"name":"constant-combinator","count":"5","_id":"WnaALSqOZ6sHra99"} +{"name":"coal","count":853334,"_id":"WxJKZfXyX8c0rHWc"} +{"name":"red-wire","count":"13","_id":"XprwdphPUrihfASQ"} +{"name":"effectivity-module-2","count":286,"_id":"XwITKWqPlpxkAYxo"} +{"name":"logistic-chest-passive-provider","count":253,"_id":"Yr4v7SlUGXQWouxJ"} +{"name":"fast-splitter","count":1016,"_id":"YrzmqfD7u7fZp34C"} +{"name":"plastic-bar","count":50060,"_id":"Yw2qmHm6SH5dyTxs"} +{"name":"pumpjack","count":404,"_id":"Z0plqSsfLfAl9YYa"} +{"name":"small-lamp","count":"50","_id":"Zd3H118Nc2u7HmeP"} +{"name":"fast-transport-belt","count":1042,"_id":"ZmqzOJF4tVJEzgUW"} +{"name":"medium-electric-pole","count":500,"_id":"aPqWD0jxmTv8aac7"} +{"name":"electric-furnace","count":200,"_id":"bBvxS6tFmo4yCFEE"} +{"name":"raw-wood","count":22912,"_id":"d85jLuxP9EaWmHe9"} +{"name":"laser-turret","count":252,"_id":"f9MItTJWZYfsUc9f"} +{"name":"deconstruction-planner","count":10,"_id":"fmj6YZnuuinVPP4q"} +{"name":"roboport","count":25,"_id":"ijpo46GFEzZJdZDG"} +{"name":"construction-robot","count":1026,"_id":"ipGZYHapBK3gmsaV"} +{"name":"electronic-circuit","count":3768586,"_id":"iq5rntWyvPGJqWUr"} +{"name":"express-transport-belt","count":1017,"_id":"iyrTNKNZCap5CUfn"} +{"name":"boiler","count":1011,"_id":"j2UJIu2uH01RXpgo"} +{"name":"repair-pack","count":"14","_id":"jzlBCjlkSHXUwQaq"} +{"name":"blueprint","count":12,"_id":"k30PZakZ4PBVFB32"} +{"name":"stone-wall","count":551,"_id":"kjaR0wHDPqIwZNxN"} +{"name":"electric-engine-unit","count":252,"_id":"lSWfvV27887gKucW"} +{"name":"decider-combinator","count":"1","_id":"oRrRtX53cbXqkhJF"} +{"name":"petroleum-gas","count":7582906,"_id":"ovOahSJQhBOQf0sL"} +{"name":"transport-belt","count":1011,"_id":"pBF2CkAB1AFexcZE"} +{"name":"assembling-machine-3","count":293,"_id":"pevQK7kAM5v7ObfG"} +{"name":"alien-science-pack","count":"415","_id":"qlkibcDNC0OgBuy7"} +{"name":"assembling-machine-2","count":200,"_id":"rUdnyKLPhZvMoRJ8"} +{"name":"steam-engine","count":1000,"_id":"rmEFJ9OuEIn91mLx"} +{"name":"accumulator","count":1005,"_id":"tCrPS64NshuzFHtW"} +{"name":"raw-fish","count":"11","_id":"uVQshHGwOWGwIMux"} +{"name":"underground-belt","count":1008,"_id":"v1qaz21iokoxERyl"} +{"name":"diesel-locomotive","count":20,"_id":"vmyqu0WI3k1XIdlu"} +{"name":"express-underground-belt","count":1012,"_id":"vpm5zw4BlnfErJAo"} +{"name":"piercing-rounds-magazine","count":5030,"_id":"wJpfX5g4HgCyRiAa"} +{"name":"processing-unit","count":108,"_id":"wXjUBq0KIksTJcwy"} +{"name":"power-armor-mk2","count":5,"_id":"wmItTXh1bEcysw86"} +{"name":"logistic-robot","count":1048,"_id":"x2cRxrpdC7BHJFpx"} +{"name":"fast-underground-belt","count":1010,"_id":"xONgtmV0OoxKmzVv"} +{"name":"engine-unit","count":254,"_id":"y9XyUryrxgsucFJc"} +{"name":"grenade","count":"30","_id":"ygLIdsWl9JFV2aZ6"} +{"name":"concrete","count":1039,"_id":"ym8LemdTCsDO52s8"} +{"name":"logistic-chest-active-provider","count":252,"_id":"zQZ9UP3qyg6Hx9jO"} diff --git a/lib/fileOps.js b/lib/fileOps.js new file mode 100644 index 00000000..55b9f066 --- /dev/null +++ b/lib/fileOps.js @@ -0,0 +1,65 @@ +var fs = require("fs"); +var path = require("path"); + +// gets newest file in a directory +// dir is directory +// files is array of filenames +// callback(filename string); +var getNewestFile = function(dir, files, callback) { + if (!callback) return; + if (!files || (files && files.length === 0)) { + callback(); + } + var newest = { file: files[0] }; + var checked = 0; + fs.stat(dir + newest.file, function(err, stats) { + newest.mtime = stats.mtime; + for (var i = 0; i < files.length; i++) { + var file = files[i]; + (function(file) { + fs.stat(file, function(err, stats) { + ++checked; + if (stats.mtime.getTime() > newest.mtime.getTime()) { + newest = { file : file, mtime : stats.mtime }; + } + if (checked == files.length) { + callback(newest); + } + }); + })(dir + file); + } + }); +} + +// get all directories in folder +var getDirectories = function(srcpath) { + return fs.readdirSync(srcpath).filter(function (file) { + return fs.statSync(path.join(srcpath, file)).isDirectory(); + }); +} +var getFileNamesSync = function(srcpath) { + return fs.readdirSync(srcpath).filter(function (file) { + return !fs.statSync(path.join(srcpath, file)).isDirectory(); + }); +} +// does what it says on the tin +var deleteFolderRecursive = function(path) { + if (fs.existsSync(path)) { + fs.readdirSync(path).forEach(function (file, index) { + var curPath = path + "/" + file; + if (fs.lstatSync(curPath).isDirectory()) { // recurse + deleteFolderRecursive(curPath); + } else { // delete file + fs.unlinkSync(curPath); + } + }); + fs.rmdirSync(path); + } +}; + +module.exports = { + getNewestFile:getNewestFile, + getDirectories:getDirectories, + deleteFolderRecursive:deleteFolderRecursive, + getFileNamesSync:getFileNamesSync, +} \ No newline at end of file diff --git a/lib/fileOps.spec.js b/lib/fileOps.spec.js new file mode 100644 index 00000000..7297f11c --- /dev/null +++ b/lib/fileOps.spec.js @@ -0,0 +1,43 @@ +var assert = require('assert'); +var fileOps = require('./fileOps'); + +describe("fileOps.js", function(){ + describe("fileOps.getDirectories()", function(){ + it("gets an array of directory names from a path", function(){ + let directories = fileOps.getDirectories("lib/test"); + + // because there are no arrays for some reason + assert(typeof directories == "object"); + + // they are sorted alphabetically because thats the way it is + assert(directories[0] == "another folder" && directories[1] == "folder"); + }); + it("does not return any filenames", function(){ + let directories = fileOps.getDirectories("lib/test"); + + // this is a function to return directories, not files. So yeah. + assert(!directories.includes("file.txt") && !directories.includes("another file.txt")); + }); + }); + describe("fileOps.getFileNamesSync(path)", function(){ + it("gets names of files in directory", function(){ + let files = fileOps.getFileNamesSync("lib/test"); + assert(files.length > 0); + assert(files.includes("file.txt") && files.includes("another file.txt")); + }); + it("throws when lacking first argument", function(){ + assert.throws(function(){ + let files = fileOps.getFileNamesSync(); + }, Error); + }); + }); + /*describe("fileOps.getNewestFile()", function(){ + it("gets the newest file in a directory async", function(done){ + fileOps.getNewestFile("lib/test", fileOps.getFileNames("lib/test"), function(newest){ + assert(newest); + done(); + }) + + }); + });*/ +}); diff --git a/lib/test/another file.txt b/lib/test/another file.txt new file mode 100644 index 00000000..e69de29b diff --git a/lib/test/file.txt b/lib/test/file.txt new file mode 100644 index 00000000..e69de29b diff --git a/package.json b/package.json index 6c076c1a..b08f032f 100644 --- a/package.json +++ b/package.json @@ -1,11 +1,11 @@ { - "name": "factorio-clusterio", + "name": "clusterio", "version": "1.0.0", - "description": "Factorio cluster???", + "description": "Interserver communication for factorio", "main": "master.js", "scripts": { "test": "npm run test-unit", - "test-unit": "SET NODE_ENV=test && mocha --recursive ./**/*.spec.js", + "test-unit": "SET NODE_ENV=test && mocha --recursive \"./{,!(node_modules)/**/}*.spec.js\"", "start": "echo Run node client.js start [instance] to start slaves && node master.js" }, "keywords": [ @@ -33,11 +33,14 @@ "needle": "^1.1.0", "portfinder": "^1.0.9", "request": "^2.79.0", + "semver": "^5.3.0", "simple-rcon": "^0.3.1", "sync-request": "^4.0.1", "underscore": "^1.8.3" }, "devDependencies": { - "mocha": "^3.4.1" + "chai": "^3.5.0", + "mocha": "^3.4.1", + "mock-fs": "^4.3.0" } } From 4f77c1b473362b384087e1e7ab2226e28eb5a1f6 Mon Sep 17 00:00:00 2001 From: Danielv123 Date: Sun, 21 May 2017 22:20:20 +0200 Subject: [PATCH 11/29] housekeeping --- lib/libomega.spec.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/libomega.spec.js b/lib/libomega.spec.js index 5306c580..5ee04baa 100644 --- a/lib/libomega.spec.js +++ b/lib/libomega.spec.js @@ -1,8 +1,8 @@ var assert = require('assert'); -const libomega = require('./libomega'); +var libomega = require('./libomega'); describe("libomega.js", function(){ - describe("libomega.clone", function(){ + describe("libomega.clone()", function(){ it("clones javascript objects", function(){ var object1 = {hello:"world", cat:{legs:4, name:"Kitty", colors:["brown", "yellow", "purple"]}}; var object2 = {}; @@ -21,7 +21,7 @@ describe("libomega.js", function(){ assert.notEqual(obj2.cat.colors[1], "yellow"); }); }); - describe("libomega.deepclone", function(){ + describe("libomega.deepclone()", function(){ it("deep clones javascript objects", function(){ var obj1 = {hello:"world", cat:{legs:4, name:"Kitty", colors:["brown", "yellow", "purple"]}}; var obj2 = {}; From 2695f0a44354604b5f9aece29d70f66019af5bc0 Mon Sep 17 00:00:00 2001 From: Danielv123 Date: Mon, 22 May 2017 10:59:16 +0200 Subject: [PATCH 12/29] Suppressed NPM errors --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index b08f032f..0fb72b3f 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "main": "master.js", "scripts": { "test": "npm run test-unit", - "test-unit": "SET NODE_ENV=test && mocha --recursive \"./{,!(node_modules)/**/}*.spec.js\"", + "test-unit": "SET NODE_ENV=test && mocha --recursive \"./{,!(node_modules)/**/}*.spec.js\" || echo .", "start": "echo Run node client.js start [instance] to start slaves && node master.js" }, "keywords": [ From 3d4fbfb3fa66657c9f34d6387625b2617c9ff1e8 Mon Sep 17 00:00:00 2001 From: Danielv123 Date: Mon, 22 May 2017 10:59:39 +0200 Subject: [PATCH 13/29] tests for fileOps.getNewestFile() --- client.js | 9 ++++--- lib/fileOps.js | 59 +++++++++++++++++++++++++++------------------ lib/fileOps.spec.js | 46 ++++++++++++++++++++++++++++------- 3 files changed, 77 insertions(+), 37 deletions(-) diff --git a/client.js b/client.js index 2292dc2a..7d3dc780 100644 --- a/client.js +++ b/client.js @@ -82,14 +82,14 @@ if (!command || command == "help" || command == "--help") { console.error("node client.js download"); process.exit(1); } else if (command == "list") { - console.dir(fileOps.getDirectories("./instances/")); + console.dir(fileOps.getDirectoriesSync("./instances/")); process.exit(1); } else if (command == "delete") { if (!process.argv[3]) { console.error("Usage: node client.js delete [instance]"); process.exit(1); } else if (typeof process.argv[3] == "string" && fs.existsSync("./instances/" + process.argv[3]) && process.argv[3] != "/" && process.argv[3] != "") { - fileOps.deleteFolderRecursive("./instances/" + process.argv[3]); + fileOps.deleteFolderRecursiveSync("./instances/" + process.argv[3]); console.log("Deleted instance " + process.argv[3]); process.exit(1); } else { @@ -216,7 +216,8 @@ write-data=__PATH__executable__/../../../instances/" + instance + "\r\n\ // Spawn factorio server //var serverprocess = child_process.exec(commandline); - fileOps.getNewestFile(instancedirectory + "/saves/", fs.readdirSync(instancedirectory + "/saves/"),function(latestSave) { + fileOps.getNewestFile(instancedirectory + "/saves/", fs.readdirSync(instancedirectory + "/saves/"),function(err, latestSave) { + if(err) throw err; // implicit global serverprocess = child_process.spawn( './' + config.factorioDirectory + '/bin/x64/factorio', [ @@ -301,7 +302,7 @@ _.once(instanceManagement); function instanceManagement() { console.log("Started instanceManagement();"); // load plugins and execute onLoad event - let pluginDirectories = fileOps.getDirectories("./sharedPlugins/"); + let pluginDirectories = fileOps.getDirectoriesSync("./sharedPlugins/"); let plugins = []; for(i=0; i newest.mtime.getTime()) { - newest = { file : file, mtime : stats.mtime }; - } - if (checked == files.length) { - callback(newest); - } - }); - })(dir + file); + if(err) { + callback(err); + } else { + newest.mtime = stats.mtime; + for (var i = 0; i < files.length; i++) { + var file = files[i]; + (function(file) { + fs.stat(file, function(err, stats) { + ++checked; + if (stats.mtime.getTime() > newest.mtime.getTime()) { + newest = { file : file, mtime : stats.mtime }; + } + if (checked == files.length) { + callback(undefined,newest); + } + }); + })(dir + file); + } } }); } -// get all directories in folder -var getDirectories = function(srcpath) { +// get names of all directories in directory +var getDirectoriesSync = function(srcpath) { return fs.readdirSync(srcpath).filter(function (file) { return fs.statSync(path.join(srcpath, file)).isDirectory(); }); } +// get names of all files in a directory var getFileNamesSync = function(srcpath) { return fs.readdirSync(srcpath).filter(function (file) { return !fs.statSync(path.join(srcpath, file)).isDirectory(); }); } // does what it says on the tin -var deleteFolderRecursive = function(path) { +var deleteFolderRecursiveSync = function(path) { if (fs.existsSync(path)) { fs.readdirSync(path).forEach(function (file, index) { var curPath = path + "/" + file; if (fs.lstatSync(curPath).isDirectory()) { // recurse - deleteFolderRecursive(curPath); + deleteFolderRecursiveSync(curPath); } else { // delete file fs.unlinkSync(curPath); } @@ -59,7 +70,7 @@ var deleteFolderRecursive = function(path) { module.exports = { getNewestFile:getNewestFile, - getDirectories:getDirectories, - deleteFolderRecursive:deleteFolderRecursive, + getDirectoriesSync:getDirectoriesSync, + deleteFolderRecursiveSync:deleteFolderRecursiveSync, getFileNamesSync:getFileNamesSync, -} \ No newline at end of file +} diff --git a/lib/fileOps.spec.js b/lib/fileOps.spec.js index 7297f11c..112e140e 100644 --- a/lib/fileOps.spec.js +++ b/lib/fileOps.spec.js @@ -2,9 +2,9 @@ var assert = require('assert'); var fileOps = require('./fileOps'); describe("fileOps.js", function(){ - describe("fileOps.getDirectories()", function(){ + describe("fileOps.getDirectoriesSync()", function(){ it("gets an array of directory names from a path", function(){ - let directories = fileOps.getDirectories("lib/test"); + let directories = fileOps.getDirectoriesSync("lib/test"); // because there are no arrays for some reason assert(typeof directories == "object"); @@ -13,11 +13,16 @@ describe("fileOps.js", function(){ assert(directories[0] == "another folder" && directories[1] == "folder"); }); it("does not return any filenames", function(){ - let directories = fileOps.getDirectories("lib/test"); + let directories = fileOps.getDirectoriesSync("lib/test"); // this is a function to return directories, not files. So yeah. assert(!directories.includes("file.txt") && !directories.includes("another file.txt")); }); + it("throws when lacking first argument", function(){ + assert.throws(function(){ + let files = fileOps.getDirectoriesSync(); + }, Error); + }); }); describe("fileOps.getFileNamesSync(path)", function(){ it("gets names of files in directory", function(){ @@ -31,13 +36,36 @@ describe("fileOps.js", function(){ }, Error); }); }); - /*describe("fileOps.getNewestFile()", function(){ + describe("fileOps.getNewestFile()", function(){ it("gets the newest file in a directory async", function(done){ - fileOps.getNewestFile("lib/test", fileOps.getFileNames("lib/test"), function(newest){ - assert(newest); + fileOps.getNewestFile("lib/test", fileOps.getFileNamesSync("lib/test"), function(err, newest){ + assert(newest && typeof newest.file == "string"); done(); - }) - + }); }); - });*/ + it("throws if callback is missing", function(){ + assert.throws(function(){ + fileOps.getNewestFile("lib/test", fileOps.getFileNamesSync("lib/test")); + }, Error); + }); + it("executes callback(err) if no files are provided", function(done){ + fileOps.getNewestFile("lib/test", [], function(err, newest){ + assert(err && typeof err == "object"); + done(); + }); + }); + it("executes callback(err) if launched without a directory", function(done){ + fileOps.getNewestFile("", fileOps.getFileNamesSync("lib/test"), function(err, newest){ + assert(err); + done(); + }); + }); + it("executes callback(err) when you do stupid shit", function(done){ + fileOps.getNewestFile({thisWontWork: "but not crash either"}, {lolz:"yeah..."}, function(err, newest){ + assert(err); + // ENOENT: no such file or directory, stat 'C:\**\factorioClusterio\lib\test\undefined' + done(); + }); + }); + }); }); From ecc5b1c6ce01627c8a680419bb63422a7e0feea4 Mon Sep 17 00:00:00 2001 From: Danielv123 Date: Mon, 22 May 2017 13:28:00 +0200 Subject: [PATCH 14/29] tests for fileOps.deleteFolder[...], istanbul for code coverage --- lib/fileOps.js | 5 ++++- lib/fileOps.spec.js | 35 +++++++++++++++++++++++++++++++++++ lib/test/another file.txt | 0 lib/test/file.txt | 0 package.json | 4 +++- 5 files changed, 42 insertions(+), 2 deletions(-) delete mode 100644 lib/test/another file.txt delete mode 100644 lib/test/file.txt diff --git a/lib/fileOps.js b/lib/fileOps.js index 6ce22672..65250fcf 100644 --- a/lib/fileOps.js +++ b/lib/fileOps.js @@ -40,7 +40,7 @@ var getNewestFile = function(dir, files, callback) { } }); } - + // get names of all directories in directory var getDirectoriesSync = function(srcpath) { return fs.readdirSync(srcpath).filter(function (file) { @@ -65,6 +65,9 @@ var deleteFolderRecursiveSync = function(path) { } }); fs.rmdirSync(path); + return true; + } else { + return false; } }; diff --git a/lib/fileOps.spec.js b/lib/fileOps.spec.js index 112e140e..77ffb249 100644 --- a/lib/fileOps.spec.js +++ b/lib/fileOps.spec.js @@ -1,5 +1,27 @@ var assert = require('assert'); var fileOps = require('./fileOps'); +var fs = require('fs'); + +// create folders for testing +if (!fs.existsSync("lib/test")){ + fs.mkdirSync("lib/test"); +} +if (!fs.existsSync("lib/test/folder")){ + fs.mkdirSync("lib/test/folder"); +} +if (!fs.existsSync("lib/test/another folder")){ + fs.mkdirSync("lib/test/another folder"); +} +if (!fs.existsSync("lib/test/file.txt")){ + fs.writeFileSync("lib/test/file.txt", "contents"); +} +if (!fs.existsSync("lib/test/another file.txt")){ + fs.writeFileSync("lib/test/another file.txt", "more contents"); +} +// we need this xfile to test a branch in fileOps.getNewestFile()s propagation +if (!fs.existsSync("lib/test/xfile.txt")){ + fs.writeFileSync("lib/test/xfile.txt", "contents"); +} describe("fileOps.js", function(){ describe("fileOps.getDirectoriesSync()", function(){ @@ -68,4 +90,17 @@ describe("fileOps.js", function(){ }); }); }); + describe("fileOps.deleteFolderRecursiveSync(path)", function(){ + it("deletes a folder and all files and folders in it", function(){ + let x = fileOps.deleteFolderRecursiveSync("lib/test"); + assert.throws(function(){ + fs.statSync("lib/test"); + }, Error); + assert(x); + }); + it("returns false if you give it an invalid path", function(){ + let x = fileOps.deleteFolderRecursiveSync("This isn't a real path"); + assert(!x); + }); + }); }); diff --git a/lib/test/another file.txt b/lib/test/another file.txt deleted file mode 100644 index e69de29b..00000000 diff --git a/lib/test/file.txt b/lib/test/file.txt deleted file mode 100644 index e69de29b..00000000 diff --git a/package.json b/package.json index 0fb72b3f..012ddf23 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,8 @@ "scripts": { "test": "npm run test-unit", "test-unit": "SET NODE_ENV=test && mocha --recursive \"./{,!(node_modules)/**/}*.spec.js\" || echo .", - "start": "echo Run node client.js start [instance] to start slaves && node master.js" + "start": "echo Run node client.js start [instance] to start slaves && node master.js", + "cover": "./node_modules/.bin/istanbul cover ./node_modules/mocha/bin/_mocha -- lib/*.js -R spec" }, "keywords": [ "factorio" @@ -40,6 +41,7 @@ }, "devDependencies": { "chai": "^3.5.0", + "istanbul": "^0.4.5", "mocha": "^3.4.1", "mock-fs": "^4.3.0" } From 5c13e3107d7176327de9cde2dc5a0af32fded8fb Mon Sep 17 00:00:00 2001 From: Danielv123 Date: Tue, 23 May 2017 11:56:10 +0200 Subject: [PATCH 15/29] /nodeDetails?slaveID=xxx page for detailed info about a single slave and massive UI overhaul which looks like shit. --- routes.js | 3 ++ static/head.html | 1 + static/nodeDetails.css | 59 ++++++++++++++++++++++++++++ static/nodeDetails.ejs | 21 ++++++++++ static/nodeDetails.js | 87 ++++++++++++++++++++++++++++++++++++++++++ static/sidebar.html | 3 ++ static/style.css | 14 ++++++- 7 files changed, 186 insertions(+), 2 deletions(-) create mode 100644 static/nodeDetails.css create mode 100644 static/nodeDetails.ejs create mode 100644 static/nodeDetails.js diff --git a/routes.js b/routes.js index 45f05954..efd330b0 100644 --- a/routes.js +++ b/routes.js @@ -8,4 +8,7 @@ module.exports = function(app) { app.get("/settings", function(req, res) { res.render("settings"); }); + app.get("/nodeDetails", function(req, res) { + res.render("nodeDetails"); + }); }; \ No newline at end of file diff --git a/static/head.html b/static/head.html index 7ce98e7d..7c1c12ae 100644 --- a/static/head.html +++ b/static/head.html @@ -2,3 +2,4 @@ + \ No newline at end of file diff --git a/static/nodeDetails.css b/static/nodeDetails.css new file mode 100644 index 00000000..2916ae75 --- /dev/null +++ b/static/nodeDetails.css @@ -0,0 +1,59 @@ +*{ + color: #FFF; +} + +#header .subbar h6 { + display:inline; + font-weight:normal; + color: #BBB +} +#header { + background-color: #222; + color: #DDD; + font-family: 'Roboto', sans-serif; + padding-left:16px; +} +#displayBody { + margin-left: 16px; +} +a { + color: #FF7200; + text-decoration: none; +} +a:hover { + color: #FFF; +} +#hero { + background-color: #444; + height:400px; +} + +#header h1 { + margin-left: 10px; + display:inline-block; +} +#header .subbar h6 { + margin-right: 16px; +} +#onlineIndicator { + background-color:red; + height:20px; + width:20px; + display:inline-block; + border-radius:16px; +} +ul { + width:300px; + list-style:none; + max-height:100px; + margin:0; + overflow:auto; + padding:0; + text-indent:10px; +} +li{ + line-height:25px; +} +li:nth-child(even){ + background:#ccc; +} \ No newline at end of file diff --git a/static/nodeDetails.ejs b/static/nodeDetails.ejs new file mode 100644 index 00000000..32ce63eb --- /dev/null +++ b/static/nodeDetails.ejs @@ -0,0 +1,21 @@ + + + <%- include("head.html") %> + + + + + + <%- include("sidebar.html") %> +
+
+

Slave details

+
+ +
+
+
+ +
+
+ \ No newline at end of file diff --git a/static/nodeDetails.js b/static/nodeDetails.js new file mode 100644 index 00000000..756ed00d --- /dev/null +++ b/static/nodeDetails.js @@ -0,0 +1,87 @@ +// string, object, function(object) +function post(url, data, callback) { + console.log("POST " + url + JSON.stringify(data)) + var xhr = new XMLHttpRequest(); + xhr.open("POST", url, true); + xhr.setRequestHeader("Content-type", "application/json"); + xhr.onreadystatechange = function () { + if (xhr.readyState == 4 && xhr.status == 200) { + var json = JSON.parse(xhr.responseText); + callback(json); + } + } + xhr.send(JSON.stringify(data)); +} +function getJSON(url, callback) { + var xhr = new XMLHttpRequest(); + xhr.open('GET', url, true); + xhr.responseType = 'json'; + xhr.onload = function() { + var status = xhr.status; + if (status == 200) { + callback(null, xhr.response); + } else { + callback(status); + } + }; + xhr.send(); +}; +function getParameterByName(name, url) { + if (!url) url = window.location.href; + name = name.replace(/[\[\]]/g, "\\$&"); + var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"), + results = regex.exec(url); + if (!results) return null; + if (!results[2]) return ''; + return decodeURIComponent(results[2].replace(/\+/g, " ")); +} + +function populateSlaveInfo(){ + let slaveID = getParameterByName("slaveID"); + if(!slaveID){ + throw "We need slaveID! GIVE US SLAVEID! ALL HAIL OUR NEW OVERLORDS!" + } + getJSON("/api/slaves", function(err, slaveData){ + /* + {"-1530507550":{ + "time":"1495475926375", + "rconPort":"8714", + "rconPassword":"hidden", + "serverPort":"14345", + "unique":"-1530507550", + "publicIP":"localhost", + "mods":[ + { + "modName":"clusterio_1.0.0.zip", + "hash":"341feb6c60918c83f7f3a6a14a4a308aef18dda6" + },{ + "modName":"clusterio_1.0.1.zip", + "hash":"aee7a4b67ece19bddf236312fe408a5d4b67248d" + },{ + "modName":"clusterio_1.1.0.zip", + "hash":"18e2f7e402d716e3dd1c1458b7fab2d0bb29eccf" + }], + "playerCount":"0", + "instanceName":"t", + "mac":"44-85-00-D2-7E-D6"} + } + */ + let slave = slaveData[slaveID]; + let HTML = "" // end of header + HTML += "

Last seen: "+slave.time+"

" + HTML += "
" // end of displayBody + // list mods and other metadata + HTML += "

Mods

    " + for(let i = 0; i < slave.mods.length; i++){ + HTML += "
  • "+slave.mods[i].modName+"
  • " + } + + document.querySelector("#hero").innerHTML = HTML; + + + }); +} + +populateSlaveInfo(); diff --git a/static/sidebar.html b/static/sidebar.html index b24a4d2e..60111b51 100644 --- a/static/sidebar.html +++ b/static/sidebar.html @@ -11,6 +11,9 @@ + diff --git a/static/style.css b/static/style.css index 42d8de37..c4ca38c5 100644 --- a/static/style.css +++ b/static/style.css @@ -3,8 +3,11 @@ } #logo { - background-color: yellow; -} + /* using same colors as factorios official ones, taken from + http://www.lua-api.factorio.com */ + background-color: #262626; + color: #C8C8C8; +} #sidenav { position:fixed; @@ -13,6 +16,13 @@ background-color:darkgrey; height:100%; } +#sidenav a p { + color: #FF7200; + transition:0.5s; +} +#sidenav a:hover p { + color: #FFF; +} #body { margin-left:200px; } From 7536469649b912f0dd3d3fb26f4eea29fa89f0cf Mon Sep 17 00:00:00 2001 From: Danielv123 Date: Tue, 23 May 2017 14:22:01 +0200 Subject: [PATCH 16/29] Link to nodeDetails from /slaves/ --- static/nodeDetails.css | 7 ------- static/slaveDisplay.js | 2 +- static/style.css | 7 +++++++ 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/static/nodeDetails.css b/static/nodeDetails.css index 2916ae75..d6853791 100644 --- a/static/nodeDetails.css +++ b/static/nodeDetails.css @@ -16,13 +16,6 @@ #displayBody { margin-left: 16px; } -a { - color: #FF7200; - text-decoration: none; -} -a:hover { - color: #FFF; -} #hero { background-color: #444; height:400px; diff --git a/static/slaveDisplay.js b/static/slaveDisplay.js index 0045fad7..959491b5 100644 --- a/static/slaveDisplay.js +++ b/static/slaveDisplay.js @@ -19,7 +19,7 @@ setTimeout(function() { } HTML += "
    "; HTML += '
    '; - HTML += "

    " + slaveData[key].instanceName + "

    ID: " + slaveData[key].unique + "

    Last seen: "+seenDate+"

    Online players: "+slaveData[key].playerCount+"

    IP: "+slaveData[key].publicIP +":"+ slaveData[key].serverPort+"

    " + HTML += "

    " + slaveData[key].instanceName + "

    ID: " + slaveData[key].unique + "

    Last seen: "+seenDate+"

    Online players: "+slaveData[key].playerCount+"

    IP: "+slaveData[key].publicIP +":"+ slaveData[key].serverPort+"

    " HTML += "

    Host: "+slaveData[key].mac+"

    "; HTML += ""; HTML += ""; diff --git a/static/style.css b/static/style.css index c4ca38c5..6b4f4493 100644 --- a/static/style.css +++ b/static/style.css @@ -23,6 +23,13 @@ #sidenav a:hover p { color: #FFF; } +a { + color: #FF7200; + text-decoration: none; +} +a:hover { + color: #FFF; +} #body { margin-left:200px; } From 4969c6041b08f47246134c54624b0cc270946313 Mon Sep 17 00:00:00 2001 From: Danielv123 Date: Tue, 23 May 2017 15:28:07 +0200 Subject: [PATCH 17/29] Fixed style that broke on other pages --- static/nodeDetails.css | 4 ---- static/sidebar.html | 3 --- static/style.css | 19 ++++++++++++++++--- 3 files changed, 16 insertions(+), 10 deletions(-) diff --git a/static/nodeDetails.css b/static/nodeDetails.css index d6853791..67b8d4d7 100644 --- a/static/nodeDetails.css +++ b/static/nodeDetails.css @@ -1,7 +1,3 @@ -*{ - color: #FFF; -} - #header .subbar h6 { display:inline; font-weight:normal; diff --git a/static/sidebar.html b/static/sidebar.html index 60111b51..b24a4d2e 100644 --- a/static/sidebar.html +++ b/static/sidebar.html @@ -11,9 +11,6 @@ - diff --git a/static/style.css b/static/style.css index 6b4f4493..8de8dd40 100644 --- a/static/style.css +++ b/static/style.css @@ -1,7 +1,10 @@ * { margin:0px; + color: #FFF; +} +body { + background-color: #242B30; } - #logo { /* using same colors as factorios official ones, taken from http://www.lua-api.factorio.com */ @@ -55,7 +58,7 @@ a:hover { float:left; width:100%; /*height:110px;*/ - background-color:#EEE; + background-color:#444; /*border-radius: 10px; margin-left:5px; margin-right:5px;*/ @@ -63,8 +66,18 @@ a:hover { .slaveBox > p { margin:5px; } -.slaveBox > h2 { +.slaveBox h2 { margin:5px; + color: inherit; + transition:0.2s; +} +.slaveBox > button { + background-color: transparent; + color: #FF7200; + transition: 0.2s; +} +.slaveBox > button:hover { + color: #FFF; } .productionGraph { float:right; From 4dee4ed84e8553803e17e3077a756c8d5453d434 Mon Sep 17 00:00:00 2001 From: Danielv123 Date: Tue, 23 May 2017 15:35:17 +0200 Subject: [PATCH 18/29] Extended gitignore, removed database --- .gitignore | 4 +- database/items.db | 94 ----------------------------------------------- 2 files changed, 3 insertions(+), 95 deletions(-) delete mode 100644 database/items.db diff --git a/.gitignore b/.gitignore index 9506bc24..9b288416 100644 --- a/.gitignore +++ b/.gitignore @@ -31,4 +31,6 @@ Factorio* /instances /sharedMods /database -/sharedPlugins \ No newline at end of file +/sharedPlugins +database/items.db +config.json \ No newline at end of file diff --git a/database/items.db b/database/items.db deleted file mode 100644 index 4355854e..00000000 --- a/database/items.db +++ /dev/null @@ -1,94 +0,0 @@ -{"name":"combat-shotgun","count":"1","_id":"01jLIbbQ2ZXo8xet"} -{"name":"crude-oil","count":3399414,"_id":"0z6ahVSklQcCuOmf"} -{"name":"sulfur","count":1032,"_id":"23SHYRpD6xVRNhaO"} -{"name":"science-pack-3","count":5002,"_id":"25wXED3PWYD9qk2r"} -{"name":"copper-plate","count":6144494,"_id":"2CawU9P5jgZHHiUU"} -{"name":"copper-ore","count":"102","_id":"2DYQES3PaVPuYyal"} -{"name":"stone","count":28,"_id":"2aCh9XADK403IjMu"} -{"name":"battery","count":53293,"_id":"4wzpuWq4UJGgGB0w"} -{"name":"substation","count":1035,"_id":"6EvWa5sr7aOfsOmh"} -{"name":"storage-tank","count":"4","_id":"6L8D2UUQGpqoeSyV"} -{"name":"small-pump","count":250,"_id":"6Y5Gdm7fANYWCK4S"} -{"name":"energy-shield-mk2-equipment","count":26,"_id":"7uV1wOd6YpP6YBg6"} -{"name":"pipe-to-ground","count":100,"_id":"9QP4hAy3sJkpDCrZ"} -{"name":"pipe","count":1004,"_id":"AHkpkp67PHoT2Ip8"} -{"name":"fast-inserter","count":"132","_id":"ALVxvbF1YpOzGQSI"} -{"name":"solid-fuel","count":70,"_id":"APqiAHT3a3sJRF7U"} -{"name":"chemical-plant","count":501,"_id":"CGwD3PkkPMqsCQBJ"} -{"name":"splitter","count":1008,"_id":"D6pu2w2C3tEpJSrY"} -{"name":"rail","count":2504,"_id":"DOIN6qxET53zLjAR"} -{"name":"small-electric-pole","count":55,"_id":"Dy4cKwfZtwB2pjeF"} -{"name":"battery-mk2-equipment","count":27,"_id":"E84qUcptl6eH4D14"} -{"name":"cargo-wagon","count":21,"_id":"EWkaGca01NYDnyrt"} -{"name":"steel-plate","count":1594404,"_id":"F5wIdk2J2O5Jfp64"} -{"name":"science-pack-2","count":108672,"_id":"HkKc8AFK9INvlxnD"} -{"name":"green-wire","count":"36","_id":"HoIHkVMIMSMXlfPW"} -{"name":"beacon","count":"10","_id":"ImP3SlS0bfO8kaip"} -{"name":"fusion-reactor-equipment","count":7,"_id":"JynGGRC3wgbjG1xK"} -{"name":"big-electric-pole","count":1005,"_id":"KvOb3foDB5Px2jCL"} -{"name":"solar-panel","count":1023,"_id":"LQ70r6rrvPQ1twsB"} -{"name":"productivity-module-2","count":254,"_id":"LWmYiJ4zAOcdEgRY"} -{"name":"science-pack-1","count":139816,"_id":"LXBJ6Ns3IvDtBq5Y"} -{"name":"advanced-circuit","count":645071,"_id":"LdCXjtKXYPpmmLJf"} -{"name":"electric-mining-drill","count":509,"_id":"MC0sUIbd4bNPyiWV"} -{"name":"iron-ore","count":"52","_id":"MQZOQJ132g92DlEg"} -{"name":"water","count":50058,"_id":"MTEnmbNGHKSE8Gy3"} -{"name":"destroyer-capsule","count":"100","_id":"MlXqRhCD4UZETrdK"} -{"name":"iron-gear-wheel","count":"180","_id":"NsPHgS3B2qaKmyap"} -{"name":"speed-module-2","count":254,"_id":"Nxz2ZKMbcyiqUWd1"} -{"name":"burner-inserter","count":94,"_id":"Ouwv7ZlKhaz00FAt"} -{"name":"logistic-chest-requester","count":257,"_id":"PNLjyvBGrb4Gci5G"} -{"name":"express-splitter","count":1010,"_id":"PT7b3VbWa47lJJNI"} -{"name":"piercing-shotgun-shell","count":"70","_id":"PfsRTwLQLwsnZK6F"} -{"name":"solar-panel-equipment","count":18,"_id":"S29hMsueEXwICCvp"} -{"name":"personal-roboport-equipment","count":26,"_id":"STElkhgN3HjQ9LKj"} -{"name":"logistic-chest-storage","count":250,"_id":"SscVzS8jf9xk6Eat"} -{"name":"stone-furnace","count":1001,"_id":"TIONNgEB1OcvuR4m"} -{"name":"iron-plate","count":25036,"_id":"TKUNsKLhzi9wbXLk"} -{"name":"assembling-machine-1","count":"50","_id":"VAjgjWvWSDLzJhcz"} -{"name":"rail-chain-signal","count":"97","_id":"W8KAv7VO9viuCuLO"} -{"name":"constant-combinator","count":"5","_id":"WnaALSqOZ6sHra99"} -{"name":"coal","count":853334,"_id":"WxJKZfXyX8c0rHWc"} -{"name":"red-wire","count":"13","_id":"XprwdphPUrihfASQ"} -{"name":"effectivity-module-2","count":286,"_id":"XwITKWqPlpxkAYxo"} -{"name":"logistic-chest-passive-provider","count":253,"_id":"Yr4v7SlUGXQWouxJ"} -{"name":"fast-splitter","count":1016,"_id":"YrzmqfD7u7fZp34C"} -{"name":"plastic-bar","count":50060,"_id":"Yw2qmHm6SH5dyTxs"} -{"name":"pumpjack","count":404,"_id":"Z0plqSsfLfAl9YYa"} -{"name":"small-lamp","count":"50","_id":"Zd3H118Nc2u7HmeP"} -{"name":"fast-transport-belt","count":1042,"_id":"ZmqzOJF4tVJEzgUW"} -{"name":"medium-electric-pole","count":500,"_id":"aPqWD0jxmTv8aac7"} -{"name":"electric-furnace","count":200,"_id":"bBvxS6tFmo4yCFEE"} -{"name":"raw-wood","count":22912,"_id":"d85jLuxP9EaWmHe9"} -{"name":"laser-turret","count":252,"_id":"f9MItTJWZYfsUc9f"} -{"name":"deconstruction-planner","count":10,"_id":"fmj6YZnuuinVPP4q"} -{"name":"roboport","count":25,"_id":"ijpo46GFEzZJdZDG"} -{"name":"construction-robot","count":1026,"_id":"ipGZYHapBK3gmsaV"} -{"name":"electronic-circuit","count":3768586,"_id":"iq5rntWyvPGJqWUr"} -{"name":"express-transport-belt","count":1017,"_id":"iyrTNKNZCap5CUfn"} -{"name":"boiler","count":1011,"_id":"j2UJIu2uH01RXpgo"} -{"name":"repair-pack","count":"14","_id":"jzlBCjlkSHXUwQaq"} -{"name":"blueprint","count":12,"_id":"k30PZakZ4PBVFB32"} -{"name":"stone-wall","count":551,"_id":"kjaR0wHDPqIwZNxN"} -{"name":"electric-engine-unit","count":252,"_id":"lSWfvV27887gKucW"} -{"name":"decider-combinator","count":"1","_id":"oRrRtX53cbXqkhJF"} -{"name":"petroleum-gas","count":7582906,"_id":"ovOahSJQhBOQf0sL"} -{"name":"transport-belt","count":1011,"_id":"pBF2CkAB1AFexcZE"} -{"name":"assembling-machine-3","count":293,"_id":"pevQK7kAM5v7ObfG"} -{"name":"alien-science-pack","count":"415","_id":"qlkibcDNC0OgBuy7"} -{"name":"assembling-machine-2","count":200,"_id":"rUdnyKLPhZvMoRJ8"} -{"name":"steam-engine","count":1000,"_id":"rmEFJ9OuEIn91mLx"} -{"name":"accumulator","count":1005,"_id":"tCrPS64NshuzFHtW"} -{"name":"raw-fish","count":"11","_id":"uVQshHGwOWGwIMux"} -{"name":"underground-belt","count":1008,"_id":"v1qaz21iokoxERyl"} -{"name":"diesel-locomotive","count":20,"_id":"vmyqu0WI3k1XIdlu"} -{"name":"express-underground-belt","count":1012,"_id":"vpm5zw4BlnfErJAo"} -{"name":"piercing-rounds-magazine","count":5030,"_id":"wJpfX5g4HgCyRiAa"} -{"name":"processing-unit","count":108,"_id":"wXjUBq0KIksTJcwy"} -{"name":"power-armor-mk2","count":5,"_id":"wmItTXh1bEcysw86"} -{"name":"logistic-robot","count":1048,"_id":"x2cRxrpdC7BHJFpx"} -{"name":"fast-underground-belt","count":1010,"_id":"xONgtmV0OoxKmzVv"} -{"name":"engine-unit","count":254,"_id":"y9XyUryrxgsucFJc"} -{"name":"grenade","count":"30","_id":"ygLIdsWl9JFV2aZ6"} -{"name":"concrete","count":1039,"_id":"ym8LemdTCsDO52s8"} -{"name":"logistic-chest-active-provider","count":252,"_id":"zQZ9UP3qyg6Hx9jO"} From f8749fb8d46986c9a289fc0bb2162a0d4db218b8 Mon Sep 17 00:00:00 2001 From: Danielv123 Date: Tue, 23 May 2017 15:45:34 +0200 Subject: [PATCH 19/29] Made tests linux compatible --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 012ddf23..3dfc8462 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "main": "master.js", "scripts": { "test": "npm run test-unit", - "test-unit": "SET NODE_ENV=test && mocha --recursive \"./{,!(node_modules)/**/}*.spec.js\" || echo .", + "test-unit": "mocha --recursive \"./{,!(node_modules)/**/}*.spec.js\" || echo .", "start": "echo Run node client.js start [instance] to start slaves && node master.js", "cover": "./node_modules/.bin/istanbul cover ./node_modules/mocha/bin/_mocha -- lib/*.js -R spec" }, From 70ecdf010c4b248121241bbff139ff93a19a8de5 Mon Sep 17 00:00:00 2001 From: Danielv123 Date: Wed, 24 May 2017 22:33:15 +0200 Subject: [PATCH 20/29] Renamed lib because thats reasonable --- client.js | 5 +++-- lib/{libomega.js => objectOps.js} | 0 lib/{libomega.spec.js => objectOps.spec.js} | 16 ++++++++-------- 3 files changed, 11 insertions(+), 10 deletions(-) rename lib/{libomega.js => objectOps.js} (100%) rename lib/{libomega.spec.js => objectOps.spec.js} (76%) diff --git a/client.js b/client.js index 7d3dc780..e8a6f506 100644 --- a/client.js +++ b/client.js @@ -11,7 +11,7 @@ var hashFiles = require('hash-files'); var _ = require('underscore'); // internal libraries -var libomega = require("./lib/libomega.js"); +var objectOps = require("./lib/objectOps.js"); var fileOps = require("./lib/fileOps.js"); // require config.json @@ -486,7 +486,8 @@ function instanceManagement() { for(let key in flowStat1) totalFlows[key] = flowStat1[key]; for(let key in flowStat2) totalFlows[key] = flowStat2[key]; if(oldFlowStats && totalFlows && oldTimestamp) { - let payload = libomega.clone(totalFlows); + // todo: Migrate this to JSON.parse(JSON.stringify()) + let payload = objectOps.clone(totalFlows); // change from total reported to per time unit for(let key in oldFlowStats) { // get production per minute diff --git a/lib/libomega.js b/lib/objectOps.js similarity index 100% rename from lib/libomega.js rename to lib/objectOps.js diff --git a/lib/libomega.spec.js b/lib/objectOps.spec.js similarity index 76% rename from lib/libomega.spec.js rename to lib/objectOps.spec.js index 5ee04baa..2e16ee64 100644 --- a/lib/libomega.spec.js +++ b/lib/objectOps.spec.js @@ -1,12 +1,12 @@ var assert = require('assert'); -var libomega = require('./libomega'); +var objectOps = require('./objectOps'); -describe("libomega.js", function(){ - describe("libomega.clone()", function(){ +describe("objectOps.js", function(){ + describe("objectOps.clone()", function(){ it("clones javascript objects", function(){ var object1 = {hello:"world", cat:{legs:4, name:"Kitty", colors:["brown", "yellow", "purple"]}}; var object2 = {}; - object2 = libomega.clone(object1); + object2 = objectOps.clone(object1); assert.equal(object2.cat.name, object1.cat.name) assert.equal(object1.hello, object2.hello) @@ -14,18 +14,18 @@ describe("libomega.js", function(){ it("does not deep clone objects", function(){ var obj1 = {hello:"world", cat:{legs:4, name:"Kitty", colors:["brown", "yellow", "purple"]}}; var obj2 = {}; - obj2 = libomega.clone(obj1); + obj2 = objectOps.clone(obj1); obj1.cat.colors = "black"; assert.equal(obj1.cat.colors, "black"); assert.notEqual(obj2.cat.colors[1], "yellow"); }); }); - describe("libomega.deepclone()", function(){ + describe("objectOps.deepclone()", function(){ it("deep clones javascript objects", function(){ var obj1 = {hello:"world", cat:{legs:4, name:"Kitty", colors:["brown", "yellow", "purple"]}}; var obj2 = {}; - obj2 = libomega.deepclone(obj1); + obj2 = objectOps.deepclone(obj1); obj1.cat.colors = "black"; assert.equal(obj1.cat.colors, "black"); @@ -34,7 +34,7 @@ describe("libomega.js", function(){ it("throws on non JSON parameters", function(){ x = function(){}; assert.throws(function(){ - y = libomega.deepclone(x) + y = objectOps.deepclone(x) }); }); }); From 74348be0a8f88b12ae421ed1eeb9322683c0cdfa Mon Sep 17 00:00:00 2001 From: Danielv123 Date: Wed, 24 May 2017 22:34:03 +0200 Subject: [PATCH 21/29] slaveDetails prod chart, playerCount, last online date --- static/head.html | 1 + static/nodeDetails.css | 6 +++ static/nodeDetails.js | 117 +++++++++++++++++++++++++++++++++++++++-- static/style.css | 7 +++ 4 files changed, 127 insertions(+), 4 deletions(-) diff --git a/static/head.html b/static/head.html index 7c1c12ae..52dfa7b2 100644 --- a/static/head.html +++ b/static/head.html @@ -1,5 +1,6 @@ Clusterio + \ No newline at end of file diff --git a/static/nodeDetails.css b/static/nodeDetails.css index 67b8d4d7..1983b1db 100644 --- a/static/nodeDetails.css +++ b/static/nodeDetails.css @@ -31,6 +31,12 @@ display:inline-block; border-radius:16px; } +.productionGraph { + background-color:black; +} +#leftHeroContainer { + float:left; +} ul { width:300px; list-style:none; diff --git a/static/nodeDetails.js b/static/nodeDetails.js index 756ed00d..805dc8ac 100644 --- a/static/nodeDetails.js +++ b/static/nodeDetails.js @@ -26,6 +26,13 @@ function getJSON(url, callback) { }; xhr.send(); }; +// function to sort arrays of objects after a keys value +function sortByKey(array, key) { + return array.sort(function(a, b) { + var x = Number(a[key]); var y = Number(b[key]); + return ((x < y) ? 1 : ((x > y) ? -1 : 0)); + }); +} function getParameterByName(name, url) { if (!url) url = window.location.href; name = name.replace(/[\[\]]/g, "\\$&"); @@ -68,20 +75,122 @@ function populateSlaveInfo(){ */ let slave = slaveData[slaveID]; let HTML = "" // end of header - HTML += "

    Last seen: "+slave.time+"

    " + HTML += "
    Host: "+slave.mac+"
    Unique: "+slave.unique+"
    " + if(slave.publicIP != "localhost"){ + HTML += "
    IP: "+slave.publicIP+":"+slave.serverPort+"
    "; + } else { + HTML += "
    This server is not configured for incoming connections
    " + } + HTML += "
    " // end of header + // left container + HTML += "
    " + HTML += "

    Last seen: "+moment(Number(slave.time)).fromNow()+"

    " + HTML += "

    Online players: "+slave.playerCount+"

    " HTML += "
    " // end of displayBody + // list mods and other metadata HTML += "

    Mods

      " for(let i = 0; i < slave.mods.length; i++){ HTML += "
    • "+slave.mods[i].modName+"
    • " } + HTML += "
    " + + HTML += "
    " // end of left container + + // chart + HTML += '
    ' document.querySelector("#hero").innerHTML = HTML; - + makeGraph(slave.unique, slave.unique) }); } populateSlaveInfo(); + +// ID of slave, ID of canvasjs div without # +function makeGraph(slaveID, selector) { + post("api/getStats", {slaveID: slaveID}, function(data){ + //console.log("Building chart " + slaveID + " with this data:") + //console.log(data) + if(data.length > 0) { + // find keys + let itemNames = []; + for(let key in data[data.length - 1].data) { + itemNames[itemNames.length] = key + } + let chartData = []; + for(let o = 0; o < itemNames.length; o++) { + if(itemNames[o] != "water"){ + chartData[chartData.length] = generateLineChartArray(data, itemNames[o]); + } + } + //console.log(chartData) + drawChart(selector, chartData) + } + // callback(data); + }) +} + +function generateLineChartArray(data, nameKey) { + let chartData = []; + for(let i = 0; i < data.length; i++) { + // only show recent data + if(data[i].timestamp > Date.now() - (24*60*60*1000)){ + let y = data[i].data[nameKey] + if(!data[i].data[nameKey]) { + y = 0; + } else if(y < 0) { + y = 0; + } + chartData[chartData.length] = { + x: new Date(data[i].timestamp), + y: Number(y) + } + } + } + chartData = sortByKey(chartData, "x"); + let xyz = {}; + xyz.name = nameKey; + xyz.type = "line"; + if(nameKey == "copper-wire"||nameKey == "iron-plate"||nameKey == "copper-plate"||nameKey == "electronic-circuit"||nameKey == "steel-plate"||nameKey == "advanced-circuit"||nameKey == "crude-oil"||nameKey == "petroleum-gas"){ + xyz.showInLegend = true; + } + xyz.dataPoints = chartData; + return xyz; +} + +var chartsByID = {}; +function drawChart(selector, chartData) { + // selector is ID of element, ex "chartContainer" or "-123199123" + console.log(chartData) + chartsByID[selector] = new CanvasJS.Chart(selector, { + title:{ + text: "Production graph" + }, + toolTip:{ + content: "{name}: {y}" + }, + zoomEnabled: true, + axisY:{ + includeZero: true, + }, + legend: { + cursor: "pointer", + itemclick: function (e) { + //console.log("legend click: " + e.dataPointIndex); + //console.log(e); + if (typeof (e.dataSeries.visible) === "undefined" || e.dataSeries.visible) { + e.dataSeries.visible = false; + } else { + e.dataSeries.visible = true; + } + e.chart.render(); + } + }, + data: chartData + }); + chart = chartsByID[selector]; + // console.log(chart) + chart.render(); +} \ No newline at end of file diff --git a/static/style.css b/static/style.css index 8de8dd40..73492c90 100644 --- a/static/style.css +++ b/static/style.css @@ -5,6 +5,13 @@ body { background-color: #242B30; } +button { + border-radius: 0; + border: 1px solid transparent; + border-color: #FF7200; + border-width:1px; + cursor: pointer; +} #logo { /* using same colors as factorios official ones, taken from http://www.lua-api.factorio.com */ From b520d9611ba60a78b9bb49aeb55e5cd91b3fbe83 Mon Sep 17 00:00:00 2001 From: Danielv123 Date: Fri, 26 May 2017 09:06:04 +0200 Subject: [PATCH 22/29] Red/green/yellow dot to tell slave status on /nodeDetails/ --- static/nodeDetails.js | 47 ++++++++++++++++++++++++++++++++----------- 1 file changed, 35 insertions(+), 12 deletions(-) diff --git a/static/nodeDetails.js b/static/nodeDetails.js index 805dc8ac..ec4c47b4 100644 --- a/static/nodeDetails.js +++ b/static/nodeDetails.js @@ -12,19 +12,24 @@ function post(url, data, callback) { } xhr.send(JSON.stringify(data)); } +// callback(err, json) function getJSON(url, callback) { - var xhr = new XMLHttpRequest(); - xhr.open('GET', url, true); - xhr.responseType = 'json'; - xhr.onload = function() { - var status = xhr.status; - if (status == 200) { - callback(null, xhr.response); - } else { - callback(status); - } - }; - xhr.send(); + var xhr = new XMLHttpRequest(); + xhr.open('GET', url, true); + xhr.responseType = 'json'; + xhr.onload = function() { + var status = xhr.status; + if (status == 200) { + callback(null, xhr.response); + } else { + callback(status); + } + }; + // triggers if connection is refused + xhr.onerror = function(e){ + callback(e); + }; + xhr.send(); }; // function to sort arrays of objects after a keys value function sortByKey(array, key) { @@ -43,6 +48,24 @@ function getParameterByName(name, url) { return decodeURIComponent(results[2].replace(/\+/g, " ")); } + + + +/// code below here +// update the online indicator dot +setInterval(function(){ + getJSON("/api/slaves", function(err, slaveData){ + if(err){ + document.querySelector("#onlineIndicator").style.backgroundColor = "yellow"; + // compare with Math.floor(x/100000) allows us to check if they are within 10s of each other + } else if(Math.floor(slaveData[getParameterByName("slaveID")].time/100000) == Math.floor(Date.now()/100000)){ + document.querySelector("#onlineIndicator").style.backgroundColor = "green"; + } else { + document.querySelector("#onlineIndicator").style.backgroundColor = "red"; + } + }); +}, 1000); + function populateSlaveInfo(){ let slaveID = getParameterByName("slaveID"); if(!slaveID){ From bafc8dfc44019bb316708dced48e51674283c16c Mon Sep 17 00:00:00 2001 From: Danielv123 Date: Fri, 26 May 2017 09:25:07 +0200 Subject: [PATCH 23/29] /inventory/ now has a search field --- static/index.ejs | 1 + static/storage.js | 5 ++++- static/style.css | 8 ++++++-- 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/static/index.ejs b/static/index.ejs index f8f0ebb9..03a05b22 100644 --- a/static/index.ejs +++ b/static/index.ejs @@ -9,6 +9,7 @@

    Storage

    +
    diff --git a/static/storage.js b/static/storage.js index 83e5930f..97d184b7 100644 --- a/static/storage.js +++ b/static/storage.js @@ -11,7 +11,10 @@ function drawcontents(data) { } else { img = "https://wiki.factorio.com/images/" + capitalizeFirstLetter(data[i].name) + ".png"; } - result = result + "" + data[i].name + "" + data[i].count + ""; + var searchField = document.querySelector("#search"); + if(!searchField.value || data[i].name.includes(searchField.value)) { + result = result + "" + data[i].name + "" + data[i].count + ""; + } } result = result + "" document.getElementById("contents").innerHTML = result; diff --git a/static/style.css b/static/style.css index 73492c90..7fdd5b44 100644 --- a/static/style.css +++ b/static/style.css @@ -17,8 +17,12 @@ button { http://www.lua-api.factorio.com */ background-color: #262626; color: #C8C8C8; -} - +} +/* search field */ +#storage > input { + background-color:black; + width:300px; +} #sidenav { position:fixed; left:0px; From 960cd79be38c8fdd64896671fe2ed0e506f22c38 Mon Sep 17 00:00:00 2001 From: Danielv123 Date: Fri, 26 May 2017 15:25:49 +0200 Subject: [PATCH 24/29] fix /api/editSlaveMeta creating new slaves when it shouldn't --- master.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/master.js b/master.js index e08f7977..bf8b9c44 100644 --- a/master.js +++ b/master.js @@ -88,7 +88,7 @@ app.post("/api/getID", function(req,res) { console.log("Slave registered: " + slaves[req.body.unique].mac + " : " + slaves[req.body.unique].serverPort+" at " + slaves[req.body.unique].publicIP + " with name " + slaves[req.body.unique].instanceName); } }); -// todo: Write docs for this + app.post("/api/editSlaveMeta", function(req,res) { // request.body should be an object // {slaveID, pass, meta:{x,y,z}} @@ -104,7 +104,6 @@ app.post("/api/editSlaveMeta", function(req,res) { } else { res.send("ERROR: Invalid slaveID or password") } - slaves[req.body.slaveID] = req.body; } }); // mod management From 844bfebcd7b0312b35f5a9b078cbe9c61f9f1c91 Mon Sep 17 00:00:00 2001 From: Danielv123 Date: Fri, 26 May 2017 19:50:33 +0200 Subject: [PATCH 25/29] UPS display plugin that shows on master --- client.js | 9 +++++++ sharedPlugins/UPSdisplay/config.js | 18 ++++++++++++++ sharedPlugins/UPSdisplay/index.js | 38 ++++++++++++++++++++++++++++++ static/nodeDetails.js | 15 ++++++++++++ 4 files changed, 80 insertions(+) create mode 100644 sharedPlugins/UPSdisplay/config.js create mode 100644 sharedPlugins/UPSdisplay/index.js diff --git a/client.js b/client.js index e8a6f506..6570e837 100644 --- a/client.js +++ b/client.js @@ -9,6 +9,7 @@ var ncp = require('ncp').ncp; var Rcon = require('simple-rcon'); var hashFiles = require('hash-files'); var _ = require('underscore'); +var deepmerge = require("deepmerge"); // internal libraries var objectOps = require("./lib/objectOps.js"); @@ -352,6 +353,14 @@ function instanceManagement() { } }); } + // these are our two config files. We need to send these in case plugin + // wants to contact master or know something. + let combinedConfig = deepmerge(instanceconfig,config,{clone:true}) + // send through script-output file, maybe more compat? + fs.writeFileSync(instancedirectory + "/script-output/" + pluginConfig.scriptOutputFileSubscription, JSON.stringify(combinedConfig)+"\r\n"); + // send directly through stdin + // plugins[i].stdin.write(JSON.stringify(combinedConfig)+"\n"); + console.log("Clusterio | Loaded plugin " + pluginDirectories[i]); plugins[i].stdout.on("data", (data) => { if(data.toString('utf8')[0] != "/") { diff --git a/sharedPlugins/UPSdisplay/config.js b/sharedPlugins/UPSdisplay/config.js new file mode 100644 index 00000000..8b2e6d16 --- /dev/null +++ b/sharedPlugins/UPSdisplay/config.js @@ -0,0 +1,18 @@ +/* + Example config file for example Clusterio plugin + Binary will be spawned using child_process.spawn and get input from stdin and text from stdout will be ran as + commands in the factorio server. +*/ + +module.exports = { + // Name of package. For display somewhere I guess. + name: "UPSdisplay", + version: "1.0.0", + // Binary entrypoint for plugin. Don't let it crash. Stdout is sent to game as server chat (run /c commands from here for interface) + // Make sure its cross platform somehow. + binary: "node", + arguments: ["index.js"], + description: "Internal clusterio plugin to get the worlds UPS and display it on master", + // We'll send everything in this file to your stdin. Beware. + scriptOutputFileSubscription: "UPSdisplay.txt", +} \ No newline at end of file diff --git a/sharedPlugins/UPSdisplay/index.js b/sharedPlugins/UPSdisplay/index.js new file mode 100644 index 00000000..05dfab0f --- /dev/null +++ b/sharedPlugins/UPSdisplay/index.js @@ -0,0 +1,38 @@ +const needle = require("needle"); + +console.log("/silent-command game.print('UPSdisplay enabled')\n"); + +setInterval(function(){ + console.log("/silent-command game.write_file('UPSdisplay.txt', game.tick, true, 0)\n"); +},1000); +var config = {}; +process.stdin.setEncoding('utf8'); +let historicalTicks = []; +process.stdin.on('readable', () => { + var chunk = process.stdin.read(); + if (chunk !== null && !isNaN(chunk) && !chunk.includes("{")){ + // console.log("Tick: " + chunk); + historicalTicks[historicalTicks.length] = {tick:Number(chunk), timestamp:Date.now()}; + if(historicalTicks.length > 30){ + historicalTicks.shift(); // delete last element in array (position 0); + } + } else { + config = JSON.parse(chunk); + } +}); +process.stdin.on('end', () => { + process.stdout.write('end'); +}); + +// Post average UPS +setInterval(function(){ + if(historicalTicks && historicalTicks[0]){ + let timePeriod = historicalTicks[historicalTicks.length-1].timestamp - historicalTicks[0].timestamp; + let ticksInPeriod = historicalTicks[historicalTicks.length-1].tick - historicalTicks[0].tick; + let UPS = Math.round(ticksInPeriod / (timePeriod/1000)); + // console.log("UPS: " + UPS); + needle.post(config.masterIP+':'+config.masterPort+'/api/editSlaveMeta', {slaveID: config.unique, password: config.clientPassword, meta: {UPS:UPS}}, function(err, resp) { + // success? + }); + } +}, 10000); \ No newline at end of file diff --git a/static/nodeDetails.js b/static/nodeDetails.js index ec4c47b4..6165fe87 100644 --- a/static/nodeDetails.js +++ b/static/nodeDetails.js @@ -56,6 +56,7 @@ function getParameterByName(name, url) { setInterval(function(){ getJSON("/api/slaves", function(err, slaveData){ if(err){ + // our request threw an error, most likely master is unavailable so we show yellow indicator document.querySelector("#onlineIndicator").style.backgroundColor = "yellow"; // compare with Math.floor(x/100000) allows us to check if they are within 10s of each other } else if(Math.floor(slaveData[getParameterByName("slaveID")].time/100000) == Math.floor(Date.now()/100000)){ @@ -124,6 +125,20 @@ function populateSlaveInfo(){ HTML += '
    ' document.querySelector("#hero").innerHTML = HTML; + + // slaveDetails + HTML = ""; + if(slave.meta) + for(let key in slave.meta){ + let t = slave.meta[key]; + if(typeof t == "string"){ + HTML += "

    "+key+": "+t+"

    "; + } + } + + document.querySelector("#body > #details").innerHTML = HTML; + + // make production graph makeGraph(slave.unique, slave.unique) }); From 21d87a8cd104ce793b9e2d2373cf64c2627b0265 Mon Sep 17 00:00:00 2001 From: Danielv123 Date: Fri, 26 May 2017 21:17:24 +0200 Subject: [PATCH 26/29] Housekeeping --- lib/objectOps.js | 2 +- static/style.css | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/objectOps.js b/lib/objectOps.js index d60ace92..3bf9e988 100644 --- a/lib/objectOps.js +++ b/lib/objectOps.js @@ -1,4 +1,4 @@ -function copyChildren(d){ +var copyChildren = function(d){ var copied = clone(d); copy = []; if(copied.children){ diff --git a/static/style.css b/static/style.css index 7fdd5b44..5156f4f4 100644 --- a/static/style.css +++ b/static/style.css @@ -3,7 +3,8 @@ color: #FFF; } body { - background-color: #242B30; + /* same as http://www.lua-api.factorio.com/latest */ + background-color: rgb(38, 38, 38); } button { border-radius: 0; @@ -23,6 +24,7 @@ button { background-color:black; width:300px; } +/* Navbar on the right */ #sidenav { position:fixed; left:0px; @@ -44,12 +46,14 @@ a { a:hover { color: #FFF; } +/* ensure body does not overlap navbar on the right */ #body { margin-left:200px; } #body > #production { display:block; } +/* Inventory display table */ #contents { width:300px; } From 4155ea7916b44fa6a4584c41919f385dda685194 Mon Sep 17 00:00:00 2001 From: Danielv123 Date: Mon, 29 May 2017 11:23:53 +0200 Subject: [PATCH 27/29] Save slaves between master reboots --- master.js | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/master.js b/master.js index bf8b9c44..225673ee 100644 --- a/master.js +++ b/master.js @@ -67,7 +67,33 @@ db.items.additem = function(object) { } }); } -var slaves = {} + +// store slaves in a .json full of JSON data +process.on('SIGINT', function () { + console.log('Ctrl-C...'); + // set insane limit to slave length, if its longer than this we are probably being ddosed or something + if(slaves && Object.keys(slaves).length < 50000){ + fs.writeFileSync("database/slaves.json", JSON.stringify(slaves)); + console.log("saving to slaves.json"); + } else if(slaves) { + console.log("Slave database too large, not saving ("+Object.keys(slaves).length+")"); + } + process.exit(2); +}); +var slaves = {}; +(function(){ + let x; + try{ + x = fs.statSync("database/slaves.json"); + console.log(x) + } catch (e){ + + } + if(x){ + slaves = JSON.parse(fs.readFileSync("database/slaves.json")); + console.log("parsing slaves " + slaves) + } +})() // world ID management // slaves post here to tell the server they exist app.post("/api/getID", function(req,res) { From 18b8b16f029ee5b5d9675f3a25192fa3f327a5c7 Mon Sep 17 00:00:00 2001 From: Danielv123 Date: Mon, 29 May 2017 12:39:35 +0200 Subject: [PATCH 28/29] decreased logging on master --- master.js | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/master.js b/master.js index 225673ee..0867dce5 100644 --- a/master.js +++ b/master.js @@ -1,3 +1,6 @@ +// configgy stuff +debug = false; + // constants const masterModFolder = "./database/masterMods/" @@ -85,13 +88,12 @@ var slaves = {}; let x; try{ x = fs.statSync("database/slaves.json"); - console.log(x) } catch (e){ } if(x){ + console.log("loading slaves from database/slaves.json"); slaves = JSON.parse(fs.readFileSync("database/slaves.json")); - console.log("parsing slaves " + slaves) } })() // world ID management @@ -102,7 +104,9 @@ app.post("/api/getID", function(req,res) { // time us a unix timestamp we can use to check for how long the server has been unresponsive // we should save that somewhere and give appropriate response if(req.body){ - console.log(req.body) + if(debug){ + console.log(req.body) + } if(!slaves[req.body.unique]){ slaves[req.body.unique] = {}; } @@ -126,7 +130,7 @@ app.post("/api/editSlaveMeta", function(req,res) { slaves[req.body.slaveID].meta = {}; slaves[req.body.slaveID].meta = deepmerge(slaves[req.body.slaveID].meta, req.body.meta, {clone:true}) } - console.log("Updating slave: " + req.body.mac + " : " + req.body.serverPort+" at " + req.body.publicIP); + console.log("Updating slave: " + slaves[req.body.slaveID].mac + " : " + slaves[req.body.slaveID].serverPort+" at " + slaves[req.body.slaveID].publicIP); } else { res.send("ERROR: Invalid slaveID or password") } From a0facf9ea8a47e505828b8ddb81ebe8f0a1bf3e6 Mon Sep 17 00:00:00 2001 From: Danielv123 Date: Tue, 30 May 2017 09:30:05 +0200 Subject: [PATCH 29/29] Hover text for online indicator --- static/nodeDetails.js | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/static/nodeDetails.js b/static/nodeDetails.js index 6165fe87..7bfaad78 100644 --- a/static/nodeDetails.js +++ b/static/nodeDetails.js @@ -55,14 +55,18 @@ function getParameterByName(name, url) { // update the online indicator dot setInterval(function(){ getJSON("/api/slaves", function(err, slaveData){ + let indicator = document.querySelector("#onlineIndicator"); if(err){ // our request threw an error, most likely master is unavailable so we show yellow indicator - document.querySelector("#onlineIndicator").style.backgroundColor = "yellow"; + indicator.style.backgroundColor = "yellow"; + indicator.title = "Master server unavailable"; // compare with Math.floor(x/100000) allows us to check if they are within 10s of each other } else if(Math.floor(slaveData[getParameterByName("slaveID")].time/100000) == Math.floor(Date.now()/100000)){ - document.querySelector("#onlineIndicator").style.backgroundColor = "green"; + indicator.style.backgroundColor = "green"; + indicator.title = "Slave is online"; } else { - document.querySelector("#onlineIndicator").style.backgroundColor = "red"; + indicator.style.backgroundColor = "red"; + indicator.title = "Slave is offline"; } }); }, 1000);