1
0
Fork 0
polaris-task-force/src/collections/intelligence/Factions.ts
Z8MB1E 9e6cd55fe6 refactor(intelligence): drop the factions.relationships array in favour of the ledger
Nothing stores a pairwise score on a faction any more, so remove the array
field, its admin UI and the masking line that blanked it for undiscovered
factions. The disposal-mask doc comment now points at the ledger, which
applies the same rule by filtering entries that name an undiscovered faction.

The second migration converts every non-zero legacy value into an
opening-balance entry (custom delta, dated at the owning faction's last update
so it does not read as a fresh event, legacy note preserved) and then drops
the table. Zero rows are skipped because a stored 0 and no entries mean the
same thing.

Two integration specs that created or asserted `relationships` now use the
ledger instead.
2026-11-18 14:13:29 -05:00

152 lines
4.4 KiB
TypeScript

import { CollectionConfig } from "payload";
import { isAdminPanelRequest, requirePermission } from "@/utils/access-control/hasPermission";
import {
canViewUndiscoveredFactions,
maskUndiscoveredFaction,
} from "@/lib/intelligence/factionDiscovery";
export const Factions: CollectionConfig = {
slug: "factions",
admin: {
group: "Intelligence",
useAsTitle: "name",
},
access: {
create: requirePermission("factions:create"),
update: requirePermission("factions:update"),
delete: requirePermission("factions:delete"),
read: ({ req }) => !!req.user,
},
hooks: {
beforeValidate: [
async ({ data, operation, originalDoc, req }) => {
if (data?.type !== "playerFaction") return data;
const payload = req.payload;
const existing = await payload.find({
collection: "factions",
where: { type: { equals: "playerFaction" } },
limit: 1,
depth: 0,
overrideAccess: true,
});
const conflict = existing.docs.find((doc) => doc.id !== originalDoc?.id);
if (conflict) {
throw new Error(
`Only one faction can be the Player Faction at a time. "${conflict.name}" is already the Player Faction.`,
);
}
return data;
},
],
afterRead: [
// Undiscovered factions stay readable; only their identity is masked.
// Admin panel reads are exempt: a masked edit form would write the
// placeholder name back over the record on save.
async ({ doc, req }) => {
const record = doc as unknown as { discovered?: boolean | null } | null;
if (!record || record.discovered !== false) return doc;
if (isAdminPanelRequest(req)) return doc;
if (await canViewUndiscoveredFactions(req.payload, req.user)) return doc;
return maskUndiscoveredFaction(doc);
},
],
},
fields: [
{
name: "name",
type: "text",
required: true,
unique: true,
},
{
name: "type",
type: "select",
options: [
{
label: "Player Faction",
value: "playerFaction",
},
{
label: "Major Faction",
value: "majorFaction",
},
{
label: "Minor Faction",
value: "minorFaction",
},
{
label: "Corporation",
value: "corporation",
},
{
label: "Special",
value: "special",
},
],
required: true,
defaultValue: "minorFaction",
admin: {
description:
"Player Faction = the faction our unit is currently playing as. Only one faction can carry this type at a time.",
},
},
{
name: "discovered",
type: "checkbox",
defaultValue: true,
index: true,
admin: {
description:
"Uncheck while players should not know this faction yet. The entry still appears in lists as UNDISCOVERED; name, description, standing, campaign links, cover image and the playable/starting flags are hidden from anyone without the View Undiscovered Factions permission.",
},
},
{
name: "description",
type: "richText",
},
{
name: "reputation",
type: "number",
admin: {
description:
"This faction's standing in the world, from -100 (reviled) to 100 (revered). 0 is neutral. Adjusted manually by admins/GMs as campaign events unfold.",
},
defaultValue: 0,
min: -100,
max: 100,
},
{
name: "playableFaction",
type: "checkbox",
admin: {
description:
"Whether or not this faction is playable. Some factions may be specifically intended to be unplayable based on narrative requirements.",
},
},
{
name: "startingFaction",
type: "checkbox",
admin: {
description:
"Whether or not this faction is/was available as a starting faction at the beginning of the campaign.",
},
},
{
name: "associatedCampaigns",
type: "relationship",
relationTo: "campaigns",
hasMany: true,
},
{
name: "coverImage",
type: "upload",
relationTo: "media",
admin: {
description:
"Background image for the faction detail page hero. Renders translucent with a top-to-bottom fade to black. Recommended aspect ratio: 16:9 or wider.",
},
},
],
};