Fixed js, items are now created correctly and dupe/loss glitches removed

This commit is contained in:
Danielv123 committed 2016-08-17 09:20:14 +02:00
1 parent 440ecfad30
commit 44e311feea
2 files changed
+23 -22

No files matched your search

+2 -1
View File
@@ -24,6 +24,7 @@ client.on('authenticated', function() {
// trigger when something happens to output.txt // trigger when something happens to output.txt
fs.watch("Factorio 0.13.9/script-output/output.txt", "utf8", function(eventType, filename) { fs.watch("Factorio 0.13.9/script-output/output.txt", "utf8", function(eventType, filename) {
console.log('hit output')
// get array of lines in file // get array of lines in file
items = fs.readFileSync("Factorio 0.13.9/script-output/output.txt", "utf8").split("\n"); items = fs.readFileSync("Factorio 0.13.9/script-output/output.txt", "utf8").split("\n");
// if you found anything, reset the file // if you found anything, reset the file
@@ -60,7 +61,7 @@ fs.watch("Factorio 0.13.9/script-output/orders.txt", "utf8", function(eventType,
// send our entity and count to the master for him to keep track of // send our entity and count to the master for him to keep track of
needle.post('localhost:8080/remove', {name:g[0], count:g[1]}, function(err, resp, body){ needle.post('localhost:8080/remove', {name:g[0], count:g[1]}, function(err, resp, body){
console.log(body); console.log(body);
if(body){ if(body == "success"){
client.exec("/c remote.call('clusterio', 'import', '" + g[0] + "', " + g[1] + ")") client.exec("/c remote.call('clusterio', 'import', '" + g[0] + "', " + g[1] + ")")
} }
}); });
+21 -21
View File
@@ -34,20 +34,26 @@ db.items.additem = function(object) {
} }
}) })
} }
db.items.removeitem = function(object) { db.items.removeitem = function(object, res) {
db.items.findOne({name:object.name}, function (err, doc) { db.items.findOne({name:object.name}, function (err, doc) {
// console.dir(doc); // console.dir(doc);
if (doc) { if (err) {
// Update existing items if item name already exists res.send('failure');
if(Number(doc.count) > Number(object.count)) { return false
object.count = Number(doc.count) - Number(object.count);
db.items.update(doc, object, {multi:true}, function (err, numReplaced) {});
return true;
} else {
return false;
}
} else { } else {
return false; if (doc) {
// Update existing items if item name already exists
if(Number(doc.count) > Number(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');
}
} }
}) })
} }
@@ -56,26 +62,20 @@ db.items.removeitem = function(object) {
app.post("/place", function(req, res) { app.post("/place", function(req, res) {
res.header("Access-Control-Allow-Origin", "*"); res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept"); res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
console.log("added: " + req.body); console.log("added: " + req.body.name + " " + req.body.count);
// save items we get // save items we get
db.items.additem(req.body) db.items.additem(req.body)
// Attempt confirming // Attempt confirming
res.send("success!"); res.send("success");
}); });
// endpoint to remove items from DB when client orders items // endpoint to remove items from DB when client orders items
app.post("/remove", function(req, res) { app.post("/remove", function(req, res) {
res.header("Access-Control-Allow-Origin", "*"); res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept"); res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
console.log("removed: " + req.body); console.log("removed: " + req.body.name + " " + req.body.count);
// save items we get // save items we get
temp = db.items.removeitem(req.body); db.items.removeitem(req.body, res);
if(temp) {
// if true, the action was successfull
res.send("success");
} else {
res.send('failure');
}
}); });
var server = app.listen(8080, function () { var server = app.listen(8080, function () {