mirror of
https://github.com/PHIDIAS0303/ExpCluster.git
synced 2026-11-18 14:53:13 +09:00
Address review on exp_server_list
The controller and instance plugins are init functions with closures. The controller batches refreshes with setMany and deleteMany, and the instance gates updates on its status instead of a started flag. short_name is now display_name. New always_on_top field sorts servers first in get_servers. The record carries has_password from the game password in factorio.settings, never the password itself, and the description falls back to the one in factorio.settings. A controller welcome is used when the instance has none, and the readme falls back to a default welcome from the locale. Lua gets is_synced for connect.lua. The readme passes an empty detail instead of nil to the join tooltip so the player count stays in __2__, restores the Password status, shows the welcome title as a centered label and shortens long descriptions with the full text as a tooltip. The Lua tests assert the lookups they index, which fixes the lint. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
4d136853c9
commit
c0289ed8a7
15 files changed
+528
-418
No files matched your search
+35
-59
@@ -1,63 +1,19 @@
|
||||
import type { Instance, InstancePluginContext } from "@clusterio/host";
|
||||
import type { InstancePluginContext } from "@clusterio/host";
|
||||
import * as lib from "@clusterio/lib";
|
||||
import * as messages from "./messages.js";
|
||||
|
||||
/** Sends the server list to the lua side, see module/control.lua. */
|
||||
export class InstancePlugin {
|
||||
/** Rcon is up and the lua side can take updates */
|
||||
started = false;
|
||||
/**
|
||||
* Sends the server list to the lua side, see module/control.lua.
|
||||
*
|
||||
* The subscription is made by the start hook once rcon is up, so updates
|
||||
* only arrive while the game can take them.
|
||||
*/
|
||||
export default async function (context: InstancePluginContext) {
|
||||
const { instance, plugin } = context;
|
||||
|
||||
private constructor(
|
||||
public instance: Instance,
|
||||
public logger: lib.Logger,
|
||||
public name: string,
|
||||
) {}
|
||||
|
||||
static async fromContext(context: InstancePluginContext) {
|
||||
const instance = context.instance;
|
||||
const plugin = new InstancePlugin(instance, context.logger, context.plugin.name);
|
||||
|
||||
instance.handle(messages.ServerUpdatesEvent, plugin.handleServerUpdatesEvent.bind(plugin));
|
||||
instance.hooks.start.attach(plugin.name, plugin.onStart.bind(plugin));
|
||||
instance.hooks.exit.attach(plugin.name, plugin.onExit.bind(plugin));
|
||||
instance.hooks.controllerConnectionEvent.attach(plugin.name, plugin.onControllerConnectionEvent.bind(plugin));
|
||||
return plugin;
|
||||
}
|
||||
|
||||
async onStart() {
|
||||
this.started = true;
|
||||
await this.syncServers();
|
||||
}
|
||||
|
||||
async onExit() {
|
||||
this.started = false;
|
||||
}
|
||||
|
||||
/** A new session means the controller restarted and forgot the subscription. */
|
||||
async onControllerConnectionEvent(event: "connect" | "drop" | "resume" | "close") {
|
||||
if (event === "connect" && this.started) {
|
||||
await this.syncServers();
|
||||
}
|
||||
}
|
||||
|
||||
/** Subscribe, then replace the whole list */
|
||||
async syncServers() {
|
||||
await this.instance.sendTo("controller", new lib.SubscriptionRequest(
|
||||
`exp_server_list:${messages.ServerUpdatesEvent.name}`, "subscribe", Date.now(),
|
||||
));
|
||||
const servers = await this.instance.sendTo("controller", new messages.ServerListRequest());
|
||||
await this.luaSendServers(servers, true);
|
||||
}
|
||||
|
||||
async handleServerUpdatesEvent(event: messages.ServerUpdatesEvent) {
|
||||
if (this.started) {
|
||||
await this.luaSendServers(event.updates, false);
|
||||
}
|
||||
}
|
||||
|
||||
async luaSendServers(servers: messages.ServerRecord[], replace: boolean) {
|
||||
async function luaSendServers(servers: messages.ServerRecord[], replace: boolean) {
|
||||
const json = JSON.stringify({
|
||||
current_id: this.instance.id,
|
||||
current_id: instance.id,
|
||||
replace,
|
||||
servers: servers.map(server => server.toJSON()),
|
||||
});
|
||||
@@ -66,12 +22,32 @@ export class InstancePlugin {
|
||||
while (json.includes(`]${level}]`)) {
|
||||
level += "=";
|
||||
}
|
||||
await this.instance.sendRcon(
|
||||
await instance.sendRcon(
|
||||
`/sc exp_server_list.receive_update(helpers.json_to_table[${level}[${json}]${level}])`, true,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
export default async function (context: InstancePluginContext) {
|
||||
await InstancePlugin.fromContext(context);
|
||||
/** Subscribe, then replace the whole list */
|
||||
async function syncServers() {
|
||||
await instance.sendTo("controller", new lib.SubscriptionRequest(
|
||||
`exp_server_list:${messages.ServerUpdatesEvent.name}`, "subscribe", Date.now(),
|
||||
));
|
||||
const servers = await instance.sendTo("controller", new messages.ServerListRequest());
|
||||
await luaSendServers(servers, true);
|
||||
}
|
||||
|
||||
instance.handle(messages.ServerUpdatesEvent, async event => {
|
||||
if (instance.status === "starting" || instance.status === "running") {
|
||||
await luaSendServers(event.updates, false);
|
||||
}
|
||||
});
|
||||
|
||||
instance.hooks.start.attach(plugin.name, syncServers);
|
||||
|
||||
// A new session means the controller restarted and forgot the subscription
|
||||
instance.hooks.controllerConnectionEvent.attach(plugin.name, async event => {
|
||||
if (event === "connect" && instance.status === "running") {
|
||||
await syncServers();
|
||||
}
|
||||
});
|
||||
}
|
||||
Reference in new issue
Block a user