Changed research sync to work with /api/slaves so the instance can ignore it's own research tree and enhanced research enabling with proper messages and sound alert, added checks so we don't flip values that haven't changed

This commit is contained in:
psihius committed 2018-04-19 18:29:05 +03:00
1 parent 9a66542270
commit ed9b4bbafa
2 files changed
+41 -30

No files matched your search

+13 -3
View File
@@ -1,6 +1,16 @@
/silent-command
if game.forces['player'].technologies['{tech_name}'] then
game.forces['player'].technologies['{tech_name}'].researched={tech_researched}
game.forces['player'].technologies['{tech_name}'].level={tech_level}
game.print("researcSync enabled research: {tech_name} to level {tech_level}")
if game.forces['player'].technologies['{tech_name}'].researched ~= {tech_researched} then
game.forces['player'].technologies['{tech_name}'].researched={tech_researched}
end
if game.forces['player'].technologies['{tech_name}'].level ~= {tech_level} then
game.forces['player'].technologies['{tech_name}'].level={tech_level }
end
script.raise_event(defines.events.on_research_finished, {research=game.forces['player'].technologies['{tech_name}'], by_script=true})
game.play_sound({path="utility/research_completed"})
if game.forces['player'].technologies['{tech_name}'].researched == false then
game.print("Technology {tech_name} synced at level {tech_level}")
else
game.print("Technology {tech_name} synced")
end
end
+28 -27
View File
@@ -22,42 +22,45 @@ class ResearchSync {
}
doSync() {
needle.post(this.config.masterIP + ':' + this.config.masterPort + '/api/getSlavesMeta', {
password: this.config.clientPassword,
}, (err, resp, body) => {
needle.get(this.config.masterIP + ':' + this.config.masterPort + '/api/slaves', (err, resp, slaveData) => {
if (err){
this.messageInterface("Unable to post JSON master/api/getSlavesMeta, master might be unaccessible");
this.messageInterface("Unable to post JSON master/api/slaves, master might be unreachable");
return false;
}
if (resp.statusCode != 200){
this.messageInterface("got error when calling getSlaveMeta", resp.statusCode, resp.body);
if (resp.statusCode !== 200){
this.messageInterface("got error when calling slaves", resp.statusCode, resp.body);
return;
}
let allmeta = JSON.parse(resp.body);
let needResearch = {};
allmeta.forEach(instance => {
if(instance){
let researchList = instance.research;
if(researchList){
Object.keys(researchList).forEach(researchName => {
if (needResearch.hasOwnProperty(researchName)) {
if (needResearch[researchName].researched === 0) {
needResearch[researchName].researched = parseInt(researchList[researchName].researched);
}
if (needResearch[researchName].level < parseInt(researchList[researchName].level)) {
needResearch[researchName].level = parseInt(researchList[researchName].level);
}
Object.keys(slaveData).forEach(instanceKey => {
if (slaveData[instanceKey].unique === this.config.unique.toString()) {
return;
}
if (!slaveData[instanceKey].meta.hasOwnProperty('research')) {
return;
}
let researchList = slaveData[instanceKey].meta.research;
if(researchList){
Object.keys(researchList).forEach(researchName => {
if (needResearch.hasOwnProperty(researchName)) {
if (needResearch[researchName].researched === 0) {
needResearch[researchName].researched = parseInt(researchList[researchName].researched);
}
else {
needResearch[researchName] = {researched: parseInt(researchList[researchName].researched), level: parseInt(researchList[researchName].level)};
if (needResearch[researchName].level < parseInt(researchList[researchName].level)) {
needResearch[researchName].level = parseInt(researchList[researchName].level);
}
});
}
}
else {
needResearch[researchName] = {researched: parseInt(researchList[researchName].researched), level: parseInt(researchList[researchName].level)};
}
});
}
});
let difference = this.filterResearchDiff(this.research, needResearch);
Object.keys(difference).forEach((key) => {
let command = this.functions.enableResearch;
while(command.includes("{tech_name}")){
@@ -69,9 +72,7 @@ class ResearchSync {
this.messageInterface("Unlocking research: " + key + " at research state = " + (difference[key].researched === 0 ? 'false' : 'true') + ' and level ' + difference[key].level);
this.research[key] = difference[key];
});
if(Object.keys(difference).length > 0){
this.messageInterface("difference from other servers", JSON.stringify(difference));
}
needle.post(this.config.masterIP + ':' + this.config.masterPort + '/api/editSlaveMeta', {
instanceID: this.config.unique,
password: this.config.clientPassword,
@@ -86,7 +87,7 @@ class ResearchSync {
let diff = {};
Object.keys(localResearch).forEach((key) => {
if (remoteResearch.hasOwnProperty(key)) {
if ((localResearch[key].researched !== remoteResearch[key].researched && remoteResearch[key].researched === 1) || localResearch[key].level < remoteResearch[key].level) {
if ((localResearch[key].researched === 0 && localResearch[key].researched !== remoteResearch[key].researched) || localResearch[key].level !== remoteResearch[key].level) {
diff[key] = remoteResearch[key];
}
}