mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-11 15:04:52 +09:00
Working on #7 configs
This commit is contained in:
1 parent
601d5edb27
commit
0e9fb267c4
3 files changed
+36
-21
No files matched your search
@@ -1,35 +1,36 @@
|
||||
var fs = require('fs');
|
||||
var needle = require("needle")
|
||||
|
||||
var needle = require("needle");
|
||||
// require config.json
|
||||
var config = require('./config');
|
||||
// connect us to the server with rcon
|
||||
// IP, port, password
|
||||
var Rcon = require('simple-rcon');
|
||||
var client = new Rcon({
|
||||
host: '81.167.2.56',
|
||||
port: '12345',
|
||||
password: '123',
|
||||
timeout: 0
|
||||
host: config.clientIP,
|
||||
port: config.clientPort,
|
||||
password: config.clientPassword,
|
||||
timeout: 0
|
||||
}).connect();
|
||||
|
||||
client.on('authenticated', function() {
|
||||
console.log('Authenticated!');
|
||||
console.log('Authenticated!');
|
||||
}).on('connected', function() {
|
||||
console.log('Connected!');
|
||||
console.log('Connected!');
|
||||
}).on('disconnected', function() {
|
||||
console.log('Disconnected!');
|
||||
// now reconnect
|
||||
client.connect();
|
||||
console.log('Disconnected!');
|
||||
// now reconnect
|
||||
client.connect();
|
||||
});
|
||||
|
||||
|
||||
// trigger when something happens to output.txt
|
||||
fs.watch("Factorio 0.13.9/script-output/output.txt", "utf8", function(eventType, filename) {
|
||||
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("Factorio 0.13.9/script-output/output.txt", "utf8").split("\n");
|
||||
items = fs.readFileSync(config.factorioDirectory + "/script-output/output.txt", "utf8").split("\n");
|
||||
// if you found anything, reset the file
|
||||
if(items[0]) {
|
||||
fs.writeFileSync("Factorio 0.13.9/script-output/output.txt", "")
|
||||
fs.writeFileSync(config.factorioDirectory + "/script-output/output.txt", "")
|
||||
}
|
||||
for(i = 0;i < items.length; i++) {
|
||||
if(items[i]) {
|
||||
@@ -37,7 +38,7 @@ fs.watch("Factorio 0.13.9/script-output/output.txt", "utf8", function(eventType,
|
||||
g[0] = g[0].replace("\u0000", "");
|
||||
console.log(g);
|
||||
// send our entity and count to the master for him to keep track of
|
||||
needle.post('localhost:8080/place', {name:g[0], count:g[1]},
|
||||
needle.post(config.masterIP + ":" + config.masterPort + '/place', {name:g[0], count:g[1]},
|
||||
function(err, resp, body){
|
||||
console.log(body);
|
||||
});
|
||||
@@ -45,12 +46,12 @@ fs.watch("Factorio 0.13.9/script-output/output.txt", "utf8", function(eventType,
|
||||
}
|
||||
})
|
||||
|
||||
fs.watch("Factorio 0.13.9/script-output/orders.txt", "utf8", function(eventType, filename) {
|
||||
fs.watch(config.factorioDirectory + "/script-output/orders.txt", "utf8", function(eventType, filename) {
|
||||
// get array of lines in file
|
||||
items = fs.readFileSync("Factorio 0.13.9/script-output/orders.txt", "utf8").split("\n");
|
||||
items = fs.readFileSync(config.factorioDirectory + "/script-output/orders.txt", "utf8").split("\n");
|
||||
// if you found anything, reset the file
|
||||
if(items[0]) {
|
||||
fs.writeFileSync("Factorio 0.13.9/script-output/orders.txt", "")
|
||||
fs.writeFileSync(config.factorioDirectory + "/script-output/orders.txt", "")
|
||||
}
|
||||
for(i = 0;i < items.length; i++) {
|
||||
if(items[i]) {
|
||||
@@ -59,7 +60,7 @@ fs.watch("Factorio 0.13.9/script-output/orders.txt", "utf8", function(eventType,
|
||||
g[0] = g[0].replace(",", "");
|
||||
console.log(g);
|
||||
// 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(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] + ")")
|
||||
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"__comment": "",
|
||||
"__comment": "IP and port and password the factorio server runs at for rcon connection",
|
||||
"clientIP": "81.167.2.56",
|
||||
"clientPort": "12345",
|
||||
"clientPassword": "123",
|
||||
"__comment": "Where factorio folder. Should be in the same folder as this config file.",
|
||||
"factorioDirectory": "factorio 0.13.9",
|
||||
"__comment": "IP the master web server runs at, can be ignored on the master system",
|
||||
"masterIP": "localhost",
|
||||
"masterPort": 8080
|
||||
}
|
||||
@@ -2,6 +2,8 @@
|
||||
var mkdirp = require("mkdirp");
|
||||
var nedb = require("nedb")
|
||||
var fs = require("fs");
|
||||
// require config.json
|
||||
var config = require('./config');
|
||||
|
||||
var express = require("express");
|
||||
// Required for express post requests
|
||||
@@ -44,6 +46,7 @@ db.items.removeitem = function(object, res) {
|
||||
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");
|
||||
@@ -73,11 +76,10 @@ app.post("/place", function(req, res) {
|
||||
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");
|
||||
console.log("removed: " + req.body.name + " " + req.body.count);
|
||||
// save items we get
|
||||
db.items.removeitem(req.body, res);
|
||||
});
|
||||
|
||||
var server = app.listen(8080, function () {
|
||||
var server = app.listen(config.masterPort || 8080, function () {
|
||||
console.log("Listening on port %s...", server.address().port);
|
||||
});
|
||||
Reference in new issue
Block a user