| 1 | import { normalizeQuestDef, normalizeDialogueTree } from "/shared/quests/normalize.js"; |
| 2 | import { emptyConditionSet } from "/shared/quests/conditions.js"; |
| 3 | import { renderConditionSet } from "./conditionSetEditor.js"; |
| 4 | |
| 5 | export function initQuestsModal ({ |
| 6 | socket, |
| 7 | MessageType, |
| 8 | describeWorldTile = null, |
| 9 | centerEditorOnTile = null, |
| 10 | getDimensionOptions = null |
| 11 | }) |
| 12 | { |
| 13 | const backdrop = document.getElementById("quests-backdrop"); |
| 14 | const modal = document.getElementById("quests-modal"); |
| 15 | const listEl = document.getElementById("quests-list"); |
| 16 | const editorEl = document.getElementById("quests-editor"); |
| 17 | const saveBtn = document.getElementById("quests-save"); |
| 18 | const deleteBtn = document.getElementById("quests-delete"); |
| 19 | let open = false; |
| 20 | /** @type {ReturnType<typeof normalizeQuestDef>[]} */ |
| 21 | let quests = []; |
| 22 | /** @type {ReturnType<typeof normalizeDialogueTree>[]} */ |
| 23 | let dialogues = []; |
| 24 | /** @type {ReturnType<typeof normalizeQuestDef>|null} */ |
| 25 | let draft = null; |
| 26 | let selectedId = null; |
| 27 | |
| 28 | function show (visible) |
| 29 | { |
| 30 | open = visible; |
| 31 | backdrop.hidden = !visible; |
| 32 | modal.hidden = !visible; |
| 33 | if (!visible) |
| 34 | { |
| 35 | draft = null; |
| 36 | selectedId = null; |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | function selectQuest (questId) |
| 41 | { |
| 42 | const found = quests.find((q) => q.questId === questId); |
| 43 | if (!found) |
| 44 | { |
| 45 | draft = null; |
| 46 | selectedId = null; |
| 47 | render(); |
| 48 | return; |
| 49 | } |
| 50 | selectedId = found.questId; |
| 51 | draft = structuredClone(found); |
| 52 | render(); |
| 53 | } |
| 54 | |
| 55 | function renderList () |
| 56 | { |
| 57 | listEl.replaceChildren(); |
| 58 | for (const quest of quests) |
| 59 | { |
| 60 | const btn = document.createElement("button"); |
| 61 | btn.type = "button"; |
| 62 | btn.textContent = quest.title || quest.questId; |
| 63 | if (quest.questId === selectedId) |
| 64 | { btn.classList.add("active"); } |
| 65 | btn.addEventListener("click", () => selectQuest(quest.questId)); |
| 66 | listEl.append(btn); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | function renderEditor () |
| 71 | { |
| 72 | editorEl.replaceChildren(); |
| 73 | saveBtn.disabled = !draft; |
| 74 | deleteBtn.disabled = !draft; |
| 75 | if (!draft) |
| 76 | { |
| 77 | const hint = document.createElement("p"); |
| 78 | hint.className = "content-editor-hint"; |
| 79 | hint.textContent = "Select a quest or create a new one."; |
| 80 | editorEl.append(hint); |
| 81 | return; |
| 82 | } |
| 83 | |
| 84 | editorEl.append( |
| 85 | field("Quest id", textInput(draft.questId, (v) => { draft.questId = v; })), |
| 86 | field("Title", textInput(draft.title, (v) => { draft.title = v; })), |
| 87 | field("Description", textArea(draft.description, (v) => { draft.description = v; }, 3)) |
| 88 | ); |
| 89 | |
| 90 | const stepsHeader = sectionHeader("Steps", () => |
| 91 | { |
| 92 | draft.steps.push({ |
| 93 | stepId: `step-${draft.steps.length + 1}`, |
| 94 | title: "New step", |
| 95 | description: "", |
| 96 | completionJournalText: "", |
| 97 | objectives: [{ |
| 98 | objectiveId: "obj-1", |
| 99 | text: "Complete this step", |
| 100 | when: emptyConditionSet() |
| 101 | }], |
| 102 | onEnter: [], |
| 103 | onComplete: [] |
| 104 | }); |
| 105 | renderEditor(); |
| 106 | }); |
| 107 | editorEl.append(stepsHeader); |
| 108 | |
| 109 | draft.steps.forEach((step, stepIndex) => |
| 110 | { |
| 111 | const box = document.createElement("div"); |
| 112 | box.className = "content-editor-section"; |
| 113 | const head = sectionHeader(`Step ${stepIndex + 1}`, null, () => |
| 114 | { |
| 115 | draft.steps.splice(stepIndex, 1); |
| 116 | if (draft.steps.length === 0) |
| 117 | { |
| 118 | draft.steps.push(normalizeQuestDef({ questId: draft.questId }).steps[0]); |
| 119 | } |
| 120 | renderEditor(); |
| 121 | }); |
| 122 | box.append(head); |
| 123 | box.append( |
| 124 | field("stepId", textInput(step.stepId, (v) => { step.stepId = v; })), |
| 125 | field("Title", textInput(step.title, (v) => { step.title = v; })), |
| 126 | field("Description", textArea(step.description, (v) => { step.description = v; }, 2)), |
| 127 | field("Completion journal", textArea(step.completionJournalText, (v) => |
| 128 | { |
| 129 | step.completionJournalText = v; |
| 130 | }, 2)) |
| 131 | ); |
| 132 | |
| 133 | const objHead = sectionHeader("Objectives", () => |
| 134 | { |
| 135 | step.objectives.push({ |
| 136 | objectiveId: `obj-${step.objectives.length + 1}`, |
| 137 | text: "New objective", |
| 138 | when: emptyConditionSet() |
| 139 | }); |
| 140 | renderEditor(); |
| 141 | }); |
| 142 | box.append(objHead); |
| 143 | |
| 144 | step.objectives.forEach((obj, objIndex) => |
| 145 | { |
| 146 | const objBox = document.createElement("div"); |
| 147 | objBox.className = "content-editor-section"; |
| 148 | objBox.append( |
| 149 | sectionHeader(`Objective ${objIndex + 1}`, null, () => |
| 150 | { |
| 151 | step.objectives.splice(objIndex, 1); |
| 152 | if (step.objectives.length === 0) |
| 153 | { |
| 154 | step.objectives.push({ |
| 155 | objectiveId: "obj-1", |
| 156 | text: "Complete this step", |
| 157 | when: emptyConditionSet() |
| 158 | }); |
| 159 | } |
| 160 | renderEditor(); |
| 161 | }), |
| 162 | field("Text", textInput(obj.text, (v) => { obj.text = v; })) |
| 163 | ); |
| 164 | objBox.append(renderConditionSet(obj.when, (next, { remount = true } = {}) => |
| 165 | { |
| 166 | obj.when = next; |
| 167 | if (remount) |
| 168 | { renderEditor(); } |
| 169 | }, { quests, dialogues, describeWorldTile, centerEditorOnTile, getDimensionOptions })); |
| 170 | box.append(objBox); |
| 171 | }); |
| 172 | editorEl.append(box); |
| 173 | }); |
| 174 | } |
| 175 | |
| 176 | function render () |
| 177 | { |
| 178 | renderList(); |
| 179 | renderEditor(); |
| 180 | } |
| 181 | |
| 182 | document.getElementById("open-quests").addEventListener("click", () => |
| 183 | { |
| 184 | socket.send({ type: MessageType.editorLoadQuests }); |
| 185 | socket.send({ type: MessageType.editorLoadDialogues }); |
| 186 | show(true); |
| 187 | render(); |
| 188 | }); |
| 189 | document.getElementById("quests-close").addEventListener("click", () => show(false)); |
| 190 | backdrop.addEventListener("click", () => show(false)); |
| 191 | document.getElementById("quests-new").addEventListener("click", () => |
| 192 | { |
| 193 | const base = normalizeQuestDef({ |
| 194 | questId: `quest-${quests.length + 1}`, |
| 195 | title: "New quest", |
| 196 | description: "" |
| 197 | }); |
| 198 | draft = structuredClone(base); |
| 199 | selectedId = null; |
| 200 | render(); |
| 201 | }); |
| 202 | saveBtn.addEventListener("click", () => |
| 203 | { |
| 204 | if (!draft) |
| 205 | { return; } |
| 206 | socket.send({ type: MessageType.editorSaveQuest, quest: draft }); |
| 207 | }); |
| 208 | deleteBtn.addEventListener("click", () => |
| 209 | { |
| 210 | if (!draft?.questId) |
| 211 | { return; } |
| 212 | if (!window.confirm(`Delete quest "${draft.questId}"?`)) |
| 213 | { return; } |
| 214 | socket.send({ type: MessageType.editorDeleteQuest, questId: draft.questId }); |
| 215 | }); |
| 216 | |
| 217 | return { |
| 218 | isOpen: () => open, |
| 219 | getQuests: () => quests, |
| 220 | handleMessage (message) |
| 221 | { |
| 222 | if (message.type === MessageType.editorDialogues) |
| 223 | { |
| 224 | dialogues = (message.dialogues ?? []).map((d) => normalizeDialogueTree(d)); |
| 225 | if (open) |
| 226 | { render(); } |
| 227 | return; |
| 228 | } |
| 229 | if (message.type !== MessageType.editorQuests) |
| 230 | { return; } |
| 231 | quests = (message.quests ?? []).map((q) => normalizeQuestDef(q)); |
| 232 | if (draft) |
| 233 | { |
| 234 | const still = quests.find((q) => q.questId === draft.questId); |
| 235 | if (still) |
| 236 | { |
| 237 | selectedId = still.questId; |
| 238 | draft = structuredClone(still); |
| 239 | } |
| 240 | else if (selectedId) |
| 241 | { |
| 242 | draft = null; |
| 243 | selectedId = null; |
| 244 | } |
| 245 | } |
| 246 | if (open) |
| 247 | { render(); } |
| 248 | } |
| 249 | }; |
| 250 | } |
| 251 | |
| 252 | function field (label, input) |
| 253 | { |
| 254 | const wrap = document.createElement("label"); |
| 255 | wrap.append(document.createTextNode(label), input); |
| 256 | return wrap; |
| 257 | } |
| 258 | |
| 259 | function textInput (value, onChange) |
| 260 | { |
| 261 | const input = document.createElement("input"); |
| 262 | input.type = "text"; |
| 263 | input.value = value ?? ""; |
| 264 | input.addEventListener("input", () => onChange(input.value)); |
| 265 | return input; |
| 266 | } |
| 267 | |
| 268 | function textArea (value, onChange, rows = 3) |
| 269 | { |
| 270 | const input = document.createElement("textarea"); |
| 271 | input.rows = rows; |
| 272 | input.value = value ?? ""; |
| 273 | input.addEventListener("input", () => onChange(input.value)); |
| 274 | return input; |
| 275 | } |
| 276 | |
| 277 | function sectionHeader (title, onAdd, onRemove) |
| 278 | { |
| 279 | const head = document.createElement("div"); |
| 280 | head.className = "content-editor-section-header"; |
| 281 | const strong = document.createElement("strong"); |
| 282 | strong.textContent = title; |
| 283 | head.append(strong); |
| 284 | const actions = document.createElement("div"); |
| 285 | actions.className = "content-editor-row"; |
| 286 | if (onAdd) |
| 287 | { |
| 288 | const add = document.createElement("button"); |
| 289 | add.type = "button"; |
| 290 | add.textContent = "+"; |
| 291 | add.title = "Add"; |
| 292 | add.addEventListener("click", onAdd); |
| 293 | actions.append(add); |
| 294 | } |
| 295 | if (onRemove) |
| 296 | { |
| 297 | const remove = document.createElement("button"); |
| 298 | remove.type = "button"; |
| 299 | remove.textContent = "×"; |
| 300 | remove.title = "Remove"; |
| 301 | remove.addEventListener("click", onRemove); |
| 302 | actions.append(remove); |
| 303 | } |
| 304 | head.append(actions); |
| 305 | return head; |
| 306 | } |
| 307 | |
| 308 | |