Fix server create plugin filtering

This commit is contained in:
npc0-hue
2026-09-07 18:17:33 +08:00
parent 86167a3a1b
commit f69429bfc7
5 changed files with 154 additions and 8 deletions
@@ -94,6 +94,24 @@ export function pluginLabel(plugin: GamePluginResponse | undefined, pluginId: st
return plugin.serverDisplayName || plugin.name || plugin.id;
}
export function serverCreatePluginOptions(plugins: GamePluginResponse[]): GamePluginResponse[] {
const selected = new Map<string, GamePluginResponse>();
for (const plugin of plugins.filter(pluginCanCreateServer)) {
const key = serverCreatePluginIdentity(plugin);
const current = selected.get(key);
if (!current || compareServerCreatePlugins(plugin, current) < 0) selected.set(key, plugin);
}
return Array.from(selected.values()).sort(compareServerCreatePlugins);
}
export function pluginCanCreateServer(plugin: GamePluginResponse): boolean {
return plugin.status === "installed" && plugin.declaredPermissions.includes("server.create");
}
export function pluginCreateInputValues(plugin: GamePluginResponse | undefined, values: Record<string, string> | undefined): Record<string, string> {
return { ...pluginCreateInputDefaults(plugin), ...(values ?? {}) };
}
export function canStartServer(state: ServerInstanceState): boolean {
return state === "ready" || state === "stopped" || state === "failed";
}
@@ -119,6 +137,35 @@ export function pluginCreateInputDefaults(plugin: GamePluginResponse | undefined
return Object.fromEntries((plugin?.createFields ?? []).map((field) => [field.key, field.defaultValue ?? ""]));
}
function serverCreatePluginIdentity(plugin: GamePluginResponse): string {
return `${plugin.serverType}:${plugin.serverDisplayName || plugin.name || plugin.id}`.toLowerCase();
}
function compareServerCreatePlugins(left: GamePluginResponse, right: GamePluginResponse): number {
const leftScore = serverCreatePluginScore(left);
const rightScore = serverCreatePluginScore(right);
for (let index = 0; index < leftScore.length; index += 1) {
const delta = leftScore[index]-rightScore[index];
if (delta !== 0) return delta;
}
const labelDelta = pluginLabel(left, left.id).localeCompare(pluginLabel(right, right.id));
return labelDelta || left.id.localeCompare(right.id);
}
function serverCreatePluginScore(plugin: GamePluginResponse): number[] {
return [
(plugin.createFields?.length ?? 0) > 0 ? 0 : 1,
plugin.tags.includes("development") ? 1 : 0,
plugin.id === `game.${plugin.serverType}` ? 0 : 1,
plugin.id.includes(".codex.") ? 1 : 0,
-versionWeight(plugin.version)
];
}
function versionWeight(version: string): number {
return version.split(/[^0-9]+/).filter(Boolean).slice(0, 3).reduce((weight, part, index) => weight+Number(part || 0)*Math.pow(1000, 2-index), 0);
}
export function serverMetadataFormFromInstance(instance: ServerInstanceResponse): ServerMetadataFormState {
return { name: instance.name };
}