mirror of
https://github.com/PHIDIAS0303/ExpCluster.git
synced 2026-08-13 00:45:11 +09:00
Add exp_roles plugin to sync roles from the controller
Clusterio already stores roles, the permissions they grant, and which user holds which role, along with a web UI for all three. What it does not have is the properties a role only needs in game, or a way for an instance to learn about any of it, since role and user updates are only sent to control connections. This plugin fills both gaps. The controller keeps a record per role holding the order, priority, short hand, tag, colour and auto assign threshold, created automatically for any role which does not have one. It then rebroadcasts roles and assignments on its own events so instances can follow them. The lua module presents the same interface the legacy expcore.roles module did, so the call sites can be moved over without being rewritten. Permission checks translate the legacy action strings using the same mapping exp_scenario defines. Two things replace features the legacy system had: - Priority replaces disallow. Only the roles with the highest priority a player holds are considered, so Jail can suppress every other role including the default one, without needing to take roles away first. - Assignments made in game are applied locally and then sent to the controller, which keeps assign_player synchronous for callers. A role which should never leave this map, such as one earned from time on the map, is assigned with assign_player_local instead. Roles earned from online time across the cluster are granted by the controller from the threshold on the role, using the online time clusterio already tracks. Nothing requires this plugin yet; moving the call sites off expcore.roles and removing the legacy config is left for a follow up. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,389 @@
|
||||
import * as lib from "@clusterio/lib";
|
||||
import { Type, Static } from "@sinclair/typebox";
|
||||
|
||||
/*
|
||||
Data records
|
||||
*/
|
||||
|
||||
/** Colour used for a role's tag and name in game. */
|
||||
export class RoleColor {
|
||||
constructor(
|
||||
public r: number,
|
||||
public g: number,
|
||||
public b: number,
|
||||
) {}
|
||||
|
||||
static jsonSchema = Type.Object({
|
||||
r: Type.Number(),
|
||||
g: Type.Number(),
|
||||
b: Type.Number(),
|
||||
});
|
||||
|
||||
toJSON(): Static<typeof RoleColor.jsonSchema> {
|
||||
return { r: this.r, g: this.g, b: this.b };
|
||||
}
|
||||
|
||||
static fromJSON(json: Static<typeof this.jsonSchema>) {
|
||||
return new this(json.r, json.g, json.b);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* In game properties of a role which clusterio's own role does not carry.
|
||||
*
|
||||
* Stored on the controller keyed by the clusterio role id, and created
|
||||
* automatically for any role which does not have one yet.
|
||||
*/
|
||||
export class RoleMetaRecord {
|
||||
constructor(
|
||||
/** Id of the clusterio role these properties belong to. */
|
||||
public id: number,
|
||||
/** Position in the role order, a lower value is a more privileged role. */
|
||||
public order: number,
|
||||
/**
|
||||
* Only the roles with the highest priority a player holds are considered,
|
||||
* which allows a role such as Jail to suppress all others.
|
||||
*/
|
||||
public priority: number = 0,
|
||||
/** Short form of the role name, used where space is limited. */
|
||||
public shortHand: string = "",
|
||||
/** Tag shown next to the names of players with this role. */
|
||||
public tag: string = "",
|
||||
/** Colour used for the role in game, null uses the default colour. */
|
||||
public color: RoleColor | null = null,
|
||||
/** Online time after which this role is granted, null never grants it. */
|
||||
public autoAssignOnlineTimeMs: number | null = null,
|
||||
/** When true holders of this role are never granted roles automatically. */
|
||||
public blockAutoAssign: boolean = false,
|
||||
public updatedAtMs: number = 0,
|
||||
public isDeleted: boolean = false,
|
||||
) {}
|
||||
|
||||
static jsonSchema = Type.Object({
|
||||
id: Type.Integer(),
|
||||
order: Type.Number(),
|
||||
priority: Type.Optional(Type.Number()),
|
||||
short_hand: Type.Optional(Type.String()),
|
||||
tag: Type.Optional(Type.String()),
|
||||
color: Type.Optional(Type.Union([RoleColor.jsonSchema, Type.Null()])),
|
||||
auto_assign_online_time_ms: Type.Optional(Type.Union([Type.Number(), Type.Null()])),
|
||||
block_auto_assign: Type.Optional(Type.Boolean()),
|
||||
updated_at_ms: Type.Optional(Type.Number()),
|
||||
is_deleted: Type.Optional(Type.Boolean()),
|
||||
});
|
||||
|
||||
toJSON() {
|
||||
const json: Static<typeof RoleMetaRecord.jsonSchema> = {
|
||||
id: this.id,
|
||||
order: this.order,
|
||||
};
|
||||
|
||||
if (this.priority) {
|
||||
json.priority = this.priority;
|
||||
}
|
||||
|
||||
if (this.shortHand) {
|
||||
json.short_hand = this.shortHand;
|
||||
}
|
||||
|
||||
if (this.tag) {
|
||||
json.tag = this.tag;
|
||||
}
|
||||
|
||||
if (this.color) {
|
||||
json.color = this.color.toJSON();
|
||||
}
|
||||
|
||||
if (this.autoAssignOnlineTimeMs !== null) {
|
||||
json.auto_assign_online_time_ms = this.autoAssignOnlineTimeMs;
|
||||
}
|
||||
|
||||
if (this.blockAutoAssign) {
|
||||
json.block_auto_assign = true;
|
||||
}
|
||||
|
||||
if (this.updatedAtMs) {
|
||||
json.updated_at_ms = this.updatedAtMs;
|
||||
}
|
||||
|
||||
if (this.isDeleted) {
|
||||
json.is_deleted = true;
|
||||
}
|
||||
|
||||
return json;
|
||||
}
|
||||
|
||||
static fromJSON(json: Static<typeof this.jsonSchema>) {
|
||||
return new this(
|
||||
json.id,
|
||||
json.order,
|
||||
json.priority ?? 0,
|
||||
json.short_hand ?? "",
|
||||
json.tag ?? "",
|
||||
json.color ? RoleColor.fromJSON(json.color) : null,
|
||||
json.auto_assign_online_time_ms ?? null,
|
||||
json.block_auto_assign ?? false,
|
||||
json.updated_at_ms ?? 0,
|
||||
json.is_deleted ?? false,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A clusterio role combined with its in game properties.
|
||||
*
|
||||
* This is the form sent to instances, because instances can not subscribe to
|
||||
* the core role updates which are only sent to control connections.
|
||||
*/
|
||||
export class RoleRecord {
|
||||
constructor(
|
||||
public id: number,
|
||||
public name: string,
|
||||
public permissions: string[],
|
||||
public meta: RoleMetaRecord,
|
||||
/** True for the role every player holds, taken from controller.default_role_id. */
|
||||
public isDefault: boolean = false,
|
||||
public updatedAtMs: number = 0,
|
||||
public isDeleted: boolean = false,
|
||||
) {}
|
||||
|
||||
static jsonSchema = Type.Object({
|
||||
id: Type.Integer(),
|
||||
name: Type.String(),
|
||||
permissions: Type.Array(Type.String()),
|
||||
meta: RoleMetaRecord.jsonSchema,
|
||||
is_default: Type.Optional(Type.Boolean()),
|
||||
updated_at_ms: Type.Optional(Type.Number()),
|
||||
is_deleted: Type.Optional(Type.Boolean()),
|
||||
});
|
||||
|
||||
toJSON() {
|
||||
const json: Static<typeof RoleRecord.jsonSchema> = {
|
||||
id: this.id,
|
||||
name: this.name,
|
||||
permissions: this.permissions,
|
||||
meta: this.meta.toJSON(),
|
||||
};
|
||||
|
||||
if (this.isDefault) {
|
||||
json.is_default = true;
|
||||
}
|
||||
|
||||
if (this.updatedAtMs) {
|
||||
json.updated_at_ms = this.updatedAtMs;
|
||||
}
|
||||
|
||||
if (this.isDeleted) {
|
||||
json.is_deleted = true;
|
||||
}
|
||||
|
||||
return json;
|
||||
}
|
||||
|
||||
static fromJSON(json: Static<typeof this.jsonSchema>) {
|
||||
return new this(
|
||||
json.id,
|
||||
json.name,
|
||||
json.permissions,
|
||||
RoleMetaRecord.fromJSON(json.meta),
|
||||
json.is_default ?? false,
|
||||
json.updated_at_ms ?? 0,
|
||||
json.is_deleted ?? false,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/** The roles a single player holds, mirroring the roles of their cluster user. */
|
||||
export class AssignmentRecord {
|
||||
constructor(
|
||||
public name: string,
|
||||
public roleIds: Set<number>,
|
||||
public updatedAtMs: number = 0,
|
||||
public isDeleted: boolean = false,
|
||||
) {}
|
||||
|
||||
get id() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
static jsonSchema = Type.Object({
|
||||
name: Type.String(),
|
||||
role_ids: Type.Array(Type.Integer()),
|
||||
updated_at_ms: Type.Optional(Type.Number()),
|
||||
is_deleted: Type.Optional(Type.Boolean()),
|
||||
});
|
||||
|
||||
toJSON() {
|
||||
const json: Static<typeof AssignmentRecord.jsonSchema> = {
|
||||
name: this.name,
|
||||
role_ids: [...this.roleIds],
|
||||
};
|
||||
|
||||
if (this.updatedAtMs) {
|
||||
json.updated_at_ms = this.updatedAtMs;
|
||||
}
|
||||
|
||||
if (this.isDeleted) {
|
||||
json.is_deleted = true;
|
||||
}
|
||||
|
||||
return json;
|
||||
}
|
||||
|
||||
static fromJSON(json: Static<typeof this.jsonSchema>) {
|
||||
return new this(
|
||||
json.name,
|
||||
new Set(json.role_ids),
|
||||
json.updated_at_ms ?? 0,
|
||||
json.is_deleted ?? false,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
Update events
|
||||
*/
|
||||
|
||||
export class RoleUpdatedEvent {
|
||||
declare ["constructor"]: typeof RoleUpdatedEvent;
|
||||
static plugin = "exp_roles" as const;
|
||||
static type = "event" as const;
|
||||
static src = "controller" as const;
|
||||
static dst = ["control", "instance"] as const;
|
||||
static permission = "exp_roles.role.subscribe" as const;
|
||||
|
||||
constructor(
|
||||
public updates: RoleRecord[],
|
||||
) {}
|
||||
|
||||
static jsonSchema = Type.Object({
|
||||
updates: Type.Array(RoleRecord.jsonSchema),
|
||||
});
|
||||
|
||||
toJSON() {
|
||||
return { updates: this.updates.map(role => role.toJSON()) };
|
||||
}
|
||||
|
||||
static fromJSON(json: Static<typeof this.jsonSchema>) {
|
||||
return new this(json.updates.map(role => RoleRecord.fromJSON(role)));
|
||||
}
|
||||
}
|
||||
|
||||
export class AssignmentUpdatedEvent {
|
||||
declare ["constructor"]: typeof AssignmentUpdatedEvent;
|
||||
static plugin = "exp_roles" as const;
|
||||
static type = "event" as const;
|
||||
static src = "controller" as const;
|
||||
static dst = ["control", "instance"] as const;
|
||||
static permission = "exp_roles.assignment.subscribe" as const;
|
||||
|
||||
constructor(
|
||||
public updates: AssignmentRecord[],
|
||||
) {}
|
||||
|
||||
static jsonSchema = Type.Object({
|
||||
updates: Type.Array(AssignmentRecord.jsonSchema),
|
||||
});
|
||||
|
||||
toJSON() {
|
||||
return { updates: this.updates.map(assignment => assignment.toJSON()) };
|
||||
}
|
||||
|
||||
static fromJSON(json: Static<typeof this.jsonSchema>) {
|
||||
return new this(json.updates.map(assignment => AssignmentRecord.fromJSON(assignment)));
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
Role requests
|
||||
*/
|
||||
|
||||
export class RoleListRequest {
|
||||
declare ["constructor"]: typeof RoleListRequest;
|
||||
static plugin = "exp_roles" as const;
|
||||
static type = "request" as const;
|
||||
static src = ["control", "instance"] as const;
|
||||
static dst = "controller" as const;
|
||||
static permission = "exp_roles.role.list" as const;
|
||||
static Response = lib.jsonArray(RoleRecord);
|
||||
|
||||
constructor() {}
|
||||
}
|
||||
|
||||
export class RoleMetaUpdateRequest {
|
||||
declare ["constructor"]: typeof RoleMetaUpdateRequest;
|
||||
static plugin = "exp_roles" as const;
|
||||
static type = "request" as const;
|
||||
static src = "control" as const;
|
||||
static dst = "controller" as const;
|
||||
static permission = "exp_roles.role.update" as const;
|
||||
|
||||
constructor(
|
||||
public meta: RoleMetaRecord,
|
||||
) {}
|
||||
|
||||
static jsonSchema = Type.Object({
|
||||
meta: RoleMetaRecord.jsonSchema,
|
||||
});
|
||||
|
||||
toJSON() {
|
||||
return { meta: this.meta.toJSON() };
|
||||
}
|
||||
|
||||
static fromJSON(json: Static<typeof this.jsonSchema>) {
|
||||
return new this(RoleMetaRecord.fromJSON(json.meta));
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
Assignment requests
|
||||
*/
|
||||
|
||||
export class AssignmentListRequest {
|
||||
declare ["constructor"]: typeof AssignmentListRequest;
|
||||
static plugin = "exp_roles" as const;
|
||||
static type = "request" as const;
|
||||
static src = ["control", "instance"] as const;
|
||||
static dst = "controller" as const;
|
||||
static permission = "exp_roles.assignment.list" as const;
|
||||
static Response = lib.jsonArray(AssignmentRecord);
|
||||
|
||||
constructor() {}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add or remove roles for a player, sent by an instance when a role is changed
|
||||
* in game. Only accepted when the instance is configured as bidirectional.
|
||||
*/
|
||||
export class AssignmentUpdateRequest {
|
||||
declare ["constructor"]: typeof AssignmentUpdateRequest;
|
||||
static plugin = "exp_roles" as const;
|
||||
static type = "request" as const;
|
||||
static src = ["control", "instance"] as const;
|
||||
static dst = "controller" as const;
|
||||
static permission = "core.user.update_roles" as const;
|
||||
|
||||
constructor(
|
||||
public name: string,
|
||||
public assign: number[],
|
||||
public unassign: number[],
|
||||
) {}
|
||||
|
||||
static jsonSchema = Type.Object({
|
||||
name: Type.String(),
|
||||
assign: Type.Array(Type.Integer()),
|
||||
unassign: Type.Array(Type.Integer()),
|
||||
});
|
||||
|
||||
toJSON() {
|
||||
return {
|
||||
name: this.name,
|
||||
assign: this.assign,
|
||||
unassign: this.unassign,
|
||||
};
|
||||
}
|
||||
|
||||
static fromJSON(json: Static<typeof this.jsonSchema>) {
|
||||
return new this(json.name, json.assign, json.unassign);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user