mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-22 16:13:12 +09:00
JS part of signals kinda finished but it aint working
This commit is contained in:
1 parent
20c591bf71
commit
a3aafd8b1d
2 files changed
+54
-14
No files matched your search
@@ -12,11 +12,19 @@ var client = new Rcon({
|
|||||||
timeout: 0
|
timeout: 0
|
||||||
}).connect();
|
}).connect();
|
||||||
|
|
||||||
|
if (!fs.existsSync(config.factorioDirectory)){
|
||||||
|
console.error("FATAL ERROR: config.factorioDirectory DOES NOT EXIST, PLEASE UPDATE CONFIG.JSON");
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
// make sure we got the files we need
|
||||||
if (!fs.existsSync(config.factorioDirectory + "/script-output/")){
|
if (!fs.existsSync(config.factorioDirectory + "/script-output/")){
|
||||||
fs.mkdirSync(config.factorioDirectory + "/script-output/");
|
fs.mkdirSync(config.factorioDirectory + "/script-output/");
|
||||||
}
|
}
|
||||||
fs.writeFileSync(config.factorioDirectory + "/script-output/output.txt", "")
|
if (!fs.existsSync(config.factorioDirectory + "/script-output/")){
|
||||||
|
fs.writeFileSync(config.factorioDirectory + "/script-output/output.txt", "")
|
||||||
|
}
|
||||||
fs.writeFileSync(config.factorioDirectory + "/script-output/orders.txt", "")
|
fs.writeFileSync(config.factorioDirectory + "/script-output/orders.txt", "")
|
||||||
|
fs.writeFileSync(config.factorioDirectory + "/script-output/txbuffer.txt", "");
|
||||||
|
|
||||||
client.on('authenticated', function() {
|
client.on('authenticated', function() {
|
||||||
console.log('Authenticated!');
|
console.log('Authenticated!');
|
||||||
@@ -30,6 +38,7 @@ client.on('authenticated', function() {
|
|||||||
|
|
||||||
// set some globals
|
// set some globals
|
||||||
confirmedOrders = [];
|
confirmedOrders = [];
|
||||||
|
lastSignalCheck = Date.now();
|
||||||
// provide items --------------------------------------------------------------
|
// provide items --------------------------------------------------------------
|
||||||
// trigger when something happens to output.txt
|
// trigger when something happens to output.txt
|
||||||
fs.watch(config.factorioDirectory + "/script-output/output.txt", "utf8", function(eventType, filename) {
|
fs.watch(config.factorioDirectory + "/script-output/output.txt", "utf8", function(eventType, filename) {
|
||||||
@@ -100,6 +109,16 @@ setInterval(function() {
|
|||||||
// COMBINATOR SIGNALS ---------------------------------------------------------
|
// COMBINATOR SIGNALS ---------------------------------------------------------
|
||||||
// send any signals the slave has been told to send
|
// send any signals the slave has been told to send
|
||||||
setInterval(function() {
|
setInterval(function() {
|
||||||
|
// Fetch combinator signals from the server
|
||||||
|
needle.post(config.masterIP + ":" + config.masterPort + '/readSignal', {since:lastSignalCheck}, function(err, response, body){
|
||||||
|
if(response && response.body && typeof response.body == "object" && response.body[0]) {
|
||||||
|
// buffer confirmed orders
|
||||||
|
console.log("YESSSSSSSS" + JSON.stringify(response.body[0]));
|
||||||
|
client.exec("/c remote.call('clusterio', 'receiveFrame', '" + JSON.stringify(response.body[0]) + "')");
|
||||||
|
// confirmedSignals[confirmedSignals.length] = {[response.body.name]: response.body.count}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
lastSignalCheck = Date.now();
|
||||||
// get array of lines in file
|
// get array of lines in file
|
||||||
signals = fs.readFileSync(config.factorioDirectory + "/script-output/txbuffer.txt", "utf8").split("\n");
|
signals = fs.readFileSync(config.factorioDirectory + "/script-output/txbuffer.txt", "utf8").split("\n");
|
||||||
// if we actually got anything from the file, proceed and reset file
|
// if we actually got anything from the file, proceed and reset file
|
||||||
@@ -109,21 +128,20 @@ setInterval(function() {
|
|||||||
(function(i){
|
(function(i){
|
||||||
if(signals[i]) {
|
if(signals[i]) {
|
||||||
// signals[i] is a JSON array, we need to unnest it
|
// signals[i] is a JSON array, we need to unnest it
|
||||||
signal = JSON.parse(signals[i])
|
// actually, we don't. That array is our "frame", and we pass that along instead of seperate signals.
|
||||||
for(o=0;o<signal.length;o++) {
|
framepart = JSON.parse(signals[i])
|
||||||
(function(o) {
|
doneframe = {
|
||||||
singleSignal = signal[o];
|
time: Date.now(),
|
||||||
singleSignal.time = Date.now();
|
frame: framepart, // thats our array of objects(single signals)
|
||||||
console.log(singleSignal)
|
|
||||||
needle.post(config.masterIP + ":" + config.masterPort + '/setSignal', singleSignal, function(err, response, body){
|
|
||||||
if(response && response.body) {
|
|
||||||
// In the future we might be interested in whether or not we actually manage to send it, but honestly I don't care.
|
|
||||||
}
|
|
||||||
});
|
|
||||||
})(o);
|
|
||||||
}
|
}
|
||||||
|
// console.log(doneframe)
|
||||||
|
needle.post(config.masterIP + ":" + config.masterPort + '/setSignal', doneframe, function(err, response, body){
|
||||||
|
if(response && response.body) {
|
||||||
|
// In the future we might be interested in whether or not we actually manage to send it, but honestly I don't care.
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
})(i);
|
})(i);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}, 3000)
|
}, 1000)
|
||||||
@@ -24,6 +24,8 @@ app.use(express.static('bower_components/jquery/dist'));
|
|||||||
var Datastore = require('nedb');
|
var Datastore = require('nedb');
|
||||||
db = {};
|
db = {};
|
||||||
db.items = new Datastore({ filename: 'database/items.db', autoload: true });
|
db.items = new Datastore({ filename: 'database/items.db', autoload: true });
|
||||||
|
db.signals = new Datastore({ filename: 'database/signals.db', autoload: true, inMemoryOnly: true});
|
||||||
|
db.signals.ensureIndex({ fieldName: 'time', expireAfterSeconds: 3600 }, function (err) {});
|
||||||
|
|
||||||
db.items.additem = function(object) {
|
db.items.additem = function(object) {
|
||||||
db.items.findOne({name:object.name}, function (err, doc) {
|
db.items.findOne({name:object.name}, function (err, doc) {
|
||||||
@@ -84,6 +86,26 @@ app.post("/remove", function(req, res) {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// circuit stuff
|
||||||
|
app.post("/setSignal", function(req,res) {
|
||||||
|
if(typeof req.body == "object" && req.body.time){
|
||||||
|
db.signals.insert(req.body);
|
||||||
|
// console.log("signal set")
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
app.post("/readSignal", function(req,res) {
|
||||||
|
// request.body should be an object
|
||||||
|
// {since:UNIXTIMESTAMP,}
|
||||||
|
// we should send an array of all signals since then
|
||||||
|
db.signals.find({time:{$gte: req.body.since}}, function (err, docs) {
|
||||||
|
// $gte means greater than or equal to, meaning we only get entries newer than the timestamp
|
||||||
|
res.send(docs);
|
||||||
|
// console.log(docs)
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
// endpoint for getting an inventory of what we got
|
// endpoint for getting an inventory of what we got
|
||||||
app.get("/inventory", function(req, res) {
|
app.get("/inventory", function(req, res) {
|
||||||
res.header("Access-Control-Allow-Origin", "*");
|
res.header("Access-Control-Allow-Origin", "*");
|
||||||
|
|||||||
Reference in new issue
Block a user