BlueGit

timfa questward

Files

questward / shared / quests / normalize.js

normalize.js

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