Instances uploads their mods to master

This commit is contained in:
Danielv123 committed 2017-01-08 21:00:24 +01:00
1 parent e961ca6d56
commit fd577054c5
3 files changed
+99 -32

No files matched your search

+49 -27
View File
@@ -4,6 +4,7 @@ var needle = require("needle");
var child_process = require('child_process'); var child_process = require('child_process');
var path = require('path'); var path = require('path');
var syncRequest = require('sync-request'); var syncRequest = require('sync-request');
var request = require("request")
var ncp = require('ncp').ncp; var ncp = require('ncp').ncp;
var Rcon = require('simple-rcon'); var Rcon = require('simple-rcon');
var hashFiles = require('hash-files'); var hashFiles = require('hash-files');
@@ -36,9 +37,9 @@ if (!fs.existsSync("./instances/")) {
if (!fs.existsSync("./sharedMods/")) { if (!fs.existsSync("./sharedMods/")) {
fs.mkdirSync("sharedMods"); fs.mkdirSync("sharedMods");
} }
var instance = process.argv[3]; const instance = process.argv[3];
var instancedirectory = './instances/' + instance; const instancedirectory = './instances/' + instance;
var command = process.argv[2]; const command = process.argv[2];
// handle commandline parameters // handle commandline parameters
if (!command || command == "help" || command == "--help") { if (!command || command == "help" || command == "--help") {
console.error("Usage: ") console.error("Usage: ")
@@ -207,6 +208,7 @@ write-data=__PATH__executable__/../../../instances/" + instance + "\r\n\
lastSignalCheck = Date.now(); lastSignalCheck = Date.now();
// world IDs ------------------------------------------------------------------ // world IDs ------------------------------------------------------------------
setInterval(getID, 10000);
function getID() { function getID() {
var payload = { var payload = {
time: Date.now(), time: Date.now(),
@@ -228,7 +230,47 @@ write-data=__PATH__executable__/../../../instances/" + instance + "\r\n\
}); });
}) })
} }
setInterval(getID, 10000) // Mod uploading and management -----------------------------------------------
// get mod names and hashes
// string: instance, function: callback
setTimeout(function(){hashMods(instance, uploadMods)}, 5000);
function uploadMods(modHashes) {
// [{modName:string,hash:string}, ... ]
for(i=0;i<modHashes.length;i++){
let payload = {
modName: modHashes[i].modName,
hash: modHashes[i].hash,
}
needle.post(config.masterIP + ":" + config.masterPort + '/checkMod', payload, function (err, response, body) {
if(err) throw err
if(response && body && body == "found") {
console.log("master has mod")
} else if (response && body && typeof body == "string") {
let mod = response.body;
console.log("Sending mod: " + mod)
// Send mods master says it wants
// response.body is a string which is a modName.zip
/*needle.post(config.masterIP + ":" + config.masterPort + '/uploadMod', payload, function (err, response, body) {
// we did it, keep going
}
*/
var req = request.post("http://"+config.masterIP + ":" + config.masterPort + '/uploadMod', function (err, resp, body) {
if (err) {
console.log('Error!');
throw err
} else {
console.log('URL: ' + body);
}
});
var form = req.form();
form.append('file', fs.createReadStream("./instances/"+instance+"/mods/"+mod));
}
});
}
}
// provide items -------------------------------------------------------------- // provide items --------------------------------------------------------------
// trigger when something happens to output.txt // trigger when something happens to output.txt
fs.watch(instancedirectory + "/script-output/output.txt", function (eventType, filename) { fs.watch(instancedirectory + "/script-output/output.txt", function (eventType, filename) {
@@ -390,14 +432,14 @@ String.prototype.hashCode = function () {
return hash; return hash;
} }
// string, function // string, function
// [{modname:string,hash:string}, ... ] // returns [{modName:string,hash:string}, ... ]
function hashMods(instanceName, callback) { function hashMods(instanceName, callback) {
if(!callback) { if(!callback) {
throw "ERROR in function hashMods NO CALLBACK" throw "ERROR in function hashMods NO CALLBACK"
} }
function callback2(hash, modname){ function callback2(hash, modName){
hashedMods[hashedMods.length] = { hashedMods[hashedMods.length] = {
modname: modname, modName: modName,
hash: hash, hash: hash,
} }
// check if this callback has ran once for each mod // check if this callback has ran once for each mod
@@ -411,31 +453,11 @@ function hashMods(instanceName, callback) {
/*let mods = fs.readdirSync("./sharedMods/")*/ /*let mods = fs.readdirSync("./sharedMods/")*/
let instanceMods = fs.readdirSync("./instances/"+instanceName+"/mods/") let instanceMods = fs.readdirSync("./instances/"+instanceName+"/mods/")
/*for(o=0;o<mods.length;o++) {
if(path.extname(mods[o]) != ".zip") {
mods = mods.splice(mods.indexOf(mods[o]), 1); // remove element from array
}
}*/
for(o=0;o<instanceMods.length;o++) { for(o=0;o<instanceMods.length;o++) {
if(path.extname(instanceMods[o]) != ".zip") { if(path.extname(instanceMods[o]) != ".zip") {
instanceMods.splice(instanceMods.indexOf(instanceMods[o]), 1); // remove element from array instanceMods.splice(instanceMods.indexOf(instanceMods[o]), 1); // remove element from array
} }
} }
/*for(i=0;i<mods.length;i++){
let path = "./sharedMods/"+mods[i];
let name = mods[i];
let options = {
files:path,
}
hashFiles(options, function(error, hash) {
// hash will be a string if no error occurred
if(!error){
callback2(hash, name);
} else {
throw error;
}
});
}*/
for(i=0; i<instanceMods.length; i++){ for(i=0; i<instanceMods.length; i++){
let path = "./instances/"+instanceName+"/mods/"+instanceMods[i]; let path = "./instances/"+instanceName+"/mods/"+instanceMods[i];
let name = instanceMods[i]; let name = instanceMods[i];
+48 -4
View File
@@ -1,17 +1,24 @@
// Library for create folder if it does not exist // constants
var mkdirp = require("mkdirp"); const masterModFolder = "./database/masterMods/"
// Library for create folder recursively if it does not exist
const mkdirp = require("mkdirp");
mkdirp.sync(masterModFolder);
const fs = require("fs")
var nedb = require("nedb") var nedb = require("nedb")
var fs = require("fs");
// require config.json // require config.json
var config = require('./config'); var config = require('./config');
var express = require("express"); var express = require("express");
// Required for express post requests // Required for express post requests
var bodyParser = require("body-parser"); var bodyParser = require("body-parser");
var fileUpload = require('express-fileupload');
var app = express(); var app = express();
app.use(bodyParser.json()); app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true })); app.use(bodyParser.urlencoded({ extended: true }));
app.use(fileUpload());
// Set folder to serve static content from (the website) // Set folder to serve static content from (the website)
app.use(express.static('static')); app.use(express.static('static'));
@@ -56,7 +63,44 @@ app.post("/getID", function(req,res) {
console.log("Slave: " + req.body.mac + " : " + req.body.serverPort+" at " + req.body.publicIP); console.log("Slave: " + req.body.mac + " : " + req.body.serverPort+" at " + req.body.publicIP);
} }
}); });
// mod management
// should handle uploading and checking if mods are uploaded
app.post("/checkMod", function(req,res) {
console.log("Checking mod")
let files = fs.readdirSync(masterModFolder)
let found = false;
console.log(files)
files.forEach(file => {
console.log(file);
if(file == req.body.modName) {
found = true;
}
});
if(!found) {
// we don't have mod, plz send
res.send(req.body.modName)
} else {
res.send("found")
}
res.end()
});
app.post("/uploadMod", function(req,res) {
console.log("/uploadMod")
if (!req.files) {
res.send('No files were uploaded.');
return;
} else {
console.log(req.files.file)
req.files.file.mv('./database/masterMods/'+req.files.file.name, function(err) {
if (err) {
res.status(500).send(err);
} else {
res.send('File uploaded!');
}
});
}
});
// endpoint for getting information about all our slaves // endpoint for getting information about all our slaves
app.get("/slaves", function(req, res) { app.get("/slaves", function(req, res) {
res.header("Access-Control-Allow-Origin", "*"); res.header("Access-Control-Allow-Origin", "*");
@@ -77,7 +121,7 @@ app.post("/place", function(req, res) {
// 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.end("success");
}); });
// endpoint to remove items from DB when client orders items // endpoint to remove items from DB when client orders items
+2 -1
View File
@@ -5,7 +5,7 @@
"main": "master.js", "main": "master.js",
"scripts": { "scripts": {
"test": "nodemon master.js", "test": "nodemon master.js",
"start": "echo Run node client.js [instance] to start slaves && node master.js" "start": "echo Run node client.js start [instance] to start slaves && node master.js"
}, },
"keywords": [ "keywords": [
"factorio" "factorio"
@@ -27,6 +27,7 @@
"nedb": "^1.8.0", "nedb": "^1.8.0",
"needle": "^1.1.0", "needle": "^1.1.0",
"portfinder": "^1.0.9", "portfinder": "^1.0.9",
"request": "^2.79.0",
"simple-rcon": "^0.3.1", "simple-rcon": "^0.3.1",
"sync-request": "^3.0.1" "sync-request": "^3.0.1"
} }