BlueGit

timfa questward

Files

questward / shared / quests / normalize.js

normalize.js

normalize.js 5.3 KiB Download Raw
1import { normalizeConditionSet, normalizeEffects, emptyConditionSet } from "./conditions.js";
2
3export function normalizeQuestDef (raw)
4{
5 const questId = slugId(raw?.questId ?? raw?.id, "quest");
6 const stepsIn = Array.isArray(raw?.steps) ? raw.steps : [];
7 const steps = stepsIn.map((step, index) => normalizeQuestStep(step, index)).filter(Boolean);
8 return {
9 questId,
10 title: String(raw?.title ?? questId).trim().slice(0, 80) || questId,
11 description: String(raw?.description ?? "").trim().slice(0, 500),
12 steps: steps.length > 0 ? steps : [normalizeQuestStep({ stepId: "step-1" }, 0)],
13 rewards: normalizeEffects(raw?.rewards),
14 cancelable: !!raw?.cancelable
15 };
16}
17
18function normalizeQuestStep (raw, index)
19{
20 const stepId = slugId(raw?.stepId ?? raw?.id, `step-${index + 1}`);
21 const objectivesIn = Array.isArray(raw?.objectives) ? raw.objectives : [];
22 let objectives = objectivesIn.map((obj, i) => normalizeObjective(obj, i)).filter(Boolean);
23 if (objectives.length === 0)
24 {
25 objectives = [normalizeObjective({
26 objectiveId: "obj-1",
27 text: "Complete this step",
28 when: emptyConditionSet()
29 }, 0)];
30 }
31 return {
32 stepId,
33 title: String(raw?.title ?? stepId).trim().slice(0, 80) || stepId,
34 description: String(raw?.description ?? "").trim().slice(0, 500),
35 completionJournalText: String(raw?.completionJournalText ?? "").trim().slice(0, 500),
36 objectives,
37 onEnter: normalizeEffects(raw?.onEnter),
38 onComplete: normalizeEffects(raw?.onComplete)
39 };
40}
41
42function normalizeObjective (raw, index)
43{
44 const objectiveId = slugId(raw?.objectiveId ?? raw?.id, `obj-${index + 1}`);
45 const when = normalizeConditionSet(raw?.when ?? raw?.require);
46 return {
47 objectiveId,
48 text: String(raw?.text ?? objectiveId).trim().slice(0, 200) || objectiveId,
49 when
50 };
51}
52
53export function normalizeDialogueTree (raw)
54{
55 const dialogueId = slugId(raw?.dialogueId ?? raw?.id, "dialogue");
56 const stepsIn = Array.isArray(raw?.steps) ? raw.steps : [];
57 const steps = stepsIn.map((step, index) => normalizeDialogueStep(step, index)).filter(Boolean);
58 const safeSteps = steps.length > 0
59 ? steps
60 : [normalizeDialogueStep({
61 stepId: "greeting",
62 label: "Greeting",
63 lines: [{ speaker: "npc", text: "…" }],
64 exit: { kind: "end" }
65 }, 0)];
66 const entryStepId = slugId(raw?.entryStepId ?? safeSteps[0].stepId, safeSteps[0].stepId);
67 const ids = new Set(safeSteps.map((s) => s.stepId));
68 return {
69 dialogueId,
70 label: String(raw?.label ?? dialogueId).trim().slice(0, 80) || dialogueId,
71 unique: !!raw?.unique,
72 entryStepId: ids.has(entryStepId) ? entryStepId : safeSteps[0].stepId,
73 steps: safeSteps
74 };
75}
76
77function normalizeDialogueStep (raw, index)
78{
79 const stepId = slugId(raw?.stepId ?? raw?.id, `step-${index + 1}`);
80 const linesIn = Array.isArray(raw?.lines) ? raw.lines : [];
81 const lines = linesIn.map(normalizeDialogueLine).filter(Boolean);
82 const exit = normalizeExit(raw?.exit, raw);
83 return {
84 stepId,
85 label: String(raw?.label ?? stepId).trim().slice(0, 80) || stepId,
86 lines: lines.length > 0 ? lines : [{ speaker: "npc", text: "…", effects: [] }],
87 exit
88 };
89}
90
91function normalizeDialogueLine (raw)
92{
93 if (!raw || typeof raw !== "object")
94 { return null; }
95 const text = String(raw.text ?? "").trim().slice(0, 500);
96 if (!text)
97 { return null; }
98 const speaker = raw.speaker === "player" ? "player" : "npc";
99 return { speaker, text, effects: normalizeEffects(raw.effects) };
100}
101
102function normalizeExit (exit, legacy)
103{
104 if (exit && typeof exit === "object" && exit.kind)
105 {
106 if (exit.kind === "triggers")
107 {
108 return {
109 kind: "triggers",
110 triggers: (Array.isArray(exit.triggers) ? exit.triggers : [])
111 .map(normalizeTrigger)
112 .filter(Boolean)
113 };
114 }
115 if (exit.kind === "branches")
116 {
117 return {
118 kind: "branches",
119 branches: (Array.isArray(exit.branches) ? exit.branches : [])
120 .map(normalizeBranch)
121 .filter(Boolean)
122 };
123 }
124 return { kind: "end" };
125 }
126 if (Array.isArray(legacy?.triggers) && legacy.triggers.length > 0)
127 {
128 return {
129 kind: "triggers",
130 triggers: legacy.triggers.map(normalizeTrigger).filter(Boolean)
131 };
132 }
133 if (Array.isArray(legacy?.branches) && legacy.branches.length > 0)
134 {
135 return {
136 kind: "branches",
137 branches: legacy.branches.map(normalizeBranch).filter(Boolean)
138 };
139 }
140 return { kind: "end" };
141}
142
143function normalizeTrigger (raw)
144{
145 if (!raw || typeof raw !== "object")
146 { return null; }
147 const trigger = slugId(raw.trigger ?? raw.id, "choice");
148 const text = String(raw.text ?? "").trim().slice(0, 120);
149 if (!text)
150 { return null; }
151 const next = raw.nextStepId == null || raw.nextStepId === ""
152 ? null
153 : slugId(raw.nextStepId, "");
154 return {
155 trigger,
156 label: String(raw.label ?? trigger).trim().slice(0, 80) || trigger,
157 text,
158 nextStepId: next || null,
159 when: normalizeConditionSet(raw.when ?? raw.require),
160 effects: normalizeEffects(raw.effects)
161 };
162}
163
164function normalizeBranch (raw)
165{
166 if (!raw || typeof raw !== "object")
167 { return null; }
168 const next = raw.nextStepId == null || raw.nextStepId === ""
169 ? null
170 : slugId(raw.nextStepId, "");
171 return {
172 label: String(raw.label ?? "").trim().slice(0, 80),
173 when: normalizeConditionSet(raw.when ?? raw.require),
174 nextStepId: next || null,
175 effects: normalizeEffects(raw.effects)
176 };
177}
178
179function slugId (raw, fallback)
180{
181 const s = String(raw ?? "")
182 .trim()
183 .toLowerCase()
184 .replace(/[^a-z0-9_-]+/g, "-")
185 .replace(/^-+|-+$/g, "")
186 .slice(0, 64);
187 return s || fallback;
188}