research_sync: cluster research queue on the controller

- QueueUpdateEvent, QueueEvent and QueueSyncRequest messages.
- The controller keeps one queue (max 7 names) in research_queue.json, saved with the other data.
- An update that changes the queue replaces it and is broadcast as QueueEvent; an equal update is ignored.
- QueueSyncRequest returns the queue and seeds it from the request when the queue is empty.
- A finished technology (completing contribution, advancing FinishedEvent, technology sync) drops its first queue entry without a broadcast.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
ILLISISandClaude Opus 5.5 committed 2026-10-18 21:31:59 +11:00
1 parent 43243eab86
commit e3099e1e05
4 files changed
+265

No files matched your search

+70
View File
@@ -13,6 +13,9 @@ import {
TechnologySync,
SyncTechnologiesRequest,
TechnologyProgress,
QueueUpdateEvent,
QueueEvent,
QueueSyncRequest,
} from "./messages.js";
type Technology = {
@@ -49,12 +52,58 @@ async function saveTechnologies(
await lib.safeOutputFile(filePath, JSON.stringify([...technologies.entries()], null, "\t"));
}
const MAX_QUEUE = 7;
async function loadQueue(controllerConfig: lib.ControllerConfig, logger: lib.Logger): Promise<string[]> {
let filePath = path.join(controllerConfig.get("controller.database_directory"), "research_queue.json");
logger.verbose(`Loading ${filePath}`);
try {
return JSON.parse(await fs.readFile(filePath, "utf8"));
} catch (err: any) {
if (err.code === "ENOENT") {
logger.verbose("Creating new research queue");
return [];
}
throw err;
}
}
async function saveQueue(controllerConfig: lib.ControllerConfig, queue: string[], logger: lib.Logger) {
let filePath = path.join(controllerConfig.get("controller.database_directory"), "research_queue.json");
logger.verbose(`writing ${filePath}`);
await lib.safeOutputFile(filePath, JSON.stringify(queue, null, "\t"));
}
export default async function(context: ControllerPluginContext) {
const { controller, logger, plugin } = context;
const technologies = await loadTechnologies(controller.config, logger);
let technologiesDirty = true;
let queue = await loadQueue(controller.config, logger);
let queueDirty = false;
const progressToBroadcast = new Set<string>();
function instanceName(src: lib.Address) {
return controller.instances.get(src.id)?.config.get("instance.name") ?? String(src.id);
}
function removeFinished(name: string) {
let index = queue.indexOf(name);
if (index !== -1) {
queue.splice(index, 1);
queueDirty = true;
}
}
function setQueue(newQueue: string[], player: string | null, src: lib.Address) {
newQueue = newQueue.slice(0, MAX_QUEUE);
if (newQueue.length === queue.length && newQueue.every((name, i) => name === queue[i])) {
return;
}
queue = newQueue;
queueDirty = true;
controller.sendTo("allInstances", new QueueEvent(queue, player, instanceName(src)));
}
function broadcastProgress() {
let techs = [];
for (let name of progressToBroadcast) {
@@ -109,6 +158,7 @@ export default async function(context: ControllerPluginContext) {
tech.researched = true;
tech.progress = null;
progressToBroadcast.delete(name);
removeFinished(name);
controller.sendTo("allInstances", new FinishedEvent(name, tech.level));
}
@@ -118,6 +168,9 @@ export default async function(context: ControllerPluginContext) {
controller.handle(FinishedEvent, async (event: FinishedEvent) => {
let { name, level } = event;
let tech = technologies.get(name);
if (!tech || tech.level < level || (tech.level === level && !tech.researched)) {
removeFinished(name);
}
if (!tech || tech.level <= level) {
controller.sendTo("allInstances", event);
progressToBroadcast.delete(name);
@@ -145,6 +198,7 @@ export default async function(context: ControllerPluginContext) {
progressToBroadcast.add(name);
} else if (researched || baseLevel(name) !== level) {
controller.sendTo("allInstances", new FinishedEvent(name, level));
removeFinished(name);
}
} else {
@@ -156,6 +210,7 @@ export default async function(context: ControllerPluginContext) {
// Send update if the unlocked level is greater
if (level - Number(!researched) > tech.level - Number(!tech.researched)) {
controller.sendTo("allInstances", new FinishedEvent(name, level - Number(!researched)));
removeFinished(name);
}
tech.level = level;
tech.progress = progress;
@@ -185,7 +240,22 @@ export default async function(context: ControllerPluginContext) {
return result;
});
controller.handle(QueueUpdateEvent, async (event: QueueUpdateEvent, src: lib.Address) => {
setQueue(event.queue, event.player, src);
});
controller.handle(QueueSyncRequest, async (request: QueueSyncRequest, src: lib.Address): Promise<string[]> => {
if (request.queue?.length && !queue.length) {
setQueue(request.queue, request.player, src);
}
return queue;
});
controller.hooks.save.attach(plugin.name, async () => {
if (queueDirty) {
queueDirty = false;
await saveQueue(controller.config, queue, logger);
}
if (technologiesDirty) {
technologiesDirty = false;
await saveTechnologies(controller.config, technologies, logger);
+3
View File
@@ -19,5 +19,8 @@ export const plugin: lib.PluginDeclaration = {
messages.ProgressEvent,
messages.FinishedEvent,
messages.SyncTechnologiesRequest,
messages.QueueUpdateEvent,
messages.QueueEvent,
messages.QueueSyncRequest,
],
};
+73
View File
@@ -136,3 +136,76 @@ export class SyncTechnologiesRequest {
static Response = lib.jsonArray(TechnologySync);
}
export class QueueUpdateEvent {
declare ["constructor"]: typeof QueueUpdateEvent;
static type = "event" as const;
static src = "instance" as const;
static dst = "controller" as const;
static plugin = "research_sync" as const;
constructor(
public queue: string[],
public player: string | null,
) {
}
static jsonSchema = Type.Object({
"queue": Type.Array(Type.String()),
"player": Type.Union([Type.String(), Type.Null()]),
});
static fromJSON(json: Static<typeof QueueUpdateEvent.jsonSchema>): QueueUpdateEvent {
return new this(json.queue, json.player);
}
}
export class QueueEvent {
declare ["constructor"]: typeof QueueEvent;
static type = "event" as const;
static src = "controller" as const;
static dst = "instance" as const;
static plugin = "research_sync" as const;
constructor(
public queue: string[],
public player: string | null,
public instance: string,
) {
}
static jsonSchema = Type.Object({
"queue": Type.Array(Type.String()),
"player": Type.Union([Type.String(), Type.Null()]),
"instance": Type.String(),
});
static fromJSON(json: Static<typeof QueueEvent.jsonSchema>): QueueEvent {
return new this(json.queue, json.player, json.instance);
}
}
export class QueueSyncRequest {
declare ["constructor"]: typeof QueueSyncRequest;
static type = "request" as const;
static src = "instance" as const;
static dst = "controller" as const;
static plugin = "research_sync" as const;
constructor(
public queue: string[] | null,
public player: string | null,
) {
}
static jsonSchema = Type.Object({
"queue": Type.Union([Type.Array(Type.String()), Type.Null()]),
"player": Type.Union([Type.String(), Type.Null()]),
});
static fromJSON(json: Static<typeof QueueSyncRequest.jsonSchema>): QueueSyncRequest {
return new this(json.queue, json.player);
}
static Response = lib.plainJson(Type.Array(Type.String()));
}
+119
View File
@@ -0,0 +1,119 @@
import assert from "node:assert/strict";
import fs from "node:fs/promises";
import path from "node:path";
import * as lib from "@clusterio/lib";
import * as mock from "../../../test/mock.js";
import controllerEntrypoint from "../dist/node/controller.js";
import { plugin as info } from "../dist/node/index.js";
import {
ContributionEvent, FinishedEvent, QueueEvent, QueueSyncRequest, QueueUpdateEvent,
SyncTechnologiesRequest, TechnologySync,
} from "../dist/node/messages.js";
describe("research_sync controller", function() {
const databaseDir = path.join("temp", "test", "research_sync");
let controller, sent;
before(function() {
try {
lib.registerPluginMessages([info]);
} catch (err) {
// Already registered by the full test suite
}
});
beforeEach(async function() {
await fs.rm(databaseDir, { recursive: true, force: true });
await fs.mkdir(databaseDir, { recursive: true });
controller = await load();
});
async function load() {
const c = await mock.loadControllerPlugin(async (context) => {
context.controller.mockConfigEntries.set("controller.database_directory", databaseDir);
await controllerEntrypoint(context);
}, info);
sent = [];
c.sendTo = (dst, message) => { sent.push([dst, message]); };
c.instances = new Map([[1, { config: { get: () => "rs-1" } }]]);
return c;
}
const src = { id: 1 };
const update = (queue, player = null) => mock.getHandler(controller, QueueUpdateEvent)(
new QueueUpdateEvent(queue, player), src
);
const sync = (queue = null, player = null) => mock.getHandler(controller, QueueSyncRequest)(
new QueueSyncRequest(queue, player), src
);
const queueEvents = () => sent.filter(([, m]) => m instanceof QueueEvent);
it("should store and broadcast an update", async function() {
await update(["a", "b"], "Bob");
assert.equal(sent.length, 1);
assert.equal(sent[0][0], "allInstances");
assert.ok(sent[0][1] instanceof QueueEvent);
assert.deepEqual(sent[0][1].queue, ["a", "b"]);
assert.equal(sent[0][1].player, "Bob");
assert.equal(sent[0][1].instance, "rs-1");
assert.deepEqual(await sync(), ["a", "b"]);
});
it("should not broadcast an unchanged update", async function() {
await update(["a", "b"]);
await update(["a", "b"]);
assert.equal(queueEvents().length, 1);
});
it("should cap the queue at 7", async function() {
await update(["1", "2", "3", "4", "5", "6", "7", "8", "9"]);
assert.deepEqual(queueEvents()[0][1].queue, ["1", "2", "3", "4", "5", "6", "7"]);
assert.deepEqual(await sync(), ["1", "2", "3", "4", "5", "6", "7"]);
});
it("should seed an empty queue from a sync request", async function() {
assert.deepEqual(await sync(["x"], "Bob"), ["x"]);
assert.equal(queueEvents().length, 1);
assert.deepEqual(queueEvents()[0][1].queue, ["x"]);
assert.equal(queueEvents()[0][1].player, "Bob");
assert.equal(queueEvents()[0][1].instance, "rs-1");
});
it("should not replace a non-empty queue from a sync request", async function() {
await update(["a"]);
sent.length = 0;
assert.deepEqual(await sync(["x"]), ["a"]);
assert.equal(sent.length, 0);
assert.deepEqual(await sync(), ["a"]);
});
it("should not broadcast when seeding with an empty queue", async function() {
assert.deepEqual(await sync([]), []);
assert.equal(sent.length, 0);
});
describe("finished technologies", function() {
it("should drop one entry when a contribution completes a technology", async function() {
await update(["m", "m", "x"]);
await mock.getHandler(controller, ContributionEvent)(new ContributionEvent("m", 3, 1), src);
assert.deepEqual(await sync(), ["m", "x"]);
});
it("should drop one entry per advancing FinishedEvent only", async function() {
await update(["m", "m", "x"]);
const finished = mock.getHandler(controller, FinishedEvent);
await finished(new FinishedEvent("m", 3), src);
assert.deepEqual(await sync(), ["m", "x"]);
await finished(new FinishedEvent("m", 3), src);
assert.deepEqual(await sync(), ["m", "x"]);
});
it("should drop an entry finished through a technology sync", async function() {
await update(["m", "x"]);
await mock.getHandler(controller, SyncTechnologiesRequest)(
new SyncTechnologiesRequest([new TechnologySync("m", 1, null, true)]), src
);
assert.deepEqual(await sync(), ["x"]);
});
});
it("should persist the queue across loads", async function() {
await update(["a", "b"]);
await controller.hooks.save.invoke();
controller = await load();
assert.deepEqual(await sync(), ["a", "b"]);
});
});