Finish the NPC dialogue flowchart work carried over from the base commit:
- Coloured NPC dialogue text via friendly names ({red}) or hex ({#ff00ff});
invalid names/hex render as literal text. Shared client-safe renderer
(DialogueText.tsx) used by both the admin canvas preview and the runtime.
- Player-flags feature: condition nodes branch on flags (evaluated
server-side), flag effects set/clear/adjust flags, PlayerFlags collection
+ migration, and event nodes can set flags.
- One-time ("once") choices: a choice flagged "Hide after picked" is hidden
for the rest of the current conversation only (per-session, resets on
begin()); GMs can persist across conversations with a condition flag.
- Admin editor: colour hint, once checkbox on choices, amber "once" badge.
Migration repair: the player_flags migration (20261005_185014) was sorting
before 20261006_000000, whose stale snapshot omitted player_flags, so any
future `migrate:create` re-emitted player_flags DDL. Restored 20261006_000000's
snapshot to the full current schema so it is the correct diff base.
Verified: `bun run payload migrate` clean, `migrate:create` reports no
schema changes, `tsc --noEmit` clean.
205 lines
6.6 KiB
TypeScript
205 lines
6.6 KiB
TypeScript
/**
|
|
* Canonical model + pure helpers for NPC conversation flowcharts.
|
|
*
|
|
* The graph is authoritative in node data: every node lists where the
|
|
* conversation can go next (`next` for a single continuation, `choices` for
|
|
* branching). Edges are a derived projection of that data, so the admin
|
|
* canvas and the runtime walker always agree.
|
|
*/
|
|
|
|
export type NpcDialogueNodeType =
|
|
| "dialogue"
|
|
| "choice"
|
|
| "event"
|
|
| "end"
|
|
| "delay"
|
|
| "condition";
|
|
|
|
/** A directed hop to another node. `label` is the choice button text. */
|
|
export interface DialogueNext {
|
|
/** Button text shown to the player (required on choice hops). */
|
|
label?: string;
|
|
/** Target node id. */
|
|
target: string;
|
|
/**
|
|
* Choice hops only: once the player picks this choice it is hidden from
|
|
* them in every later conversation turn (tracked per player as a reserved
|
|
* `dlg-once:` player flag).
|
|
*/
|
|
once?: boolean;
|
|
}
|
|
|
|
/**
|
|
* Branch condition evaluated server-side against live web-app state when the
|
|
* conversation arrives at a `condition` node. Boolean flags compare against
|
|
* true/false; numeric flags compare against `amount` (a missing flag counts
|
|
* as false / zero).
|
|
*/
|
|
export interface DialogueCondition {
|
|
/** Currently only generic player flags; more kinds can join later. */
|
|
kind: "flag";
|
|
/** `player-flags` key checked for the reading user. */
|
|
key: string;
|
|
/** Comparison applied to the flag's effective value. */
|
|
op: "is_true" | "is_false" | "gte" | "lte" | "gt" | "lt" | "eq";
|
|
/** Right-hand side for the numeric comparisons. */
|
|
amount?: number;
|
|
}
|
|
|
|
/** How a flag effect writes the flag. */
|
|
export type DialogueFlagOp = "set_true" | "set_false" | "set_number" | "add";
|
|
|
|
/** Configured effect for an `event` node. */
|
|
export interface NpcDialogueEffect {
|
|
type: "notify" | "emit" | "reward" | "flag";
|
|
/** In-app notification title (notify). */
|
|
title?: string;
|
|
/** Notification / event-log body (notify, emit). */
|
|
message?: string;
|
|
/** Flag key to write (flag). */
|
|
flagKey?: string;
|
|
/** Write mode (flag). */
|
|
flagOp?: DialogueFlagOp;
|
|
/** Value for set_number / addend for add (flag). */
|
|
flagAmount?: number;
|
|
}
|
|
|
|
export type DialogueNodeData = {
|
|
/** Speaker label shown above the line. Defaults to the NPC's name at runtime. */
|
|
speaker?: string;
|
|
/** The spoken line (dialogue) or narrative description (event nodes). */
|
|
body?: string;
|
|
/** Branching options (choice nodes). */
|
|
choices?: DialogueNext[];
|
|
/** Optional lead-in text before the choices. */
|
|
leadIn?: string;
|
|
/** Triggered when the conversation arrives at this node. */
|
|
effect?: NpcDialogueEffect;
|
|
/** Closing text shown when the conversation ends. */
|
|
closing?: string;
|
|
/** Marks this node as the conversation start. */
|
|
isStart?: boolean;
|
|
/** Single continuation (dialogue / event / end nodes). */
|
|
next?: DialogueNext;
|
|
/** Pause in seconds before the conversation continues (delay nodes). */
|
|
delaySeconds?: number;
|
|
/** Branch condition evaluated when the conversation arrives (condition nodes). */
|
|
condition?: DialogueCondition;
|
|
/** Continuation when the condition passes (condition nodes). */
|
|
trueTarget?: string;
|
|
/** Continuation when the condition fails (condition nodes). */
|
|
falseTarget?: string;
|
|
} & Record<string, unknown>;
|
|
|
|
export interface DialogueNode {
|
|
id: string;
|
|
type: NpcDialogueNodeType;
|
|
position: { x: number; y: number };
|
|
data: DialogueNodeData;
|
|
}
|
|
|
|
export interface DialogueEdge {
|
|
id: string;
|
|
source: string;
|
|
target: string;
|
|
label?: string;
|
|
}
|
|
|
|
export interface FlowData {
|
|
nodes: DialogueNode[];
|
|
edges: DialogueEdge[];
|
|
}
|
|
|
|
/** All effect types the runtime can actually trigger. */
|
|
export const EFFECT_TYPES: NpcDialogueEffect["type"][] = [
|
|
"notify",
|
|
"emit",
|
|
"reward",
|
|
"flag",
|
|
];
|
|
|
|
/**
|
|
* Every outgoing hop of a node, in canvas order: choice buttons, the single
|
|
* continuation, or the condition's true/false branches. The edge projection,
|
|
* validation, and the runtime walker all share this so they never disagree.
|
|
*/
|
|
export function outgoingHops(node: DialogueNode): DialogueNext[] {
|
|
if (node.type === "choice") return node.data.choices ?? [];
|
|
if (node.type === "condition") {
|
|
const hops: DialogueNext[] = [];
|
|
if (node.data.trueTarget) hops.push({ label: "true", target: node.data.trueTarget });
|
|
if (node.data.falseTarget) hops.push({ label: "false", target: node.data.falseTarget });
|
|
return hops;
|
|
}
|
|
return node.data.next ? [node.data.next] : [];
|
|
}
|
|
|
|
/** True when a node has no outgoing hops (conversation ends here). */
|
|
export function isTerminalNode(node: DialogueNode): boolean {
|
|
return outgoingHops(node).length === 0;
|
|
}
|
|
|
|
/** Resolves the node the conversation should start at. */
|
|
export function findStartNode(nodes: DialogueNode[]): DialogueNode | null {
|
|
if (nodes.length === 0) return null;
|
|
const marked = nodes.find((node) => node.data.isStart === true);
|
|
if (marked) return marked;
|
|
// Fall back to the first node with no incoming edges.
|
|
const targets = new Set(nodes.flatMap((n) => outgoingHops(n).map((h) => h.target)));
|
|
const roots = nodes.filter((n) => !targets.has(n.id));
|
|
return (roots.length ? roots[0] : nodes[0]) ?? null;
|
|
}
|
|
|
|
/** Builds the edge projection that mirrors the node graph. */
|
|
export function deriveEdges(nodes: DialogueNode[]): DialogueEdge[] {
|
|
const edges: DialogueEdge[] = [];
|
|
for (const node of nodes) {
|
|
outgoingHops(node).forEach((hop, index) => {
|
|
if (!hop || !hop.target) return;
|
|
edges.push({
|
|
id: `${node.id}->${index}`,
|
|
source: node.id,
|
|
target: hop.target,
|
|
label: hop.label,
|
|
});
|
|
});
|
|
}
|
|
return edges;
|
|
}
|
|
|
|
export interface GraphValidation {
|
|
valid: boolean;
|
|
errors: string[];
|
|
}
|
|
|
|
/**
|
|
* Validates a flowchart: a start node exists, every hop target resolves, and
|
|
* choice hops carry a label. Does not hard-fail on cycles (branching can be
|
|
* intentionally re-entrant) but reports them.
|
|
*/
|
|
export function validateGraph(nodes: DialogueNode[]): GraphValidation {
|
|
const errors: string[] = [];
|
|
const ids = new Set(nodes.map((n) => n.id));
|
|
|
|
if (nodes.length === 0) {
|
|
return { valid: false, errors: ["Add at least one node to start building the conversation."] };
|
|
}
|
|
if (!findStartNode(nodes)) {
|
|
errors.push("Mark a node as the conversation start.");
|
|
}
|
|
|
|
for (const node of nodes) {
|
|
const hops = outgoingHops(node);
|
|
for (const hop of hops) {
|
|
if (!hop) continue;
|
|
if (hop.target && !ids.has(hop.target)) {
|
|
errors.push(`"${node.id}" points to missing node "${hop.target}".`);
|
|
}
|
|
if (node.type === "choice" && !hop.label?.trim()) {
|
|
errors.push(`A choice on "${node.id}" needs button text.`);
|
|
}
|
|
}
|
|
}
|
|
|
|
return { valid: errors.length === 0, errors };
|
|
}
|