From fd577054c5ce7474dd67efdf2ad73d07088d5af9 Mon Sep 17 00:00:00 2001 From: Danielv123 Date: Sun, 8 Jan 2017 21:00:24 +0100 Subject: [PATCH] Instances uploads their mods to master --- client.js | 250 ++++++++++++++++++++++++++++----------------------- master.js | 54 +++++++++-- package.json | 3 +- 3 files changed, 187 insertions(+), 120 deletions(-) diff --git a/client.js b/client.js index 56e1f66a..48b51110 100644 --- a/client.js +++ b/client.js @@ -4,6 +4,7 @@ var needle = require("needle"); var child_process = require('child_process'); var path = require('path'); var syncRequest = require('sync-request'); +var request = require("request") var ncp = require('ncp').ncp; var Rcon = require('simple-rcon'); var hashFiles = require('hash-files'); @@ -36,9 +37,9 @@ if (!fs.existsSync("./instances/")) { if (!fs.existsSync("./sharedMods/")) { fs.mkdirSync("sharedMods"); } -var instance = process.argv[3]; -var instancedirectory = './instances/' + instance; -var command = process.argv[2]; +const instance = process.argv[3]; +const instancedirectory = './instances/' + instance; +const command = process.argv[2]; // handle commandline parameters if (!command || command == "help" || command == "--help") { console.error("Usage: ") @@ -207,6 +208,7 @@ write-data=__PATH__executable__/../../../instances/" + instance + "\r\n\ lastSignalCheck = Date.now(); // world IDs ------------------------------------------------------------------ + setInterval(getID, 10000); function getID() { var payload = { time: Date.now(), @@ -228,107 +230,147 @@ write-data=__PATH__executable__/../../../instances/" + instance + "\r\n\ }); }) } - setInterval(getID, 10000) - // provide items -------------------------------------------------------------- - // trigger when something happens to output.txt - fs.watch(instancedirectory + "/script-output/output.txt", function (eventType, filename) { - // get array of lines in file - items = fs.readFileSync(instancedirectory + "/script-output/output.txt", "utf8").split("\n"); - // if you found anything, reset the file - if (items[0]) { - fs.writeFileSync(instancedirectory + "/script-output/output.txt", "") + // 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 { + 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 app.get("/slaves", function(req, res) { res.header("Access-Control-Allow-Origin", "*"); @@ -77,7 +121,7 @@ app.post("/place", function(req, res) { // save items we get db.items.additem(req.body) // Attempt confirming - res.send("success"); + res.end("success"); }); // endpoint to remove items from DB when client orders items diff --git a/package.json b/package.json index 9261da04..94494409 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "main": "master.js", "scripts": { "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": [ "factorio" @@ -27,6 +27,7 @@ "nedb": "^1.8.0", "needle": "^1.1.0", "portfinder": "^1.0.9", + "request": "^2.79.0", "simple-rcon": "^0.3.1", "sync-request": "^3.0.1" }