researchSync: Fixed importing research

This commit is contained in:
Danielv123 committed 2018-04-13 20:18:47 +02:00
1 parent c9bd26a01e
commit 1d0d9765bd
4 files changed
+43 -37

No files matched your search

+3 -1
View File
@@ -238,7 +238,9 @@ app.post("/api/editSlaveMeta", function(req,res) {
slaves[req.body.instanceID].meta = {}; slaves[req.body.instanceID].meta = {};
} }
slaves[req.body.instanceID].meta = deepmerge(slaves[req.body.instanceID].meta, req.body.meta, {clone:true}); slaves[req.body.instanceID].meta = deepmerge(slaves[req.body.instanceID].meta, req.body.meta, {clone:true});
console.log("Updating slave: " + slaves[req.body.instanceID].mac + " : " + slaves[req.body.instanceID].serverPort+" at " + slaves[req.body.instanceID].publicIP, req.body.meta); let metaPortion = JSON.stringify(req.body.meta);
if(metaPortion.length > 50) metaPortion = metaPortion.substring(0,20) + "...";
console.log("Updating slave "+slaves[req.body.instanceID].instanceName+": " + slaves[req.body.instanceID].mac + " : " + slaves[req.body.instanceID].serverPort+" at " + slaves[req.body.instanceID].publicIP, metaPortion);
} else { } else {
res.send("ERROR: Invalid instanceID or password") res.send("ERROR: Invalid instanceID or password")
} }
+1 -4
View File
@@ -3,10 +3,7 @@
local lines = ""; local lines = "";
for _, tech in pairs(game.forces["player"].technologies) do for _, tech in pairs(game.forces["player"].technologies) do
local dict = {}; lines = lines .. tech.name .. ":" .. tostring(tech.researched) .. "\n";
dict[tech.name] = tech.researched;
lines = lines .. serpent.line(dict) .. "\n";
end end
game.write_file("researchSync.txt",lines) game.write_file("researchSync.txt",lines)
+18 -20
View File
@@ -35,17 +35,20 @@ class ResearchSync {
let needResearch = {}; let needResearch = {};
Object.entries(allmeta).map((instance) => { allmeta.forEach(instance => {
if (instance[1] == null || instance[1].research == null) return; if(instance){
Object.entries(instance[1].research).map((research) => { let researchList = instance.research;
if (research[1] == 1) { if(researchList){
needResearch[research[0]] = 1; Object.keys(researchList).forEach(researchName => {
} let researched = researchList[researchName];
}); if(JSON.parse(researched)){
}) needResearch[researchName] = true;
}
});
}
}
});
var difference = this.diff(this.research, needResearch); var difference = this.diff(this.research, needResearch);
Object.keys(difference).forEach((key) => { Object.keys(difference).forEach((key) => {
if (difference[key] == 1) { if (difference[key] == 1) {
let command = this.functions.enableResearch; let command = this.functions.enableResearch;
@@ -53,6 +56,7 @@ class ResearchSync {
command = command.replace("£key", key); command = command.replace("£key", key);
} }
this.messageInterface(command); this.messageInterface(command);
this.messageInterface("Unlocking research: "+key)
// this.messageInterface("/c game.forces['player'].technologies['" + key + "'].researched=true"); // this.messageInterface("/c game.forces['player'].technologies['" + key + "'].researched=true");
} }
}) })
@@ -91,16 +95,10 @@ class ResearchSync {
} }
scriptOutput(data){ scriptOutput(data){
try { try {
// remove Lua table junk let kv = data.split(":");
let currentResearch = data.replace("=",":").replace("[","").replace("]","").replace('"', "").replace('"', ""); let name = kv[0];
// add first " before key let value = JSON.parse(kv[1]);
currentResearch = '{"' + currentResearch.slice(1); this.research[name] = value;
// add second " after the key to make it a string
currentResearch = currentResearch.split(" ")[0] + '"' + currentResearch.slice(currentResearch.split(" ")[0].length);
// turn our constructed JSON string into an object
currentResearch = JSON.parse(currentResearch);
// console.log(currentResearch)
this.research = Object.assign(this.research, currentResearch);
} catch (e) { } catch (e) {
} }
} }
+21 -12
View File
@@ -47,23 +47,32 @@ describe("researchSync/index.js", ()=>{
assert(ioRecieved); assert(ioRecieved);
}); });
describe(".scriptOutput(data) handles file writes from factorio", ()=>{ describe(".scriptOutput(data) handles file writes from factorio", ()=>{
it("wants a Lua table of a single research, parses and saves it", ()=>{ it("wants a key:value pair of a single research, parses and saves it", ()=>{
let reSync = new researchSync({hi:"hello"}, console.log); let reSync = new researchSync({hi:"hello"}, console.log);
let researches = [ let researches = [
'{automation = false}', 'automation:false',
'{["automation-2"] = false}', 'automation-2:false',
'{["automation-3"] = false}', 'automation-3:false',
'{electronics = false}', 'electronics:false',
'{["advanced-electronics"] = false}', 'advanced-electronics:false',
'{["advanced-electronics-2"] = false}', 'advanced-electronics-2:false',
'{["circuit-network"] = false}', 'circuit-network:false',
'{explosives = false}', 'explosives:false',
'{logistics = false}', 'logistics:false',
'{["logistics-2"] = false}', 'logistics-2:false',
'logistics-3:false',
'optics:false',
'laser:false',
'solar-energy:false',
'turrets:false',
'laser-turrets:false',
'stone-walls:false',
'gates:false',
'engine:false',
] ]
researches.forEach(research => { researches.forEach(research => {
reSync.scriptOutput(research); reSync.scriptOutput(research);
assert(isFactorioCommand("/c x = "+research)); assert(!isFactorioCommand("/c x = "+research));
}); });
assert(Object.keys(reSync.research).length == researches.length); assert(Object.keys(reSync.research).length == researches.length);
}); });