Updated research dump and enabling with a fix for infinite-research

This commit is contained in:
psihius committed 2018-04-17 13:57:36 +03:00
1 parent 0dee243bf9
commit c7cd922541
4 files changed
+53 -44

No files matched your search

+3 -3
View File
@@ -1,9 +1,9 @@
/silent-command /silent-command
local lines = ""; local data = {};
for _, tech in pairs(game.forces["player"].technologies) do for _, tech in pairs(game.forces["player"].technologies) do
lines = lines .. tech.name .. ":" .. tostring(tech.researched) .. "\n"; table.insert(data, tech.name .. ":" .. tostring(tech.researched) .. ":" .. tostring(tech.level));
end end
game.write_file("researchSync.txt",lines) game.write_file("researchSync.txt", table.concat(data, "\n"))
@@ -1,5 +1,6 @@
/silent-command /silent-command
if game.forces['player'].technologies['£key'] then if game.forces['player'].technologies['{tech_name}'] then
game.forces['player'].technologies['£key'].researched=true game.forces['player'].technologies['{tech_name}}'].researched={tech_researched}
game.print("researcSync enabled research: £key") game.forces['player'].technologies['{tech_name}}'].level={tech_level}
game.print("researcSync enabled research: {tech_name} to level {tech_level}")
end end
+31 -23
View File
@@ -33,7 +33,7 @@ class ResearchSync {
this.messageInterface("got error when calling getSlaveMeta", resp.statusCode, resp.body); this.messageInterface("got error when calling getSlaveMeta", resp.statusCode, resp.body);
return; return;
} }
var allmeta = JSON.parse(resp.body); let allmeta = JSON.parse(resp.body);
let needResearch = {}; let needResearch = {};
@@ -42,27 +42,34 @@ class ResearchSync {
let researchList = instance.research; let researchList = instance.research;
if(researchList){ if(researchList){
Object.keys(researchList).forEach(researchName => { Object.keys(researchList).forEach(researchName => {
let researched = researchList[researchName]; if (needResearch.hasOwnProperty(researchName)) {
if(JSON.parse(researched)){ if (needResearch[researchName][0] !== false) {
needResearch[researchName] = true; needResearch[researchName][0] = researchList[researchName][0];
} }
if (needResearch[researchName][1] < researchList[researchName][1]) {
needResearch[researchName][1] = researchList[researchName][1];
}
}
else {
needResearch[researchName] = researchList[researchName];
}
}); });
} }
} }
}); });
var difference = this.diff(this.research, needResearch); let difference = this.filterResearchDiff(this.research, needResearch);
Object.keys(difference).forEach((key) => { Object.keys(difference).forEach((key) => {
if (difference[key] == 1) { let command = this.functions.enableResearch;
let command = this.functions.enableResearch; while(command.includes("{tech_name}")){
while(command.includes("£key")){ command = command.replace("{tech_name}", key);
command = command.replace("£key", key); command = command.replace("{tech_researched}", difference[key][0]);
} command = command.replace("{tech_level}", difference[key][1]);
this.messageInterface(command);
this.messageInterface("Unlocking research: "+key)
// this.messageInterface("/c game.forces['player'].technologies['" + key + "'].researched=true");
} }
}) this.messageInterface(command);
if(Object.keys(difference).length > 0){ this.messageInterface("Unlocking research: "+key);
// this.messageInterface("/c game.forces['player'].technologies['" + key + "'].researched=true");
});
if(Object.keys(difference).length > 0){
this.messageInterface("difference from other servers", JSON.stringify(difference)); this.messageInterface("difference from other servers", JSON.stringify(difference));
} }
needle.post(this.config.masterIP + ':' + this.config.masterPort + '/api/editSlaveMeta', { needle.post(this.config.masterIP + ':' + this.config.masterPort + '/api/editSlaveMeta', {
@@ -75,11 +82,11 @@ class ResearchSync {
}); });
} }
diff(array1, array2) { filterResearchDiff(localResearch, remoteResearch) {
var diff = {}; let diff = {};
Object.keys(array2).forEach((element) => { Object.keys(localResearch).forEach((key) => {
if (array2[element] != array1[element]){ if (localResearch[key][0] != remoteResearch[key][0] || localResearch[key][1] != remoteResearch[key][1]){
diff[element] = array2[element]; diff[key] = remoteResearch[key];
} }
}); });
return diff; return diff;
@@ -99,8 +106,9 @@ class ResearchSync {
try { try {
let kv = data.split(":"); let kv = data.split(":");
let name = kv[0]; let name = kv[0];
let value = JSON.parse(kv[1]); let researched = JSON.parse(kv[1]) === 'true' ? true : false;
this.research[name] = value; let level = parseInt(kv[2]);
this.research[name] = [researched, level];
} catch (e) { } catch (e) {
} }
} }
+15 -15
View File
@@ -9,28 +9,28 @@ describe("researchSync/index.js", ()=>{
assert.equal(typeof researchSync, "function"); assert.equal(typeof researchSync, "function");
}); });
describe("class researchSync()", ()=>{ describe("class researchSync()", ()=>{
it(".diff(object1, object2) returns key:value pairs that exist in object2 but not object1", ()=>{ it(".filterResearchDiff(object1, object2) returns research that needs enabling on the local instance", ()=>{
let reSync = new researchSync({}, function(){}); let reSync = new researchSync({}, function(){});
let obj1 = { let obj1 = {
a:"str", a:[true, 0],
b:123, b:[true, 0],
c:console.log, c:[false, 0],
d:"apple cake", d:[false, 10],
} };
let obj2 = { let obj2 = {
a:"str", a:[true, 0],
b:9999, b:[true, 1],
c:console.log, c:[false, 0],
e:"truthy", e:[false, 50],
} };
assert(typeof reSync.diff == "function"); assert(typeof reSync.filterResearchDiff == "function");
let diffResult = reSync.diff(obj1, obj2); let diffResult = reSync.filterResearchDiff(obj1, obj2);
assert(diffResult.a === undefined); assert(diffResult.a === undefined);
assert(diffResult.b === 9999); assert(diffResult.b === [true, 1]);
assert(diffResult.d === undefined); assert(diffResult.d === undefined);
assert(diffResult.e === "truthy"); assert(diffResult.e === [false, 50]);
}); });
it(".pollResearch() dumps a long Lua command", ()=>{ it(".pollResearch() dumps a long Lua command", ()=>{
let reSync = new researchSync({}, io); let reSync = new researchSync({}, io);