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.
This commit is contained in:
parent
cb06ba79c0
commit
9e6cd55fe6
9 changed files with 46391 additions and 51 deletions
|
|
@ -74,7 +74,7 @@ Permission tiers and panel gates:
|
|||
|
||||
## Hooks with side effects
|
||||
|
||||
- `Factions` `afterRead`: masks undiscovered factions (name, description, standing, relationships, campaigns, cover image, playable/starting) for readers without `factions:view-undiscovered` or `system:admin-access`. Admin panel reads are exempt; see `src/lib/intelligence/factionDiscovery.ts`
|
||||
- `Factions` `afterRead`: masks undiscovered factions (name, description, standing, campaigns, cover image, playable/starting) for readers without `factions:view-undiscovered` or `system:admin-access`. Admin panel reads are exempt; see `src/lib/intelligence/factionDiscovery.ts`. Pairwise relationships are not on the faction record at all: they are computed from `faction-relation-entries`, and the ledger read module (`src/lib/intelligence/factionRelationLedger.ts`) filters the same way.
|
||||
- `Structures` `beforeChange`: emits `structure:resize`; blueprint "Staffing Defaults" group via `staffingFields.ts`
|
||||
- `GameStructures` `afterChange`: emits storage edit events; `beforeChange` copies blueprint staffing defaults on create
|
||||
- `Users` `afterChange`: maintains profile sync
|
||||
|
|
|
|||
|
|
@ -99,7 +99,7 @@ export const Factions: CollectionConfig = {
|
|||
index: true,
|
||||
admin: {
|
||||
description:
|
||||
"Uncheck while players should not know this faction yet. The entry still appears in lists as UNDISCOVERED; name, description, standing, relationships, campaign links, cover image and the playable/starting flags are hidden from anyone without the View Undiscovered Factions permission.",
|
||||
"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.",
|
||||
},
|
||||
},
|
||||
{
|
||||
|
|
@ -117,47 +117,6 @@ export const Factions: CollectionConfig = {
|
|||
min: -100,
|
||||
max: 100,
|
||||
},
|
||||
{
|
||||
name: "relationships",
|
||||
type: "array",
|
||||
labels: {
|
||||
singular: "Relationship",
|
||||
plural: "Relationships",
|
||||
},
|
||||
admin: {
|
||||
description:
|
||||
"Pairwise standing between this faction and another faction, from -100 (hostile) to 100 (allied). 0 is neutral. One entry per counterpart faction.",
|
||||
},
|
||||
fields: [
|
||||
{
|
||||
name: "targetFaction",
|
||||
type: "relationship",
|
||||
relationTo: "factions",
|
||||
required: true,
|
||||
admin: {
|
||||
description: "The other faction in this pairwise relationship.",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "value",
|
||||
type: "number",
|
||||
required: true,
|
||||
defaultValue: 0,
|
||||
min: -100,
|
||||
max: 100,
|
||||
admin: {
|
||||
description: "Standing from -100 (hostile) to 100 (allied). 0 is neutral.",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "notes",
|
||||
type: "textarea",
|
||||
admin: {
|
||||
description: "Optional context for why this relationship stands where it does.",
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
name: "playableFaction",
|
||||
type: "checkbox",
|
||||
|
|
|
|||
|
|
@ -76,7 +76,7 @@ base-tick script ── lib/base/tick.ts (processBaseTick: salaries, maintenance
|
|||
`redactText()` replaces every non-space glyph with a block character, for spoilered copy (mission objectives, voiceover scripts).
|
||||
|
||||
### `intelligence/factionDiscovery.ts`
|
||||
Faction discovery gate. `canViewUndiscoveredFactions(payload, user)` checks `factions:view-undiscovered` **or** `system:admin-access`; `maskUndiscoveredFaction(doc)` blanks name, description, standing, relationships, campaign links, cover image and the playable/starting flags. Consumed by the Factions `afterRead` hook, so masking applies to every reader (pages, REST, populated relationships) without per-page work. Admin panel reads are exempt so a masked edit form can never write a placeholder name back over a record.
|
||||
Faction discovery gate. `canViewUndiscoveredFactions(payload, user)` checks `factions:view-undiscovered` **or** `system:admin-access`; `maskUndiscoveredFaction(doc)` blanks name, description, standing, campaign links, cover image and the playable/starting flags. Consumed by the Factions `afterRead` hook, so masking applies to every reader (pages, REST, populated relationships) without per-page work. Admin panel reads are exempt so a masked edit form can never write a placeholder name back over a record. Pairwise relations moved off the faction record: `factionRelations.ts` (pure math) and `factionRelationLedger.ts` (access-respecting reads) derive them from the `faction-relation-entries` ledger, and the ledger module filters entries naming an undiscovered faction for the same readers.
|
||||
|
||||
### `storageRules.ts`
|
||||
Enforcement order: **prohibited → whitelist → per-item cap**. Functions: `checkStorageDeposit`, `isResourceProhibited`, `isResourceWhitelisted`, `getResourceStorageCap`, `storageViolationMessage`. Used in structure actions, shipment creation, and arrival processing.
|
||||
|
|
|
|||
|
|
@ -40,7 +40,6 @@ export function maskUndiscoveredFaction<T extends object>(doc: T): T {
|
|||
name: UNDISCOVERED_FACTION_NAME,
|
||||
description: null,
|
||||
reputation: null,
|
||||
relationships: [],
|
||||
playableFaction: false,
|
||||
startingFaction: false,
|
||||
associatedCampaigns: [],
|
||||
|
|
|
|||
46295
src/migrations/20261118_184851_drop_factions_relationships_array.json
Normal file
46295
src/migrations/20261118_184851_drop_factions_relationships_array.json
Normal file
File diff suppressed because it is too large
Load diff
|
|
@ -0,0 +1,86 @@
|
|||
import { MigrateUpArgs, MigrateDownArgs, sql } from '@payloadcms/db-postgres'
|
||||
|
||||
/**
|
||||
* Retire the hand-typed `factions.relationships[]` array.
|
||||
*
|
||||
* Pairwise faction attitudes are now derived from `faction_relation_entries`,
|
||||
* so the old array is the only place those numbers could disagree with the
|
||||
* ledger. Before the table goes away, every non-zero legacy value becomes an
|
||||
* "opening balance" entry: it is dated at the owning faction's last update (not
|
||||
* at migration time, so it does not look like a fresh event), carries the
|
||||
* legacy note, and is a custom-delta entry because the old values were typed
|
||||
* by hand rather than picked from a severity tier.
|
||||
*
|
||||
* Zero rows are skipped: the ledger treats "no entries" as the neutral default,
|
||||
* so a stored 0 is the same thing with extra noise.
|
||||
*/
|
||||
export async function up({ db, payload, req }: MigrateUpArgs): Promise<void> {
|
||||
await db.execute(sql`
|
||||
DO $$
|
||||
BEGIN
|
||||
IF to_regclass('public.factions_relationships') IS NULL THEN
|
||||
RETURN;
|
||||
END IF;
|
||||
|
||||
INSERT INTO "faction_relation_entries" (
|
||||
"subject_id",
|
||||
"target_id",
|
||||
"mutual",
|
||||
"polarity",
|
||||
"severity",
|
||||
"delta",
|
||||
"category",
|
||||
"summary",
|
||||
"notes",
|
||||
"occurred_at",
|
||||
"decays",
|
||||
"hidden_from_players",
|
||||
"voided"
|
||||
)
|
||||
SELECT
|
||||
r."_parent_id",
|
||||
r."target_faction_id",
|
||||
false,
|
||||
CASE WHEN r."value" > 0 THEN 'beneficial'::"public"."enum_faction_relation_entries_polarity"
|
||||
ELSE 'detrimental'::"public"."enum_faction_relation_entries_polarity" END,
|
||||
'custom'::"public"."enum_faction_relation_entries_severity",
|
||||
r."value",
|
||||
'other'::"public"."enum_faction_relation_entries_category",
|
||||
'Opening balance carried over from the legacy relationship record.',
|
||||
r."notes",
|
||||
COALESCE(f."updated_at", now()),
|
||||
false,
|
||||
false,
|
||||
false
|
||||
FROM "factions_relationships" r
|
||||
JOIN "factions" f ON f."id" = r."_parent_id"
|
||||
WHERE r."value" <> 0
|
||||
AND r."_parent_id" <> r."target_faction_id";
|
||||
END $$;`)
|
||||
|
||||
await db.execute(sql`
|
||||
DROP TABLE "factions_relationships" CASCADE;`)
|
||||
}
|
||||
|
||||
/**
|
||||
* The old array comes back empty on a down migration: the ledger is the source
|
||||
* of truth now, so there is nothing to write back into it and inventing values
|
||||
* from ledger sums would be lossy in the other direction.
|
||||
*/
|
||||
export async function down({ db, payload, req }: MigrateDownArgs): Promise<void> {
|
||||
await db.execute(sql`
|
||||
CREATE TABLE "factions_relationships" (
|
||||
"_order" integer NOT NULL,
|
||||
"_parent_id" integer NOT NULL,
|
||||
"id" varchar PRIMARY KEY NOT NULL,
|
||||
"target_faction_id" integer NOT NULL,
|
||||
"value" numeric DEFAULT 0 NOT NULL,
|
||||
"notes" varchar
|
||||
);
|
||||
|
||||
ALTER TABLE "factions_relationships" ADD CONSTRAINT "factions_relationships_target_faction_id_factions_id_fk" FOREIGN KEY ("target_faction_id") REFERENCES "public"."factions"("id") ON DELETE set null ON UPDATE no action;
|
||||
ALTER TABLE "factions_relationships" ADD CONSTRAINT "factions_relationships_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."factions"("id") ON DELETE cascade ON UPDATE no action;
|
||||
CREATE INDEX "factions_relationships_order_idx" ON "factions_relationships" USING btree ("_order");
|
||||
CREATE INDEX "factions_relationships_parent_id_idx" ON "factions_relationships" USING btree ("_parent_id");
|
||||
CREATE INDEX "factions_relationships_target_faction_idx" ON "factions_relationships" USING btree ("target_faction_id");`)
|
||||
}
|
||||
|
|
@ -160,6 +160,7 @@ import * as migration_20261112_171955_add_npc_dialogue_generation_provenance fro
|
|||
import * as migration_20261117_212337_add_faction_discovered_field from './20261117_212337_add_faction_discovered_field';
|
||||
import * as migration_20261118_143000_add_wiki_pages_draft_and_created_by from './20261118_143000_add_wiki_pages_draft_and_created_by';
|
||||
import * as migration_20261118_184813_add_faction_relation_entries from './20261118_184813_add_faction_relation_entries';
|
||||
import * as migration_20261118_184851_drop_factions_relationships_array from './20261118_184851_drop_factions_relationships_array';
|
||||
|
||||
export const migrations = [
|
||||
{
|
||||
|
|
@ -972,4 +973,9 @@ export const migrations = [
|
|||
down: migration_20261118_184813_add_faction_relation_entries.down,
|
||||
name: '20261118_184813_add_faction_relation_entries',
|
||||
},
|
||||
{
|
||||
up: migration_20261118_184851_drop_factions_relationships_array.up,
|
||||
down: migration_20261118_184851_drop_factions_relationships_array.down,
|
||||
name: '20261118_184851_drop_factions_relationships_array'
|
||||
},
|
||||
];
|
||||
|
|
|
|||
|
|
@ -127,7 +127,6 @@ describe("Faction discovery masking", () => {
|
|||
description: DESCRIPTION,
|
||||
playableFaction: true,
|
||||
startingFaction: true,
|
||||
relationships: [{ targetFaction: overtId, value: -80, notes: "Black ops history" }],
|
||||
associatedCampaigns: [],
|
||||
},
|
||||
overrideAccess: true,
|
||||
|
|
@ -173,7 +172,6 @@ describe("Faction discovery masking", () => {
|
|||
expect(masked.name).toBe(UNDISCOVERED_FACTION_NAME);
|
||||
expect(masked.description).toBeNull();
|
||||
expect(masked.reputation).toBeNull();
|
||||
expect(masked.relationships ?? []).toHaveLength(0);
|
||||
expect(masked.playableFaction).toBe(false);
|
||||
expect(masked.startingFaction).toBe(false);
|
||||
expect(masked.coverImage ?? null).toBeNull();
|
||||
|
|
@ -194,7 +192,6 @@ describe("Faction discovery masking", () => {
|
|||
expect(faction.name).toBe(`${RUN}-Covert Directorate`);
|
||||
expect(faction.reputation).toBe(64);
|
||||
expect(faction.description).not.toBeNull();
|
||||
expect(faction.relationships ?? []).toHaveLength(1);
|
||||
expect(faction.playableFaction).toBe(true);
|
||||
});
|
||||
|
||||
|
|
@ -249,7 +246,6 @@ describe("Faction discovery masking", () => {
|
|||
discovered: false,
|
||||
description: DESCRIPTION,
|
||||
reputation: 90,
|
||||
relationships: [{ id: "r1", targetFaction: 7, value: 10, notes: "n" }],
|
||||
associatedCampaigns: [1, 2],
|
||||
playableFaction: true,
|
||||
startingFaction: true,
|
||||
|
|
@ -263,7 +259,6 @@ describe("Faction discovery masking", () => {
|
|||
discovered: false,
|
||||
description: null,
|
||||
reputation: null,
|
||||
relationships: [],
|
||||
associatedCampaigns: [],
|
||||
playableFaction: false,
|
||||
startingFaction: false,
|
||||
|
|
|
|||
|
|
@ -114,7 +114,7 @@ describe("world map screen eligibility", () => {
|
|||
it("persists the optional intel fields", async () => {
|
||||
const faction = (await payload.create({
|
||||
collection: "factions",
|
||||
data: { name: `${RUN} Faction`, type: "minorFaction", reputation: 0, relationships: [] },
|
||||
data: { name: `${RUN} Faction`, type: "minorFaction", reputation: 0 },
|
||||
overrideAccess: true,
|
||||
depth: 0,
|
||||
})) as unknown as { id: number };
|
||||
|
|
|
|||
Loading…
Reference in a new issue