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,145 @@
|
||||
import React, { useContext, useEffect, useState } from "react";
|
||||
import { Button, ColorPicker, Form, Input, InputNumber, Space, Switch, Tooltip } from "antd";
|
||||
|
||||
import * as lib from "@clusterio/lib";
|
||||
import { ControlContext, SectionHeader, useAccount, notifyErrorHandler } from "@clusterio/web_ui";
|
||||
|
||||
import { RoleColor, RoleMetaRecord, RoleMetaUpdateRequest } from "../../messages";
|
||||
import type { WebPlugin } from "..";
|
||||
|
||||
const MS_PER_HOUR = 3600000;
|
||||
|
||||
type RoleFormValues = {
|
||||
order: number;
|
||||
priority: number;
|
||||
shortHand: string;
|
||||
tag: string;
|
||||
color: string | { toRgb(): { r: number, g: number, b: number } } | null;
|
||||
autoAssignHours: number | null;
|
||||
blockAutoAssign: boolean;
|
||||
};
|
||||
|
||||
/**
|
||||
* In game properties of a role, shown at the end of the core role page.
|
||||
*
|
||||
* The name, description and permissions of a role are owned by clusterio, this
|
||||
* only covers the properties which only mean something in game.
|
||||
*/
|
||||
export default function RoleProperties(props: { plugin: WebPlugin, role?: lib.Role }) {
|
||||
const control = useContext(ControlContext);
|
||||
const account = useAccount();
|
||||
const [form] = Form.useForm<RoleFormValues>();
|
||||
const [applying, setApplying] = useState(false);
|
||||
|
||||
const [roles] = props.plugin.useRoles();
|
||||
const record = props.role ? roles.get(props.role.id) : undefined;
|
||||
const meta = record?.meta;
|
||||
const canUpdate = account.hasPermission("exp_roles.role.update");
|
||||
|
||||
useEffect(() => {
|
||||
if (!meta) {
|
||||
return;
|
||||
}
|
||||
form.setFieldsValue({
|
||||
order: meta.order,
|
||||
priority: meta.priority,
|
||||
shortHand: meta.shortHand,
|
||||
tag: meta.tag,
|
||||
color: meta.color ? `rgb(${meta.color.r}, ${meta.color.g}, ${meta.color.b})` : null,
|
||||
autoAssignHours: meta.autoAssignOnlineTimeMs === null
|
||||
? null
|
||||
: meta.autoAssignOnlineTimeMs / MS_PER_HOUR,
|
||||
blockAutoAssign: meta.blockAutoAssign,
|
||||
});
|
||||
}, [meta, form]);
|
||||
|
||||
if (!props.role || !meta) {
|
||||
return null;
|
||||
}
|
||||
|
||||
async function apply() {
|
||||
const values = form.getFieldsValue();
|
||||
|
||||
let color: RoleColor | null = null;
|
||||
if (values.color && typeof values.color !== "string") {
|
||||
const rgb = values.color.toRgb();
|
||||
color = new RoleColor(Math.round(rgb.r), Math.round(rgb.g), Math.round(rgb.b));
|
||||
} else if (typeof values.color === "string") {
|
||||
const match = values.color.match(/(\d+)\D+(\d+)\D+(\d+)/);
|
||||
if (match) {
|
||||
color = new RoleColor(Number(match[1]), Number(match[2]), Number(match[3]));
|
||||
}
|
||||
}
|
||||
|
||||
await control.send(new RoleMetaUpdateRequest(new RoleMetaRecord(
|
||||
props.role!.id,
|
||||
values.order,
|
||||
values.priority,
|
||||
values.shortHand ?? "",
|
||||
values.tag ?? "",
|
||||
color,
|
||||
values.autoAssignHours === null || values.autoAssignHours === undefined
|
||||
? null
|
||||
: values.autoAssignHours * MS_PER_HOUR,
|
||||
values.blockAutoAssign ?? false,
|
||||
)));
|
||||
}
|
||||
|
||||
return <>
|
||||
<SectionHeader title="In Game Properties" />
|
||||
<Form form={form} labelCol={{ span: 8 }} wrapperCol={{ span: 16 }} disabled={!canUpdate}>
|
||||
<Form.Item
|
||||
name="order"
|
||||
label={<Tooltip title="A lower value is a more privileged role">Order</Tooltip>}
|
||||
>
|
||||
<InputNumber />
|
||||
</Form.Item>
|
||||
<Form.Item
|
||||
name="priority"
|
||||
label={
|
||||
<Tooltip title="Only the highest priority roles a player holds apply, used by Jail">
|
||||
Priority
|
||||
</Tooltip>
|
||||
}
|
||||
>
|
||||
<InputNumber />
|
||||
</Form.Item>
|
||||
<Form.Item name="shortHand" label="Short hand">
|
||||
<Input />
|
||||
</Form.Item>
|
||||
<Form.Item name="tag" label="Tag">
|
||||
<Input />
|
||||
</Form.Item>
|
||||
<Form.Item name="color" label="Colour">
|
||||
<ColorPicker allowClear format="rgb" disabledAlpha />
|
||||
</Form.Item>
|
||||
<Form.Item
|
||||
name="autoAssignHours"
|
||||
label={
|
||||
<Tooltip title="Granted once a player reaches this much online time across the cluster">
|
||||
Auto assign after (hours)
|
||||
</Tooltip>
|
||||
}
|
||||
>
|
||||
<InputNumber min={0} placeholder="Never" />
|
||||
</Form.Item>
|
||||
<Form.Item name="blockAutoAssign" label="Block auto assign" valuePropName="checked">
|
||||
<Switch />
|
||||
</Form.Item>
|
||||
{canUpdate && <Form.Item wrapperCol={{ offset: 8 }}>
|
||||
<Space>
|
||||
<Button
|
||||
type="primary"
|
||||
loading={applying}
|
||||
onClick={() => {
|
||||
setApplying(true);
|
||||
apply()
|
||||
.catch(notifyErrorHandler("Error updating role properties"))
|
||||
.finally(() => setApplying(false));
|
||||
}}
|
||||
>Apply</Button>
|
||||
</Space>
|
||||
</Form.Item>}
|
||||
</Form>
|
||||
</>;
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
import React, { useCallback, useSyncExternalStore } from "react";
|
||||
import { BaseWebPlugin } from "@clusterio/web_ui";
|
||||
|
||||
import * as lib from "@clusterio/lib";
|
||||
import * as messages from "../messages";
|
||||
|
||||
import RoleProperties from "./components/RoleProperties";
|
||||
|
||||
export class WebPlugin extends BaseWebPlugin {
|
||||
roles = new lib.MapSubscriber(messages.RoleUpdatedEvent, this.control);
|
||||
|
||||
async init() {
|
||||
// The core components pass a role and the plugin, which componentExtra
|
||||
// does not carry in its type
|
||||
this.componentExtra = {
|
||||
RoleViewPage: RoleProperties as React.ComponentType,
|
||||
};
|
||||
}
|
||||
|
||||
useRoles() {
|
||||
const subscribe = useCallback((cb: () => void) => this.roles.subscribe(cb), []);
|
||||
return useSyncExternalStore(subscribe, () => this.roles.getSnapshot());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user