Files
browser/platform_web/contracts/serverManagement.ts
T

129 lines
3.8 KiB
TypeScript

import type {
GamePluginResponse,
JobResponse,
RunEndpointResponse,
ServerDeploymentMode,
ServerInstanceResponse,
ServerInstanceState
} from "../api/types";
export type ServerWorkflowViewState = "api" | "local" | "saving";
export type ServerLifecycleActionLabel = "create" | "start" | "stop" | "refresh";
export type ServerRemovalAction = "delete";
export interface ServerCreateFormState {
id: string;
name: string;
pluginId: string;
createInputs: Record<string, string>;
deploymentMode: ServerDeploymentMode;
serverRoot: string;
workingDirectory: string;
installCommand: string;
startCommand: string;
stopCommand: string;
statusCommand: string;
shell: "" | "posix-sh" | "powershell" | "cmd";
}
export interface ServerWorkflowActionState {
label: ServerLifecycleActionLabel;
serverInstanceId?: string;
success: boolean;
message: string;
}
export interface ServerMetadataFormState {
name: string;
}
export interface ServerRemovalConfirmationState {
action: ServerRemovalAction;
serverInstanceId: string;
name: string;
state: ServerInstanceState;
}
export interface ServerManagementSummary {
total: number;
running: number;
pendingJobs: number;
failed: number;
}
export type RuntimeObservationFreshness = "fresh" | "unverified";
export function runtimeObservationFreshness(instance: ServerInstanceResponse, endpoint: RunEndpointResponse | undefined, now = Date.now()): RuntimeObservationFreshness {
if (!endpoint || endpoint.status !== "online") return "unverified";
const heartbeat = Date.parse(endpoint.lastHeartbeatAt);
return Number.isFinite(heartbeat) && now-heartbeat <= 30_000 ? "fresh" : "unverified";
}
export const emptyServerCreateForm: ServerCreateFormState = {
id: "",
name: "",
pluginId: "",
createInputs: {},
deploymentMode: "guided-install",
serverRoot: "",
workingDirectory: "",
installCommand: "",
startCommand: "",
stopCommand: "",
statusCommand: "",
shell: ""
};
export function summarizeServerManagement(instances: ServerInstanceResponse[], jobs: JobResponse[]): ServerManagementSummary {
return {
total: instances.length,
running: instances.filter((instance) => instance.state === "running").length,
pendingJobs: jobs.filter((job) => isPendingJobState(job.state)).length,
failed: instances.filter((instance) => instance.state === "failed").length
};
}
export function pendingJobsForServer(jobs: JobResponse[], serverInstanceId: string): JobResponse[] {
return jobs.filter((job) => job.serverInstanceId === serverInstanceId && isPendingJobState(job.state));
}
export function pluginLabel(plugin: GamePluginResponse | undefined, pluginId: string): string {
if (!plugin) {
return pluginId;
}
return plugin.serverDisplayName || plugin.name || plugin.id;
}
export function canStartServer(state: ServerInstanceState): boolean {
return state === "ready" || state === "stopped" || state === "failed";
}
export function canStopServer(state: ServerInstanceState): boolean {
return state === "running";
}
export function isPendingJobState(state: JobResponse["state"]): boolean {
return state === "queued" || state === "accepted" || state === "running" || state === "retrying";
}
export function defaultServerCreateForm(plugins: GamePluginResponse[]): ServerCreateFormState {
const plugin = plugins[0];
return {
...emptyServerCreateForm,
pluginId: plugin?.id ?? "",
createInputs: pluginCreateInputDefaults(plugin)
};
}
export function pluginCreateInputDefaults(plugin: GamePluginResponse | undefined): Record<string, string> {
return Object.fromEntries((plugin?.createFields ?? []).map((field) => [field.key, field.defaultValue ?? ""]));
}
export function serverMetadataFormFromInstance(instance: ServerInstanceResponse): ServerMetadataFormState {
return { name: instance.name };
}
export function canDeleteServer(state: ServerInstanceState): boolean {
return state !== "deleted";
}