mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-21 08:03:13 +09:00
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:
1 parent
36ced0d3ba
commit
1afbbe04e1
3 files changed
+26
-19
No files matched your search
@@ -1,5 +1,6 @@
|
|||||||
import fs from "fs-extra";
|
import fs from "fs-extra";
|
||||||
import path from "path";
|
import path from "path";
|
||||||
|
import { Static } from "@sinclair/typebox";
|
||||||
|
|
||||||
import * as lib from "@clusterio/lib";
|
import * as lib from "@clusterio/lib";
|
||||||
const { RateLimiter } = lib;
|
const { RateLimiter } = lib;
|
||||||
@@ -8,20 +9,26 @@ import {
|
|||||||
ContributionEvent,
|
ContributionEvent,
|
||||||
ProgressEvent,
|
ProgressEvent,
|
||||||
FinishedEvent,
|
FinishedEvent,
|
||||||
Technology,
|
TechnologySync,
|
||||||
SyncTechnologiesRequest,
|
SyncTechnologiesRequest,
|
||||||
|
TechnologyProgress,
|
||||||
} from "./messages";
|
} from "./messages";
|
||||||
|
|
||||||
|
type Technology = {
|
||||||
|
level: number,
|
||||||
|
progress: number | null,
|
||||||
|
researched: boolean,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
async function loadTechnologies(
|
async function loadTechnologies(
|
||||||
controllerConfig: lib.ControllerConfig,
|
controllerConfig: lib.ControllerConfig,
|
||||||
logger: lib.Logger
|
logger: lib.Logger
|
||||||
): Promise<Map<any, any>> {
|
): Promise<Map<string, Technology>> {
|
||||||
let filePath = path.join(controllerConfig.get("controller.database_directory"), "technologies.json");
|
let filePath = path.join(controllerConfig.get("controller.database_directory"), "technologies.json");
|
||||||
logger.verbose(`Loading ${filePath}`);
|
logger.verbose(`Loading ${filePath}`);
|
||||||
try {
|
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) {
|
} catch (err: any) {
|
||||||
if (err.code === "ENOENT") {
|
if (err.code === "ENOENT") {
|
||||||
logger.verbose("Creating new technologies database");
|
logger.verbose("Creating new technologies database");
|
||||||
@@ -33,7 +40,7 @@ async function loadTechnologies(
|
|||||||
|
|
||||||
async function saveTechnologies(
|
async function saveTechnologies(
|
||||||
controllerConfig: lib.ControllerConfig,
|
controllerConfig: lib.ControllerConfig,
|
||||||
technologies: Map<any, any>,
|
technologies: Map<string, Technology>,
|
||||||
logger: lib.Logger
|
logger: lib.Logger
|
||||||
) {
|
) {
|
||||||
let filePath = path.join(controllerConfig.get("controller.database_directory"), "technologies.json");
|
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) {
|
for (let name of this.progressToBroadcast) {
|
||||||
let tech = this.technologies.get(name);
|
let tech = this.technologies.get(name);
|
||||||
if (tech && tech.progress) {
|
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();
|
this.progressToBroadcast.clear();
|
||||||
@@ -86,7 +93,7 @@ export class ControllerPlugin extends lib.BaseControllerPlugin {
|
|||||||
let { name, level, contribution } = event;
|
let { name, level, contribution } = event;
|
||||||
let tech = this.technologies.get(name);
|
let tech = this.technologies.get(name);
|
||||||
if (!tech) {
|
if (!tech) {
|
||||||
tech = new Technology(name, level, 0, false);
|
tech = { level, progress: 0, researched: false };
|
||||||
this.technologies.set(name, tech);
|
this.technologies.set(name, tech);
|
||||||
|
|
||||||
// Ignore contribution to already researched technologies
|
// Ignore contribution to already researched technologies
|
||||||
@@ -125,11 +132,11 @@ export class ControllerPlugin extends lib.BaseControllerPlugin {
|
|||||||
let tech = this.technologies.get(name);
|
let tech = this.technologies.get(name);
|
||||||
if (!tech || tech.level <= level) {
|
if (!tech || tech.level <= level) {
|
||||||
this.progressToBroadcast.delete(name);
|
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 {
|
function baseLevel(name: string): number {
|
||||||
let match = /-(\d+)$/.exec(name);
|
let match = /-(\d+)$/.exec(name);
|
||||||
if (!match) {
|
if (!match) {
|
||||||
@@ -142,7 +149,7 @@ export class ControllerPlugin extends lib.BaseControllerPlugin {
|
|||||||
let { name, level, progress, researched } = instanceTech;
|
let { name, level, progress, researched } = instanceTech;
|
||||||
let tech = this.technologies.get(name);
|
let tech = this.technologies.get(name);
|
||||||
if (!tech) {
|
if (!tech) {
|
||||||
this.technologies.set(name, new Technology(name, level, progress, researched));
|
this.technologies.set(name, { level, progress, researched });
|
||||||
if (progress) {
|
if (progress) {
|
||||||
this.progressToBroadcast.add(name);
|
this.progressToBroadcast.add(name);
|
||||||
} else if (researched || baseLevel(name) !== level) {
|
} else if (researched || baseLevel(name) !== level) {
|
||||||
@@ -179,7 +186,7 @@ export class ControllerPlugin extends lib.BaseControllerPlugin {
|
|||||||
|
|
||||||
let technologies = [];
|
let technologies = [];
|
||||||
for (let [name, tech] of this.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;
|
return technologies;
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ import {
|
|||||||
ContributionEvent,
|
ContributionEvent,
|
||||||
ProgressEvent,
|
ProgressEvent,
|
||||||
FinishedEvent,
|
FinishedEvent,
|
||||||
Technology,
|
TechnologySync,
|
||||||
SyncTechnologiesRequest,
|
SyncTechnologiesRequest,
|
||||||
} from "./messages";
|
} from "./messages";
|
||||||
|
|
||||||
@@ -73,7 +73,7 @@ export class InstancePlugin extends lib.BaseInstancePlugin {
|
|||||||
let techsToSend = [];
|
let techsToSend = [];
|
||||||
let instanceTechs = new Map();
|
let instanceTechs = new Map();
|
||||||
for (let tech of JSON.parse(dumpJson)) {
|
for (let tech of JSON.parse(dumpJson)) {
|
||||||
techsToSend.push(new Technology(
|
techsToSend.push(new TechnologySync(
|
||||||
tech.name,
|
tech.name,
|
||||||
tech.level,
|
tech.level,
|
||||||
tech.progress || null,
|
tech.progress || null,
|
||||||
|
|||||||
@@ -88,7 +88,7 @@ export class FinishedEvent {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export class Technology {
|
export class TechnologySync {
|
||||||
constructor(
|
constructor(
|
||||||
public name: string,
|
public name: string,
|
||||||
public level: number,
|
public level: number,
|
||||||
@@ -107,7 +107,7 @@ export class Technology {
|
|||||||
return [this.name, this.level, this.progress, this.researched];
|
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]);
|
return new this(json[0], json[1], json[2], json[3]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -120,18 +120,18 @@ export class SyncTechnologiesRequest {
|
|||||||
static plugin = "research_sync" as const;
|
static plugin = "research_sync" as const;
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
public technologies: Technology[]
|
public technologies: TechnologySync[]
|
||||||
) {
|
) {
|
||||||
}
|
}
|
||||||
|
|
||||||
static jsonSchema = Type.Array(Technology.jsonSchema);
|
static jsonSchema = Type.Array(TechnologySync.jsonSchema);
|
||||||
toJSON() {
|
toJSON() {
|
||||||
return this.technologies;
|
return this.technologies;
|
||||||
}
|
}
|
||||||
|
|
||||||
static fromJSON(json: Static<typeof SyncTechnologiesRequest.jsonSchema>): SyncTechnologiesRequest {
|
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);
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user