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.
282 lines
8.4 KiB
TypeScript
282 lines
8.4 KiB
TypeScript
/**
|
|
* Server-side flag reads for NPC conversations. Shared by the conversation
|
|
* runtime actions and the contact page's conversation picker so condition
|
|
* semantics never drift apart.
|
|
*/
|
|
|
|
import type { Payload } from "payload";
|
|
import type { DialogueCondition, DialogueFlagOp } from "./dialogue";
|
|
|
|
/**
|
|
* Resolve who a flag effect targets. `global` -> null (the shared flag);
|
|
* `user` -> the chosen player id, falling back to `selfUserId` when none was
|
|
* set, so a misconfigured effect never nudges the global flag by accident;
|
|
* `self`/default -> the active conversation participant. The return value is
|
|
* exactly the `player-flags.user` column to query and write.
|
|
*/
|
|
export function resolveFlagOwner(
|
|
target: "self" | "global" | "user" | undefined,
|
|
targetUserId: number | null | undefined,
|
|
selfUserId: number,
|
|
): number | null {
|
|
switch (target) {
|
|
case "global":
|
|
return null;
|
|
case "user":
|
|
return targetUserId ?? selfUserId;
|
|
case "self":
|
|
default:
|
|
return selfUserId;
|
|
}
|
|
}
|
|
|
|
/** Effective value of a flag for reading + interpolation. */
|
|
export interface FlagValues {
|
|
bool: boolean;
|
|
num: number;
|
|
text: string;
|
|
/** True when a non-expired flag document exists (even if not yet active). */
|
|
exists: boolean;
|
|
/**
|
|
* When the flag is time-gated and not yet active, its ISO activation time
|
|
* (so `{{countdown:key}}` tokens can tick toward it). Undefined otherwise.
|
|
*/
|
|
activatesAt?: string;
|
|
}
|
|
|
|
/** True when the doc's activation window covers now (missing bounds = open). */
|
|
function activeNow(
|
|
doc: { activatesAt?: string | null; expiresAt?: string | null },
|
|
now = Date.now(),
|
|
): boolean {
|
|
if (doc.activatesAt) {
|
|
const t = Date.parse(doc.activatesAt);
|
|
if (!Number.isNaN(t) && t > now) return false;
|
|
}
|
|
if (doc.expiresAt) {
|
|
const t = Date.parse(doc.expiresAt);
|
|
if (!Number.isNaN(t) && t <= now) return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
/** True while the doc has an activation time strictly in the future. */
|
|
function pendingActivation(
|
|
doc: { activatesAt?: string | null },
|
|
now = Date.now(),
|
|
): boolean {
|
|
if (!doc.activatesAt) return false;
|
|
const t = Date.parse(doc.activatesAt);
|
|
return !Number.isNaN(t) && t > now;
|
|
}
|
|
|
|
/** The inactive read value, carrying the countdown target when pending. */
|
|
function inactiveValues(doc: { activatesAt?: string | null }, now = Date.now()): FlagValues {
|
|
return pendingActivation(doc, now)
|
|
? { bool: false, num: 0, text: "", exists: true, activatesAt: doc.activatesAt ?? undefined }
|
|
: { bool: false, num: 0, text: "", exists: false };
|
|
}
|
|
|
|
/**
|
|
* Effective value of a flag: player-specific entry takes priority; if absent,
|
|
* fall back to the global entry (user is null). Missing flags read false / 0 /
|
|
* empty. `text` is the string the flag contributes to interpolated dialogue
|
|
* (booleans render "true"/"false", numbers their digits, text flags verbatim).
|
|
* Time windows apply: a not-yet-active flag reads as false (and reports its
|
|
* `activatesAt` for countdowns); an expired flag reads as missing and falls
|
|
* through to the global fallback.
|
|
*/
|
|
export async function readFlag(
|
|
payload: Payload,
|
|
userId: number,
|
|
key: string,
|
|
): Promise<FlagValues> {
|
|
// Player-specific flag (priority).
|
|
const playerRes = await payload.find({
|
|
collection: "player-flags",
|
|
where: {
|
|
user: { equals: userId },
|
|
key: { equals: key },
|
|
},
|
|
limit: 1,
|
|
depth: 0,
|
|
overrideAccess: true,
|
|
});
|
|
const playerDoc = playerRes.docs[0];
|
|
if (playerDoc) {
|
|
if (!activeNow(playerDoc)) return inactiveValues(playerDoc);
|
|
return flagValues(playerDoc);
|
|
}
|
|
|
|
// Global fallback (user is null).
|
|
const globalRes = await payload.find({
|
|
collection: "player-flags",
|
|
where: {
|
|
user: { equals: null },
|
|
key: { equals: key },
|
|
},
|
|
limit: 1,
|
|
depth: 0,
|
|
overrideAccess: true,
|
|
});
|
|
const globalDoc = globalRes.docs[0];
|
|
if (!globalDoc) return { bool: false, num: 0, text: "", exists: false };
|
|
if (!activeNow(globalDoc)) return inactiveValues(globalDoc);
|
|
return flagValues(globalDoc);
|
|
}
|
|
|
|
/** Reads a stored flag doc into its condition + interpolation values. */
|
|
export function flagValues(doc: {
|
|
flagType: "boolean" | "number" | "text";
|
|
boolValue?: boolean | null;
|
|
numberValue?: number | null;
|
|
textValue?: string | null;
|
|
}): FlagValues {
|
|
if (doc.flagType === "number") {
|
|
const num = doc.numberValue ?? 0;
|
|
return { bool: num !== 0, num, text: String(num), exists: true };
|
|
}
|
|
if (doc.flagType === "text") {
|
|
const text = doc.textValue ?? "";
|
|
return { bool: text.trim().length > 0, num: 0, text, exists: true };
|
|
}
|
|
const bool = doc.boolValue === true;
|
|
return { bool, num: bool ? 1 : 0, text: bool ? "true" : "false", exists: true };
|
|
}
|
|
|
|
/** Evaluates one authored condition against the user's live flags. */
|
|
export async function evaluateCondition(
|
|
payload: Payload,
|
|
userId: number,
|
|
condition: DialogueCondition,
|
|
): Promise<boolean> {
|
|
const flag = await readFlag(payload, userId, condition.key);
|
|
const amount = condition.amount ?? 0;
|
|
switch (condition.op) {
|
|
case "is_true":
|
|
return flag.bool;
|
|
case "is_false":
|
|
return !flag.bool;
|
|
case "gte":
|
|
return flag.num >= amount;
|
|
case "lte":
|
|
return flag.num <= amount;
|
|
case "gt":
|
|
return flag.num > amount;
|
|
case "lt":
|
|
return flag.num < amount;
|
|
case "eq":
|
|
return flag.num === amount;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Create-or-update a player flag for `owner` according to the effect's write
|
|
* mode. `owner` is a user id (the active participant or a targeted player) or
|
|
* null for the shared global flag. A positive `delayMinutes` stamps a future
|
|
* `activatesAt`, so the flag reads as false until then (conditions fail,
|
|
* countdowns tick toward it). This is the single write path shared by the
|
|
* dialogue runtime and its tests, so flag semantics never drift.
|
|
*/
|
|
export async function applyFlagEffect(
|
|
payload: Payload,
|
|
owner: number | null,
|
|
key: string,
|
|
op: DialogueFlagOp,
|
|
amount: number,
|
|
effectText?: string,
|
|
delayMinutes?: number,
|
|
): Promise<void> {
|
|
const activatesAt =
|
|
delayMinutes && delayMinutes > 0
|
|
? new Date(Date.now() + delayMinutes * 60_000).toISOString()
|
|
: null;
|
|
const existing = await payload.find({
|
|
collection: "player-flags",
|
|
where: { user: { equals: owner }, key: { equals: key } },
|
|
limit: 1,
|
|
depth: 0,
|
|
overrideAccess: true,
|
|
});
|
|
const doc = existing.docs[0];
|
|
|
|
if (op === "set_true" || op === "set_false") {
|
|
const boolValue = op === "set_true";
|
|
if (doc) {
|
|
await payload.update({
|
|
collection: "player-flags",
|
|
id: doc.id,
|
|
data: { flagType: "boolean", boolValue, activatesAt },
|
|
overrideAccess: true,
|
|
});
|
|
} else {
|
|
await payload.create({
|
|
collection: "player-flags",
|
|
data: { user: owner, key, flagType: "boolean", boolValue, activatesAt },
|
|
overrideAccess: true,
|
|
});
|
|
}
|
|
return;
|
|
}
|
|
|
|
if (op === "set_number") {
|
|
if (doc) {
|
|
await payload.update({
|
|
collection: "player-flags",
|
|
id: doc.id,
|
|
data: { flagType: "number", numberValue: amount, activatesAt },
|
|
overrideAccess: true,
|
|
});
|
|
} else {
|
|
await payload.create({
|
|
collection: "player-flags",
|
|
data: { user: owner, key, flagType: "number", numberValue: amount, activatesAt },
|
|
overrideAccess: true,
|
|
});
|
|
}
|
|
return;
|
|
}
|
|
|
|
if (op === "set_text") {
|
|
const textValue = effectText ?? "";
|
|
if (doc) {
|
|
await payload.update({
|
|
collection: "player-flags",
|
|
id: doc.id,
|
|
data: { flagType: "text", textValue, activatesAt },
|
|
overrideAccess: true,
|
|
});
|
|
} else {
|
|
await payload.create({
|
|
collection: "player-flags",
|
|
data: { user: owner, key, flagType: "text", textValue, activatesAt },
|
|
overrideAccess: true,
|
|
});
|
|
}
|
|
return;
|
|
}
|
|
|
|
// add: numeric flags accumulate; boolean flags convert (true = 1).
|
|
const current = doc
|
|
? doc.flagType === "number"
|
|
? doc.numberValue ?? 0
|
|
: doc.boolValue
|
|
? 1
|
|
: 0
|
|
: 0;
|
|
const numberValue = current + amount;
|
|
if (doc) {
|
|
await payload.update({
|
|
collection: "player-flags",
|
|
id: doc.id,
|
|
data: { flagType: "number", numberValue, activatesAt },
|
|
overrideAccess: true,
|
|
});
|
|
} else {
|
|
await payload.create({
|
|
collection: "player-flags",
|
|
data: { user: owner, key, flagType: "number", numberValue, activatesAt },
|
|
overrideAccess: true,
|
|
});
|
|
}
|
|
}
|