Fix issue with research_sync plugin (#502)

* fix type issues on loading technologies and broadcasting progress

* Distinguish Technology and TechnologySync type
This commit is contained in:
Luciole authored and GitHub committed 2023-08-26 16:35:39 +02:00
1 parent 36ced0d3ba
commit 1afbbe04e1
3 files changed
+26 -19

No files matched your search

+18 -11
View File
@@ -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<Map<any, any>> {
): Promise<Map<string, Technology>> {
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<any, any>,
technologies: Map<string, Technology>,
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<Technology[]> {
async handleSyncTechnologiesRequest(request: SyncTechnologiesRequest): Promise<TechnologySync[]> {
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;
+2 -2
View File
@@ -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,
+6 -6
View File
@@ -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<typeof Technology.jsonSchema>): Technology {
static fromJSON(json: Static<typeof TechnologySync.jsonSchema>): 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<typeof SyncTechnologiesRequest.jsonSchema>): 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);
}