Files
factorio-clusterio/plugins/research_sync/messages.js
T
Hornwitser 7f8a700716 Refactor message registration and validation
Split up the action of registring an event or request data class to be
decoded by the Link class and the action of registring a handler for
those requests and events.  This moves the validation work from Link
instantiation time to startup initialisation which makes logic errors
fail on startup instead of failing instantiation of the link when a
connection attempt is made.

Additionally implement a partial validation logic for messages received
using the newly refactored message registration.  This unfortunately
ended up merged with the registration refactor.
2023-06-03 16:47:47 +02:00

179 lines
3.2 KiB
JavaScript

"use strict";
const { jsonArray } = require("@clusterio/lib/data");
class ContributionEvent {
static type = "event";
static src = "instance";
static dst = "controller";
static plugin = "research_sync";
/** @type {string} */
name;
/** @type {number} */
level;
/** @type {number} */
contribution;
constructor(name, level, contribution) {
this.name = name;
this.level = level;
this.contribution = contribution;
}
static jsonSchema = {
type: "object",
required: ["name", "level", "contribution"],
properties: {
name: { type: "string" },
level: { type: "integer" },
contribution: { type: "number" },
},
};
static fromJSON(json) {
return new this(json.name, json.level, json.contribution);
}
}
class ProgressEvent {
static type = "event";
static src = "controller";
static dst = "instance";
static plugin = "research_sync";
/** @type {Array<>} */
technologies;
constructor(technologies) {
this.technologies = technologies;
}
static jsonSchema = {
type: "object",
required: ["technologies"],
properties: {
technologies: {
type: "array",
items: {
required: ["name", "level", "progress"],
properties: {
name: { type: "string" },
level: { type: "integer" },
progress: { type: "number" },
},
additionalProperties: false,
},
},
},
};
static fromJSON(json) {
return new this(json.technologies);
}
}
class FinishedEvent {
static type = "event";
static src = ["instance", "controller"];
static dst = "instance";
static plugin = "research_sync";
/** @type {string} */
name;
/** @type {number} */
level;
constructor(name, level) {
this.name = name;
this.level = level;
}
static jsonSchema = {
type: "object",
required: ["name", "level"],
properties: {
name: { type: "string" },
level: { type: "integer" },
},
};
static fromJSON(json) {
return new this(json.name, json.level);
}
}
class Technology {
/** @type {string} */
name;
/** @type {number} */
level;
/** @type {?number} */
progress;
/** @type {boolean} */
researched;
constructor(name, level, progress, researched) {
this.name = name;
this.level = level;
this.progress = progress;
this.researched = researched;
}
static jsonSchema = {
type: "array",
minItems: 4,
maxItems: 4,
items: [
{ type: "string" },
{ type: "integer" },
{ type: ["null", "number"] },
{ type: "boolean" },
],
};
toJSON() {
return [this.name, this.level, this.progress, this.researched];
}
static fromJSON(json) {
return new this(json[0], json[1], json[2], json[3]);
}
}
class SyncTechnologiesRequest {
static type = "request";
static src = "instance";
static dst = "controller";
static plugin = "research_sync";
/** @type {Array<Technology>} */
technologies;
constructor(technologies) {
this.technologies = technologies;
}
static jsonSchema = {
type: "array",
items: Technology.jsonSchema,
};
toJSON() {
return this.technologies;
}
static fromJSON(json) {
return new this(json.map(e => Technology.fromJSON(e)));
}
static Response = jsonArray(Technology);
}
module.exports = {
ContributionEvent,
ProgressEvent,
FinishedEvent,
Technology,
SyncTechnologiesRequest,
};