From ce534230a54b2c4fc5d5a0e4f77adcacedb5d84c Mon Sep 17 00:00:00 2001 From: Z8MB1E Date: Tue, 1 Dec 2026 14:51:19 -0500 Subject: [PATCH] feat(npc-dialogue): walk a preview branch your own flags cannot reach A condition in a draft could only be walked the way the author's flags really resolve, so a conversation gated behind a flag they have not earned was untestable in the preview: the other branch was unreachable without granting themselves the flag for real. A condition node now offers both exits. One is labelled "your flags" and is the branch a player with the author's flags would take; the other is an amber override that walks the opposite branch anyway. The same applies to a choice whose gate fails: it stays clickable, reads as an override, and says so in its gate note. conditionBranches in preview.ts pairs the two targets and labels them, so the component holds no branch logic of its own; an unwired branch is reported as unwired rather than silently offering nothing. The footer counts overrides against the author's flags, so an override walk is never mistaken for what a player would see, and Back/Restart behave as before. Verified in the running editor with a draft whose condition tests a flag that is false for the dev account: the gate card showed "false for you, so a player matching your flags takes the false branch" with both buttons, and taking the true branch landed on the true card with "1 override against your flags" in the footer. 3 pure tests cover the pairing and the unwired case; 223 npc-dialogue tests pass. --- .../npc-dialogue/DialoguePreviewDialog.tsx | 106 ++++++++++++++---- src/lib/npc-dialogue/AGENTS.md | 6 + src/lib/npc-dialogue/preview.ts | 33 ++++++ tests/int/npc-dialogue-preview.int.spec.ts | 36 ++++++ 4 files changed, 157 insertions(+), 24 deletions(-) diff --git a/src/components/admin/npc-dialogue/DialoguePreviewDialog.tsx b/src/components/admin/npc-dialogue/DialoguePreviewDialog.tsx index 5cabfa70..21a6301d 100644 --- a/src/components/admin/npc-dialogue/DialoguePreviewDialog.tsx +++ b/src/components/admin/npc-dialogue/DialoguePreviewDialog.tsx @@ -23,6 +23,7 @@ import type { DialogueCondition, DialogueNode, FlowData } from "@/lib/npc-dialog import { findStartNode, waitsForContinue } from "@/lib/npc-dialogue/dialogue"; import { automaticTarget, + conditionBranches, describeCondition, isTerminal, nodeForTarget, @@ -65,8 +66,11 @@ export function DialoguePreviewDialog({ const [gateErrors, setGateErrors] = useState(null); const [checking, setChecking] = useState(false); /** Gate answers for conditional choices, keyed `:`, so the - * preview disables a choice the runtime would hide. */ + * preview can mark a choice the runtime would hide. */ const [choiceGates, setChoiceGates] = useState>({}); + /** Steps taken against the author's own flags, reported in the footer so an + * override walk is never mistaken for what a player would see. */ + const [overrides, setOverrides] = useState(0); const reset = useCallback(() => { setCurrentId(findStartNode(flow.nodes as DialogueNode[])?.id ?? null); @@ -74,6 +78,7 @@ export function DialoguePreviewDialog({ setTrail([]); setGates({}); setGateErrors(null); + setOverrides(0); }, [flow.nodes]); // Reopening starts a fresh walk rather than resuming wherever it stopped. @@ -148,8 +153,9 @@ export function DialoguePreviewDialog({ const gateAnswer = node && node.id in gates ? gates[node.id] : null; const auto = node ? automaticTarget(node, gateAnswer) : { target: null, waitingOnGate: false }; - const go = (target: string | null) => { + const go = (target: string | null, options: { override?: boolean } = {}) => { if (!target) return; + if (options.override) setOverrides((current) => current + 1); setTrail((current) => (currentId ? [...current, currentId] : current)); setVisited((current) => (current.includes(target) ? current : [...current, target])); setCurrentId(target); @@ -170,11 +176,17 @@ export function DialoguePreviewDialog({ const choiceAvailable = (index: number) => { const choice = node?.data.choices?.[index]; if (!choice) return false; + // A spent `once` choice is bookkeeping, not a flag, so it stays unavailable. if (choice.once === true && choice.target && visited.includes(choice.target)) return false; - if (!choice.condition) return true; - const answer = node ? choiceGates[`${node.id}:${index}`] : undefined; - // Not answered yet: keep it clickable rather than flashing disabled. - return answer === undefined ? true : answer; + return true; + }; + + /** True when the choice's own gate fails for this author. It stays clickable + * so the branch behind it can be walked anyway, and reads as an override. */ + const choiceOverridesFlags = (index: number) => { + const choice = node?.data.choices?.[index]; + if (!choice?.condition || !node) return false; + return choiceGates[`${node.id}:${index}`] === false; }; return ( @@ -272,8 +284,8 @@ export function DialoguePreviewDialog({ {gateErrors &&
{gateErrors}
} {gateAnswer !== null && (
- {gateAnswer ? "true" : "false"} for you, so this draft continues{" "} - {gateAnswer ? "true" : "false"} + {gateAnswer ? "true" : "false"} for you, so a player matching your flags takes + the {gateAnswer ? "true" : "false"} branch
)} @@ -286,15 +298,21 @@ export function DialoguePreviewDialog({ )} {step.choices.map((choice) => { const available = choiceAvailable(choice.index); + const overriding = available && choiceOverridesFlags(choice.index); + const gateKnown = node + ? choiceGates[`${node.id}:${choice.index}`] + : undefined; return ( - )} + {node.type === "condition" && + gateAnswer !== null && + (() => { + const branches = conditionBranches(node, gateAnswer); + if (!branches) return null; + return ( +
+ + + {!branches.actual.target && ( + + the {branches.actual.label} branch is not wired + + )} + {!branches.override.target && ( + + the {branches.override.label} branch is not wired either + + )} +
+ ); + })()} {node.type !== "choice" && node.type !== "condition" && !isTerminal(node) && (
@@ -374,8 +427,13 @@ export function DialoguePreviewDialog({
{trail.length} step{trail.length === 1 ? "" : "s"} deep, {visited.length} card - {visited.length === 1 ? "" : "s"} visited. Text shows tokens as - authored; the player sees them resolved. + {visited.length === 1 ? "" : "s"} visited + {overrides > 0 && ( + + , {overrides} override{overrides === 1 ? "" : "s"} against your flags + + )} + . Text shows tokens as authored; the player sees them resolved.
diff --git a/src/lib/npc-dialogue/AGENTS.md b/src/lib/npc-dialogue/AGENTS.md index 6a180a67..6dd4192c 100644 --- a/src/lib/npc-dialogue/AGENTS.md +++ b/src/lib/npc-dialogue/AGENTS.md @@ -302,6 +302,12 @@ Two traps specific to this component, both costs of rendering inside the admin: editor's depth. It falls back to `contact `, which is honest; do not try to fetch the name here. +**Overrides.** A condition node offers both exits: the branch the author's own +flags take (labelled "your flags") and the other one ("override"). So can a +choice whose gate fails. `conditionBranches` in `preview.ts` is what pairs them, +and the footer counts overrides so an override walk is never mistaken for what a +player would see. A missing branch is shown as unwired rather than invented. + The panel is centred vertically and keeps a `trail` of the cards walked through: `Back` pops it. `visited` is deliberately separate, so stepping back rewinds the walk without un-spending a `once` choice the player already took. diff --git a/src/lib/npc-dialogue/preview.ts b/src/lib/npc-dialogue/preview.ts index 7304d1f4..f0e5932a 100644 --- a/src/lib/npc-dialogue/preview.ts +++ b/src/lib/npc-dialogue/preview.ts @@ -177,6 +177,39 @@ export function automaticTarget( return { target: node.data.next?.target ?? null, waitingOnGate: false }; } +/** The two ways out of a condition node, for the preview's branching step. */ +export interface ConditionBranches { + /** The branch a player with these flags would really take. */ + readonly actual: { readonly label: "true" | "false"; readonly target: string | null }; + /** + * The other branch. The preview offers it so an author can reach a path their + * own account cannot: a conversation gated on a flag they have not earned is + * otherwise untestable without granting themselves the flag for real. + */ + readonly override: { readonly label: "true" | "false"; readonly target: string | null }; +} + +/** + * Both branches of a condition node, labelled. `conditionPassed` is the real + * answer for this author, so `actual` is what the runtime would do and + * `override` is the walk the preview can take instead. + */ +export function conditionBranches( + node: DialogueNode, + conditionPassed: boolean, +): ConditionBranches | null { + if (node.type !== "condition") return null; + return conditionPassed + ? { + actual: { label: "true", target: node.data.trueTarget ?? null }, + override: { label: "false", target: node.data.falseTarget ?? null }, + } + : { + actual: { label: "false", target: node.data.falseTarget ?? null }, + override: { label: "true", target: node.data.trueTarget ?? null }, + }; +} + /** Node types the preview can continue through without the player picking. */ export function isTerminal(node: DialogueNode): boolean { return node.type === "end"; diff --git a/tests/int/npc-dialogue-preview.int.spec.ts b/tests/int/npc-dialogue-preview.int.spec.ts index 5668fc7f..b1429d73 100644 --- a/tests/int/npc-dialogue-preview.int.spec.ts +++ b/tests/int/npc-dialogue-preview.int.spec.ts @@ -4,6 +4,7 @@ import { describe, expect, it } from "vitest"; import type { DialogueNode } from "@/lib/npc-dialogue/dialogue"; import { automaticTarget, + conditionBranches, describeCondition, isTerminal, nodeForTarget, @@ -128,3 +129,38 @@ describe("draft preview reading the graph", () => { expect(nodeForTarget(nodes, undefined)).toBeNull(); }); }); + +describe("condition branches in the preview", () => { + const gate = node({ + id: "gate", + type: "condition", + data: { trueTarget: "yes", falseTarget: "no" }, + }); + + it("labels the real branch and the override one", () => { + expect(conditionBranches(gate, true)).toEqual({ + actual: { label: "true", target: "yes" }, + override: { label: "false", target: "no" }, + }); + expect(conditionBranches(gate, false)).toEqual({ + actual: { label: "false", target: "no" }, + override: { label: "true", target: "yes" }, + }); + }); + + it("reports an unwired branch as no target instead of inventing one", () => { + const halfWired = node({ + id: "half", + type: "condition", + data: { trueTarget: "yes" }, + }); + // The condition is false for this author, and only the true side is wired. + expect(conditionBranches(halfWired, false)?.actual.target).toBeNull(); + expect(conditionBranches(halfWired, false)?.override.target).toBe("yes"); + }); + + it("is null for a node that is not a condition", () => { + const line = node({ id: "line", type: "dialogue", data: { next: { target: "next" } } }); + expect(conditionBranches(line, true)).toBeNull(); + }); +});