| 1 | import { normalizeQuestDef } from "/shared/quests/normalize.js"; |
| 2 | import { ConditionKind, emptyConditionSet, normalizeConditionSet } from "/shared/quests/conditions.js"; |
| 3 | |
| 4 | const SIMPLE_KINDS = [ |
| 5 | ConditionKind.hasItem, |
| 6 | ConditionKind.questState, |
| 7 | ConditionKind.dialogueHeard, |
| 8 | ConditionKind.killCount, |
| 9 | ConditionKind.atTile, |
| 10 | ConditionKind.inDimension, |
| 11 | ConditionKind.hasVisitedDimension |
| 12 | ]; |
| 13 | |
| 14 | export function initQuestsModal ({ socket, MessageType }) |
| 15 | { |
| 16 | const backdrop = document.getElementById("quests-backdrop"); |
| 17 | const modal = document.getElementById("quests-modal"); |
| 18 | const listEl = document.getElementById("quests-list"); |
| 19 | const editorEl = document.getElementById("quests-editor"); |
| 20 | const saveBtn = document.getElementById("quests-save"); |
| 21 | const deleteBtn = document.getElementById("quests-delete"); |
| 22 | let open = false; |
| 23 | /** @type {ReturnType<typeof normalizeQuestDef>[]} */ |
| 24 | let quests = []; |
| 25 | /** @type {ReturnType<typeof normalizeQuestDef>|null} */ |
| 26 | let draft = null; |
| 27 | let selectedId = null; |
| 28 | |
| 29 | function show (visible) |
| 30 | { |
| 31 | open = visible; |
| 32 | backdrop.hidden = !visible; |
| 33 | modal.hidden = !visible; |
| 34 | if (!visible) |
| 35 | { |
| 36 | draft = null; |
| 37 | selectedId = null; |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | function selectQuest (questId) |
| 42 | { |
| 43 | const found = quests.find((q) => q.questId === questId); |
| 44 | if (!found) |
| 45 | { |
| 46 | draft = null; |
| 47 | selectedId = null; |
| 48 | render(); |
| 49 | return; |
| 50 | } |
| 51 | selectedId = found.questId; |
| 52 | draft = structuredClone(found); |
| 53 | render(); |
| 54 | } |
| 55 | |
| 56 | function renderList () |
| 57 | { |
| 58 | listEl.replaceChildren(); |
| 59 | for (const quest of quests) |
| 60 | { |
| 61 | const btn = document.createElement("button"); |
| 62 | btn.type = "button"; |
| 63 | btn.textContent = quest.title || quest.questId; |
| 64 | if (quest.questId === selectedId) |
| 65 | { btn.classList.add("active"); } |
| 66 | btn.addEventListener("click", () => selectQuest(quest.questId)); |
| 67 | listEl.append(btn); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | function renderEditor () |
| 72 | { |
| 73 | editorEl.replaceChildren(); |
| 74 | saveBtn.disabled = !draft; |
| 75 | deleteBtn.disabled = !draft; |
| 76 | if (!draft) |
| 77 | { |
| 78 | const hint = document.createElement("p"); |
| 79 | hint.className = "content-editor-hint"; |
| 80 | hint.textContent = "Select a quest or create a new one."; |
| 81 | editorEl.append(hint); |
| 82 | return; |
| 83 | } |
| 84 | |
| 85 | editorEl.append( |
| 86 | field("Quest id", textInput(draft.questId, (v) => { draft.questId = v; })), |
| 87 | field("Title", textInput(draft.title, (v) => { draft.title = v; })), |
| 88 | field("Description", textArea(draft.description, (v) => { draft.description = v; }, 3)) |
| 89 | ); |
| 90 | |
| 91 | const stepsHeader = sectionHeader("Steps", () => |
| 92 | { |
| 93 | draft.steps.push({ |
| 94 | stepId: `step-${draft.steps.length + 1}`, |
| 95 | title: "New step", |
| 96 | description: "", |
| 97 | completionJournalText: "", |
| 98 | objectives: [{ |
| 99 | objectiveId: "obj-1", |
| 100 | text: "Complete this step", |
| 101 | when: emptyConditionSet() |
| 102 | }], |
| 103 | onEnter: [], |
| 104 | onComplete: [] |
| 105 | }); |
| 106 | renderEditor(); |
| 107 | }); |
| 108 | editorEl.append(stepsHeader); |
| 109 | |
| 110 | draft.steps.forEach((step, stepIndex) => |
| 111 | { |
| 112 | const box = document.createElement("div"); |
| 113 | box.className = "content-editor-section"; |
| 114 | const head = sectionHeader(`Step ${stepIndex + 1}`, null, () => |
| 115 | { |
| 116 | draft.steps.splice(stepIndex, 1); |
| 117 | if (draft.steps.length === 0) |
| 118 | { |
| 119 | draft.steps.push(normalizeQuestDef({ questId: draft.questId }).steps[0]); |
| 120 | } |
| 121 | renderEditor(); |
| 122 | }); |
| 123 | box.append(head); |
| 124 | box.append( |
| 125 | field("stepId", textInput(step.stepId, (v) => { step.stepId = v; })), |
| 126 | field("Title", textInput(step.title, (v) => { step.title = v; })), |
| 127 | field("Description", textArea(step.description, (v) => { step.description = v; }, 2)), |
| 128 | field("Completion journal", textArea(step.completionJournalText, (v) => |
| 129 | { |
| 130 | step.completionJournalText = v; |
| 131 | }, 2)) |
| 132 | ); |
| 133 | |
| 134 | const objHead = sectionHeader("Objectives", () => |
| 135 | { |
| 136 | step.objectives.push({ |
| 137 | objectiveId: `obj-${step.objectives.length + 1}`, |
| 138 | text: "New objective", |
| 139 | when: emptyConditionSet() |
| 140 | }); |
| 141 | renderEditor(); |
| 142 | }); |
| 143 | box.append(objHead); |
| 144 | |
| 145 | step.objectives.forEach((obj, objIndex) => |
| 146 | { |
| 147 | const objBox = document.createElement("div"); |
| 148 | objBox.className = "content-editor-section"; |
| 149 | objBox.append( |
| 150 | sectionHeader(`Objective ${objIndex + 1}`, null, () => |
| 151 | { |
| 152 | step.objectives.splice(objIndex, 1); |
| 153 | if (step.objectives.length === 0) |
| 154 | { |
| 155 | step.objectives.push({ |
| 156 | objectiveId: "obj-1", |
| 157 | text: "Complete this step", |
| 158 | when: emptyConditionSet() |
| 159 | }); |
| 160 | } |
| 161 | renderEditor(); |
| 162 | }), |
| 163 | field("Text", textInput(obj.text, (v) => { obj.text = v; })) |
| 164 | ); |
| 165 | objBox.append(renderConditionSet(obj.when, (next) => |
| 166 | { |
| 167 | obj.when = next; |
| 168 | renderEditor(); |
| 169 | })); |
| 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 | show(true); |
| 186 | render(); |
| 187 | }); |
| 188 | document.getElementById("quests-close").addEventListener("click", () => show(false)); |
| 189 | backdrop.addEventListener("click", () => show(false)); |
| 190 | document.getElementById("quests-new").addEventListener("click", () => |
| 191 | { |
| 192 | const base = normalizeQuestDef({ |
| 193 | questId: `quest-${quests.length + 1}`, |
| 194 | title: "New quest", |
| 195 | description: "" |
| 196 | }); |
| 197 | draft = structuredClone(base); |
| 198 | selectedId = null; |
| 199 | render(); |
| 200 | }); |
| 201 | saveBtn.addEventListener("click", () => |
| 202 | { |
| 203 | if (!draft) |
| 204 | { return; } |
| 205 | socket.send({ type: MessageType.editorSaveQuest, quest: draft }); |
| 206 | }); |
| 207 | deleteBtn.addEventListener("click", () => |
| 208 | { |
| 209 | if (!draft?.questId) |
| 210 | { return; } |
| 211 | if (!window.confirm(`Delete quest "${draft.questId}"?`)) |
| 212 | { return; } |
| 213 | socket.send({ type: MessageType.editorDeleteQuest, questId: draft.questId }); |
| 214 | }); |
| 215 | |
| 216 | return { |
| 217 | isOpen: () => open, |
| 218 | handleMessage (message) |
| 219 | { |
| 220 | if (message.type !== MessageType.editorQuests) |
| 221 | { return; } |
| 222 | quests = (message.quests ?? []).map((q) => normalizeQuestDef(q)); |
| 223 | if (draft) |
| 224 | { |
| 225 | const still = quests.find((q) => q.questId === draft.questId); |
| 226 | if (still) |
| 227 | { |
| 228 | selectedId = still.questId; |
| 229 | draft = structuredClone(still); |
| 230 | } |
| 231 | else if (selectedId) |
| 232 | { |
| 233 | draft = null; |
| 234 | selectedId = null; |
| 235 | } |
| 236 | } |
| 237 | if (open) |
| 238 | { render(); } |
| 239 | } |
| 240 | }; |
| 241 | } |
| 242 | |
| 243 | function field (label, input) |
| 244 | { |
| 245 | const wrap = document.createElement("label"); |
| 246 | wrap.append(document.createTextNode(label), input); |
| 247 | return wrap; |
| 248 | } |
| 249 | |
| 250 | function textInput (value, onChange) |
| 251 | { |
| 252 | const input = document.createElement("input"); |
| 253 | input.type = "text"; |
| 254 | input.value = value ?? ""; |
| 255 | input.addEventListener("input", () => onChange(input.value)); |
| 256 | return input; |
| 257 | } |
| 258 | |
| 259 | function textArea (value, onChange, rows = 3) |
| 260 | { |
| 261 | const input = document.createElement("textarea"); |
| 262 | input.rows = rows; |
| 263 | input.value = value ?? ""; |
| 264 | input.addEventListener("input", () => onChange(input.value)); |
| 265 | return input; |
| 266 | } |
| 267 | |
| 268 | function sectionHeader (title, onAdd, onRemove) |
| 269 | { |
| 270 | const head = document.createElement("div"); |
| 271 | head.className = "content-editor-section-header"; |
| 272 | const strong = document.createElement("strong"); |
| 273 | strong.textContent = title; |
| 274 | head.append(strong); |
| 275 | const actions = document.createElement("div"); |
| 276 | actions.className = "content-editor-row"; |
| 277 | if (onAdd) |
| 278 | { |
| 279 | const add = document.createElement("button"); |
| 280 | add.type = "button"; |
| 281 | add.textContent = "+"; |
| 282 | add.title = "Add"; |
| 283 | add.addEventListener("click", onAdd); |
| 284 | actions.append(add); |
| 285 | } |
| 286 | if (onRemove) |
| 287 | { |
| 288 | const remove = document.createElement("button"); |
| 289 | remove.type = "button"; |
| 290 | remove.textContent = "×"; |
| 291 | remove.title = "Remove"; |
| 292 | remove.addEventListener("click", onRemove); |
| 293 | actions.append(remove); |
| 294 | } |
| 295 | head.append(actions); |
| 296 | return head; |
| 297 | } |
| 298 | |
| 299 | function renderConditionSet (when, onChange) |
| 300 | { |
| 301 | const wrap = document.createElement("div"); |
| 302 | const set = normalizeConditionSet(when); |
| 303 | const title = document.createElement("strong"); |
| 304 | title.textContent = "When (all of)"; |
| 305 | wrap.append(title); |
| 306 | |
| 307 | set.all.forEach((clause, index) => |
| 308 | { |
| 309 | const row = document.createElement("div"); |
| 310 | row.className = "content-editor-clause"; |
| 311 | const notLabel = document.createElement("label"); |
| 312 | const notBox = document.createElement("input"); |
| 313 | notBox.type = "checkbox"; |
| 314 | notBox.checked = !!clause.not; |
| 315 | notBox.addEventListener("change", () => |
| 316 | { |
| 317 | set.all[index].not = notBox.checked; |
| 318 | onChange(set); |
| 319 | }); |
| 320 | notLabel.append(notBox, document.createTextNode(" NOT")); |
| 321 | row.append(notLabel); |
| 322 | |
| 323 | const kind = document.createElement("select"); |
| 324 | for (const k of SIMPLE_KINDS) |
| 325 | { |
| 326 | const opt = document.createElement("option"); |
| 327 | opt.value = k; |
| 328 | opt.textContent = k; |
| 329 | if ((clause.condition?.kind ?? "") === k) |
| 330 | { opt.selected = true; } |
| 331 | kind.append(opt); |
| 332 | } |
| 333 | const otherOpt = document.createElement("option"); |
| 334 | otherOpt.value = "__other__"; |
| 335 | otherOpt.textContent = "(other / JSON)"; |
| 336 | if (!SIMPLE_KINDS.includes(clause.condition?.kind)) |
| 337 | { otherOpt.selected = true; } |
| 338 | kind.append(otherOpt); |
| 339 | kind.addEventListener("change", () => |
| 340 | { |
| 341 | if (kind.value === "__other__") |
| 342 | { return; } |
| 343 | set.all[index].condition = defaultCondition(kind.value); |
| 344 | onChange(set); |
| 345 | }); |
| 346 | row.append(kind); |
| 347 | |
| 348 | const cond = clause.condition ?? { kind: ConditionKind.hasItem }; |
| 349 | if (cond.kind === ConditionKind.hasItem) |
| 350 | { |
| 351 | row.append(miniField("itemId", cond.itemId ?? "", (v) => |
| 352 | { |
| 353 | cond.itemId = Number(v) || 0; |
| 354 | set.all[index].condition = cond; |
| 355 | onChange(set); |
| 356 | })); |
| 357 | row.append(miniField("qty", cond.quantity ?? 1, (v) => |
| 358 | { |
| 359 | cond.quantity = Number(v) || 1; |
| 360 | set.all[index].condition = cond; |
| 361 | onChange(set); |
| 362 | })); |
| 363 | } |
| 364 | else if (cond.kind === ConditionKind.questState) |
| 365 | { |
| 366 | row.append(miniField("questId", cond.questId ?? "", (v) => |
| 367 | { |
| 368 | cond.questId = v; |
| 369 | set.all[index].condition = cond; |
| 370 | onChange(set); |
| 371 | })); |
| 372 | row.append(miniField("state", cond.state ?? "not-started", (v) => |
| 373 | { |
| 374 | const n = Number(v); |
| 375 | cond.state = Number.isFinite(n) && String(n) === String(v).trim() ? n : v; |
| 376 | set.all[index].condition = cond; |
| 377 | onChange(set); |
| 378 | })); |
| 379 | } |
| 380 | else if (cond.kind === ConditionKind.dialogueHeard) |
| 381 | { |
| 382 | row.append(miniField("dialogueId", cond.dialogueId ?? "", (v) => |
| 383 | { |
| 384 | cond.dialogueId = v; |
| 385 | set.all[index].condition = cond; |
| 386 | onChange(set); |
| 387 | })); |
| 388 | row.append(miniField("stepId", cond.stepId ?? "", (v) => |
| 389 | { |
| 390 | cond.stepId = v; |
| 391 | set.all[index].condition = cond; |
| 392 | onChange(set); |
| 393 | })); |
| 394 | } |
| 395 | else if (cond.kind === ConditionKind.killCount) |
| 396 | { |
| 397 | row.append(miniField("mobTypeId", cond.mobTypeId ?? "", (v) => |
| 398 | { |
| 399 | cond.mobTypeId = v || undefined; |
| 400 | set.all[index].condition = cond; |
| 401 | onChange(set); |
| 402 | })); |
| 403 | row.append(miniField("count", cond.count ?? 1, (v) => |
| 404 | { |
| 405 | cond.count = Number(v) || 1; |
| 406 | set.all[index].condition = cond; |
| 407 | onChange(set); |
| 408 | })); |
| 409 | } |
| 410 | else if (cond.kind === ConditionKind.atTile) |
| 411 | { |
| 412 | row.append(miniField("dimensionId", cond.dimensionId ?? "overworld", (v) => |
| 413 | { |
| 414 | cond.dimensionId = String(v || "overworld"); |
| 415 | set.all[index].condition = cond; |
| 416 | onChange(set); |
| 417 | })); |
| 418 | row.append(miniField("tileX", cond.tileX ?? 0, (v) => |
| 419 | { |
| 420 | cond.tileX = Number(v) | 0; |
| 421 | set.all[index].condition = cond; |
| 422 | onChange(set); |
| 423 | })); |
| 424 | row.append(miniField("tileY", cond.tileY ?? 0, (v) => |
| 425 | { |
| 426 | cond.tileY = Number(v) | 0; |
| 427 | set.all[index].condition = cond; |
| 428 | onChange(set); |
| 429 | })); |
| 430 | } |
| 431 | else if ( |
| 432 | cond.kind === ConditionKind.inDimension |
| 433 | || cond.kind === ConditionKind.hasVisitedDimension |
| 434 | ) |
| 435 | { |
| 436 | row.append(miniField("dimensionId", cond.dimensionId ?? "overworld", (v) => |
| 437 | { |
| 438 | cond.dimensionId = String(v || "overworld"); |
| 439 | set.all[index].condition = cond; |
| 440 | onChange(set); |
| 441 | })); |
| 442 | } |
| 443 | else |
| 444 | { |
| 445 | const json = document.createElement("textarea"); |
| 446 | json.rows = 3; |
| 447 | json.value = JSON.stringify(cond, null, 2); |
| 448 | json.addEventListener("change", () => |
| 449 | { |
| 450 | try |
| 451 | { |
| 452 | set.all[index].condition = JSON.parse(json.value); |
| 453 | onChange(set); |
| 454 | } |
| 455 | catch |
| 456 | { /* keep typing */ } |
| 457 | }); |
| 458 | row.append(json); |
| 459 | } |
| 460 | |
| 461 | const remove = document.createElement("button"); |
| 462 | remove.type = "button"; |
| 463 | remove.textContent = "×"; |
| 464 | remove.addEventListener("click", () => |
| 465 | { |
| 466 | set.all.splice(index, 1); |
| 467 | onChange(set); |
| 468 | }); |
| 469 | row.append(remove); |
| 470 | wrap.append(row); |
| 471 | }); |
| 472 | |
| 473 | const add = document.createElement("button"); |
| 474 | add.type = "button"; |
| 475 | add.textContent = "Add clause"; |
| 476 | add.addEventListener("click", () => |
| 477 | { |
| 478 | set.all.push({ not: false, condition: defaultCondition(ConditionKind.hasItem) }); |
| 479 | onChange(set); |
| 480 | }); |
| 481 | wrap.append(add); |
| 482 | |
| 483 | const jsonFallback = document.createElement("details"); |
| 484 | const summary = document.createElement("summary"); |
| 485 | summary.textContent = "Edit condition set as JSON"; |
| 486 | const area = document.createElement("textarea"); |
| 487 | area.rows = 5; |
| 488 | area.value = JSON.stringify(set, null, 2); |
| 489 | area.addEventListener("change", () => |
| 490 | { |
| 491 | try |
| 492 | { |
| 493 | onChange(normalizeConditionSet(JSON.parse(area.value))); |
| 494 | } |
| 495 | catch |
| 496 | { /* ignore */ } |
| 497 | }); |
| 498 | jsonFallback.append(summary, area); |
| 499 | wrap.append(jsonFallback); |
| 500 | return wrap; |
| 501 | } |
| 502 | |
| 503 | function miniField (label, value, onChange) |
| 504 | { |
| 505 | const wrap = document.createElement("label"); |
| 506 | wrap.textContent = `${label} `; |
| 507 | const input = document.createElement("input"); |
| 508 | input.type = "text"; |
| 509 | input.value = String(value ?? ""); |
| 510 | input.style.width = "7rem"; |
| 511 | input.addEventListener("input", () => onChange(input.value)); |
| 512 | wrap.append(input); |
| 513 | return wrap; |
| 514 | } |
| 515 | |
| 516 | function defaultCondition (kind) |
| 517 | { |
| 518 | switch (kind) |
| 519 | { |
| 520 | case ConditionKind.hasItem: |
| 521 | return { kind, itemId: 1, quantity: 1 }; |
| 522 | case ConditionKind.questState: |
| 523 | return { kind, questId: "", state: "not-started" }; |
| 524 | case ConditionKind.dialogueHeard: |
| 525 | return { kind, dialogueId: "", stepId: "" }; |
| 526 | case ConditionKind.killCount: |
| 527 | return { kind, mobTypeId: "deer", count: 1 }; |
| 528 | case ConditionKind.atTile: |
| 529 | return { kind, dimensionId: "overworld", tileX: 0, tileY: 0 }; |
| 530 | case ConditionKind.inDimension: |
| 531 | case ConditionKind.hasVisitedDimension: |
| 532 | return { kind, dimensionId: "overworld" }; |
| 533 | default: |
| 534 | return { kind }; |
| 535 | } |
| 536 | } |