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