Hardened NaN checks, added file read delay for research-sync so factorio has time to finish writing to the file

This commit is contained in:
psihius committed 2018-04-19 23:33:09 +03:00
1 parent ed9b4bbafa
commit 03511fc240
4 files changed
+47 -33

No files matched your search

+17 -11
View File
@@ -476,17 +476,23 @@ function instanceManagement() {
// send file contents to plugin for processing
function fileChangeHandler(eventType, filename) {
if(filename != null){
// get array of lines in file
let stuff = fs.readFileSync(instancedirectory + "/script-output/" + filename, "utf8").split("\n");
// if you found anything, reset the file
if (stuff[0]) {
fs.writeFileSync(instancedirectory + "/script-output/" + filename, "");
}
for(let i = 0; i < stuff.length; i++) {
if(stuff[i] && !stuff[i].includes('\u0000\u0000')) {
plugins[I].scriptOutput(stuff[i]);
}
}
setTimeout(
()=>{
// get array of lines in file
let stuff = fs.readFileSync(instancedirectory + "/script-output/" + filename, "utf8").split("\n");
// if you found anything, reset the file
if (stuff[0]) {
fs.writeFileSync(instancedirectory + "/script-output/" + filename, "");
}
for(let i = 0; i < stuff.length; i++) {
if(stuff[i] && !stuff[i].includes('\u0000\u0000')) {
plugins[I].scriptOutput(stuff[i]);
}
}
},
pluginConfig.fileReadDelay || 0
);
}
}
}
+2 -1
View File
@@ -11,4 +11,5 @@ module.exports = {
description: "syncs research across servers",
// We'll send everything in this file to your stdin. Beware.
scriptOutputFileSubscription: "researchSync.txt",
}
fileReadDelay: 50
}
@@ -1,10 +1,10 @@
/silent-command
if game.forces['player'].technologies['{tech_name}'] then
if game.forces['player'].technologies['{tech_name}'].researched ~= {tech_researched} then
game.forces['player'].technologies['{tech_name}'].researched={tech_researched}
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 }
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"})
+26 -19
View File
@@ -38,12 +38,15 @@ class ResearchSync {
if (slaveData[instanceKey].unique === this.config.unique.toString()) {
return;
}
if (!slaveData[instanceKey].meta.hasOwnProperty('research')) {
if (!slaveData[instanceKey].hasOwnProperty('meta') || !slaveData[instanceKey].meta.hasOwnProperty('research')) {
return;
}
let researchList = slaveData[instanceKey].meta.research;
if(researchList){
Object.keys(researchList).forEach(researchName => {
if (isNaN(researchList[researchName].researched) || isNaN(researchList[researchName].level)) {
return;
}
if (needResearch.hasOwnProperty(researchName)) {
if (needResearch[researchName].researched === 0) {
needResearch[researchName].researched = parseInt(researchList[researchName].researched);
@@ -62,15 +65,18 @@ class ResearchSync {
let difference = this.filterResearchDiff(this.research, needResearch);
Object.keys(difference).forEach((key) => {
let command = this.functions.enableResearch;
while(command.includes("{tech_name}")){
command = command.replace("{tech_name}", key);
command = command.replace("{tech_researched}", difference[key].researched);
command = command.replace("{tech_level}", difference[key].level);
if (this.research[key]) {
let command = this.functions.enableResearch;
while(command.includes("{tech_name}")){
command = command.replace("{tech_name}", key);
command = command.replace("{tech_researched}", difference[key].researched);
command = command.replace("{tech_level}", difference[key].level);
}
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);
this.messageInterface("Unlocking research: " + key + " at research state = " + (difference[key].researched === 0 ? 'false' : 'true') + ' and level ' + difference[key].level);
this.research[key] = difference[key];
}
this.messageInterface(command);
this.messageInterface("Unlocking research: " + key + " at research state = " + (difference[key].researched === 0 ? 'false' : 'true') + ' and level ' + difference[key].level);
this.research[key] = difference[key];
});
needle.post(this.config.masterIP + ':' + this.config.masterPort + '/api/editSlaveMeta', {
@@ -87,8 +93,10 @@ class ResearchSync {
let diff = {};
Object.keys(localResearch).forEach((key) => {
if (remoteResearch.hasOwnProperty(key)) {
if ((localResearch[key].researched === 0 && localResearch[key].researched !== remoteResearch[key].researched) || localResearch[key].level !== remoteResearch[key].level) {
diff[key] = remoteResearch[key];
if ((localResearch[key].researched === 0 && localResearch[key].researched !== remoteResearch[key].researched) || localResearch[key].level < remoteResearch[key].level) {
if (!isNaN(remoteResearch[key].researched) && !isNaN(remoteResearch[key].level)) {
diff[key] = {researched: remoteResearch[key].researched, level: remoteResearch[key].level};
}
}
}
});
@@ -106,15 +114,14 @@ class ResearchSync {
return fs.readFileSync("sharedPlugins/researchSync/" + path,'utf-8').replace(/\r?\n|\r/g,' ');
}
scriptOutput(data){
try {
let kv = data.split(":");
let name = kv[0];
let researched = ('true' !== kv[1]
? 0
: 1);
let level = parseInt(kv[2]);
let kv = data.split(":");
let name = kv[0];
let researched = ('true' !== kv[1]
? 0
: 1);
let level = parseInt(kv[2]);
if (!isNaN(level) && !isNaN(researched)) {
this.research[name] = {researched: researched, level: level};
} catch (e) {
}
}
}