Port Research Sync plugin

Rewriwe the researchSync plugin into plugins/research_sync.
This commit is contained in:
Hornwitser committed 2020-03-17 01:31:51 +01:00
1 parent 737c9924b9
commit 9dac57b36b
13 files changed
+570 -615

No files matched your search

+80
View File
@@ -0,0 +1,80 @@
const link = require("lib/link");
// schema used for syncing technologies to and from the master
const technologies = {
type: "array",
items: {
type: "array",
minItems: 4,
maxItems: 4,
items: [
{ type: "string" },
{ type: "integer" },
{ type: ["null", "number"] },
{ type: "boolean" },
]
},
};
module.exports = {
name: "research_sync",
title: "Research Sync",
description: "Synchronises technology research progress between instances.",
version: "2.0.0-alpha",
instanceEntrypoint: "instance",
masterEntrypoint: "master",
messages: {
contribution: new link.Event({
type: "research_sync:contribution",
links: ["instance-slave", "slave-master"],
forwardTo: "master",
eventProperties: {
"name": { type: "string" },
"level": { type: "integer" },
"contribution": { type: "number" },
},
}),
progress: new link.Event({
type: "research_sync:progress",
links: ["master-slave", "slave-instance"],
broadcastTo: "instance",
eventProperties: {
"technologies": {
type: "array",
items: {
additionalProperties: false,
required: ["name", "level", "progress"],
properties: {
"name": { type: "string" },
"level": { type: "integer" },
"progress": { type: "number" },
},
},
},
},
}),
finished: new link.Event({
type: "research_sync:finished",
links: ["instance-slave", "slave-master", "master-slave", "slave-instance"],
forwardTo: "master",
broadcastTo: "instance",
eventProperties: {
"name": { type: "string" },
"level": { type: "integer" },
},
}),
syncTechnologies: new link.Request({
type: "research_sync:sync_technologies",
links: ["instance-slave", "slave-master"],
forwardTo: "master",
requestProperties: {
"technologies": technologies,
},
responseProperties: {
"technologies": technologies,
},
}),
},
}
+81
View File
@@ -0,0 +1,81 @@
const needle = require("needle");
const plugin = require("lib/plugin");
const luaTools = require("lib/luaTools");
function unexpectedError(err) {
console.log("Unexpected error in research_sync")
console.log("---------------------------------")
console.log(err);
}
class InstancePlugin extends plugin.BaseInstancePlugin {
async init() {
this.instance.server.on("ipc-research_sync:contribution", (tech) => {
this.researchContribution(tech).catch(unexpectedError);
});
this.instance.server.on("ipc-research_sync:finished", (tech) => {
this.researchFinished(tech).catch(unexpectedError);
});
}
async researchContribution(tech) {
this.info.messages.contribution.send(this.instance, tech);
}
async progressEventHandler(message) {
let techsJson = luaTools.escapeString(JSON.stringify(message.data.technologies));
await this.instance.server.sendRcon(`/sc research_sync.update_progress("${techsJson}")`, true);
}
async researchFinished(tech) {
this.info.messages.finished.send(this.instance, tech);
}
async finishedEventHandler(message) {
let { name, level, researched } = message.data;
await this.instance.server.sendRcon(
`/sc research_sync.research_technology("${luaTools.escapeString(name)}", ${level})`, true
);
}
async onStart() {
let dumpJson = await this.instance.server.sendRcon("/sc research_sync.dump_technologies()");
let techsToSend = [];
let instanceTechs = new Map();
for (let tech of JSON.parse(dumpJson)) {
techsToSend.push([
tech.name,
tech.level,
tech.progress || null,
tech.researched,
]);
instanceTechs.set(tech.name, tech);
}
let response = await this.info.messages.syncTechnologies.send(this.instance, { technologies: techsToSend });
let techsToSync = [];
for (let masterTech of response.technologies) {
let [name, level, progress, researched] = masterTech;
let instanceTech = instanceTechs.get(name);
if (
!instanceTech
|| instanceTech.level !== level
|| (instanceTech.progress || null) !== progress
|| instanceTech.researched !== researched
) {
techsToSync.push(masterTech);
}
}
if (techsToSync.length) {
let syncJson = luaTools.escapeString(JSON.stringify(techsToSync));
await this.instance.server.sendRcon(`/sc research_sync.sync_technologies("${syncJson}")`, true);
}
}
}
module.exports = {
InstancePlugin,
}
+184
View File
@@ -0,0 +1,184 @@
const fs = require("fs-extra");
const path = require("path");
const plugin = require("lib/plugin");
class MasterPlugin extends plugin.BaseMasterPlugin {
async init() {
this.technologies = await loadTechnologies(this.master.config);
this.lastProgressBroadcast = Date.now();
this.progressBroadcastId = null;
this.progressToBroadcast = new Set();
}
async onExit() {
if (this.progressBroadcastId) {
clearTimeout(this.progressBroadcastId);
}
await saveTechnologies(this.master.config, this.technologies);
}
registerProgress() {
// Rate limit progress broadcasts to one per second
if (Date.now() < this.lastProgressBroadcast + 1000) {
if (!this.progressBroadcastId) {
this.progressBroadcastId = setTimeout(() => {
this.broadcastProgress();
this.progressBroadcastId = null;
}, 2000);
}
} else {
if (this.progressBroadcastId) {
clearTimeout(this.progressBroadcastId);
}
this.broadcastProgress();
}
}
broadcastProgress() {
let techs = []
for (let name of this.progressToBroadcast) {
let tech = this.technologies.get(name);
if (tech.progress) {
techs.push({ name, level: tech.level, progress: tech.progress });
}
}
this.progressToBroadcast.clear();
this.lastProgressBroadcast = Date.now();
if (techs.length) {
for (let slaveConnection of this.master.slaveConnections.values()) {
this.info.messages.progress.send(slaveConnection, { technologies: techs });
}
}
}
async contributionEventHandler(message) {
let { name, level, contribution } = message.data;
let tech = this.technologies.get(name);
if (!tech) {
tech = { level, progress: 0, researched: false };
this.technologies.set(name, tech)
// Ignore contribution to already researched technologies
} else if (tech.level > level || tech.level === level && tech.researched) {
return;
}
let newProgress = tech.progress + contribution;
if (newProgress < 1) {
tech.progress = newProgress;
this.progressToBroadcast.add(name);
this.registerProgress();
} else {
tech.researched = true;
tech.progress = null;
this.progressToBroadcast.delete(name);
for (let slaveConnection of this.master.slaveConnections.values()) {
this.info.messages.finished.send(slaveConnection, {
name,
level: tech.level
});
}
}
}
async finishedEventHandler(message) {
let { name, level } = message.data;
let tech = this.technologies.get(name);
if (!tech || tech.level <= level) {
this.progressToBroadcast.delete(name);
this.technologies.set(name, { level, progress: null, researched: true })
}
}
async syncTechnologiesRequestHandler(message) {
function baseLevel(name) {
let match = /-(\d+)$/.exec(name);
if (!match) {
return 1;
} else {
return Number.parseInt(match[1], 10);
}
}
for (let instanceTech of message.data.technologies) {
let [name, level, progress, researched] = instanceTech;
let tech = this.technologies.get(name);
if (!tech) {
this.technologies.set(name, { level, progress, researched });
if (progress) {
this.progressToBroadcast.add(name);
} else if (researched || baseLevel(name) != level) {
for (let slaveConnection of this.master.slaveConnections.values()) {
this.info.messages.finished.send(slaveConnection, { name, level });
}
}
} else {
if (tech.level > level || tech.level === level && tech.researched) {
continue;
}
if (tech.level < level || researched) {
tech.level = level;
tech.progress = progress;
tech.researched = researched;
for (let slaveConnection of this.master.slaveConnections.values()) {
this.info.messages.finished.send(slaveConnection, { name, level });
}
if (progress) {
this.progressToBroadcast.add(name);
} else {
this.progressToBroadcast.delete(name);
}
} else if (tech.progress < progress) {
tech.progress = progress;
this.progressToBroadcast.add(name);
}
}
}
this.registerProgress();
let technologies = [];
for (let [name, tech] of this.technologies) {
technologies.push([ name, tech.level, tech.progress, tech.researched ])
}
return { technologies }
}
}
async function loadTechnologies(masterConfig) {
let filePath = path.join(masterConfig.get("master.database_directory"), "technologies.json");
console.log(`Loading ${filePath}`);
try {
return new Map(JSON.parse(await fs.readFile(filePath)));
} catch (err) {
if (err.code === "ENOENT") {
console.log("Creating new technologies database");
return new Map();
} else {
throw err;
}
}
}
async function saveTechnologies(masterConfig, technologies) {
let filePath = path.join(masterConfig.get("master.database_directory"), "technologies.json");
console.log(`writing ${filePath}`);
await fs.outputFile(filePath, JSON.stringify([...technologies.entries()], null, 4));
}
module.exports = {
MasterPlugin
}
+4
View File
@@ -0,0 +1,4 @@
{
"name": "research_sync",
"load": ["sync.lua"]
}
+215
View File
@@ -0,0 +1,215 @@
local clusterio_api = require("modules/clusterio/api")
local sync = {}
local function get_technology_progress(tech)
if tech == tech.force.current_research then
return tech.force.research_progress
else
return tech.force.get_saved_technology_progress(tech.name)
end
end
local function set_technology_progress(tech, progress)
if tech == tech.force.current_research then
tech.force.research_progress = progress
else
tech.force.set_saved_technology_progress(tech.name, progress)
end
end
sync.events = {}
sync.events[clusterio_api.events.on_server_startup] = function(event)
if not global.research_sync then
global.research_sync = {
technologies = {},
}
end
-- Used when syncing completed technologies from the master
global.research_sync.ignore_research_finished = false
local force = game.forces["player"]
for _, tech in pairs(force.technologies) do
if tech.enabled then
local progress = get_technology_progress(tech)
global.research_sync.technologies[tech.name] = {
level = tech.level,
researched = tech.researched,
progress = progress,
}
end
end
end
local function get_contribution(tech)
local progress = get_technology_progress(tech)
if not progress then
return 0, nil
end
local prev_tech = global.research_sync.technologies[tech.name]
if prev_tech.progress and prev_tech.level == tech.level then
return progress - prev_tech.progress, progress
else
return progress, progress
end
end
local function send_contribution(tech)
local contribution, progress = get_contribution(tech)
if contribution ~= 0 then
clusterio_api.send_json("research_sync:contribution", {
name = tech.name,
level = tech.level,
contribution = contribution,
})
global.research_sync.technologies[tech.name].progress = progress
end
end
sync.events[defines.events.on_research_started] = function(event)
local tech = event.last_research
if tech then
send_contribution(tech)
end
end
sync.events[defines.events.on_research_finished] = function(event)
if global.research_sync.ignore_research_finished then
return
end
local tech = event.research
global.research_sync.technologies[tech.name] = {
level = tech.level,
researched = tech.researched,
}
local level = tech.level
if not tech.researched then
level = level - 1
end
clusterio_api.send_json("research_sync:finished", {
name = tech.name,
level = level,
})
end
sync.on_nth_tick = {}
sync.on_nth_tick[79] = function(event)
local tech = game.forces["player"].current_research
if tech then
send_contribution(tech)
end
end
research_sync = {}
function research_sync.dump_technologies()
local force = game.forces["player"]
local techs = {}
for _, tech in pairs(force.technologies) do
if tech.enabled then
table.insert(techs, {
name = tech.name,
level = tech.level,
progress = get_technology_progress(tech),
researched = tech.researched,
})
end
end
if #techs == 0 then
rcon.print("[]")
else
rcon.print(game.table_to_json(techs))
end
end
function research_sync.sync_technologies(data)
local force = game.forces["player"]
local nameIndex = 1
local levelIndex = 2
local progressIndex = 3
local researchedIndex = 4
global.research_sync.ignore_research_finished = true
for _, tech_data in pairs(game.json_to_table(data)) do
local tech = force.technologies[tech_data[nameIndex]]
if tech and tech.enabled and tech.level <= tech_data[levelIndex] then
tech.level = tech_data[levelIndex]
local progress
if tech_data[researchedIndex] then
tech.researched = true
progress = nil
elseif tech_data[progressIndex] then
send_contribution(tech)
progress = tech_data[progressIndex]
set_technology_progress(tech, progress)
else
progress = get_technology_progress(tech)
end
global.research_sync.technologies[tech.name] = {
level = tech.level,
researched = tech.researched,
progress = progress,
}
end
end
global.research_sync.ignore_research_finished = false
end
function research_sync.update_progress(data)
local techs = game.json_to_table(data)
local force = game.forces["player"]
for _, masterTech in ipairs(techs) do
local tech = force.technologies[masterTech.name]
if tech and tech.enabled and tech.level == masterTech.level then
send_contribution(tech)
set_technology_progress(tech, masterTech.progress)
global.research_sync.technologies[tech.name] = {
level = tech.level,
progress = masterTech.progress
}
end
end
end
function research_sync.research_technology(name, level)
local force = game.forces["player"]
local tech = force.technologies[name]
if not tech or not tech.enabled or tech.level > level then
return
end
global.research_sync.ignore_research_finished = true
if tech == force.current_research and tech.level == level then
force.research_progress = 1
elseif tech.level < level or tech.level == level and not tech.researched then
tech.level = level
tech.researched = true
if tech.name:find("-%d+$") then
game.print {"", "Researched ", {"technology-name." .. tech.name:gsub("-%d+$", "")}, level}
else
game.print {"", "Researched ", {"technology-name." .. tech.name}}
end
game.play_sound { path = "utility/research_completed" }
end
global.research_sync.ignore_research_finished = false
global.research_sync.technologies[tech.name] = {
level = tech.level,
researched = tech.researched,
}
end
return sync
+5
View File
@@ -0,0 +1,5 @@
// These are libraries that does not have any units test, they are
// required here in order to track coverage for them.
require('../instance');
require('../master');