Formatting and logging adjustments

This commit is contained in:
psihius committed 2019-07-14 01:32:34 +02:00
1 parent 0afe35b67c
commit b666a909ab
2 files changed
+56 -41

No files matched your search

@@ -8,8 +8,8 @@ if game.forces['player'].technologies['{tech_name}'] then
script.raise_event(defines.events.on_research_finished, {research=game.forces['player'].technologies['{tech_name}'], by_script=true}) 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"}) game.play_sound({path="utility/research_completed"})
if 1 == {tech_infinite} then if 1 == {tech_infinite} then
game.print("Technology {tech_name} synced at level {tech_level}") game.print("Infinite technology {tech_name} level {tech_level} unlocked")
else else
game.print("Technology {tech_name} synced") game.print("Technology {tech_name} researched")
end end
end end
+54 -39
View File
@@ -1,16 +1,16 @@
const fs = require('fs'); const fs = require('fs');
const needle = require("needle"); const needle = require("needle");
class ResearchSync { class ResearchSync {
constructor(slaveConfig, messageInterface, extras = {}){ constructor(slaveConfig, messageInterface, extras = {}) {
this.config = slaveConfig; this.config = slaveConfig;
this.messageInterface = messageInterface; this.messageInterface = messageInterface;
this.functions = this.loadFunctions(); this.functions = this.loadFunctions();
this.research = {}; this.research = {};
setInterval(() => { setInterval(
() => {
this.pollResearch(); this.pollResearch();
setTimeout(this.doSync.bind(this), 5000); setTimeout(this.doSync.bind(this), 5000);
}, },
@@ -25,11 +25,11 @@ class ResearchSync {
doSync() { doSync() {
needle.get(this.config.masterIP + ':' + this.config.masterPort + '/api/slaves', (err, resp, slaveData) => { needle.get(this.config.masterIP + ':' + this.config.masterPort + '/api/slaves', (err, resp, slaveData) => {
if (err){ if (err) {
this.messageInterface("Unable to post JSON master/api/slaves, master might be unreachable"); this.messageInterface("Unable to post JSON master/api/slaves, master might be unreachable");
return false; return false;
} }
if (resp.statusCode !== 200){ if (resp.statusCode !== 200) {
this.messageInterface("got error when calling slaves", resp.statusCode, resp.body); this.messageInterface("got error when calling slaves", resp.statusCode, resp.body);
return; return;
} }
@@ -40,58 +40,72 @@ class ResearchSync {
if (slaveData[instanceKey].unique === this.config.unique.toString()) { if (slaveData[instanceKey].unique === this.config.unique.toString()) {
return; return;
} }
if (!slaveData[instanceKey].hasOwnProperty('meta') || !slaveData[instanceKey].meta.hasOwnProperty('research')) { if (!slaveData[instanceKey].hasOwnProperty('meta')
|| !slaveData[instanceKey].meta.hasOwnProperty('research')
) {
return; return;
} }
let researchList = slaveData[instanceKey].meta.research; let researchList = slaveData[instanceKey].meta.research;
if(researchList){ if (researchList) {
Object.keys(researchList).forEach(researchName => { Object.keys(researchList).forEach(researchName => {
if (isNaN(researchList[researchName].researched) || isNaN(researchList[researchName].level) || isNaN(researchList[researchName].infinite)) { if (
isNaN(researchList[researchName].researched)
|| isNaN(researchList[researchName].level)
|| isNaN(researchList[researchName].infinite))
{
return; return;
} }
if (needResearch.hasOwnProperty(researchName)) { if (needResearch.hasOwnProperty(researchName)) {
if (needResearch[researchName].infinite === 1 if (
&& needResearch[researchName].level < parseInt(researchList[researchName].level)) needResearch[researchName].infinite === 1
{ && needResearch[researchName].level < parseInt(researchList[researchName].level)
) {
needResearch[researchName].level = parseInt(researchList[researchName].level); needResearch[researchName].level = parseInt(researchList[researchName].level);
} else if (needResearch[researchName].researched === 0 } else if (
&& parseInt(researchList[researchName].researched) === 1) needResearch[researchName].researched === 0
{ && parseInt(researchList[researchName].researched) === 1
) {
needResearch[researchName].researched = 1 needResearch[researchName].researched = 1
} }
} else { } else {
needResearch[researchName] = { needResearch[researchName] = {
researched: parseInt(researchList[researchName].researched), researched: parseInt(researchList[researchName].researched),
level: parseInt(researchList[researchName].level), level : parseInt(researchList[researchName].level),
infinite: parseInt(researchList[researchName].infinite) infinite : parseInt(researchList[researchName].infinite)
}; };
} }
}); });
} }
}); });
let difference = this.filterResearchDiff(this.research, needResearch); let difference = this.filterResearchDiff(this.research, needResearch);
Object.keys(difference).forEach((key) => { Object.keys(difference).forEach((key) => {
if (this.research[key]) { if (this.research[key]) {
let command = this.functions.enableResearch; let command = this.functions.enableResearch;
while(command.includes("{tech_name}")){ while (command.includes("{tech_name}")) {
command = command.replace("{tech_name}", key); command = command.replace("{tech_name}", key);
command = command.replace("{tech_researched}", difference[key].researched); command = command.replace("{tech_researched}", difference[key].researched);
command = command.replace("{tech_level}", difference[key].level); command = command.replace("{tech_level}", difference[key].level);
command = command.replace("{tech_infinite}", difference[key].infinite); command = command.replace("{tech_infinite}", difference[key].infinite);
} }
this.messageInterface(command); this.messageInterface(command);
console.log('Unlocking '+ key + ': ' + (difference[key].researched === 0 ? 'false' : 'true') + ' and level ' + difference[key].level + ', was '+ (this.research[key].researched === 0 ? 'false' : 'true') + ' at level ' + this.research[key].level); let log_message = '';
this.messageInterface("Unlocking research: " + key + " at research state = " + (difference[key].researched === 0 ? 'false' : 'true') + ' and level ' + difference[key].level); if (difference[key].infinite === 1) {
log_message = 'Unlocking infinite research ' + key + 'at level ' + this.research[key].level;
} else {
log_message = 'Unlocking research ' + key;
}
console.log(log_message);
this.messageInterface(log_message);
this.research[key] = difference[key]; this.research[key] = difference[key];
} }
}); });
needle.post(this.config.masterIP + ':' + this.config.masterPort + '/api/editSlaveMeta', { needle.post(this.config.masterIP + ':' + this.config.masterPort + '/api/editSlaveMeta', {
instanceID: this.config.unique, instanceID: this.config.unique,
password: this.config.clientPassword, password : this.config.clientPassword,
meta: {research: this.research} meta : {research: this.research}
}, {headers: {'x-access-token': this.config.masterAuthToken}}, function (err, resp) { }, {headers: {'x-access-token': this.config.masterAuthToken}}, function (err, resp) {
// success? // success?
}); });
@@ -118,22 +132,23 @@ class ResearchSync {
loadFunctions() { loadFunctions() {
return { return {
dumpResearch: this.loadFunc("dumpResearch.lua"), dumpResearch : this.loadFunc("dumpResearch.lua"),
enableResearch: this.loadFunc("enableResearch.lua"), enableResearch: this.loadFunc("enableResearch.lua"),
}; };
} }
loadFunc(path) { loadFunc(path) {
return fs.readFileSync("sharedPlugins/researchSync/" + path,'utf-8').replace(/\r?\n|\r/g,' '); return fs.readFileSync("sharedPlugins/researchSync/" + path, 'utf-8').replace(/\r?\n|\r/g, ' ');
} }
scriptOutput(data){
let kv = data.split(":"); scriptOutput(data) {
let name = kv[0]; let kv = data.split(":");
let researched = ('true' !== kv[1] let name = kv[0];
let researched = ('true' !== kv[1]
? 0 ? 0
: 1); : 1);
let level = parseInt(kv[2]); let level = parseInt(kv[2]);
let infinite = ('true' !== kv[3] let infinite = ('true' !== kv[3]
? 0 ? 0
: 1); : 1);
if (!isNaN(level) && !isNaN(researched)) { if (!isNaN(level) && !isNaN(researched)) {