BlueGit

timfa questward

Files

questward / shared / quests / normalize.js

normalize.js

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