Merge pull request #243 from Hornwitser/fix-researchsync

Try to fix researchSync
This commit is contained in:
Arvids Godjuks authored and GitHub committed 2019-10-12 21:48:09 +02:00
commit 475128c8c4
4 files changed
+112 -66

No files matched your search

+13
View File
@@ -1,6 +1,19 @@
Changelog Changelog
========= =========
Version 1.2.2
-------------
- Fixed possible crash with modded technologies named the same as a built-in
Object prototype property in researchSync.
- Fixed progress of a current infinite tech carrying over to the next one
when researching it and another node completes it in researchSync.
- Fixed progress of a previous infinite tech from another node being applied
to the current one in researchSync.
- Fixed crash in researchSync when modded technologies are present only on some
nodes.
Version 1.2.1 Version 1.2.1
------------- -------------
+1 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "clusterio", "name": "clusterio",
"version": "1.2.1", "version": "1.2.2-alpha",
"description": "Interserver communication for factorio", "description": "Interserver communication for factorio",
"repository": "https://github.com/Danielv123/factorioClusterio", "repository": "https://github.com/Danielv123/factorioClusterio",
"main": "master.js", "main": "master.js",
+12 -5
View File
@@ -11,15 +11,22 @@ local function updateTech()
end end
end end
if game.forces['player'].technologies['{tech_name}'] then local force = game.forces['player']
local tech = force.technologies['{tech_name}']
if tech then
if 1 == {tech_infinite} then if 1 == {tech_infinite} then
if (game.forces['player'].technologies['{tech_name}'].level ~= level) then if (tech.level ~= level) then
game.forces['player'].technologies['{tech_name}'].level = {tech_level} tech.level = {tech_level}
if tech == force.current_research then
force.research_progress = {tech_progress}
else
force.set_saved_technology_progress(tech.name, {tech_progress});
end
updateTech() updateTech()
end end
else else
if not game.forces['player'].technologies['{tech_name}'].researched then if not tech.researched then
game.forces['player'].technologies['{tech_name}'].researched = true tech.researched = true
updateTech() updateTech()
end end
end end
+86 -60
View File
@@ -169,21 +169,24 @@ class ResearchSync {
}) })
} }
clear_contribution_to_researched_techs() { clear_contribution_to_researched_techs() {
// if between updates tech was researched for (let [name, tech] of Object.entries(this.research)) {
if (!this.prev_research) if (!Object.hasOwnProperty.call(this.prev_research, name)) {
return continue;
for (let [name, research] of Object.entries(this.research)) { }
let researched
if (research.infinite)
researched = this.prev_research[name].level < research.level
else
researched = this.prev_research[name].researched < research.researched
if (researched) let researched;
research.contribution = 0 if (tech.infinite) {
} researched = this.prev_research[name].level < tech.level;
} } else {
researched = this.prev_research[name].researched < tech.researched;
}
if (researched) {
tech.contribution = 0;
}
}
}
get_cluster_techs(slavesData) { get_cluster_techs(slavesData) {
let cluster_techs = {} let cluster_techs = {}
@@ -193,7 +196,7 @@ class ResearchSync {
if (isNaN(node_tech.researched) || isNaN(node_tech.level) || isNaN(node_tech.infinite)) if (isNaN(node_tech.researched) || isNaN(node_tech.level) || isNaN(node_tech.infinite))
continue continue
if (cluster_techs[name]) { if (Object.hasOwnProperty.call(cluster_techs, name)) {
if (cluster_techs[name].infinite === 1 && cluster_techs[name].level < node_tech.level) { if (cluster_techs[name].infinite === 1 && cluster_techs[name].level < node_tech.level) {
cluster_techs[name].level = node_tech.level cluster_techs[name].level = node_tech.level
} else if (node_tech.researched > cluster_techs[name].researched) { } else if (node_tech.researched > cluster_techs[name].researched) {
@@ -208,65 +211,85 @@ class ResearchSync {
} }
recount_cluster_research_progress(slaves_data, cluster_researches) { recount_cluster_research_progress(slaves_data, cluster_researches) {
for (let [name, research] of Object.entries(cluster_researches)) for (let [name, tech] of Object.entries(cluster_researches)) {
research.progress = this.research[name].contribution if (Object.hasOwnProperty.call(this.research, name)) {
tech.progress = this.research[name].contribution
} else {
tech.progress = 0;
}
}
for (let slave_data of slaves_data) { for (let slave_data of slaves_data) {
for (let [name, research] of Object.entries(slave_data.meta.research)) { for (let [name, tech] of Object.entries(slave_data.meta.research)) {
if (!cluster_researches[name]) if (!Object.hasOwnProperty.call(cluster_researches, name)) {
continue continue;
if (isNaN(cluster_researches[name].progress) }
|| isNaN(research.contribution)
|| isNaN(research.level) if (
|| isNaN(cluster_researches[name].level)) isNaN(cluster_researches[name].progress)
continue || isNaN(tech.contribution)
if (cluster_researches[name].level === research.level) || isNaN(tech.level)
cluster_researches[name].progress += research.contribution || isNaN(cluster_researches[name].level)
) {
continue;
}
if (cluster_researches[name].level === tech.level) {
cluster_researches[name].progress += tech.contribution;
}
} }
} }
for (let [name, research] of Object.entries(cluster_researches)) { for (let [name, tech] of Object.entries(cluster_researches)) {
if (research.progress > 1) { if (Object.hasOwnProperty.call(this.research, name) && tech.progress > 1) {
research.progress = null tech.progress = null
research.researched = 1 tech.researched = 1
this.research[name].contribution = 0 this.research[name].contribution = 0
research.contribution = 0 tech.contribution = 0
if (this.research[name].level >= research.level) if (this.research[name].level >= tech.level)
research.level = this.research[name].level + 1 tech.level = this.research[name].level + 1
} }
} }
} }
filter_researched_techs(cluster_researches) { filter_researched_techs(cluster_researches) {
let local_researches = this.research
let result = {}; let result = {};
for (let key in local_researches) { for (let [name, tech] of Object.entries(this.research)) {
if (!cluster_researches[key]) if (!Object.hasOwnProperty.call(cluster_researches, name)) {
continue continue;
if (isNaN(cluster_researches[key].researched) || isNaN(cluster_researches[key].level)) }
if (isNaN(cluster_researches[name].researched) || isNaN(cluster_researches[name].level))
continue continue
let researched let researched
if (local_researches[key].infinite) if (tech.infinite) {
researched = local_researches[key].level < cluster_researches[key].level researched = tech.level < cluster_researches[name].level;
else } else {
researched = local_researches[key].researched < cluster_researches[key].researched researched = tech.researched < cluster_researches[name].researched;
}
if (researched) if (researched)
result[key] = cluster_researches[key] result[name] = cluster_researches[name]
} }
return result; return result;
} }
filter_updated_techs(cluster_techs, to_research) { filter_updated_techs(cluster_techs, to_research) {
let local_techs = this.research
let result = {} let result = {}
for (let name in local_techs) { for (let [name, tech] of Object.entries(this.research)) {
if (name in to_research || !cluster_techs[name]) if (Object.hasOwnProperty.call(to_research, name) || !Object.hasOwnProperty.call(cluster_techs, name)) {
continue continue;
}
if (isNaN(cluster_techs[name].progress)) if (isNaN(cluster_techs[name].progress))
continue continue
if (local_techs[name].progress < cluster_techs[name].progress)
// Do not update progress for infinite techs with a local level that's higher.
if (tech.infinite && tech.level > cluster_techs[name].level) {
continue;
}
if (tech.progress < cluster_techs[name].progress)
result[name] = cluster_techs[name] result[name] = cluster_techs[name]
} }
return result return result
@@ -274,8 +297,9 @@ class ResearchSync {
research_technologies(to_research) { research_technologies(to_research) {
for (let name of Object.keys(to_research)) for (let name of Object.keys(to_research))
if (!this.research[name]) if (!Object.hasOwnProperty.call(this.research, name)) {
delete to_research[name] delete to_research[name];
}
const notify = Object.keys(to_research).length === 1 const notify = Object.keys(to_research).length === 1
for (let [name, tech] of Object.entries(to_research)) { for (let [name, tech] of Object.entries(to_research)) {
@@ -283,9 +307,9 @@ class ResearchSync {
this.research[name].progress = null this.research[name].progress = null
let command = this.functions.enableResearch; let command = this.functions.enableResearch;
command = command.replace(/{tech_name}/g, name); command = command.replace(/{tech_name}/g, name);
command = command.replace(/{tech_researched}/g, tech.researched);
command = command.replace(/{tech_level}/g, tech.level); command = command.replace(/{tech_level}/g, tech.level);
command = command.replace(/{tech_infinite}/g, tech.infinite); command = command.replace(/{tech_infinite}/g, tech.infinite);
command = command.replace(/{tech_progress}/g, tech.progress)
command = command.replace(/{notify}/g, notify); command = command.replace(/{notify}/g, notify);
this.messageInterface(command); this.messageInterface(command);
let log_message = tech.infinite let log_message = tech.infinite
@@ -299,8 +323,9 @@ class ResearchSync {
update_technologies_progress(to_update) { update_technologies_progress(to_update) {
for (let [name, tech] of Object.entries(to_update)) { for (let [name, tech] of Object.entries(to_update)) {
if (!this.research[name]) if (!Object.hasOwnProperty.call(this.research, name)) {
continue continue;
}
let progress = this.research[name].progress let progress = this.research[name].progress
if (progress === null) if (progress === null)
progress = 'nil' progress = 'nil'
@@ -317,11 +342,10 @@ class ResearchSync {
} }
print_own_contribution() { print_own_contribution() {
if (!this.prev_research)
return
for (let [name, tech] of Object.entries(this.research)) { for (let [name, tech] of Object.entries(this.research)) {
if (!this.prev_research[name]) if (!Object.hasOwnProperty.call(this.prev_research, name)) {
continue continue;
}
let diff = this.research[name].contribution - this.prev_research[name].contribution let diff = this.research[name].contribution - this.prev_research[name].contribution
if (Math.abs(diff) > Number.EPSILON * 1000) if (Math.abs(diff) > Number.EPSILON * 1000)
this.log(`Own research ${name}: ${this.research[name].progress} += ${diff}`) this.log(`Own research ${name}: ${this.research[name].progress} += ${diff}`)
@@ -346,8 +370,10 @@ class ResearchSync {
if (isNaN(level) || isNaN(researched)) if (isNaN(level) || isNaN(researched))
return return
this.prev_research[name] = this.research[name]
if (!this.prev_research[name]) { if (Object.hasOwnProperty.call(this.research, name)) {
this.prev_research[name] = this.research[name];
} else {
this.prev_research[name] = { this.prev_research[name] = {
researched: null, researched: null,
level: null, level: null,