From f595828c99d92cbede4fb0044ab9652a8ee9a5c4 Mon Sep 17 00:00:00 2001 From: Z8MB1E Date: Tue, 6 Oct 2026 17:06:27 -0400 Subject: [PATCH] feat(npc-dialogue): stacked markup, link popups, new badges, locked choices, timed flags - DialogueText rewritten as a recursive parser so markup accumulates ({green}**bold green**{/} and friends nest freely, colors close inner-first) - [label](url) dialogue links open in an iframe popup dialog so conversation progress survives; only http(s)/relative urls render - NEW badges: Converse button + choice buttons badge unread paths via a per-browser seen map (ptf:npc-dialogue-seen) keyed by flow hash, so GM edits re-flag the conversation - choices gain lockedHint: failing conditions can show the choice disabled with a rich tooltip instead of hiding it (evaluateNpcDialogueChoices now returns ChoiceGate pass/showLocked pairs) - player flags gain activatesAt/expiresAt time windows honored by readFlag, flag effects gain flagDelayMinutes for slow-burn writes, and {{countdown:key}} tokens tick live toward activation (done/missing states) - migration 20261006_add_flag_time_windows; spec extended to 33 tests --- .../personnel/contacts/[id]/actions.ts | 69 +- src/collections/game/PlayerFlags.ts | 18 + .../admin/npc-dialogue/NodeEditDialog.tsx | 44 +- .../frontend/personnel/NpcConversation.tsx | 127 +- src/lib/npc-dialogue/DialogueText.tsx | 382 +- src/lib/npc-dialogue/dialogue.ts | 20 + src/lib/npc-dialogue/flags.ts | 57 +- src/lib/npc-dialogue/interpolate.ts | 61 +- src/lib/npc-dialogue/seen.ts | 75 + ...20261006_205845_add_flag_time_windows.json | 37438 ++++++++++++++++ .../20261006_205845_add_flag_time_windows.ts | 13 + src/migrations/index.ts | 8 +- src/payload-types.ts | 10 + .../int/npc-dialogue-interpolate.int.spec.ts | 164 +- 14 files changed, 38344 insertions(+), 142 deletions(-) create mode 100644 src/lib/npc-dialogue/seen.ts create mode 100644 src/migrations/20261006_205845_add_flag_time_windows.json create mode 100644 src/migrations/20261006_205845_add_flag_time_windows.ts diff --git a/src/app/(frontend)/personnel/contacts/[id]/actions.ts b/src/app/(frontend)/personnel/contacts/[id]/actions.ts index 85e4ca80..3311a99b 100644 --- a/src/app/(frontend)/personnel/contacts/[id]/actions.ts +++ b/src/app/(frontend)/personnel/contacts/[id]/actions.ts @@ -10,7 +10,12 @@ import { notifyUser } from "@/lib/notifications"; import { collectNodeTokens, resolveNodeTexts } from "@/lib/npc-dialogue/interpolate"; import type { ResolvedNodeTexts } from "@/lib/npc-dialogue/interpolate"; import { evaluateCondition, readFlag } from "@/lib/npc-dialogue/flags"; -import type { DialogueFlagOp, FlowData, NpcDialogueEffect } from "@/lib/npc-dialogue/dialogue"; +import type { + ChoiceGate, + DialogueFlagOp, + FlowData, + NpcDialogueEffect, +} from "@/lib/npc-dialogue/dialogue"; interface ActionResult { success: boolean; @@ -122,7 +127,15 @@ async function runEffect( if (!key) break; const op: DialogueFlagOp = effect.flagOp ?? "set_true"; const amount = effect.flagAmount ?? 0; - await applyFlagEffect(payload, userId, key, op, amount, effect.flagText); + await applyFlagEffect( + payload, + userId, + key, + op, + amount, + effect.flagText, + effect.flagDelayMinutes, + ); await emitGameEvent(payload, { type: EventTypes.NpcDialogue, message: `Conversation set flag "${key}" (${op}).`, @@ -136,7 +149,9 @@ async function runEffect( } } -/** Create-or-update a player flag according to the effect's write mode. */ +/** Create-or-update a player flag according to the effect's write mode. A + * positive `delayMinutes` stamps the flag with a future `activatesAt`, so it + * reads as false until then (conditions fail, countdowns tick toward it). */ async function applyFlagEffect( payload: Awaited>, userId: number, @@ -144,7 +159,12 @@ async function applyFlagEffect( op: DialogueFlagOp, amount: number, effectText?: string, + delayMinutes?: number, ): Promise { + const activatesAt = + delayMinutes && delayMinutes > 0 + ? new Date(Date.now() + delayMinutes * 60_000).toISOString() + : null; const existing = await payload.find({ collection: "player-flags", where: { @@ -163,13 +183,13 @@ async function applyFlagEffect( await payload.update({ collection: "player-flags", id: doc.id, - data: { flagType: "boolean", boolValue }, + data: { flagType: "boolean", boolValue, activatesAt }, overrideAccess: true, }); } else { await payload.create({ collection: "player-flags", - data: { user: userId, key, flagType: "boolean", boolValue }, + data: { user: userId, key, flagType: "boolean", boolValue, activatesAt }, overrideAccess: true, }); } @@ -181,13 +201,13 @@ async function applyFlagEffect( await payload.update({ collection: "player-flags", id: doc.id, - data: { flagType: "number", numberValue: amount }, + data: { flagType: "number", numberValue: amount, activatesAt }, overrideAccess: true, }); } else { await payload.create({ collection: "player-flags", - data: { user: userId, key, flagType: "number", numberValue: amount }, + data: { user: userId, key, flagType: "number", numberValue: amount, activatesAt }, overrideAccess: true, }); } @@ -200,13 +220,13 @@ async function applyFlagEffect( await payload.update({ collection: "player-flags", id: doc.id, - data: { flagType: "text", textValue }, + data: { flagType: "text", textValue, activatesAt }, overrideAccess: true, }); } else { await payload.create({ collection: "player-flags", - data: { user: userId, key, flagType: "text", textValue }, + data: { user: userId, key, flagType: "text", textValue, activatesAt }, overrideAccess: true, }); } @@ -226,13 +246,13 @@ async function applyFlagEffect( await payload.update({ collection: "player-flags", id: doc.id, - data: { flagType: "number", numberValue }, + data: { flagType: "number", numberValue, activatesAt }, overrideAccess: true, }); } else { await payload.create({ collection: "player-flags", - data: { user: userId, key, flagType: "number", numberValue }, + data: { user: userId, key, flagType: "number", numberValue, activatesAt }, overrideAccess: true, }); } @@ -278,15 +298,16 @@ export async function evaluateNpcDialogueCondition( /** * Evaluate every choice's visibility gate for a `choice` node in one round - * trip. Ungated choices are always visible; gated ones appear only while the - * reading user's flags satisfy their condition. The node is looked up - * server-side from the stored flowchart, so clients never see gate config - * for conversations an admin has not authored. + * trip. Ungated choices always pass; gated ones report whether the reading + * user's flags satisfy their condition and whether a failing choice should + * still render locked (with its authored hint) instead of hiding. The node is + * looked up server-side from the stored flowchart, so clients never see gate + * config for conversations an admin has not authored. */ export async function evaluateNpcDialogueChoices( npcId: number, nodeId: string, -): Promise> { +): Promise> { try { const { payload, user } = await authenticate(); @@ -304,15 +325,17 @@ export async function evaluateNpcDialogueChoices( return { success: true, data: [] }; } - const visible: boolean[] = []; + const gates: ChoiceGate[] = []; for (const choice of node.data.choices ?? []) { + const showLocked = Boolean(choice.lockedHint?.trim()); if (!choice.condition?.key?.trim()) { - visible.push(true); + gates.push({ pass: true, showLocked }); continue; } - visible.push(await evaluateCondition(payload, user.id as number, choice.condition)); + const pass = await evaluateCondition(payload, user.id as number, choice.condition); + gates.push({ pass, showLocked }); } - return { success: true, data: visible }; + return { success: true, data: gates }; } catch (error) { const message = error instanceof Error ? error.message : "Unknown error"; if (message === "Unauthorized") { @@ -349,11 +372,15 @@ export async function resolveNpcDialogueTexts( const keys = collectNodeTokens(node); const values: Record = {}; + const countdowns: Record = {}; for (const key of keys) { const flag = await readFlag(payload, user.id as number, key); values[key] = flag.text; + // Countdown leaf: pending flags tick toward activation, active ones read + // "done", missing ones render nothing. + countdowns[key] = flag.activatesAt ?? (flag.exists ? "done" : ""); } - return { success: true, data: resolveNodeTexts(node, values) }; + return { success: true, data: resolveNodeTexts(node, values, countdowns) }; } catch (error) { const message = error instanceof Error ? error.message : "Unknown error"; if (message === "Unauthorized") { diff --git a/src/collections/game/PlayerFlags.ts b/src/collections/game/PlayerFlags.ts index 4bcb7878..b4369e06 100644 --- a/src/collections/game/PlayerFlags.ts +++ b/src/collections/game/PlayerFlags.ts @@ -79,6 +79,24 @@ export const PlayerFlags: CollectionConfig = { "Inserted into conversation text wherever {{key}} appears. May contain dialogue markup like {green}NOMINAL{/}. A missing text flag reads as empty.", }, }, + { + name: "activatesAt", + type: "date", + admin: { + date: { pickerAppearance: "dayAndTime" }, + description: + "Time-gated narrative: until this moment the flag reads as false / 0 / empty (conditions fail, {{countdown:key}} ticks). Leave empty for immediate.", + }, + }, + { + name: "expiresAt", + type: "date", + admin: { + date: { pickerAppearance: "dayAndTime" }, + description: + "After this moment the flag reads as missing again (falls back to a global flag of the same key, else false / 0 / empty). Use for offers and windows that lapse.", + }, + }, ], timestamps: true, indexes: [{ fields: ["user", "key"], unique: true }], diff --git a/src/components/admin/npc-dialogue/NodeEditDialog.tsx b/src/components/admin/npc-dialogue/NodeEditDialog.tsx index c59f5d38..86ed85c2 100644 --- a/src/components/admin/npc-dialogue/NodeEditDialog.tsx +++ b/src/components/admin/npc-dialogue/NodeEditDialog.tsx @@ -337,9 +337,23 @@ export function NodeEditDialog({ node, allNodes, open, onOpenChange, onSave }: N /> )} + +