Rearranging nodes on the canvas (positions, edge ids, styling) no longer changes the flow hash, so tidying a conversation in the editor does not re-badge it as NEW for every player. The hash now covers node identity, type, data, and edge source/target with order-independent serialization; real text or branching edits still re-flag.
106 lines
4 KiB
TypeScript
106 lines
4 KiB
TypeScript
/**
|
|
* Per-browser conversation progress tracking that powers the "NEW" badges.
|
|
*
|
|
* For each conversation (npc-dialogues id) we remember a hash of its
|
|
* flowchart plus the node ids this browser has read. The Converse button
|
|
* shows a badge when the flow changed (GM added paths -> new hash) or when
|
|
* nodes remain unread; choice buttons badge their unseen targets. Stored in
|
|
* localStorage like the tour/announcement dismissals: no server state, and a
|
|
* fresh device rediscovers every path once. Pure module, client-safe.
|
|
*/
|
|
|
|
const STORAGE_KEY = "ptf:npc-dialogue-seen";
|
|
|
|
export interface SeenEntry {
|
|
/** flowHash of the flowchart these node ids belong to. */
|
|
hash: string;
|
|
/** Node ids rendered at least once. */
|
|
nodes: string[];
|
|
}
|
|
|
|
export type SeenMap = Record<string, SeenEntry>;
|
|
|
|
/** Deterministic djb2 hash of the flowchart's CONTENT (base36). Node
|
|
* positions and edge plumbing (ids, handles, styling) are excluded on
|
|
* purpose: rearranging the canvas is not a story change and must not
|
|
* re-badge conversations as NEW. Object keys are sorted so an equivalent
|
|
* document serialized with a different key order hashes the same. */
|
|
export function flowHash(flowData: unknown): string {
|
|
const json = stableStringify(contentOnly(flowData));
|
|
let h = 5381;
|
|
for (let i = 0; i < json.length; i += 1) {
|
|
h = ((h << 5) + h + json.charCodeAt(i)) | 0;
|
|
}
|
|
return (h >>> 0).toString(36);
|
|
}
|
|
|
|
/** Strips layout from a flowchart, keeping node identity/branching/text. */
|
|
function contentOnly(flowData: unknown): unknown {
|
|
const doc = (flowData ?? {}) as { nodes?: unknown; edges?: unknown };
|
|
const nodes = Array.isArray(doc.nodes) ? doc.nodes : [];
|
|
const edges = Array.isArray(doc.edges) ? doc.edges : [];
|
|
return {
|
|
nodes: nodes.map((node) => {
|
|
const n = (node ?? {}) as { id?: unknown; type?: unknown; data?: unknown };
|
|
return { id: n.id, type: n.type, data: n.data };
|
|
}),
|
|
edges: edges.map((edge) => {
|
|
const e = (edge ?? {}) as { source?: unknown; target?: unknown };
|
|
return { source: e.source, target: e.target };
|
|
}),
|
|
};
|
|
}
|
|
|
|
/** JSON.stringify with sorted object keys for order-independent hashing. */
|
|
function stableStringify(value: unknown): string {
|
|
if (value === null || typeof value !== "object") return JSON.stringify(value) ?? "null";
|
|
if (Array.isArray(value)) return `[${value.map(stableStringify).join(",")}]`;
|
|
const entries = Object.entries(value as Record<string, unknown>)
|
|
.filter(([, v]) => v !== undefined)
|
|
.sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0));
|
|
return `{${entries.map(([k, v]) => `${JSON.stringify(k)}:${stableStringify(v)}`).join(",")}}`;
|
|
}
|
|
|
|
/** Reads the seen map; corrupt or absent storage reads empty. */
|
|
export function loadSeenMap(): SeenMap {
|
|
if (typeof window === "undefined") return {};
|
|
try {
|
|
const raw = window.localStorage.getItem(STORAGE_KEY);
|
|
if (!raw) return {};
|
|
const parsed = JSON.parse(raw) as SeenMap;
|
|
return parsed && typeof parsed === "object" ? parsed : {};
|
|
} catch {
|
|
return {};
|
|
}
|
|
}
|
|
|
|
/** Persists the seen map; storage failures (private mode) are ignored. */
|
|
export function saveSeenMap(map: SeenMap): void {
|
|
if (typeof window === "undefined") return;
|
|
try {
|
|
window.localStorage.setItem(STORAGE_KEY, JSON.stringify(map));
|
|
} catch {
|
|
// Best effort only.
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Node ids from `nodeIds` this browser has not read yet. A missing entry or a
|
|
* hash mismatch (the GM edited the flowchart) counts everything as unseen.
|
|
*/
|
|
export function unseenNodeIds(
|
|
entry: SeenEntry | undefined,
|
|
hash: string,
|
|
nodeIds: readonly string[],
|
|
): string[] {
|
|
if (!entry || entry.hash !== hash) return [...nodeIds];
|
|
const seen = new Set(entry.nodes);
|
|
return nodeIds.filter((id) => !seen.has(id));
|
|
}
|
|
|
|
/** Returns the updated node list for one conversation (deduped, sorted-free). */
|
|
export function mergeSeenNodes(entry: SeenEntry | undefined, hash: string, nodeId: string): string[] {
|
|
const nodes = entry && entry.hash === hash ? [...entry.nodes] : [];
|
|
if (!nodes.includes(nodeId)) nodes.push(nodeId);
|
|
return nodes;
|
|
}
|