mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-11 23:04:52 +09:00
Merge remote-tracking branch 'refs/remotes/Danielv123/master'
This commit is contained in:
commit
090910aeef
4 files changed
+83
-49
No files matched your search
@@ -12,6 +12,12 @@ 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() {
|
||||
console.log('Authenticated!');
|
||||
}).on('connected', function() {
|
||||
@@ -22,10 +28,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,36 +43,62 @@ 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);
|
||||
});
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
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 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++) {
|
||||
if(items[i]) {
|
||||
g = items[i].split(" ");
|
||||
g[0] = g[0].replace("\u0000", "");
|
||||
g[0] = g[0].replace(",", "");
|
||||
console.log(g);
|
||||
// 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){
|
||||
console.log(body);
|
||||
if(body == "success"){
|
||||
client.exec("/c remote.call('clusterio', 'import', '" + g[0] + "', " + g[1] + ")")
|
||||
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(",", "");
|
||||
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])};
|
||||
}
|
||||
} 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<Object.keys(preparedPackage).length;i++){
|
||||
console.log(preparedPackage[Object.keys(preparedPackage)[i]])
|
||||
needle.post(config.masterIP + ":" + config.masterPort + '/remove', preparedPackage[Object.keys(preparedPackage)[i]], 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 we got some confirmed orders
|
||||
console.log("Importing " + confirmedOrders.length + " items! " + JSON.stringify(confirmedOrders));
|
||||
sadas = JSON.stringify(confirmedOrders)
|
||||
confirmedOrders = [];
|
||||
// send our RCON command with whatever we got
|
||||
client.exec("/c remote.call('clusterio', 'importMany', '" + sadas + "')");
|
||||
}
|
||||
})
|
||||
}, 10000)
|
||||
/*
|
||||
fs.watch(config.factorioDirectory + "/script-output/orders.txt", "utf8", function(eventType, filename) {
|
||||
|
||||
})
|
||||
*/
|
||||
+1
-1
@@ -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.",
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
cd "Factorio 0.13.9"
|
||||
cd "Factorio_0.13.17"
|
||||
@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 ..
|
||||
|
||||
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
|
||||
|
||||
@@ -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,32 @@ 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);
|
||||
objectUpdate = {
|
||||
"name": object.name,
|
||||
"count": Number(doc.count) - Number(object.count),
|
||||
};
|
||||
// db.items.update(doc, objectUpdate, {multi:true}, function (err, numReplaced) {});
|
||||
db.items.update(doc, {$inc:{count:object.count*-1}}, {multi:true}, function (err, numReplaced) {});
|
||||
// res.send("successier");
|
||||
res.send(object);
|
||||
} 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) {
|
||||
|
||||
Reference in new issue
Block a user