1
0
Fork 0
polaris-task-force/src/lib/npc-dialogue/interpolate.ts

123 lines
4.3 KiB
TypeScript

/**
* Dynamic text interpolation for NPC conversation strings.
*
* Authors embed flag values with double-brace tokens: `{{network_status}}`.
* Resolution happens server-side when a conversation line is about to render
* (see the `resolveNpcDialogueTexts` action), so an interpolated value may
* itself contain display markup like `{green}NOMINAL{/}` and still render
* colored through DialogueText. Values are substituted in a single pass and
* never re-scanned, so a flag value can never smuggle in another token.
*
* Pure module: client-safe (no Payload imports).
*/
import type { DialogueNode } from "./dialogue";
/** One `{{key}}` token. Keys are matched leniently and lowercased; flag keys
* are documented as lowercase-with-underscores. */
const FLAG_TOKEN = /\{\{\s*([a-zA-Z0-9_]+)\s*\}\}/g;
/** The display strings of a node that support interpolation. */
export interface ResolvedNodeTexts {
body?: string;
leadIn?: string;
closing?: string;
/** Event node narration fallback (`effect.message`). */
message?: string;
/** Choice button labels, index-aligned with the node's choices. */
labels?: string[];
/** Choice tooltips, index-aligned with the node's choices. */
tooltips?: string[];
}
/** Every distinct flag key referenced by `text`, lowercased. */
export function extractFlagTokens(text: string | undefined): string[] {
if (!text) return [];
const keys = new Set<string>();
for (const match of text.matchAll(FLAG_TOKEN)) {
keys.add(match[1].toLowerCase());
}
return [...keys];
}
/** True when `text` contains at least one interpolation token. */
export function hasFlagTokens(text: string | undefined): boolean {
if (!text) return false;
FLAG_TOKEN.lastIndex = 0;
return FLAG_TOKEN.test(text);
}
/**
* Replace every `{{key}}` token with its value. Keys missing from `values`
* resolve to an empty string; substituted values are NOT re-scanned.
*/
export function interpolateFlags(
text: string | undefined,
values: Record<string, string>,
): string {
if (!text) return "";
return text.replace(FLAG_TOKEN, (_match, key: string) => values[key.toLowerCase()] ?? "");
}
/** Minimal shape needed to format a flag's value for interpolation. */
export interface FlagValueLike {
flagType: "boolean" | "number" | "text";
boolValue?: boolean | null;
numberValue?: number | null;
textValue?: string | null;
}
/** The string a flag contributes to interpolated text. */
export function formatFlagValue(flag: FlagValueLike): string {
switch (flag.flagType) {
case "number":
return String(flag.numberValue ?? 0);
case "text":
return flag.textValue ?? "";
default:
return flag.boolValue === true ? "true" : "false";
}
}
/** All flag keys the node's display strings reference (deduped, lowercased). */
export function collectNodeTokens(node: DialogueNode): string[] {
const keys = new Set<string>();
const texts = [
node.data.body,
node.data.leadIn,
node.data.closing,
node.data.effect?.message,
...(node.data.choices?.flatMap((c) => [c.label, c.tooltip]) ?? []),
];
for (const text of texts) {
for (const key of extractFlagTokens(text)) keys.add(key);
}
return [...keys];
}
/**
* Interpolates every display string of a node against resolved flag values.
* Only fields the node actually defines are returned, so the client can tell
* "resolved to empty" apart from "no such field".
*/
export function resolveNodeTexts(
node: DialogueNode,
values: Record<string, string>,
): ResolvedNodeTexts {
const resolved: ResolvedNodeTexts = {};
if (node.data.body !== undefined) resolved.body = interpolateFlags(node.data.body, values);
if (node.data.leadIn !== undefined) resolved.leadIn = interpolateFlags(node.data.leadIn, values);
if (node.data.closing !== undefined)
resolved.closing = interpolateFlags(node.data.closing, values);
if (node.data.effect?.message !== undefined)
resolved.message = interpolateFlags(node.data.effect.message, values);
if (node.data.choices) {
resolved.labels = node.data.choices.map((choice) => interpolateFlags(choice.label, values));
if (node.data.choices.some((choice) => choice.tooltip !== undefined)) {
resolved.tooltips = node.data.choices.map((choice) =>
choice.tooltip === undefined ? "" : interpolateFlags(choice.tooltip, values),
);
}
}
return resolved;
}