From f7c78d9f742715b350989bd48f8906b573e04ce5 Mon Sep 17 00:00:00 2001 From: ILLISIS Date: Mon, 19 Oct 2026 00:06:35 +1100 Subject: [PATCH] Remove one research queue entry per level a technology sync unlocks Co-Authored-By: Claude Opus 5.5 (1M context) --- plugins/research_sync/controller.ts | 18 ++++++++++++------ plugins/research_sync/test/controller.js | 17 +++++++++++++++++ 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/plugins/research_sync/controller.ts b/plugins/research_sync/controller.ts index 2878c0ec..add05517 100644 --- a/plugins/research_sync/controller.ts +++ b/plugins/research_sync/controller.ts @@ -86,9 +86,13 @@ export default async function(context: ControllerPluginContext) { 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) { + // Removes one entry per level that finished + function removeFinished(name: string, levels = 1) { + for (let i = 0; i < levels; i++) { + let index = queue.indexOf(name); + if (index === -1) { + return; + } queue.splice(index, 1); queueDirty = true; } @@ -198,7 +202,8 @@ export default async function(context: ControllerPluginContext) { progressToBroadcast.add(name); } else if (researched || baseLevel(name) !== level) { controller.sendTo("allInstances", new FinishedEvent(name, level)); - removeFinished(name); + // One queue entry per level unlocked + removeFinished(name, Math.max(level - Number(!researched) - baseLevel(name) + 1, 1)); } } else { @@ -208,9 +213,10 @@ export default async function(context: ControllerPluginContext) { if (tech.level < level || researched) { // Send update if the unlocked level is greater - if (level - Number(!researched) > tech.level - Number(!tech.researched)) { + let unlocked = level - Number(!researched) - (tech.level - Number(!tech.researched)); + if (unlocked > 0) { controller.sendTo("allInstances", new FinishedEvent(name, level - Number(!researched))); - removeFinished(name); + removeFinished(name, unlocked); } tech.level = level; tech.progress = progress; diff --git a/plugins/research_sync/test/controller.js b/plugins/research_sync/test/controller.js index b3cf0a9d..83171d9a 100644 --- a/plugins/research_sync/test/controller.js +++ b/plugins/research_sync/test/controller.js @@ -119,6 +119,23 @@ describe("research_sync controller", function() { }); }); + describe("technology sync unlocking several levels", function() { + const techSync = (name, level, researched) => mock.getHandler(controller, SyncTechnologiesRequest)( + new SyncTechnologiesRequest([new TechnologySync(name, level, null, researched)]), src + ); + it("should drop one entry per level unlocked on a known technology", async function() { + await techSync("m", 4, true); + await update(["m", "m", "m", "x"]); + await techSync("m", 7, true); + assert.deepEqual(await sync(), ["x"]); + }); + it("should drop one entry per level unlocked on a new technology", async function() { + await update(["m", "m", "x"]); + await techSync("m", 2, true); + assert.deepEqual(await sync(), ["x"]); + }); + }); + it("should persist the queue across loads", async function() { await update(["a", "b"]); await controller.hooks.save.invoke();