mirror of
https://github.com/PHIDIAS0303/ExpCluster.git
synced 2026-09-21 17:04:00 +00:00
The module no longer presents the interface of the legacy expcore.roles module. Nothing outside this repository depends on it, so rather than carry the legacy action strings and the transform which mapped them onto permission names, call sites now check the clusterio permission name directly. That removes the one invariant which silently broke every check if the lua and typescript transforms drifted, and makes a check in lua greppable against its definition. - player_allowed and player_has_flag become player_has_permission; flags were only permissions with a change trigger, which define_permission_trigger now provides for any permission. - on_role_assigned and on_role_unassigned become one on_player_roles_changed event carrying the assigned and unassigned names. Every consumer registered both for the same handler. It is also raised for connected players when a role is edited on the controller, which the old events never were, and for changes made on the controller to the roles a player holds. - player_outranks and player_outranks_role replace the repeated comparison of highest role indexes, and apply the core.admin bypass consistently, which two of the six call sites did not. - get_role takes a name, clusterio id, or role; get_roles replaces get_roles_ordered. The config views of the roles are gone, with role:get_player_names covering the one use of config.players. - Roles carry a permission group, which the legacy system mapped roles to and the first version of the plugin dropped. A player is moved into the group of their most privileged role which names one. It is edited with the other in game properties. - skip_checks is dropped from assign_player and unassign_player. A player object with index 0 is treated as the server, which is how exp_commands represents rcon. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
109 lines
3.4 KiB
TypeScript
109 lines
3.4 KiB
TypeScript
import { BaseInstancePlugin } from "@clusterio/host";
|
|
import * as lib from "@clusterio/lib";
|
|
import * as messages from "./messages";
|
|
|
|
/** Sent by the lua side when roles are changed in game. */
|
|
export type IpcAssignmentUpdate = {
|
|
name: string,
|
|
assign: number[] | undefined,
|
|
unassign: number[] | undefined,
|
|
};
|
|
|
|
export class InstancePlugin extends BaseInstancePlugin {
|
|
async init() {
|
|
this.instance.handle(messages.RoleUpdatedEvent, this.handleRoleUpdatedEvent.bind(this));
|
|
this.instance.handle(messages.AssignmentUpdatedEvent, this.handleAssignmentUpdatedEvent.bind(this));
|
|
this.instance.server.handle("exp_roles:assignment_update", this.handleAssignmentUpdateIPC.bind(this));
|
|
}
|
|
|
|
get syncMode() {
|
|
return this.instance.config.get("exp_roles.sync_mode");
|
|
}
|
|
|
|
async onInstanceConfigFieldChanged(field: string, curr: unknown, prev: unknown) {
|
|
switch (field) {
|
|
case "exp_roles.sync_mode":
|
|
await this.luaSetEmitEvents(curr === "bidirectional");
|
|
break;
|
|
}
|
|
}
|
|
|
|
async onStart() {
|
|
if (this.syncMode === "disabled") {
|
|
return;
|
|
}
|
|
|
|
// Date.now() is used because the lua state is initialised from the full
|
|
// list below, so only updates made after this point are of interest
|
|
const subscribedAtMs = Date.now();
|
|
await this.instance.sendTo("controller", new lib.SubscriptionRequest(
|
|
`exp_roles:${messages.RoleUpdatedEvent.name}`, true, subscribedAtMs
|
|
));
|
|
await this.instance.sendTo("controller", new lib.SubscriptionRequest(
|
|
`exp_roles:${messages.AssignmentUpdatedEvent.name}`, true, subscribedAtMs
|
|
));
|
|
|
|
const [roles, assignments] = await Promise.all([
|
|
this.instance.sendTo("controller", new messages.RoleListRequest()),
|
|
this.instance.sendTo("controller", new messages.AssignmentListRequest()),
|
|
]);
|
|
|
|
if (!roles.some(role => role.isDefault)) {
|
|
this.logger.warn("No default role is set on the controller, players will hold no roles in game");
|
|
}
|
|
|
|
await this.luaSend("initialise", {
|
|
...messages.encodeRolesForLua(roles),
|
|
assignments: assignments.map(assignment => assignment.toJSON()),
|
|
});
|
|
await this.luaSetEmitEvents(this.syncMode === "bidirectional");
|
|
}
|
|
|
|
async handleRoleUpdatedEvent(event: messages.RoleUpdatedEvent) {
|
|
if (this.syncMode === "disabled") {
|
|
return;
|
|
}
|
|
|
|
await this.luaSend("receive_role_updates", event.updates.map(role => role.toJSON()));
|
|
}
|
|
|
|
async handleAssignmentUpdatedEvent(event: messages.AssignmentUpdatedEvent) {
|
|
if (this.syncMode === "disabled") {
|
|
return;
|
|
}
|
|
|
|
await this.luaSend("receive_assignment_updates", event.updates.map(a => a.toJSON()));
|
|
}
|
|
|
|
async handleAssignmentUpdateIPC(event: IpcAssignmentUpdate) {
|
|
if (this.syncMode !== "bidirectional") {
|
|
return;
|
|
}
|
|
|
|
const assign = event.assign ?? [];
|
|
try {
|
|
await this.instance.sendTo("controller", new messages.AssignmentUpdateRequest(
|
|
event.name, assign, event.unassign ?? [],
|
|
));
|
|
} catch (err: any) {
|
|
// The roles were already applied in game, so they have to be taken
|
|
// back off again now that the controller has refused them
|
|
this.logger.warn(`Role change for ${event.name} was rejected: ${err.message}`);
|
|
if (assign.length) {
|
|
await this.luaSend("reject_assignment", { name: event.name, role_ids: assign });
|
|
}
|
|
return;
|
|
}
|
|
}
|
|
|
|
async luaSetEmitEvents(emitEvents: boolean) {
|
|
await this.luaSend("set_emit_events", emitEvents);
|
|
}
|
|
|
|
async luaSend(receiver: string, json: any) {
|
|
await this.instance.sendRcon(
|
|
`/sc exp_roles.${receiver}(helpers.json_to_table[=[${JSON.stringify(json)}]=])`, true
|
|
);
|
|
}
|
|
}
|