mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-22 16:13:12 +09:00
Make researchSync work for any research on any node
This commit is contained in:
1 parent
fa4c10dce2
commit
98c6795f8f
4 files changed
+208
-94
No files matched your search
@@ -1,11 +1,19 @@
|
|||||||
|
|
||||||
local data = {};
|
local data = {};
|
||||||
|
local force = game.forces["player"];
|
||||||
|
|
||||||
for _, tech in pairs(game.forces["player"].technologies) do
|
for _, tech in pairs(force.technologies) do
|
||||||
|
local progress;
|
||||||
|
if tech == force.current_research then
|
||||||
|
progress = force.research_progress
|
||||||
|
else
|
||||||
|
progress = force.get_saved_technology_progress(tech.name);
|
||||||
|
end
|
||||||
local tech_data = {
|
local tech_data = {
|
||||||
tech.name,
|
tech.name,
|
||||||
tostring(tech.researched),
|
tostring(tech.researched),
|
||||||
tostring(tech.level),
|
tostring(tech.level),
|
||||||
|
tostring(progress),
|
||||||
}
|
}
|
||||||
tech_data = table.concat(tech_data, ':')
|
tech_data = table.concat(tech_data, ':')
|
||||||
table.insert(data, tech_data);
|
table.insert(data, tech_data);
|
||||||
|
|||||||
@@ -6,24 +6,22 @@ function format_tech(tech) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
function log_tech_unlocking(key, current, prev) {
|
|
||||||
console.log(
|
|
||||||
`Unlocking ${key}: ${format_tech(current)}, was ${format_tech(prev)}`
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
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 = {
|
||||||
|
dumpResearch: this.loadFunc("dumpResearch.lua"),
|
||||||
|
enableResearch: this.loadFunc("enableResearch.lua"),
|
||||||
|
updateProgress: this.loadFunc("updateProgress.lua")
|
||||||
|
}
|
||||||
|
|
||||||
this.research = {};
|
this.research = {};
|
||||||
|
this.prev_research = {};
|
||||||
|
|
||||||
setInterval(() => {
|
setInterval(() => {
|
||||||
this.pollResearch();
|
this.pollResearch();
|
||||||
setTimeout(this.doSync.bind(this), 2000);
|
setTimeout(this.request_cluster_data.bind(this), 2000);
|
||||||
}, extras.researchSyncPollInterval || 5000);
|
}, extras.researchSyncPollInterval || 5000);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -32,99 +30,154 @@ class ResearchSync {
|
|||||||
this.messageInterface(this.functions.dumpResearch);
|
this.messageInterface(this.functions.dumpResearch);
|
||||||
}
|
}
|
||||||
|
|
||||||
doSync() {
|
request_cluster_data() {
|
||||||
const slaves_data_url = `${this.config.masterIP}:${this.config.masterPort}/api/slaves`
|
const slaves_data_url = `${this.config.masterIP}:${this.config.masterPort}/api/slaves`
|
||||||
needle.get(slaves_data_url, (err, resp, slavesData) => {
|
needle.get(slaves_data_url, this.sync_researches.bind(this))
|
||||||
if (err) {
|
}
|
||||||
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 slaves", resp.statusCode, resp.body);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
let needResearch = {};
|
sync_researches(err, resp, slaves_data) {
|
||||||
for (let slave_data of Object.values(slavesData)) {
|
if (err) {
|
||||||
if (slave_data.unique === this.config.unique.toString())
|
this.messageInterface("Unable to post JSON master/api/slaves, master might be unreachable");
|
||||||
continue;
|
return false;
|
||||||
|
}
|
||||||
|
if (resp.statusCode !== 200) {
|
||||||
|
this.messageInterface("got error when calling slaves", resp.statusCode, resp.body);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (!slave_data.meta || !slave_data.meta.research)
|
slaves_data = Object.values(slaves_data)
|
||||||
continue;
|
slaves_data = slaves_data.filter(
|
||||||
|
slave_data => slave_data.unique !== this.config.unique.toString()
|
||||||
|
&& slave_data.meta && slave_data.meta.research
|
||||||
|
)
|
||||||
|
|
||||||
let researchList = slave_data.meta.research;
|
let cluster_techs = this.get_cluster_techs(slaves_data)
|
||||||
for (let [researchName, research] of Object.entries(researchList)) {
|
this.recount_cluster_research_progress(slaves_data, cluster_techs)
|
||||||
if (isNaN(research.researched) || isNaN(research.level))
|
|
||||||
continue;
|
|
||||||
|
|
||||||
if (needResearch[researchName]) {
|
let to_research = this.filter_researched_techs(cluster_techs)
|
||||||
if (needResearch[researchName].researched === 0) {
|
let to_update_progress = this.filter_updated_techs(cluster_techs, to_research)
|
||||||
needResearch[researchName].researched = parseInt(research.researched);
|
|
||||||
}
|
|
||||||
if (needResearch[researchName].level < parseInt(research.level)) {
|
|
||||||
needResearch[researchName].level = parseInt(research.level);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
needResearch[researchName] = {
|
|
||||||
researched: parseInt(researchList[researchName].researched),
|
|
||||||
level: parseInt(researchList[researchName].level)
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let difference = this.filterResearchDiff(this.research, needResearch);
|
this.research_technologies(to_research)
|
||||||
|
this.update_technologies_progress(to_update_progress)
|
||||||
|
|
||||||
Object.keys(difference).forEach((key) => {
|
needle.post(this.config.masterIP + ':' + this.config.masterPort + '/api/editSlaveMeta', {
|
||||||
if (!this.research[key])
|
instanceID: this.config.unique,
|
||||||
return
|
password: this.config.clientPassword,
|
||||||
let command = this.functions.enableResearch;
|
meta: {research: this.research}
|
||||||
command = command.replace(/{tech_name}/g, key);
|
}, {headers: {'x-access-token': this.config.masterAuthToken}}, function (err, resp) {
|
||||||
command = command.replace(/{tech_researched}/g, difference[key].researched);
|
// success?
|
||||||
command = command.replace(/{tech_level}/g, difference[key].level);
|
|
||||||
this.messageInterface(command);
|
|
||||||
log_tech_unlocking(key, difference[key], this.research[key])
|
|
||||||
this.messageInterface(
|
|
||||||
`Unlocking research: ${key} with research state ${format_tech(difference[key])}`
|
|
||||||
);
|
|
||||||
this.research[key] = difference[key];
|
|
||||||
});
|
|
||||||
|
|
||||||
needle.post(this.config.masterIP + ':' + this.config.masterPort + '/api/editSlaveMeta', {
|
|
||||||
instanceID: this.config.unique,
|
|
||||||
password: this.config.clientPassword,
|
|
||||||
meta: {research: this.research}
|
|
||||||
}, {headers: {'x-access-token': this.config.masterAuthToken}}, function (err, resp) {
|
|
||||||
// success?
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
filterResearchDiff(localResearch, remoteResearch) {
|
get_cluster_techs(slavesData) {
|
||||||
let diff = {};
|
let researches = {}
|
||||||
for (let key in localResearch) {
|
for (let slave_data of slavesData) {
|
||||||
if (!remoteResearch[key])
|
let node_researches = slave_data.meta.research
|
||||||
continue
|
for (let [name, research] of Object.entries(node_researches)) {
|
||||||
if (isNaN(remoteResearch[key].researched) || isNaN(remoteResearch[key].level))
|
if (isNaN(research.researched) || isNaN(research.level))
|
||||||
continue
|
continue
|
||||||
|
|
||||||
if (remoteResearch[key].researched > localResearch[key].researched
|
if (researches[name]) {
|
||||||
|| remoteResearch[key].level > localResearch[key].level) {
|
if (researches[name].researched === 0) {
|
||||||
diff[key] = {
|
researches[name].researched = parseInt(research.researched)
|
||||||
researched: remoteResearch[key].researched,
|
}
|
||||||
level: remoteResearch[key].level
|
if (researches[name].level < parseInt(research.level)) {
|
||||||
};
|
researches[name].level = parseInt(research.level)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
researches[name] = {
|
||||||
|
researched: parseInt(research.researched),
|
||||||
|
level: parseInt(research.level)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return diff;
|
return researches
|
||||||
}
|
}
|
||||||
|
|
||||||
loadFunctions() {
|
recount_cluster_research_progress(slaves_data, cluster_researches) {
|
||||||
return {
|
for (let research of Object.values(cluster_researches))
|
||||||
dumpResearch: this.loadFunc("dumpResearch.lua"),
|
research.progress = research.contribution
|
||||||
enableResearch: this.loadFunc("enableResearch.lua"),
|
|
||||||
};
|
for (let slave_data of slaves_data)
|
||||||
|
for (let [name, research] of Object.entries(slave_data.meta.research))
|
||||||
|
if (!isNaN(cluster_researches[name].progress) && !isNaN(research.contribution))
|
||||||
|
cluster_researches[name].progress += research.contribution
|
||||||
|
|
||||||
|
for (let [name, research] of Object.entries(cluster_researches)) {
|
||||||
|
if (research.progress > 1) {
|
||||||
|
research.progress = null
|
||||||
|
research.researched = 1
|
||||||
|
if (this.research[name].level >= research.level)
|
||||||
|
research.level = this.research[name].level + 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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))
|
||||||
|
continue
|
||||||
|
|
||||||
|
if (cluster_researches[key].researched > local_researches[key].researched
|
||||||
|
|| cluster_researches[key].level > local_researches[key].level) {
|
||||||
|
result[key] = cluster_researches[key]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
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
|
||||||
|
if (isNaN(cluster_techs[name].progress))
|
||||||
|
continue
|
||||||
|
if (local_techs[name].progress < cluster_techs[name].progress)
|
||||||
|
result[name] = cluster_techs[name]
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
research_technologies(to_research) {
|
||||||
|
for (let [name, tech] of Object.entries(to_research)) {
|
||||||
|
if (!this.research[name])
|
||||||
|
continue
|
||||||
|
let command = this.functions.enableResearch;
|
||||||
|
command = command.replace(/{tech_name}/g, key);
|
||||||
|
command = command.replace(/{tech_researched}/g, tech.researched);
|
||||||
|
command = command.replace(/{tech_level}/g, tech.level);
|
||||||
|
this.messageInterface(command);
|
||||||
|
console.log(
|
||||||
|
`Unlocking ${name}: ${format_tech(tech)}, was ${format_tech(this.research[name])}`
|
||||||
|
);
|
||||||
|
this.messageInterface(
|
||||||
|
`Unlocking research: ${name} with research state ${format_tech(tech)}`
|
||||||
|
);
|
||||||
|
this.research[key] = tech;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
update_technologies_progress(to_update) {
|
||||||
|
for (let [name, tech] of Object.entries(to_update)) {
|
||||||
|
if (!this.research[name])
|
||||||
|
continue
|
||||||
|
let command = this.functions.updateProgress
|
||||||
|
command = command.replace(/{tech_name}/g, name)
|
||||||
|
command = command.replace(/{last_check_progress}/g, this.research[name].progress)
|
||||||
|
command = command.replace(/{new_progress}/g, tech.progress)
|
||||||
|
this.messageInterface(command);
|
||||||
|
console.log(
|
||||||
|
`Updating ${name}: progress += ${tech.progress - this.research[name].progress}`
|
||||||
|
);
|
||||||
|
this.research[name].progress = tech.progress
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
loadFunc(path) {
|
loadFunc(path) {
|
||||||
@@ -134,11 +187,36 @@ class ResearchSync {
|
|||||||
return command;
|
return command;
|
||||||
}
|
}
|
||||||
scriptOutput(data) {
|
scriptOutput(data) {
|
||||||
let [name, researched, level] = data.split(":")
|
let [name, researched, level, progress] = data.split(":")
|
||||||
researched = +(researched === 'true');
|
researched = +(researched === 'true');
|
||||||
level = parseInt(level);
|
level = parseInt(level);
|
||||||
if (!isNaN(level) && !isNaN(researched)) {
|
if (progress === 'nil')
|
||||||
this.research[name] = {researched: researched, level: level};
|
progress = null
|
||||||
|
else
|
||||||
|
progress = parseFloat(progress)
|
||||||
|
|
||||||
|
if (isNaN(level) || isNaN(researched))
|
||||||
|
return
|
||||||
|
this.prev_research[name] = this.research[name]
|
||||||
|
if (!this.prev_research[name]) {
|
||||||
|
this.prev_research[name] = {
|
||||||
|
researched: null,
|
||||||
|
level: null,
|
||||||
|
progress: null,
|
||||||
|
contribution: 0,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
this.research[name] = {
|
||||||
|
researched,
|
||||||
|
level,
|
||||||
|
progress,
|
||||||
|
contribution: this.prev_research[name].contribution
|
||||||
|
}
|
||||||
|
if (this.prev_research[name].progress && this.research[name].progress) {
|
||||||
|
// this.prev_research[name].progress gets updated to overall cluster progress
|
||||||
|
// therefore contribution should be own research progress change over sync interval
|
||||||
|
let contribution = this.research[name].progress - this.prev_research[name].progress
|
||||||
|
this.research[name].contribution += contribution
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ describe("researchSync/index.js", ()=>{
|
|||||||
assert.equal(typeof researchSync, "function");
|
assert.equal(typeof researchSync, "function");
|
||||||
});
|
});
|
||||||
describe("class researchSync()", ()=>{
|
describe("class researchSync()", ()=>{
|
||||||
it(".filterResearchDiff(object1, object2) returns research that needs enabling on the local instance", ()=>{
|
it(".filter_researched_techs(object1, object2) returns research that needs enabling on the local instance", ()=>{
|
||||||
let reSync = new researchSync({}, function(){});
|
let reSync = new researchSync({}, function(){});
|
||||||
|
|
||||||
let obj1 = {
|
let obj1 = {
|
||||||
@@ -24,9 +24,9 @@ describe("researchSync/index.js", ()=>{
|
|||||||
c:{researched: false, level: 0},
|
c:{researched: false, level: 0},
|
||||||
e:{researched: false, level: 50},
|
e:{researched: false, level: 50},
|
||||||
};
|
};
|
||||||
assert(typeof reSync.filterResearchDiff == "function");
|
assert(typeof reSync.filter_researched_techs == "function");
|
||||||
|
|
||||||
let diffResult = reSync.filterResearchDiff(obj1, obj2);
|
let diffResult = reSync.filter_researched_techs(obj1, obj2);
|
||||||
|
|
||||||
assert(diffResult.a === undefined);
|
assert(diffResult.a === undefined);
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,28 @@
|
|||||||
|
|
||||||
|
local function get_tech_progress(tech)
|
||||||
|
if tech == force.current_research then
|
||||||
|
return force.research_progress
|
||||||
|
else
|
||||||
|
return force.get_saved_technology_progress(tech.name);
|
||||||
|
end
|
||||||
|
end
|
||||||
|
local function set_tech_progress(tech, progress)
|
||||||
|
if tech == force.current_research then
|
||||||
|
force.research_progress = progress
|
||||||
|
else
|
||||||
|
force.set_saved_technology_progress(tech.name, progress);
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local force = game.force['player']
|
||||||
|
local tech_name = {tech_name}
|
||||||
|
local tech = force.technologies[tech_name]
|
||||||
|
local last_check_progress = {last_check_progress}
|
||||||
|
local new_progress = {new_progress}
|
||||||
|
|
||||||
|
local progress_delta = get_tech_progress(tech) - last_check_progress
|
||||||
|
new_progress = new_progress + progress_delta
|
||||||
|
if new_progress > 1 then
|
||||||
|
new_progress = 1
|
||||||
|
end
|
||||||
|
set_tech_progress(tech, new_progress)
|
||||||
Reference in new issue
Block a user