From 129ef8d2f8788ae449d9ad212809397a28cd4643 Mon Sep 17 00:00:00 2001 From: Danielv123 Date: Thu, 25 Aug 2016 20:50:00 +0200 Subject: [PATCH 1/4] Tried to cache rcon requests, ended up with dupe machine Me needs help, I don't understand where the game is getting free items from. --- client.js | 40 +++++++++++++++++++++++++++++++++++----- config.json | 2 +- launchEverything.bat | 2 +- 3 files changed, 37 insertions(+), 7 deletions(-) diff --git a/client.js b/client.js index 13427b87..27fc37d2 100644 --- a/client.js +++ b/client.js @@ -12,6 +12,8 @@ var client = new Rcon({ timeout: 0 }).connect(); +fs.writeFileSync(config.factorioDirectory + "/script-output/orders.txt", "") + client.on('authenticated', function() { console.log('Authenticated!'); }).on('connected', function() { @@ -22,10 +24,11 @@ client.on('authenticated', function() { client.connect(); }); +// set some globals +confirmedOrders = []; // trigger when something happens to output.txt fs.watch(config.factorioDirectory + "/script-output/output.txt", "utf8", function(eventType, filename) { - console.log('hit output') // get array of lines in file items = fs.readFileSync(config.factorioDirectory + "/script-output/output.txt", "utf8").split("\n"); // if you found anything, reset the file @@ -36,11 +39,11 @@ fs.watch(config.factorioDirectory + "/script-output/output.txt", "utf8", functio if(items[i]) { g = items[i].split(" "); g[0] = g[0].replace("\u0000", ""); - console.log(g); + console.log("exporting " + JSON.stringify(g)); // send our entity and count to the master for him to keep track of needle.post(config.masterIP + ":" + config.masterPort + '/place', {name:g[0], count:g[1]}, function(err, resp, body){ - console.log(body); + // console.log(body); }); } } @@ -53,7 +56,34 @@ fs.watch(config.factorioDirectory + "/script-output/orders.txt", "utf8", functio if(items[0]) { fs.writeFileSync(config.factorioDirectory + "/script-output/orders.txt", "") } + for(i = 0;i < items.length; i++) { + if(items[i]) { + g = items[i].split(" "); + g[0] = g[0].replace("\u0000", ""); + g[0] = g[0].replace(",", ""); + // send our entity and count to the master for him to keep track of + needle.post(config.masterIP + ":" + config.masterPort + '/remove', {name:g[0], count:g[1]}, function(err, resp, body){ + if(body == "success"){ + console.log("importing: " + JSON.stringify({[g[0]]: g[1]})); + // buffer confirmed orders + confirmedOrders[confirmedOrders.length] = {[g[0]]: g[1]} + //client.exec("/c remote.call('clusterio', 'importMany', '[" + JSON.stringify(jsonobject) + "]')") + } else { + console.log("ERROR: " + body) + } + }); + } + } + // if we got some confirmed orders + console.log("Importing " + confirmedOrders.length + " items! " + JSON.stringify(confirmedOrders)); + if(confirmedOrders[1]) { + // send our RCON command + client.exec("/c remote.call('clusterio', 'importMany', '" + JSON.stringify(confirmedOrders) + "')"); + confirmedOrders = []; + } + + /*for(i = 0;i < items.length; i++) { if(items[i]) { g = items[i].split(" "); g[0] = g[0].replace("\u0000", ""); @@ -67,5 +97,5 @@ fs.watch(config.factorioDirectory + "/script-output/orders.txt", "utf8", functio } }); } - } -}) \ No newline at end of file + }*/ +}) diff --git a/config.json b/config.json index 5315ce2d..d3b3df82 100644 --- a/config.json +++ b/config.json @@ -1,7 +1,7 @@ { "__comment": "", "__comment": "IP and port and password the factorio server runs at for rcon connection", - "clientIP": "81.167.2.56", + "clientIP": "localhost", "clientPort": "12345", "clientPassword": "123", "__comment": "Where factorio folder. Should be in the same folder as this config file.", diff --git a/launchEverything.bat b/launchEverything.bat index 1834cafd..5b985f81 100644 --- a/launchEverything.bat +++ b/launchEverything.bat @@ -1,7 +1,7 @@ cd "Factorio 0.13.9" @RD /S /Q "temp" cd "bin/x64/" -start factorio.exe --start-server-load-latest --rcon-port 12345 --rcon-password 123 --server-settings server-settings.json --no-auto-pause +start factorio.exe --start-server testmap.zip --rcon-port 12345 --rcon-password 123 --server-settings server-settings.json cd .. cd .. cd .. From 06a4d498840d933609af4281825b9023e1aa518b Mon Sep 17 00:00:00 2001 From: Danielv123 Date: Fri, 9 Sep 2016 08:25:39 +0200 Subject: [PATCH 2/4] WIP, still not working --- client.js | 58 +++++++++++++++++++++++--------------------- launchEverything.bat | 6 ++--- master.js | 46 ++++++++++++++++------------------- 3 files changed, 55 insertions(+), 55 deletions(-) diff --git a/client.js b/client.js index 27fc37d2..4d384cb2 100644 --- a/client.js +++ b/client.js @@ -48,40 +48,39 @@ fs.watch(config.factorioDirectory + "/script-output/output.txt", "utf8", functio } } }) - -fs.watch(config.factorioDirectory + "/script-output/orders.txt", "utf8", function(eventType, filename) { +setInterval(function() { // get array of lines in file items = fs.readFileSync(config.factorioDirectory + "/script-output/orders.txt", "utf8").split("\n"); // if you found anything, reset the file if(items[0]) { fs.writeFileSync(config.factorioDirectory + "/script-output/orders.txt", "") + for(i = 0;i < items.length; i++) { + if(items[i]) { + items[i] = items[i].split(" "); + items[i][0] = items[i][0].replace("\u0000", ""); + items[i][0] = items[i][0].replace(",", ""); + console.log(items) + // send our entity and count to the master for him to keep track of + needle.post(config.masterIP + ":" + config.masterPort + '/remove', {name:items[i][0], count:items[i][1]}, function(err, response, body){ + if(response && response.body === "successier"){ + console.log(response.body + " : importing: " + JSON.stringify({[items[i][0]]: items[i][1]})); + // buffer confirmed orders + confirmedOrders[confirmedOrders.length] = {[items[i][0]]: items[i][1]} + //client.exec("/c remote.call('clusterio', 'importMany', '[" + JSON.stringify(jsonobject) + "]')") + } else { + console.log("ERROR: "+JSON.stringify({[items[i][0]]: items[i][1]})); + } + }); + } + } + // if we got some confirmed orders + console.log("Importing " + confirmedOrders.length + " items! " + JSON.stringify(confirmedOrders)); + sadas = JSON.stringify(confirmedOrders) + confirmedOrders = []; + // send our RCON command + client.exec("/c remote.call('clusterio', 'importMany', '" + sadas + "')"); } - for(i = 0;i < items.length; i++) { - if(items[i]) { - g = items[i].split(" "); - g[0] = g[0].replace("\u0000", ""); - g[0] = g[0].replace(",", ""); - // send our entity and count to the master for him to keep track of - needle.post(config.masterIP + ":" + config.masterPort + '/remove', {name:g[0], count:g[1]}, function(err, resp, body){ - if(body == "success"){ - console.log("importing: " + JSON.stringify({[g[0]]: g[1]})); - // buffer confirmed orders - confirmedOrders[confirmedOrders.length] = {[g[0]]: g[1]} - //client.exec("/c remote.call('clusterio', 'importMany', '[" + JSON.stringify(jsonobject) + "]')") - } else { - console.log("ERROR: " + body) - } - }); - } - } - // if we got some confirmed orders - console.log("Importing " + confirmedOrders.length + " items! " + JSON.stringify(confirmedOrders)); - if(confirmedOrders[1]) { - // send our RCON command - client.exec("/c remote.call('clusterio', 'importMany', '" + JSON.stringify(confirmedOrders) + "')"); - confirmedOrders = []; - } /*for(i = 0;i < items.length; i++) { if(items[i]) { @@ -98,4 +97,9 @@ fs.watch(config.factorioDirectory + "/script-output/orders.txt", "utf8", functio }); } }*/ +}, 10000) +/* +fs.watch(config.factorioDirectory + "/script-output/orders.txt", "utf8", function(eventType, filename) { + }) +*/ \ No newline at end of file diff --git a/launchEverything.bat b/launchEverything.bat index 5b985f81..33516b3f 100644 --- a/launchEverything.bat +++ b/launchEverything.bat @@ -1,4 +1,4 @@ -cd "Factorio 0.13.9" +cd "Factorio_0.13.17" @RD /S /Q "temp" cd "bin/x64/" start factorio.exe --start-server testmap.zip --rcon-port 12345 --rcon-password 123 --server-settings server-settings.json @@ -6,8 +6,8 @@ cd .. cd .. cd .. -start cmd /C node client.js -start cmd /C node master.js +::start cmd /C node client.js +::start cmd /C node master.js ::cd "Factorio 0.13.9 - Copy/bin/x64" ::@start factorio.exe diff --git a/master.js b/master.js index c95932ef..9dc1b76d 100644 --- a/master.js +++ b/master.js @@ -40,30 +40,6 @@ db.items.additem = function(object) { } }) } -db.items.removeitem = function(object, res) { - db.items.findOne({name:object.name}, function (err, doc) { - // console.dir(doc); - if (err) { - res.send('failure'); - return false - } else { - if (doc) { - // Update existing items if item name already exists - if(Number(doc.count) > Number(object.count)) { - console.log("removed: " + object.name + " " + object.count); - object.count = Number(doc.count) - Number(object.count); - db.items.update(doc, object, {multi:true}, function (err, numReplaced) {}); - res.send("success"); - return true; - } else { - res.send('failure'); - } - } else { - res.send('failure'); - } - } - }) -} // endpoint to send items to app.post("/place", function(req, res) { @@ -81,7 +57,27 @@ app.post("/remove", function(req, res) { res.header("Access-Control-Allow-Origin", "*"); res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept"); // save items we get - db.items.removeitem(req.body, res); + var object = req.body; + db.items.findOne({name:object.name}, function (err, doc) { + // console.dir(doc); + if (err) { + console.log('failure'); + } else { + if (doc) { + // Update existing items if item name already exists + if(Number(doc.count) > Number(object.count)) { + console.log("removed: " + object.name + " " + object.count + " . " + doc.count); + object.count = Number(doc.count) - Number(object.count); + db.items.update(doc, object, {multi:true}, function (err, numReplaced) {}); + res.send("successier"); + } else { + console.log('failure'); + } + } else { + console.log('failure ' + object.name); + } + } + }) }); // endpoint for getting an inventory of what we got app.get("/inventory", function(req, res) { From 1e65eb40c5b742f32bd7afe952bf702ef0bd1b64 Mon Sep 17 00:00:00 2001 From: Danielv123 Date: Mon, 26 Sep 2016 08:07:19 +0200 Subject: [PATCH 3/4] Changed up the tranfer format, everything works! --- client.js | 39 ++++++++++++++++++++++----------------- master.js | 10 +++++++--- 2 files changed, 29 insertions(+), 20 deletions(-) diff --git a/client.js b/client.js index 4d384cb2..251a7e2a 100644 --- a/client.js +++ b/client.js @@ -55,23 +55,28 @@ setInterval(function() { if(items[0]) { fs.writeFileSync(config.factorioDirectory + "/script-output/orders.txt", "") for(i = 0;i < items.length; i++) { - if(items[i]) { - items[i] = items[i].split(" "); - items[i][0] = items[i][0].replace("\u0000", ""); - items[i][0] = items[i][0].replace(",", ""); - console.log(items) - // send our entity and count to the master for him to keep track of - needle.post(config.masterIP + ":" + config.masterPort + '/remove', {name:items[i][0], count:items[i][1]}, function(err, response, body){ - if(response && response.body === "successier"){ - console.log(response.body + " : importing: " + JSON.stringify({[items[i][0]]: items[i][1]})); - // buffer confirmed orders - confirmedOrders[confirmedOrders.length] = {[items[i][0]]: items[i][1]} - //client.exec("/c remote.call('clusterio', 'importMany', '[" + JSON.stringify(jsonobject) + "]')") - } else { - console.log("ERROR: "+JSON.stringify({[items[i][0]]: items[i][1]})); - } - }); - } + (function(i){ + if(items[i]) { + items[i] = items[i].split(" "); + items[i][0] = items[i][0].replace("\u0000", ""); + items[i][0] = items[i][0].replace(",", ""); + // send our entity and count to the master for him to keep track of + needle.post(config.masterIP + ":" + config.masterPort + '/remove', {name:items[i][0], count:items[i][1]}, function(err, response, body){ + if(response && response.body && typeof response.body == "object") { + // buffer confirmed orders + confirmedOrders[confirmedOrders.length] = {[response.body.name]: response.body.count} + } + /*if(response && response.body === "successier"){ + console.log(response.body + " : importing: " + JSON.stringify({[items[i][0]]: items[i][1]})); + // buffer confirmed orders + confirmedOrders[confirmedOrders.length] = {[items[i][0]]: items[i][1]} + //client.exec("/c remote.call('clusterio', 'importMany', '[" + JSON.stringify(jsonobject) + "]')") + } else { + console.log("ERROR: "+JSON.stringify({[items[i][0]]: items[i][1]})); + }*/ + }); + } + })(i); } // if we got some confirmed orders console.log("Importing " + confirmedOrders.length + " items! " + JSON.stringify(confirmedOrders)); diff --git a/master.js b/master.js index 9dc1b76d..6d311c0d 100644 --- a/master.js +++ b/master.js @@ -67,9 +67,13 @@ app.post("/remove", function(req, res) { // Update existing items if item name already exists if(Number(doc.count) > Number(object.count)) { console.log("removed: " + object.name + " " + object.count + " . " + doc.count); - object.count = Number(doc.count) - Number(object.count); - db.items.update(doc, object, {multi:true}, function (err, numReplaced) {}); - res.send("successier"); + objectUpdate = { + "name": object.name, + "count": Number(doc.count) - Number(object.count), + }; + db.items.update(doc, objectUpdate, {multi:true}, function (err, numReplaced) {}); + // res.send("successier"); + res.send(object); } else { console.log('failure'); } From da9d2c14dc68fb39691bfb1f4e76d0808de999c0 Mon Sep 17 00:00:00 2001 From: Danielv123 Date: Tue, 27 Sep 2016 11:24:39 +0200 Subject: [PATCH 4/4] Solved #14 and #10, preparing for prerelease --- client.js | 64 +++++++++++++++++++++++++------------------------------ master.js | 3 ++- 2 files changed, 31 insertions(+), 36 deletions(-) diff --git a/client.js b/client.js index 251a7e2a..ab6cf8e3 100644 --- a/client.js +++ b/client.js @@ -12,6 +12,10 @@ var client = new Rcon({ timeout: 0 }).connect(); +if (!fs.existsSync(config.factorioDirectory + "/script-output/")){ + fs.mkdirSync(config.factorioDirectory + "/script-output/"); +} +fs.writeFileSync(config.factorioDirectory + "/script-output/output.txt", "") fs.writeFileSync(config.factorioDirectory + "/script-output/orders.txt", "") client.on('authenticated', function() { @@ -48,60 +52,50 @@ fs.watch(config.factorioDirectory + "/script-output/output.txt", "utf8", functio } } }) + setInterval(function() { // get array of lines in file items = fs.readFileSync(config.factorioDirectory + "/script-output/orders.txt", "utf8").split("\n"); - // if you found anything, reset the file + // if we actually got anything from the file, proceed and reset file if(items[0]) { - fs.writeFileSync(config.factorioDirectory + "/script-output/orders.txt", "") - for(i = 0;i < items.length; i++) { + fs.writeFileSync(config.factorioDirectory + "/script-output/orders.txt", ""); + // prepare a package of all our requested items in a more tranfer friendly format + var preparedPackage = {}; + for(i = 2;i < items.length; i++) { (function(i){ if(items[i]) { items[i] = items[i].split(" "); items[i][0] = items[i][0].replace("\u0000", ""); items[i][0] = items[i][0].replace(",", ""); - // send our entity and count to the master for him to keep track of - needle.post(config.masterIP + ":" + config.masterPort + '/remove', {name:items[i][0], count:items[i][1]}, function(err, response, body){ - if(response && response.body && typeof response.body == "object") { - // buffer confirmed orders - confirmedOrders[confirmedOrders.length] = {[response.body.name]: response.body.count} + if(preparedPackage[items[i][0]]){ + if(typeof Number(preparedPackage[items[i][0]].count) == "number" && typeof Number(items[i][1]) == "number") { + preparedPackage[items[i][0]] = {"name":items[i][0], "count":Number(preparedPackage[items[i][0]].count) + Number(items[i][1])}; + } else if (typeof Number(items[i][1]) == "number") { + preparedPackage[items[i][0]] = {"name":items[i][0], "count":Number(items[i][1])}; } - /*if(response && response.body === "successier"){ - console.log(response.body + " : importing: " + JSON.stringify({[items[i][0]]: items[i][1]})); - // buffer confirmed orders - confirmedOrders[confirmedOrders.length] = {[items[i][0]]: items[i][1]} - //client.exec("/c remote.call('clusterio', 'importMany', '[" + JSON.stringify(jsonobject) + "]')") - } else { - console.log("ERROR: "+JSON.stringify({[items[i][0]]: items[i][1]})); - }*/ - }); + } else if (typeof Number(items[i][1]) == "number") { + preparedPackage[items[i][0]] = {"name":items[i][0], "count":Number(items[i][1])}; + } } })(i); } + // request our items, one item at a time + for(i = 0;i