Event node flag effects previously always wrote the active participant's
flag. A flag's identity is (owner, key), so the same key can hold
independent values for different owners, but there was no way to target
them from the editor.
- Add flagTarget ("self" | "global" | "user") + flagTargetUserId to
NpcDialogueEffect. "self" (default) keeps current behavior.
- Add resolveFlagOwner (flags.ts) to map a scope onto the player-flags
user column; "user" with no id falls back to the participant so a
misconfigured effect never nudges the global flag.
- Move applyFlagEffect into flags.ts, parameterized by resolved owner;
the runtime action resolves the owner and delegates.
- Add a Target control (+ user picker) to the event node editor and
validateGraph enforcement for the "user" scope.
- Integration tests cover owner resolution and self/global/user writes.
121 lines
4.3 KiB
TypeScript
121 lines
4.3 KiB
TypeScript
import { getPayload, Payload } from "payload";
|
|
import config from "@/payload.config";
|
|
|
|
import { afterAll, beforeAll, describe, expect, it } from "vitest";
|
|
|
|
import { applyFlagEffect, resolveFlagOwner } from "@/lib/npc-dialogue/flags";
|
|
|
|
let payload: Payload;
|
|
|
|
const RUN = `dlg_target_${Date.now().toString(36)}`;
|
|
const TIMEOUT = 30_000;
|
|
|
|
const userIds: number[] = [];
|
|
|
|
/** Minimal playable user so the player-flags user FK resolves. */
|
|
async function createUser(username: string): Promise<{ id: number }> {
|
|
const user = (await payload.create({
|
|
collection: "users",
|
|
data: {
|
|
username,
|
|
discordUsername: username,
|
|
displayName: `TARGET ${username}`,
|
|
steamId: `7656119${Math.floor(Math.random() * 1e9)}`,
|
|
password: "Test123",
|
|
roles: ["user"],
|
|
},
|
|
overrideAccess: true,
|
|
depth: 0,
|
|
})) as unknown as { id: number };
|
|
userIds.push(user.id);
|
|
return user;
|
|
}
|
|
|
|
describe("flag owner targeting (event node effects)", () => {
|
|
beforeAll(async () => {
|
|
payload = await getPayload({ config });
|
|
}, TIMEOUT);
|
|
|
|
afterAll(async () => {
|
|
const flags = await payload.find({
|
|
collection: "player-flags",
|
|
where: { key: { like: `${RUN}_%` } },
|
|
limit: 200,
|
|
overrideAccess: true,
|
|
});
|
|
for (const doc of flags.docs) {
|
|
await payload.delete({ collection: "player-flags", id: doc.id, overrideAccess: true });
|
|
}
|
|
for (const id of userIds) {
|
|
await payload.delete({ collection: "users", id, overrideAccess: true }).catch(() => {});
|
|
}
|
|
});
|
|
|
|
it("resolves the owner column from the effect scope", () => {
|
|
const participant = 42;
|
|
expect(resolveFlagOwner(undefined, undefined, participant)).toBe(participant);
|
|
expect(resolveFlagOwner("self", undefined, participant)).toBe(participant);
|
|
expect(resolveFlagOwner("global", undefined, participant)).toBeNull();
|
|
expect(resolveFlagOwner("user", 7, participant)).toBe(7);
|
|
// A "user" target with no id falls back to the participant so a
|
|
// misconfigured effect never nudges the global flag by accident.
|
|
expect(resolveFlagOwner("user", undefined, participant)).toBe(participant);
|
|
expect(resolveFlagOwner("user", null, participant)).toBe(participant);
|
|
});
|
|
|
|
it("writes the self flag for the active participant", async () => {
|
|
const participant = await createUser(`${RUN}_self_player`);
|
|
await applyFlagEffect(payload, participant.id, `${RUN}_self`, "set_true", 0);
|
|
const res = await payload.find({
|
|
collection: "player-flags",
|
|
where: { user: { equals: participant.id }, key: { equals: `${RUN}_self` } },
|
|
limit: 1,
|
|
depth: 0,
|
|
overrideAccess: true,
|
|
});
|
|
expect(res.docs).toHaveLength(1);
|
|
expect(res.docs[0].flagType).toBe("boolean");
|
|
expect(res.docs[0].boolValue).toBe(true);
|
|
});
|
|
|
|
it("writes the global flag (owner null) under the shared key", async () => {
|
|
await applyFlagEffect(payload, null, `${RUN}_global`, "set_number", 5);
|
|
const res = await payload.find({
|
|
collection: "player-flags",
|
|
where: { user: { equals: null }, key: { equals: `${RUN}_global` } },
|
|
limit: 1,
|
|
depth: 0,
|
|
overrideAccess: true,
|
|
});
|
|
expect(res.docs).toHaveLength(1);
|
|
expect(res.docs[0].flagType).toBe("number");
|
|
expect(res.docs[0].numberValue).toBe(5);
|
|
});
|
|
|
|
it("writes a targeted user's flag independently of the participant and the key", async () => {
|
|
const target = await createUser(`${RUN}_target_player`);
|
|
const participant = await createUser(`${RUN}_participant`);
|
|
// Same key, different owner: the participant gets nothing.
|
|
await applyFlagEffect(payload, target.id, `${RUN}_shared`, "set_text", 0, "{green}OK{/}");
|
|
|
|
const forTarget = await payload.find({
|
|
collection: "player-flags",
|
|
where: { user: { equals: target.id }, key: { equals: `${RUN}_shared` } },
|
|
limit: 1,
|
|
depth: 0,
|
|
overrideAccess: true,
|
|
});
|
|
expect(forTarget.docs).toHaveLength(1);
|
|
expect(forTarget.docs[0].flagType).toBe("text");
|
|
expect(forTarget.docs[0].textValue).toBe("{green}OK{/}");
|
|
|
|
const forParticipant = await payload.find({
|
|
collection: "player-flags",
|
|
where: { user: { equals: participant.id }, key: { equals: `${RUN}_shared` } },
|
|
limit: 1,
|
|
depth: 0,
|
|
overrideAccess: true,
|
|
});
|
|
expect(forParticipant.docs).toHaveLength(0);
|
|
});
|
|
});
|