BlueGit

timfa questward

Files

questward / shared / quests / normalize.js

normalize.js

normalize.js 6.9 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 const out = {
84 stepId,
85 label: String(raw?.label ?? stepId).trim().slice(0, 80) || stepId,
86 lines: lines.length > 0 ? lines : [{ speaker: "npc", text: "…", effects: [] }],
87 itemRewards: normalizeItemRewards(raw?.itemRewards),
88 itemTakes: normalizeItemRewards(raw?.itemTakes),
89 questRewards: normalizeQuestRewards(raw?.questRewards),
90 exit
91 };
92 const editorX = Number(raw?.editorX);
93 const editorY = Number(raw?.editorY);
94 if (Number.isFinite(editorX))
95 { out.editorX = editorX; }
96 if (Number.isFinite(editorY))
97 { out.editorY = editorY; }
98 return out;
99}
100
101/** Step-level gifts granted after all lines are read, before exit triggers/branches. */
102function normalizeItemRewards (raw)
103{
104 if (!Array.isArray(raw))
105 { return []; }
106 const out = [];
107 const seen = new Set();
108 for (const entry of raw)
109 {
110 if (!entry || typeof entry !== "object")
111 { continue; }
112 const itemId = Number(entry.itemId);
113 if (!Number.isFinite(itemId) || itemId <= 0 || seen.has(itemId))
114 { continue; }
115 seen.add(itemId);
116 out.push({
117 itemId: itemId | 0,
118 quantity: Math.max(1, Math.min(99_999, Number(entry.quantity) || 1)) | 0
119 });
120 }
121 return out;
122}
123
124/** Step-level quests started after lines (same moment as item rewards). */
125function normalizeQuestRewards (raw)
126{
127 if (!Array.isArray(raw))
128 { return []; }
129 const out = [];
130 const seen = new Set();
131 for (const entry of raw)
132 {
133 const questId = String(entry?.questId ?? entry ?? "").trim().slice(0, 80);
134 if (!questId || seen.has(questId))
135 { continue; }
136 seen.add(questId);
137 out.push(questId);
138 }
139 return out;
140}
141
142function normalizeDialogueLine (raw)
143{
144 if (!raw || typeof raw !== "object")
145 { return null; }
146 const text = String(raw.text ?? "").trim().slice(0, 500);
147 if (!text)
148 { return null; }
149 const speaker = raw.speaker === "player" ? "player" : "npc";
150 return { speaker, text, effects: normalizeEffects(raw.effects) };
151}
152
153function normalizeExit (exit, legacy)
154{
155 if (exit && typeof exit === "object" && exit.kind)
156 {
157 if (exit.kind === "triggers")
158 {
159 return {
160 kind: "triggers",
161 triggers: (Array.isArray(exit.triggers) ? exit.triggers : [])
162 .map(normalizeTrigger)
163 .filter(Boolean)
164 };
165 }
166 if (exit.kind === "branches")
167 {
168 return {
169 kind: "branches",
170 branches: (Array.isArray(exit.branches) ? exit.branches : [])
171 .map(normalizeBranch)
172 .filter(Boolean)
173 };
174 }
175 if (exit.kind === "continue")
176 {
177 const next = exit.nextStepId == null || exit.nextStepId === ""
178 ? null
179 : slugId(exit.nextStepId, "");
180 return {
181 kind: "continue",
182 nextStepId: next || null
183 };
184 }
185 return { kind: "end" };
186 }
187 if (Array.isArray(legacy?.triggers) && legacy.triggers.length > 0)
188 {
189 return {
190 kind: "triggers",
191 triggers: legacy.triggers.map(normalizeTrigger).filter(Boolean)
192 };
193 }
194 if (Array.isArray(legacy?.branches) && legacy.branches.length > 0)
195 {
196 return {
197 kind: "branches",
198 branches: legacy.branches.map(normalizeBranch).filter(Boolean)
199 };
200 }
201 return { kind: "end" };
202}
203
204function normalizeTrigger (raw)
205{
206 if (!raw || typeof raw !== "object")
207 { return null; }
208 const trigger = slugId(raw.trigger ?? raw.id, "choice");
209 const text = String(raw.text ?? "").trim().slice(0, 120);
210 if (!text)
211 { return null; }
212 const next = raw.nextStepId == null || raw.nextStepId === ""
213 ? null
214 : slugId(raw.nextStepId, "");
215 return {
216 trigger,
217 label: String(raw.label ?? trigger).trim().slice(0, 80) || trigger,
218 text,
219 nextStepId: next || null,
220 when: normalizeConditionSet(raw.when ?? raw.require),
221 effects: normalizeEffects(raw.effects)
222 };
223}
224
225function normalizeBranch (raw)
226{
227 if (!raw || typeof raw !== "object")
228 { return null; }
229 const next = raw.nextStepId == null || raw.nextStepId === ""
230 ? null
231 : slugId(raw.nextStepId, "");
232 return {
233 label: String(raw.label ?? "").trim().slice(0, 80),
234 when: normalizeConditionSet(raw.when ?? raw.require),
235 nextStepId: next || null,
236 effects: normalizeEffects(raw.effects)
237 };
238}
239
240function slugId (raw, fallback)
241{
242 const s = String(raw ?? "")
243 .trim()
244 .toLowerCase()
245 .replace(/[^a-z0-9_-]+/g, "-")
246 .replace(/^-+|-+$/g, "")
247 .slice(0, 64);
248 return s || fallback;
249}