From 1afbbe04e106af1810dde2e01ca834f5493e7ea8 Mon Sep 17 00:00:00 2001 From: Luciole <31817566+Luciole0x@users.noreply.github.com> Date: Sat, 26 Aug 2023 16:35:39 +0200 Subject: [PATCH] Fix issue with research_sync plugin (#502) * fix type issues on loading technologies and broadcasting progress * Distinguish Technology and TechnologySync type --- plugins/research_sync/controller.ts | 29 ++++++++++++++++++----------- plugins/research_sync/instance.ts | 4 ++-- plugins/research_sync/messages.ts | 12 ++++++------ 3 files changed, 26 insertions(+), 19 deletions(-) diff --git a/plugins/research_sync/controller.ts b/plugins/research_sync/controller.ts index cedb268c..02882301 100644 --- a/plugins/research_sync/controller.ts +++ b/plugins/research_sync/controller.ts @@ -1,5 +1,6 @@ import fs from "fs-extra"; import path from "path"; +import { Static } from "@sinclair/typebox"; import * as lib from "@clusterio/lib"; const { RateLimiter } = lib; @@ -8,20 +9,26 @@ import { ContributionEvent, ProgressEvent, FinishedEvent, - Technology, + TechnologySync, SyncTechnologiesRequest, + TechnologyProgress, } from "./messages"; +type Technology = { + level: number, + progress: number | null, + researched: boolean, +} + async function loadTechnologies( controllerConfig: lib.ControllerConfig, logger: lib.Logger -): Promise> { +): Promise> { let filePath = path.join(controllerConfig.get("controller.database_directory"), "technologies.json"); logger.verbose(`Loading ${filePath}`); try { - return new Map(JSON.parse(await fs.readFile(filePath, { encoding: "utf8" }))); - + return new Map(JSON.parse(await fs.readFile(filePath, "utf8"))); } catch (err: any) { if (err.code === "ENOENT") { logger.verbose("Creating new technologies database"); @@ -33,7 +40,7 @@ async function loadTechnologies( async function saveTechnologies( controllerConfig: lib.ControllerConfig, - technologies: Map, + technologies: Map, logger: lib.Logger ) { let filePath = path.join(controllerConfig.get("controller.database_directory"), "technologies.json"); @@ -72,7 +79,7 @@ export class ControllerPlugin extends lib.BaseControllerPlugin { for (let name of this.progressToBroadcast) { let tech = this.technologies.get(name); if (tech && tech.progress) { - techs.push(new Technology(name, tech.level, tech.progress, false)); + techs.push(new TechnologyProgress(name, tech.level, tech.progress)); } } this.progressToBroadcast.clear(); @@ -86,7 +93,7 @@ export class ControllerPlugin extends lib.BaseControllerPlugin { let { name, level, contribution } = event; let tech = this.technologies.get(name); if (!tech) { - tech = new Technology(name, level, 0, false); + tech = { level, progress: 0, researched: false }; this.technologies.set(name, tech); // Ignore contribution to already researched technologies @@ -125,11 +132,11 @@ export class ControllerPlugin extends lib.BaseControllerPlugin { let tech = this.technologies.get(name); if (!tech || tech.level <= level) { this.progressToBroadcast.delete(name); - this.technologies.set(name, new Technology(name, level, null, true)); + this.technologies.set(name, { level, progress: null, researched: true }); } } - async handleSyncTechnologiesRequest(request: SyncTechnologiesRequest): Promise { + async handleSyncTechnologiesRequest(request: SyncTechnologiesRequest): Promise { function baseLevel(name: string): number { let match = /-(\d+)$/.exec(name); if (!match) { @@ -142,7 +149,7 @@ export class ControllerPlugin extends lib.BaseControllerPlugin { let { name, level, progress, researched } = instanceTech; let tech = this.technologies.get(name); if (!tech) { - this.technologies.set(name, new Technology(name, level, progress, researched)); + this.technologies.set(name, { level, progress, researched }); if (progress) { this.progressToBroadcast.add(name); } else if (researched || baseLevel(name) !== level) { @@ -179,7 +186,7 @@ export class ControllerPlugin extends lib.BaseControllerPlugin { let technologies = []; for (let [name, tech] of this.technologies) { - technologies.push(new Technology(name, tech.level, tech.progress, tech.researched)); + technologies.push(new TechnologySync(name, tech.level, tech.progress, tech.researched)); } return technologies; diff --git a/plugins/research_sync/instance.ts b/plugins/research_sync/instance.ts index b1e7bc01..86bec441 100644 --- a/plugins/research_sync/instance.ts +++ b/plugins/research_sync/instance.ts @@ -3,7 +3,7 @@ import { ContributionEvent, ProgressEvent, FinishedEvent, - Technology, + TechnologySync, SyncTechnologiesRequest, } from "./messages"; @@ -73,7 +73,7 @@ export class InstancePlugin extends lib.BaseInstancePlugin { let techsToSend = []; let instanceTechs = new Map(); for (let tech of JSON.parse(dumpJson)) { - techsToSend.push(new Technology( + techsToSend.push(new TechnologySync( tech.name, tech.level, tech.progress || null, diff --git a/plugins/research_sync/messages.ts b/plugins/research_sync/messages.ts index 81a33d98..b0666010 100644 --- a/plugins/research_sync/messages.ts +++ b/plugins/research_sync/messages.ts @@ -88,7 +88,7 @@ export class FinishedEvent { } } -export class Technology { +export class TechnologySync { constructor( public name: string, public level: number, @@ -107,7 +107,7 @@ export class Technology { return [this.name, this.level, this.progress, this.researched]; } - static fromJSON(json: Static): Technology { + static fromJSON(json: Static): TechnologySync { return new this(json[0], json[1], json[2], json[3]); } } @@ -120,18 +120,18 @@ export class SyncTechnologiesRequest { static plugin = "research_sync" as const; constructor( - public technologies: Technology[] + public technologies: TechnologySync[] ) { } - static jsonSchema = Type.Array(Technology.jsonSchema); + static jsonSchema = Type.Array(TechnologySync.jsonSchema); toJSON() { return this.technologies; } static fromJSON(json: Static): SyncTechnologiesRequest { - return new this(json.map(e => Technology.fromJSON(e))); + return new this(json.map(e => TechnologySync.fromJSON(e))); } - static Response = lib.jsonArray(Technology); + static Response = lib.jsonArray(TechnologySync); }