feat(arma): import vehicle mass and category defaults, add library picker
This commit is contained in:
parent
0d73da1142
commit
053c35a70c
4 changed files with 178 additions and 4 deletions
|
|
@ -34,6 +34,7 @@ import { default as default_701da9bef3b3cc3d8567adf41fa08aa3 } from '@/component
|
|||
import { default as default_4b88a624e9f0b376cffcc6814d459adb } from '@/components/admin/assignments/AssignmentMembersField'
|
||||
import { default as default_0edde40a6ccd30c893b1227360f69360 } from '@/components/static/TickDurationDescription'
|
||||
import { default as default_2d468cc5cf0b647ffca3201592c20825 } from '@/components/static/WarningMessage'
|
||||
import { default as default_8e873aa39584420378774e6c9c4ac54b } from '@/components/admin/VehicleLibraryPicker'
|
||||
import { AudioPreviewField as AudioPreviewField_6bccef9bf4ea41ed573ab9f36fa579b2 } from '@/components/admin/voiceover/AudioPreviewField'
|
||||
import { default as default_7fa52484aec46e988e9cfb5e86c0c759 } from '@/components/admin/narrative-flow/NarrativeFlowEditor'
|
||||
import { default as default_b7f86fb8e028bca592f57e6d387a0380 } from '@/components/admin/minigames/PeriodCloseControl'
|
||||
|
|
@ -89,6 +90,7 @@ export const importMap = {
|
|||
"@/components/admin/assignments/AssignmentMembersField#default": default_4b88a624e9f0b376cffcc6814d459adb,
|
||||
"@/components/static/TickDurationDescription#default": default_0edde40a6ccd30c893b1227360f69360,
|
||||
"@/components/static/WarningMessage#default": default_2d468cc5cf0b647ffca3201592c20825,
|
||||
"@/components/admin/VehicleLibraryPicker#default": default_8e873aa39584420378774e6c9c4ac54b,
|
||||
"@/components/admin/voiceover/AudioPreviewField#AudioPreviewField": AudioPreviewField_6bccef9bf4ea41ed573ab9f36fa579b2,
|
||||
"@/components/admin/narrative-flow/NarrativeFlowEditor#default": default_7fa52484aec46e988e9cfb5e86c0c759,
|
||||
"@/components/admin/minigames/PeriodCloseControl#default": default_b7f86fb8e028bca592f57e6d387a0380,
|
||||
|
|
|
|||
|
|
@ -19,6 +19,17 @@ export const Vehicles: CollectionConfig = {
|
|||
delete: requireLogisticsPermission("vehicles:delete"),
|
||||
},
|
||||
fields: [
|
||||
{
|
||||
// D11b: searchable imported-library picker (name, classname, thumbnail)
|
||||
// that copies an imported entry into this form on click.
|
||||
name: "libraryPicker",
|
||||
type: "ui",
|
||||
admin: {
|
||||
components: {
|
||||
Field: "@/components/admin/VehicleLibraryPicker",
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
label: "Identity",
|
||||
type: "group",
|
||||
|
|
|
|||
135
src/components/admin/VehicleLibraryPicker.tsx
Normal file
135
src/components/admin/VehicleLibraryPicker.tsx
Normal file
|
|
@ -0,0 +1,135 @@
|
|||
"use client";
|
||||
|
||||
import { useEffect, useState } from "react";
|
||||
import { SearchIcon, TruckIcon } from "lucide-react";
|
||||
import { toast, useField } from "@payloadcms/ui";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { Input } from "@/components/ui/input";
|
||||
|
||||
/**
|
||||
* Imported library picker for vehicle templates (D11b). Searches the vehicles
|
||||
* collection (the rows importArmaLibrary maintains) by display name or
|
||||
* classname with thumbnails, and copies the chosen entry's name + classname
|
||||
* into the open document so officers never hand-type classnames. Applies only
|
||||
* on explicit click; nothing is written automatically.
|
||||
*/
|
||||
|
||||
interface LibraryEntry {
|
||||
id: number;
|
||||
name?: string | null;
|
||||
classname?: string | null;
|
||||
transportMode?: string | null;
|
||||
image?: { id?: number; url?: string | null } | string | number | null;
|
||||
}
|
||||
|
||||
export function VehicleLibraryPicker() {
|
||||
const nameField = useField<string>({ path: "name" });
|
||||
const classnameField = useField<string>({ path: "classname" });
|
||||
const imageField = useField<unknown>({ path: "image" });
|
||||
const [query, setQuery] = useState("");
|
||||
const [results, setResults] = useState<LibraryEntry[]>([]);
|
||||
const [searching, setSearching] = useState(false);
|
||||
|
||||
useEffect(() => {
|
||||
const q = query.trim();
|
||||
if (q.length < 2) {
|
||||
setResults([]);
|
||||
return;
|
||||
}
|
||||
let cancelled = false;
|
||||
setSearching(true);
|
||||
const where = encodeURIComponent(
|
||||
JSON.stringify({ or: [{ name: { like: q } }, { classname: { like: q } }] }),
|
||||
);
|
||||
const timer = setTimeout(async () => {
|
||||
try {
|
||||
const res = await fetch(`/api/vehicles?where=${where}&limit=12&depth=1`, {
|
||||
credentials: "include",
|
||||
});
|
||||
const json = (await res.json()) as { docs?: LibraryEntry[] };
|
||||
if (!cancelled) setResults(Array.isArray(json.docs) ? json.docs : []);
|
||||
} catch {
|
||||
if (!cancelled) setResults([]);
|
||||
} finally {
|
||||
if (!cancelled) setSearching(false);
|
||||
}
|
||||
}, 250);
|
||||
return () => {
|
||||
cancelled = true;
|
||||
clearTimeout(timer);
|
||||
};
|
||||
}, [query]);
|
||||
|
||||
function apply(entry: LibraryEntry) {
|
||||
nameField.setValue(entry.name ?? "");
|
||||
classnameField.setValue(entry.classname ?? "");
|
||||
// Fill the thumbnail only when this template has none yet.
|
||||
if (imageField.value == null && entry.image != null && entry.image !== "") {
|
||||
const mediaId =
|
||||
typeof entry.image === "object" ? entry.image.id : Number(entry.image);
|
||||
if (mediaId != null && Number.isFinite(Number(mediaId))) {
|
||||
imageField.setValue(Number(mediaId));
|
||||
}
|
||||
}
|
||||
toast.success(`Loaded ${entry.name || entry.classname} into this template.`);
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="border border-border rounded-lg p-4 flex flex-col gap-3">
|
||||
<div className="flex items-center justify-between gap-2">
|
||||
<h3 className="text-sm font-semibold">Imported library</h3>
|
||||
<span className="text-xs text-[var(--theme-elevation-500)]">
|
||||
Search imported Arma vehicle templates and copy them into this form
|
||||
</span>
|
||||
</div>
|
||||
<div className="relative">
|
||||
<SearchIcon className="size-4 absolute left-2.5 top-1/2 -translate-y-1/2 text-[var(--theme-elevation-500)]" />
|
||||
<Input
|
||||
value={query}
|
||||
onChange={(e) => setQuery(e.target.value)}
|
||||
placeholder="Search by name or classname (2+ characters)..."
|
||||
className="pl-8"
|
||||
/>
|
||||
</div>
|
||||
{searching && <p className="text-xs text-[var(--theme-elevation-500)]">Searching...</p>}
|
||||
{!searching && query.trim().length >= 2 && results.length === 0 && (
|
||||
<p className="text-xs text-[var(--theme-elevation-500)]">
|
||||
Nothing found. Run the catalog import to add it first.
|
||||
</p>
|
||||
)}
|
||||
{results.length > 0 && (
|
||||
<ul className="max-h-64 overflow-auto divide-y divide-[var(--theme-border-color)] border border-[var(--theme-border-color)] rounded-md">
|
||||
{results.map((entry) => {
|
||||
const image = entry.image && typeof entry.image === "object" ? entry.image : null;
|
||||
return (
|
||||
<li key={entry.id} className="flex items-center gap-3 px-3 py-2">
|
||||
{image?.url ? (
|
||||
// eslint-disable-next-line @next/next/no-img-element
|
||||
<img
|
||||
src={image.url}
|
||||
alt=""
|
||||
className="size-10 rounded object-contain bg-[var(--theme-elevation-100)] shrink-0"
|
||||
/>
|
||||
) : (
|
||||
<TruckIcon className="size-5 shrink-0 text-[var(--theme-elevation-500)]" />
|
||||
)}
|
||||
<div className="min-w-0 flex-1">
|
||||
<div className="truncate text-sm">{entry.name || "(unnamed)"}</div>
|
||||
<div className="truncate font-mono text-xs text-[var(--theme-elevation-500)]">
|
||||
{entry.classname}
|
||||
{entry.transportMode ? ` · ${entry.transportMode}` : ""}
|
||||
</div>
|
||||
</div>
|
||||
<Button type="button" size="sm" variant="outline" onClick={() => apply(entry)}>
|
||||
Use
|
||||
</Button>
|
||||
</li>
|
||||
);
|
||||
})}
|
||||
</ul>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
export default VehicleLibraryPicker;
|
||||
|
|
@ -12,8 +12,8 @@
|
|||
* - For "vehicle" records `extra` is the transport mode ("air" | "sea" |
|
||||
* "ground"; anything else -> "ground").
|
||||
* - For "magazine" records `extra` is the round count (magazine roundCapacity).
|
||||
* - `mass` is accepted for contract completeness; no collection field maps to
|
||||
* it yet, so it is not imported.
|
||||
* - `mass` imports into `vehicles.maxMass` for vehicle records (physical mass
|
||||
* from the export); asset records do not carry a matching field.
|
||||
*
|
||||
* Legacy field aliases (`class`, `category`, `name`) are still accepted.
|
||||
*
|
||||
|
|
@ -56,7 +56,7 @@ export interface CatalogRecord {
|
|||
picture?: string;
|
||||
mod?: string;
|
||||
description?: string;
|
||||
/** Accepted by the contract; no collection field maps to it (not imported). */
|
||||
/** Vehicles only: physical mass, imported into maxMass. */
|
||||
mass?: number;
|
||||
/** Transport mode for vehicles, round count for magazines, otherwise "". */
|
||||
extra?: number | string;
|
||||
|
|
@ -82,6 +82,8 @@ export interface ImportPlan {
|
|||
assetType: AssetType | "vehicle";
|
||||
/** Vehicles only. */
|
||||
transportMode: TransportMode;
|
||||
/** Vehicles only: physical mass from the export (written to maxMass). */
|
||||
mass?: number;
|
||||
/** Magazines only: round count from `extra`. */
|
||||
roundCapacity?: number;
|
||||
/** Weapons only: compatibility references (classnames), linked in a second pass. */
|
||||
|
|
@ -181,6 +183,20 @@ function transportModeFromExtra(extra: number | string | undefined): TransportMo
|
|||
return "ground";
|
||||
}
|
||||
|
||||
/**
|
||||
* Default Vehicles.category derived from the transport mode. Only applied
|
||||
* when a template has no category yet; officers refine it by hand (the export
|
||||
* carries no doctrine classification).
|
||||
*/
|
||||
export function defaultCategoryForTransport(mode: TransportMode): "utility" | "transport" {
|
||||
return mode === "ground" ? "utility" : "transport";
|
||||
}
|
||||
|
||||
/** Positive finite mass from a catalog record, undefined otherwise. */
|
||||
function positiveMass(mass: number | undefined): number | undefined {
|
||||
return typeof mass === "number" && Number.isFinite(mass) && mass > 0 ? mass : undefined;
|
||||
}
|
||||
|
||||
/**
|
||||
* Plan one catalog record (pure — no filesystem or database access).
|
||||
* Returns undefined for records without a usable classname.
|
||||
|
|
@ -196,6 +212,7 @@ export function planRecord(rec: CatalogRecord, lookups: PlanLookups = {}): Impor
|
|||
const pictureRef = lookupIcon(lookups.icons, className) || String(rec.picture ?? "").trim();
|
||||
|
||||
if (rawType === "vehicle" || (!ASSET_TYPE_SET.has(rawType) && looksLikeVehicle(className, rawType))) {
|
||||
const mass = positiveMass(rec.mass);
|
||||
return {
|
||||
kind: "vehicle",
|
||||
className,
|
||||
|
|
@ -203,6 +220,7 @@ export function planRecord(rec: CatalogRecord, lookups: PlanLookups = {}): Impor
|
|||
description,
|
||||
assetType: "vehicle",
|
||||
transportMode: transportModeFromExtra(rec.extra),
|
||||
...(mass !== undefined ? { mass } : {}),
|
||||
mod,
|
||||
pictureRef,
|
||||
media: { paaPath: "", pngPath: "", screenshotPath: "" },
|
||||
|
|
@ -484,9 +502,15 @@ async function upsertVehicle(payload: Payload, plan: ImportPlan, fuelId: number
|
|||
transportMode: plan.transportMode,
|
||||
};
|
||||
if (plan.description) data.description = plaintextToLexical(plan.description);
|
||||
// Physical mass is pure game data: refresh it on every import.
|
||||
if (plan.mass !== undefined) data.maxMass = plan.mass;
|
||||
const imagePath = planImagePath(plan);
|
||||
// Same rule as assets: fill an empty image, never replace one that is set.
|
||||
const current = existing.docs[0] as unknown as { image?: unknown };
|
||||
const current = existing.docs[0] as unknown as { image?: unknown; category?: unknown };
|
||||
// Category default only fills templates nobody has classified yet.
|
||||
if (!Array.isArray(current.category) || current.category.length === 0) {
|
||||
data.category = [defaultCategoryForTransport(plan.transportMode)];
|
||||
}
|
||||
if (imagePath && !current.image) {
|
||||
const media = await resolveMedia(payload, imagePath);
|
||||
if (media) data.image = media.id;
|
||||
|
|
@ -502,6 +526,8 @@ async function upsertVehicle(payload: Payload, plan: ImportPlan, fuelId: number
|
|||
classname: plan.className,
|
||||
fuel: { fuelType: fuelId, fuelCapacity: 100, fuelConsumptionRate: 1 },
|
||||
transportMode: plan.transportMode,
|
||||
category: [defaultCategoryForTransport(plan.transportMode)],
|
||||
...(plan.mass !== undefined ? { maxMass: plan.mass } : {}),
|
||||
...(plan.description ? { description: plaintextToLexical(plan.description) } : {}),
|
||||
...(mediaId !== undefined ? { image: mediaId } : {}),
|
||||
};
|
||||
|
|
|
|||
Loading…
Reference in a new issue