From 06a4d498840d933609af4281825b9023e1aa518b Mon Sep 17 00:00:00 2001 From: Danielv123 Date: Fri, 9 Sep 2016 08:25:39 +0200 Subject: [PATCH] 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) {