mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-11 23:04:52 +09:00
* Refactor UserManager to use Datastore * Rename lib.User to UserDetails also adds to/fromUserDetails * Fix existing tests * Add user class tests * Add tests for finaliseUsers * Move HostDetails to own file with tests * Move InstanceDetails to its own file with tests * Rename HostInfo and add tests * Rename InstanceInfo and add tests - Also changes the names of the host properties related to instances - Also removes the need to specify the location of InstanceInfo * Make json round trip test validate schema * Add InstanceManager * Fix lint errors * Extra tests * Add final test coverage * Add one last test to InstanceManager * Response to comments * Use generator rather than return anonumous generator Co-authored-by: Hornwitser <github@hornwitser.no> * Fix lint issue with generator --------- Co-authored-by: Hornwitser <github@hornwitser.no>
100 lines
2.3 KiB
TypeScript
100 lines
2.3 KiB
TypeScript
import * as lib from "@clusterio/lib";
|
|
import { Static, Type } from "@sinclair/typebox";
|
|
|
|
/** Underlying data class for instances on the controller */
|
|
export default class InstanceRecord {
|
|
constructor(
|
|
public config: lib.InstanceConfig,
|
|
public status: lib.InstanceStatus,
|
|
public gamePort?: number,
|
|
public factorioVersion?: lib.TargetVersion,
|
|
public startedAtMs = 0,
|
|
public updatedAtMs = 0,
|
|
) {
|
|
this.config = config;
|
|
this.status = status;
|
|
}
|
|
|
|
get id(): number {
|
|
return this.config.get("instance.id");
|
|
}
|
|
|
|
get isDeleted() {
|
|
return this.status === "deleted";
|
|
}
|
|
|
|
set isDeleted(value: boolean) {
|
|
if (value === false) {
|
|
throw new Error("Setting isDeleted to false is not supported, use status instead.");
|
|
}
|
|
this.status = "deleted";
|
|
}
|
|
|
|
static jsonSchema = Type.Object({
|
|
"config": lib.InstanceConfig.jsonSchema,
|
|
"status": lib.InstanceStatus,
|
|
"gamePort": Type.Optional(Type.Number()),
|
|
"startedAtMs": Type.Optional(Type.Number()),
|
|
"updatedAtMs": Type.Optional(Type.Number()),
|
|
"factorioVersion": Type.Optional(lib.TargetVersionSchema),
|
|
});
|
|
|
|
static fromJSON(json: Static<typeof this.jsonSchema>) {
|
|
return new this(
|
|
lib.InstanceConfig.fromJSON(json.config, "controller"),
|
|
json.status,
|
|
json.gamePort,
|
|
json.factorioVersion,
|
|
json.startedAtMs,
|
|
json.updatedAtMs,
|
|
);
|
|
}
|
|
|
|
toJSON(): Static<typeof InstanceRecord.jsonSchema> {
|
|
const json: any = {
|
|
config: this.config.toJSON(),
|
|
status: this.status,
|
|
};
|
|
|
|
if (this.gamePort !== undefined) {
|
|
json.gamePort = this.gamePort;
|
|
}
|
|
if (this.factorioVersion !== undefined) {
|
|
json.factorioVersion = this.factorioVersion;
|
|
}
|
|
if (this.startedAtMs !== 0) {
|
|
json.startedAtMs = this.startedAtMs;
|
|
}
|
|
if (this.updatedAtMs !== 0) {
|
|
json.updatedAtMs = this.updatedAtMs;
|
|
}
|
|
|
|
return json;
|
|
}
|
|
|
|
toInstanceDetails() {
|
|
return new lib.InstanceDetails(
|
|
this.config.get("instance.name"),
|
|
this.id,
|
|
this.config.get("instance.assigned_host") ?? undefined,
|
|
this.gamePort,
|
|
this.status,
|
|
this.factorioVersion,
|
|
this.startedAtMs,
|
|
this.updatedAtMs,
|
|
this.config.get("instance.exclude_from_start_all"),
|
|
);
|
|
}
|
|
|
|
static fromInstanceDetails(details: lib.InstanceDetails, config: lib.InstanceConfig) {
|
|
return new InstanceRecord(
|
|
config,
|
|
details.status,
|
|
details.gamePort,
|
|
details.factorioVersion,
|
|
details.startedAtMs,
|
|
details.updatedAtMs,
|
|
);
|
|
}
|
|
}
|