1
0
Fork 0
polaris-task-force/src/collections/world/MapObjectives.ts
Z8MB1E 3bef63af33 feat(map-objectives): superuser-authored strategic objectives on mission maps
Add a superuser-authored objective layer (defend, assault, secure, etc.)
shown as labelled map markers on both the world map and mission planning
map, with per-layer toggles on the planning map.

- collection: MapObjectives (map_objectives table, enums, FKs)
- lib: objectives service + objectiveTypes + permissions + event emit
- actions/api: create/update/delete objectives server actions
- UI: feature-layer toggles, objective picker dialog, world/plan markers,
  SQF export, dossier dialog integration
- generated: regenerate payload schema + types for map_objectives
- tests: 10 integration specs + e2e spec
- docs: CHANGELOG, map-system, AGENTS guides
2026-10-27 02:12:31 -04:00

207 lines
6.4 KiB
TypeScript

import type { CollectionConfig } from "payload";
import { hasPermission, requirePermission } from "@/utils/access-control/hasPermission";
import { emitGameEvent } from "@/utils/event-log/emit";
import { EventTypes } from "@/utils/event-log/eventTypes";
import { validatePosition } from "@/lib/map/points";
import {
DEFAULT_OBJECTIVE_MARKER_TYPE,
MAP_OBJECTIVE_STATUSES,
OBJECTIVE_STATUS_META,
objectiveStatusLabel,
} from "@/lib/map/objectiveTypes";
/** Payload's loose doc shape inside hooks (avoids a generated-types dependency). */
interface ObjectiveHookDoc {
id: number;
map?: number | { id: number } | null;
name?: string | null;
status?: string | null;
redacted?: boolean | null;
}
function mapIdOf(doc: ObjectiveHookDoc): number | null {
const value = doc.map;
if (typeof value === "number") return value;
if (value && typeof value === "object") return value.id;
return null;
}
/**
* Strategic objectives: Arma-style command markers that belong to a terrain
* map and are visible to every mission planned on it.
*
* - Read: any logged-in user, except `redacted` rows which Payload filters out
* for anyone without the superuser tier.
* - Write: `system:admin-access` only (built-in admin role + superuser roles).
* Division roles are never granted this tier.
*
* Frontend authoring flows through `src/app/(frontend)/map/actions.ts`; this
* collection-level gate governs the REST API and the admin panel.
*/
export const MapObjectives: CollectionConfig = {
slug: "map-objectives",
admin: {
group: "World",
useAsTitle: "name",
defaultColumns: ["name", "map", "status", "redacted", "position"],
description:
"Strategic objectives shown on the world map and mission planning maps. Superuser-authored.",
},
access: {
read: async ({ req }) => {
if (!req.user) return false;
if (await hasPermission(req.payload, req.user, "system:admin-access")) return true;
return { redacted: { equals: false } };
},
create: requirePermission("system:admin-access"),
update: requirePermission("system:admin-access"),
delete: requirePermission("system:admin-access"),
},
hooks: {
afterChange: [
async ({ doc, previousDoc, operation, req }) => {
const current = doc as unknown as ObjectiveHookDoc;
const previous = previousDoc as unknown as ObjectiveHookDoc | undefined;
const mapId = mapIdOf(current) ?? (previous ? mapIdOf(previous) : null);
if (!mapId) return;
const name = current.name ?? previous?.name ?? `Objective #${current.id}`;
const actor = req.user?.id;
if (operation === "create") {
await emitGameEvent(req.payload, {
type: EventTypes.MapObjectiveCreate,
message: `Added objective ${name}`,
actor,
targetCollection: "map-objectives",
targetId: current.id,
data: { mapId, objectiveId: current.id },
});
return;
}
if (previous && previous.status !== current.status) {
await emitGameEvent(req.payload, {
type: EventTypes.MapObjectiveStatus,
message: `Objective ${name} is now ${objectiveStatusLabel(current.status).toLowerCase()}`,
actor,
targetCollection: "map-objectives",
targetId: current.id,
data: { mapId, objectiveId: current.id, status: current.status },
});
return;
}
if (previous && previous.redacted !== current.redacted) {
await emitGameEvent(req.payload, {
type: EventTypes.MapObjectiveReveal,
message: `${current.redacted ? "Hid" : "Revealed"} objective ${name}`,
actor,
targetCollection: "map-objectives",
targetId: current.id,
data: { mapId, objectiveId: current.id, redacted: current.redacted },
});
return;
}
await emitGameEvent(req.payload, {
type: EventTypes.MapObjectiveUpdate,
message: `Updated objective ${name}`,
actor,
targetCollection: "map-objectives",
targetId: current.id,
data: { mapId, objectiveId: current.id },
});
},
],
afterDelete: [
async ({ doc, req }) => {
const deleted = doc as unknown as ObjectiveHookDoc;
const mapId = mapIdOf(deleted);
await emitGameEvent(req.payload, {
type: EventTypes.MapObjectiveDelete,
message: `Removed objective ${deleted.name ?? `#${deleted.id}`}`,
actor: req.user?.id,
targetCollection: "map-objectives",
targetId: deleted.id,
data: mapId ? { mapId, objectiveId: deleted.id } : { objectiveId: deleted.id },
});
},
],
},
fields: [
{
name: "name",
type: "text",
required: true,
maxLength: 80,
admin: {
description: 'Map label, e.g. "OBJ ALPHA". Shown under the marker on every map.',
},
},
{
name: "description",
type: "textarea",
maxLength: 500,
admin: {
description: "Planning note shown in the marker popup.",
},
},
{
name: "map",
type: "relationship",
relationTo: "maps",
required: true,
index: true,
},
{
name: "markerType",
type: "text",
defaultValue: DEFAULT_OBJECTIVE_MARKER_TYPE,
admin: {
description: "CfgMarkers class name, e.g. mil_objective, o_objective, hd_objective.",
},
},
{
name: "status",
type: "select",
required: true,
defaultValue: "active",
options: MAP_OBJECTIVE_STATUSES.map((status) => ({
label: OBJECTIVE_STATUS_META[status].label,
value: status,
})),
admin: {
description:
"Drives the marker colour: red for active, green for secured, grey for failed.",
},
},
{
name: "redacted",
type: "checkbox",
label: "Redacted",
defaultValue: false,
admin: {
description:
"Hidden from everyone except superusers (and drawn dimmed for them) until revealed.",
},
},
{
name: "position",
type: "json",
required: true,
validate: validatePosition,
admin: {
description: "[x, y] in map meters (origin top-left, y south).",
},
},
{
name: "createdBy",
type: "relationship",
relationTo: "users",
admin: {
readOnly: true,
position: "sidebar",
},
},
],
};