Fix incorrect property checks in researchSync

Fix naive checks for properties being set of the form obj[key] into the
proper form Object.hasOwnProperty.call(obj, key).  The reason for this
is that if the key has a value of a built-in property like 'constructor'
then a check of the form obj[key] will always return true even if that
property hasn't been set on the object.
This commit is contained in:
Hornwitser committed 2019-10-12 17:07:07 +02:00
1 parent a9cc743106
commit 6a6274e85c
3 files changed
+46 -32

No files matched your search

+7
View File
@@ -1,6 +1,13 @@
Changelog
=========
Version 1.2.2
-------------
- Fixed possible crash with modded technologies named the same as a built-in
Object prototype property in researchSync.
Version 1.2.1
-------------
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "clusterio",
"version": "1.2.1",
"version": "1.2.2-alpha",
"description": "Interserver communication for factorio",
"repository": "https://github.com/Danielv123/factorioClusterio",
"main": "master.js",
+38 -31
View File
@@ -170,10 +170,11 @@ class ResearchSync {
}
clear_contribution_to_researched_techs() {
// if between updates tech was researched
if (!this.prev_research)
return
for (let [name, research] of Object.entries(this.research)) {
if (!Object.hasOwnProperty.call(this.prev_research, name)) {
continue;
}
let researched
if (research.infinite)
researched = this.prev_research[name].level < research.level
@@ -193,7 +194,7 @@ class ResearchSync {
if (isNaN(node_tech.researched) || isNaN(node_tech.level) || isNaN(node_tech.infinite))
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) {
cluster_techs[name].level = node_tech.level
} else if (node_tech.researched > cluster_techs[name].researched) {
@@ -213,8 +214,9 @@ class ResearchSync {
for (let slave_data of slaves_data) {
for (let [name, research] of Object.entries(slave_data.meta.research)) {
if (!cluster_researches[name])
continue
if (!Object.hasOwnProperty.call(cluster_researches, name)) {
continue;
}
if (isNaN(cluster_researches[name].progress)
|| isNaN(research.contribution)
|| isNaN(research.level)
@@ -238,35 +240,37 @@ class ResearchSync {
}
filter_researched_techs(cluster_researches) {
let local_researches = this.research
let result = {};
for (let key in local_researches) {
if (!cluster_researches[key])
continue
if (isNaN(cluster_researches[key].researched) || isNaN(cluster_researches[key].level))
for (let [name, tech] of Object.entries(this.research)) {
if (!Object.hasOwnProperty.call(cluster_researches, name)) {
continue;
}
if (isNaN(cluster_researches[name].researched) || isNaN(cluster_researches[name].level))
continue
let researched
if (local_researches[key].infinite)
researched = local_researches[key].level < cluster_researches[key].level
else
researched = local_researches[key].researched < cluster_researches[key].researched
if (tech.infinite) {
researched = tech.level < cluster_researches[name].level;
} else {
researched = tech.researched < cluster_researches[name].researched;
}
if (researched)
result[key] = cluster_researches[key]
result[name] = cluster_researches[name]
}
return result;
}
filter_updated_techs(cluster_techs, to_research) {
let local_techs = this.research
let result = {}
for (let name in local_techs) {
if (name in to_research || !cluster_techs[name])
continue
for (let [name, tech] of Object.entries(this.research)) {
if (Object.hasOwnProperty.call(to_research, name) || !Object.hasOwnProperty.call(cluster_techs, name)) {
continue;
}
if (isNaN(cluster_techs[name].progress))
continue
if (local_techs[name].progress < cluster_techs[name].progress)
if (tech.progress < cluster_techs[name].progress)
result[name] = cluster_techs[name]
}
return result
@@ -274,8 +278,9 @@ class ResearchSync {
research_technologies(to_research) {
for (let name of Object.keys(to_research))
if (!this.research[name])
delete to_research[name]
if (!Object.hasOwnProperty.call(this.research, name)) {
delete to_research[name];
}
const notify = Object.keys(to_research).length === 1
for (let [name, tech] of Object.entries(to_research)) {
@@ -299,8 +304,9 @@ class ResearchSync {
update_technologies_progress(to_update) {
for (let [name, tech] of Object.entries(to_update)) {
if (!this.research[name])
continue
if (!Object.hasOwnProperty.call(this.research, name)) {
continue;
}
let progress = this.research[name].progress
if (progress === null)
progress = 'nil'
@@ -317,11 +323,10 @@ class ResearchSync {
}
print_own_contribution() {
if (!this.prev_research)
return
for (let [name, tech] of Object.entries(this.research)) {
if (!this.prev_research[name])
continue
if (!Object.hasOwnProperty.call(this.prev_research, name)) {
continue;
}
let diff = this.research[name].contribution - this.prev_research[name].contribution
if (Math.abs(diff) > Number.EPSILON * 1000)
this.log(`Own research ${name}: ${this.research[name].progress} += ${diff}`)
@@ -346,8 +351,10 @@ class ResearchSync {
if (isNaN(level) || isNaN(researched))
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] = {
researched: null,
level: null,