mirror of
https://github.com/Corfirean/coa-server-manager.git
synced 2026-11-21 03:25:56 +00:00
Phase 7a: real population (players vs companions), hardware-aware companion sizes, RA bot spawn, Bots page card
Verified: fixture received 'botcmd spawnleveled 12' over RA from the UI and the counts moved; population query also read-only verified against the live server (33 companions online). Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
981ee4d4d9
commit
9504e9133b
9 files changed
+351
-3
No files matched your search
@@ -0,0 +1,97 @@
|
||||
import { useEffect, useState } from "react";
|
||||
import { Loader2 } from "lucide-react";
|
||||
import { api, asUiError, type CompanionSizes, type Population, type UiError } from "@/lib/api";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { Card } from "@/components/ui/card";
|
||||
|
||||
export function CompanionsCard({ serverId }: { serverId: string }) {
|
||||
const [sizes, setSizes] = useState<CompanionSizes | null>(null);
|
||||
const [pop, setPop] = useState<Population | null>(null);
|
||||
const [running, setRunning] = useState(false);
|
||||
const [choice, setChoice] = useState<string>("small");
|
||||
const [custom, setCustom] = useState("100");
|
||||
const [busy, setBusy] = useState(false);
|
||||
const [error, setError] = useState<UiError | null>(null);
|
||||
const [note, setNote] = useState<string | null>(null);
|
||||
|
||||
useEffect(() => {
|
||||
void api.companionSizes().then(setSizes);
|
||||
let alive = true;
|
||||
const poll = async () => {
|
||||
const s = await api.status(serverId).catch(() => null);
|
||||
if (!alive || !s) return;
|
||||
const up = s.observed.world.state === "running";
|
||||
setRunning(up);
|
||||
setPop(up ? await api.population(serverId).catch(() => null) : null);
|
||||
};
|
||||
void poll();
|
||||
const t = setInterval(poll, 4000);
|
||||
return () => {
|
||||
alive = false;
|
||||
clearInterval(t);
|
||||
};
|
||||
}, [serverId]);
|
||||
|
||||
const count = choice === "custom" ? Number(custom) : (sizes?.sizes.find((s) => s.id === choice)?.bots ?? 0);
|
||||
const warning = choice === "custom" ? (count > 500 ? "Large populations may need more CPU and memory. Increase gradually." : null) : (sizes?.sizes.find((s) => s.id === choice)?.warning ?? null);
|
||||
const valid = Number.isInteger(count) && count >= 1 && count <= 2000;
|
||||
|
||||
async function add() {
|
||||
setBusy(true);
|
||||
setError(null);
|
||||
setNote(null);
|
||||
try {
|
||||
const r = await api.addCompanions(serverId, count);
|
||||
setNote(r.spawned ? `Creating ${count} companions. They appear gradually over the next minutes.` : `Saved. ${count} companions will log in when the server starts.`);
|
||||
} catch (e) {
|
||||
setError(asUiError(e));
|
||||
} finally {
|
||||
setBusy(false);
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<Card className="mt-6 p-6">
|
||||
<h2 className="font-semibold">AI companions</h2>
|
||||
<p className="mt-1 text-sm text-muted">
|
||||
{pop
|
||||
? `${pop.bots_online} online now · ${pop.bots_total} created in total`
|
||||
: running
|
||||
? "Reading the current population…"
|
||||
: "Start the server to see how many companions are online."}
|
||||
</p>
|
||||
|
||||
<fieldset className="mt-4">
|
||||
<legend className="text-sm text-muted">Would you like more companions?</legend>
|
||||
<div className="mt-2 flex flex-wrap gap-2">
|
||||
{sizes?.sizes.map((s) => (
|
||||
<label key={s.id} className={`cursor-pointer rounded-md border px-3 py-2 text-sm ${choice === s.id ? "border-gold bg-gold/10" : "border-line hover:bg-white/5"}`}>
|
||||
<input type="radio" name="size" className="sr-only" checked={choice === s.id} onChange={() => setChoice(s.id)} />
|
||||
{s.title} <span className="text-muted">· {s.bots}</span>
|
||||
</label>
|
||||
))}
|
||||
<label className={`cursor-pointer rounded-md border px-3 py-2 text-sm ${choice === "custom" ? "border-gold bg-gold/10" : "border-line hover:bg-white/5"}`}>
|
||||
<input type="radio" name="size" className="sr-only" checked={choice === "custom"} onChange={() => setChoice("custom")} />
|
||||
Custom
|
||||
</label>
|
||||
{choice === "custom" && (
|
||||
<input aria-label="Number of companions" value={custom} onChange={(e) => setCustom(e.target.value)} inputMode="numeric" className="w-24 rounded-md border border-line bg-bg px-3 py-2 text-right text-sm outline-none focus:border-gold" />
|
||||
)}
|
||||
</div>
|
||||
</fieldset>
|
||||
|
||||
{warning && <p className="mt-3 text-sm text-warn" role="status">{warning}</p>}
|
||||
{sizes && <p className="mt-2 text-xs text-muted">This computer: {sizes.hardware.cores} cores, {sizes.hardware.ram_gb.toFixed(0)} GB memory.</p>}
|
||||
|
||||
<div className="mt-4 flex items-center gap-3">
|
||||
<Button variant="primary" disabled={busy || !valid} onClick={() => void add()}>
|
||||
{busy && <Loader2 className="h-4 w-4 animate-spin" aria-hidden />}
|
||||
Add {valid ? count : ""} companions
|
||||
</Button>
|
||||
{!running && <span className="text-xs text-muted">The server is stopped: they will be created the next time it runs.</span>}
|
||||
</div>
|
||||
{note && <p className="mt-3 text-sm text-ok" role="status">{note}</p>}
|
||||
{error && <p className="mt-3 text-sm text-bad" role="alert">{error.human.code === "unknown" ? error.technical : error.human.message}</p>}
|
||||
</Card>
|
||||
);
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
import { useCallback, useEffect, useRef, useState } from "react";
|
||||
import { Loader2 } from "lucide-react";
|
||||
import { api, asUiError, type Human, type ServerSummary, type ServiceStatus, type StatusView } from "@/lib/api";
|
||||
import { api, asUiError, type Human, type Population, type ServerSummary, type ServiceStatus, type StatusView } from "@/lib/api";
|
||||
import { cn, formatUptime } from "@/lib/utils";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { Card } from "@/components/ui/card";
|
||||
@@ -36,12 +36,17 @@ export function Overview({ server, onForget }: { server: ServerSummary; onForget
|
||||
const [action, setAction] = useState<Action>(null);
|
||||
const [failure, setFailure] = useState<Failure | null>(null);
|
||||
const [showDetails, setShowDetails] = useState(false);
|
||||
const [pop, setPop] = useState<Population | null>(null);
|
||||
const alive = useRef(true);
|
||||
|
||||
const poll = useCallback(async () => {
|
||||
try {
|
||||
const s = await api.status(server.id);
|
||||
if (alive.current) setStatus(s);
|
||||
if (s.observed.world.state === "running") {
|
||||
const p = await api.population(server.id).catch(() => null);
|
||||
if (alive.current) setPop(p);
|
||||
} else if (alive.current) setPop(null);
|
||||
} catch {
|
||||
/* transient; next poll retries */
|
||||
}
|
||||
@@ -139,7 +144,7 @@ export function Overview({ server, onForget }: { server: ServerSummary; onForget
|
||||
</div>
|
||||
<div>
|
||||
<dt className="text-muted">Players online</dt>
|
||||
<dd className="mt-0.5 text-lg text-muted">—</dd>
|
||||
<dd className="mt-0.5 text-lg">{pop ? `${pop.players_online}` : "—"}{pop && pop.bots_online > 0 ? <span className="ml-2 text-sm text-muted">+ {pop.bots_online} companions</span> : null}</dd>
|
||||
</div>
|
||||
</dl>
|
||||
|
||||
|
||||
@@ -16,6 +16,7 @@ import {
|
||||
import { cn } from "@/lib/utils";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { Card } from "@/components/ui/card";
|
||||
import { CompanionsCard } from "@/screens/CompanionsCard";
|
||||
|
||||
const RESTART_LABEL = { runtime: "Applies immediately", world: "Needs a world server restart", full: "Needs a full server restart" } as const;
|
||||
|
||||
@@ -242,6 +243,8 @@ export function SettingsPage(props: { serverId: string; scope: Scope; title: str
|
||||
<h1 className="text-2xl font-semibold">{props.title}</h1>
|
||||
<p className="mt-1 text-muted">{props.question}</p>
|
||||
|
||||
{scope === "bots" && <CompanionsCard serverId={serverId} />}
|
||||
|
||||
{view.drift_keys.length > 0 && (
|
||||
<Card className="mt-5 border-warn/40 p-4" role="status">
|
||||
<p className="font-medium text-warn">{view.drift_keys.length} setting(s) were edited by hand</p>
|
||||
|
||||
Reference in new issue
Block a user