| 1 | import { normalizeQuestDef, normalizeDialogueTree } from "/shared/quests/normalize.js"; |
| 2 | import { ConditionKind, emptyConditionSet, normalizeConditionSet } from "/shared/quests/conditions.js"; |
| 3 | import { itemDefinition, listAllItemPickerEntries } from "/shared/definitions/items.js"; |
| 4 | |
| 5 | const SIMPLE_KINDS = [ |
| 6 | ConditionKind.hasItem, |
| 7 | ConditionKind.questState, |
| 8 | ConditionKind.dialogueHeard, |
| 9 | ConditionKind.killCount, |
| 10 | ConditionKind.atTile, |
| 11 | ConditionKind.inDimension, |
| 12 | ConditionKind.hasVisitedDimension |
| 13 | ]; |
| 14 | |
| 15 | export function initQuestsModal ({ socket, MessageType }) |
| 16 | { |
| 17 | const backdrop = document.getElementById("quests-backdrop"); |
| 18 | const modal = document.getElementById("quests-modal"); |
| 19 | const listEl = document.getElementById("quests-list"); |
| 20 | const editorEl = document.getElementById("quests-editor"); |
| 21 | const saveBtn = document.getElementById("quests-save"); |
| 22 | const deleteBtn = document.getElementById("quests-delete"); |
| 23 | let open = false; |
| 24 | /** @type {ReturnType<typeof normalizeQuestDef>[]} */ |
| 25 | let quests = []; |
| 26 | /** @type {ReturnType<typeof normalizeDialogueTree>[]} */ |
| 27 | let dialogues = []; |
| 28 | /** @type {ReturnType<typeof normalizeQuestDef>|null} */ |
| 29 | let draft = null; |
| 30 | let selectedId = null; |
| 31 | |
| 32 | function show (visible) |
| 33 | { |
| 34 | open = visible; |
| 35 | backdrop.hidden = !visible; |
| 36 | modal.hidden = !visible; |
| 37 | if (!visible) |
| 38 | { |
| 39 | draft = null; |
| 40 | selectedId = null; |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | function selectQuest (questId) |
| 45 | { |
| 46 | const found = quests.find((q) => q.questId === questId); |
| 47 | if (!found) |
| 48 | { |
| 49 | draft = null; |
| 50 | selectedId = null; |
| 51 | render(); |
| 52 | return; |
| 53 | } |
| 54 | selectedId = found.questId; |
| 55 | draft = structuredClone(found); |
| 56 | render(); |
| 57 | } |
| 58 | |
| 59 | function renderList () |
| 60 | { |
| 61 | listEl.replaceChildren(); |
| 62 | for (const quest of quests) |
| 63 | { |
| 64 | const btn = document.createElement("button"); |
| 65 | btn.type = "button"; |
| 66 | btn.textContent = quest.title || quest.questId; |
| 67 | if (quest.questId === selectedId) |
| 68 | { btn.classList.add("active"); } |
| 69 | btn.addEventListener("click", () => selectQuest(quest.questId)); |
| 70 | listEl.append(btn); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | function renderEditor () |
| 75 | { |
| 76 | editorEl.replaceChildren(); |
| 77 | saveBtn.disabled = !draft; |
| 78 | deleteBtn.disabled = !draft; |
| 79 | if (!draft) |
| 80 | { |
| 81 | const hint = document.createElement("p"); |
| 82 | hint.className = "content-editor-hint"; |
| 83 | hint.textContent = "Select a quest or create a new one."; |
| 84 | editorEl.append(hint); |
| 85 | return; |
| 86 | } |
| 87 | |
| 88 | editorEl.append( |
| 89 | field("Quest id", textInput(draft.questId, (v) => { draft.questId = v; })), |
| 90 | field("Title", textInput(draft.title, (v) => { draft.title = v; })), |
| 91 | field("Description", textArea(draft.description, (v) => { draft.description = v; }, 3)) |
| 92 | ); |
| 93 | |
| 94 | const stepsHeader = sectionHeader("Steps", () => |
| 95 | { |
| 96 | draft.steps.push({ |
| 97 | stepId: `step-${draft.steps.length + 1}`, |
| 98 | title: "New step", |
| 99 | description: "", |
| 100 | completionJournalText: "", |
| 101 | objectives: [{ |
| 102 | objectiveId: "obj-1", |
| 103 | text: "Complete this step", |
| 104 | when: emptyConditionSet() |
| 105 | }], |
| 106 | onEnter: [], |
| 107 | onComplete: [] |
| 108 | }); |
| 109 | renderEditor(); |
| 110 | }); |
| 111 | editorEl.append(stepsHeader); |
| 112 | |
| 113 | draft.steps.forEach((step, stepIndex) => |
| 114 | { |
| 115 | const box = document.createElement("div"); |
| 116 | box.className = "content-editor-section"; |
| 117 | const head = sectionHeader(`Step ${stepIndex + 1}`, null, () => |
| 118 | { |
| 119 | draft.steps.splice(stepIndex, 1); |
| 120 | if (draft.steps.length === 0) |
| 121 | { |
| 122 | draft.steps.push(normalizeQuestDef({ questId: draft.questId }).steps[0]); |
| 123 | } |
| 124 | renderEditor(); |
| 125 | }); |
| 126 | box.append(head); |
| 127 | box.append( |
| 128 | field("stepId", textInput(step.stepId, (v) => { step.stepId = v; })), |
| 129 | field("Title", textInput(step.title, (v) => { step.title = v; })), |
| 130 | field("Description", textArea(step.description, (v) => { step.description = v; }, 2)), |
| 131 | field("Completion journal", textArea(step.completionJournalText, (v) => |
| 132 | { |
| 133 | step.completionJournalText = v; |
| 134 | }, 2)) |
| 135 | ); |
| 136 | |
| 137 | const objHead = sectionHeader("Objectives", () => |
| 138 | { |
| 139 | step.objectives.push({ |
| 140 | objectiveId: `obj-${step.objectives.length + 1}`, |
| 141 | text: "New objective", |
| 142 | when: emptyConditionSet() |
| 143 | }); |
| 144 | renderEditor(); |
| 145 | }); |
| 146 | box.append(objHead); |
| 147 | |
| 148 | step.objectives.forEach((obj, objIndex) => |
| 149 | { |
| 150 | const objBox = document.createElement("div"); |
| 151 | objBox.className = "content-editor-section"; |
| 152 | objBox.append( |
| 153 | sectionHeader(`Objective ${objIndex + 1}`, null, () => |
| 154 | { |
| 155 | step.objectives.splice(objIndex, 1); |
| 156 | if (step.objectives.length === 0) |
| 157 | { |
| 158 | step.objectives.push({ |
| 159 | objectiveId: "obj-1", |
| 160 | text: "Complete this step", |
| 161 | when: emptyConditionSet() |
| 162 | }); |
| 163 | } |
| 164 | renderEditor(); |
| 165 | }), |
| 166 | field("Text", textInput(obj.text, (v) => { obj.text = v; })) |
| 167 | ); |
| 168 | objBox.append(renderConditionSet(obj.when, (next, { remount = true } = {}) => |
| 169 | { |
| 170 | obj.when = next; |
| 171 | if (remount) |
| 172 | { renderEditor(); } |
| 173 | }, { quests, dialogues })); |
| 174 | box.append(objBox); |
| 175 | }); |
| 176 | editorEl.append(box); |
| 177 | }); |
| 178 | } |
| 179 | |
| 180 | function render () |
| 181 | { |
| 182 | renderList(); |
| 183 | renderEditor(); |
| 184 | } |
| 185 | |
| 186 | document.getElementById("open-quests").addEventListener("click", () => |
| 187 | { |
| 188 | socket.send({ type: MessageType.editorLoadQuests }); |
| 189 | socket.send({ type: MessageType.editorLoadDialogues }); |
| 190 | show(true); |
| 191 | render(); |
| 192 | }); |
| 193 | document.getElementById("quests-close").addEventListener("click", () => show(false)); |
| 194 | backdrop.addEventListener("click", () => show(false)); |
| 195 | document.getElementById("quests-new").addEventListener("click", () => |
| 196 | { |
| 197 | const base = normalizeQuestDef({ |
| 198 | questId: `quest-${quests.length + 1}`, |
| 199 | title: "New quest", |
| 200 | description: "" |
| 201 | }); |
| 202 | draft = structuredClone(base); |
| 203 | selectedId = null; |
| 204 | render(); |
| 205 | }); |
| 206 | saveBtn.addEventListener("click", () => |
| 207 | { |
| 208 | if (!draft) |
| 209 | { return; } |
| 210 | socket.send({ type: MessageType.editorSaveQuest, quest: draft }); |
| 211 | }); |
| 212 | deleteBtn.addEventListener("click", () => |
| 213 | { |
| 214 | if (!draft?.questId) |
| 215 | { return; } |
| 216 | if (!window.confirm(`Delete quest "${draft.questId}"?`)) |
| 217 | { return; } |
| 218 | socket.send({ type: MessageType.editorDeleteQuest, questId: draft.questId }); |
| 219 | }); |
| 220 | |
| 221 | return { |
| 222 | isOpen: () => open, |
| 223 | handleMessage (message) |
| 224 | { |
| 225 | if (message.type === MessageType.editorDialogues) |
| 226 | { |
| 227 | dialogues = (message.dialogues ?? []).map((d) => normalizeDialogueTree(d)); |
| 228 | if (open) |
| 229 | { render(); } |
| 230 | return; |
| 231 | } |
| 232 | if (message.type !== MessageType.editorQuests) |
| 233 | { return; } |
| 234 | quests = (message.quests ?? []).map((q) => normalizeQuestDef(q)); |
| 235 | if (draft) |
| 236 | { |
| 237 | const still = quests.find((q) => q.questId === draft.questId); |
| 238 | if (still) |
| 239 | { |
| 240 | selectedId = still.questId; |
| 241 | draft = structuredClone(still); |
| 242 | } |
| 243 | else if (selectedId) |
| 244 | { |
| 245 | draft = null; |
| 246 | selectedId = null; |
| 247 | } |
| 248 | } |
| 249 | if (open) |
| 250 | { render(); } |
| 251 | } |
| 252 | }; |
| 253 | } |
| 254 | |
| 255 | function field (label, input) |
| 256 | { |
| 257 | const wrap = document.createElement("label"); |
| 258 | wrap.append(document.createTextNode(label), input); |
| 259 | return wrap; |
| 260 | } |
| 261 | |
| 262 | function textInput (value, onChange) |
| 263 | { |
| 264 | const input = document.createElement("input"); |
| 265 | input.type = "text"; |
| 266 | input.value = value ?? ""; |
| 267 | input.addEventListener("input", () => onChange(input.value)); |
| 268 | return input; |
| 269 | } |
| 270 | |
| 271 | function textArea (value, onChange, rows = 3) |
| 272 | { |
| 273 | const input = document.createElement("textarea"); |
| 274 | input.rows = rows; |
| 275 | input.value = value ?? ""; |
| 276 | input.addEventListener("input", () => onChange(input.value)); |
| 277 | return input; |
| 278 | } |
| 279 | |
| 280 | function sectionHeader (title, onAdd, onRemove) |
| 281 | { |
| 282 | const head = document.createElement("div"); |
| 283 | head.className = "content-editor-section-header"; |
| 284 | const strong = document.createElement("strong"); |
| 285 | strong.textContent = title; |
| 286 | head.append(strong); |
| 287 | const actions = document.createElement("div"); |
| 288 | actions.className = "content-editor-row"; |
| 289 | if (onAdd) |
| 290 | { |
| 291 | const add = document.createElement("button"); |
| 292 | add.type = "button"; |
| 293 | add.textContent = "+"; |
| 294 | add.title = "Add"; |
| 295 | add.addEventListener("click", onAdd); |
| 296 | actions.append(add); |
| 297 | } |
| 298 | if (onRemove) |
| 299 | { |
| 300 | const remove = document.createElement("button"); |
| 301 | remove.type = "button"; |
| 302 | remove.textContent = "×"; |
| 303 | remove.title = "Remove"; |
| 304 | remove.addEventListener("click", onRemove); |
| 305 | actions.append(remove); |
| 306 | } |
| 307 | head.append(actions); |
| 308 | return head; |
| 309 | } |
| 310 | |
| 311 | function renderConditionSet (when, onChange, ctx = {}) |
| 312 | { |
| 313 | const wrap = document.createElement("div"); |
| 314 | const set = normalizeConditionSet(when); |
| 315 | const bump = (remount = true) => onChange(set, { remount }); |
| 316 | const questList = Array.isArray(ctx.quests) ? ctx.quests : []; |
| 317 | const dialogueList = Array.isArray(ctx.dialogues) ? ctx.dialogues : []; |
| 318 | const title = document.createElement("strong"); |
| 319 | title.textContent = "When (all of)"; |
| 320 | wrap.append(title); |
| 321 | |
| 322 | set.all.forEach((clause, index) => |
| 323 | { |
| 324 | const row = document.createElement("div"); |
| 325 | row.className = "content-editor-clause"; |
| 326 | const notLabel = document.createElement("label"); |
| 327 | const notBox = document.createElement("input"); |
| 328 | notBox.type = "checkbox"; |
| 329 | notBox.checked = !!clause.not; |
| 330 | notBox.addEventListener("change", () => |
| 331 | { |
| 332 | set.all[index].not = notBox.checked; |
| 333 | bump(false); |
| 334 | }); |
| 335 | notLabel.append(notBox, document.createTextNode(" NOT")); |
| 336 | row.append(notLabel); |
| 337 | |
| 338 | const kind = document.createElement("select"); |
| 339 | for (const k of SIMPLE_KINDS) |
| 340 | { |
| 341 | const opt = document.createElement("option"); |
| 342 | opt.value = k; |
| 343 | opt.textContent = k; |
| 344 | if ((clause.condition?.kind ?? "") === k) |
| 345 | { opt.selected = true; } |
| 346 | kind.append(opt); |
| 347 | } |
| 348 | const otherOpt = document.createElement("option"); |
| 349 | otherOpt.value = "__other__"; |
| 350 | otherOpt.textContent = "(other / JSON)"; |
| 351 | if (!SIMPLE_KINDS.includes(clause.condition?.kind)) |
| 352 | { otherOpt.selected = true; } |
| 353 | kind.append(otherOpt); |
| 354 | kind.addEventListener("change", () => |
| 355 | { |
| 356 | if (kind.value === "__other__") |
| 357 | { return; } |
| 358 | set.all[index].condition = defaultCondition(kind.value); |
| 359 | bump(true); |
| 360 | }); |
| 361 | row.append(kind); |
| 362 | |
| 363 | const cond = clause.condition ?? { kind: ConditionKind.hasItem }; |
| 364 | if (cond.kind === ConditionKind.hasItem) |
| 365 | { |
| 366 | row.append(itemConditionPicker(cond, () => |
| 367 | { |
| 368 | set.all[index].condition = cond; |
| 369 | bump(false); |
| 370 | })); |
| 371 | row.append(miniField("qty", cond.quantity ?? 1, (v) => |
| 372 | { |
| 373 | cond.quantity = Number(v) || 1; |
| 374 | set.all[index].condition = cond; |
| 375 | bump(false); |
| 376 | })); |
| 377 | } |
| 378 | else if (cond.kind === ConditionKind.questState) |
| 379 | { |
| 380 | row.append(questStateConditionFields(cond, questList, () => |
| 381 | { |
| 382 | set.all[index].condition = cond; |
| 383 | bump(false); |
| 384 | })); |
| 385 | } |
| 386 | else if (cond.kind === ConditionKind.dialogueHeard) |
| 387 | { |
| 388 | row.append(dialogueHeardConditionFields(cond, dialogueList, () => |
| 389 | { |
| 390 | set.all[index].condition = cond; |
| 391 | bump(false); |
| 392 | })); |
| 393 | } |
| 394 | else if (cond.kind === ConditionKind.killCount) |
| 395 | { |
| 396 | row.append(miniField("mobTypeId", cond.mobTypeId ?? "", (v) => |
| 397 | { |
| 398 | cond.mobTypeId = v || undefined; |
| 399 | set.all[index].condition = cond; |
| 400 | bump(false); |
| 401 | })); |
| 402 | row.append(miniField("count", cond.count ?? 1, (v) => |
| 403 | { |
| 404 | cond.count = Number(v) || 1; |
| 405 | set.all[index].condition = cond; |
| 406 | bump(false); |
| 407 | })); |
| 408 | } |
| 409 | else if (cond.kind === ConditionKind.atTile) |
| 410 | { |
| 411 | row.append(miniField("dimensionId", cond.dimensionId ?? "overworld", (v) => |
| 412 | { |
| 413 | cond.dimensionId = String(v || "overworld"); |
| 414 | set.all[index].condition = cond; |
| 415 | bump(false); |
| 416 | })); |
| 417 | row.append(miniField("tileX", cond.tileX ?? 0, (v) => |
| 418 | { |
| 419 | cond.tileX = Number(v) | 0; |
| 420 | set.all[index].condition = cond; |
| 421 | bump(false); |
| 422 | })); |
| 423 | row.append(miniField("tileY", cond.tileY ?? 0, (v) => |
| 424 | { |
| 425 | cond.tileY = Number(v) | 0; |
| 426 | set.all[index].condition = cond; |
| 427 | bump(false); |
| 428 | })); |
| 429 | } |
| 430 | else if ( |
| 431 | cond.kind === ConditionKind.inDimension |
| 432 | || cond.kind === ConditionKind.hasVisitedDimension |
| 433 | ) |
| 434 | { |
| 435 | row.append(miniField("dimensionId", cond.dimensionId ?? "overworld", (v) => |
| 436 | { |
| 437 | cond.dimensionId = String(v || "overworld"); |
| 438 | set.all[index].condition = cond; |
| 439 | bump(false); |
| 440 | })); |
| 441 | } |
| 442 | else |
| 443 | { |
| 444 | const json = document.createElement("textarea"); |
| 445 | json.rows = 3; |
| 446 | json.value = JSON.stringify(cond, null, 2); |
| 447 | json.addEventListener("change", () => |
| 448 | { |
| 449 | try |
| 450 | { |
| 451 | set.all[index].condition = JSON.parse(json.value); |
| 452 | bump(false); |
| 453 | } |
| 454 | catch |
| 455 | { /* keep typing */ } |
| 456 | }); |
| 457 | row.append(json); |
| 458 | } |
| 459 | |
| 460 | const remove = document.createElement("button"); |
| 461 | remove.type = "button"; |
| 462 | remove.textContent = "×"; |
| 463 | remove.addEventListener("click", () => |
| 464 | { |
| 465 | set.all.splice(index, 1); |
| 466 | bump(true); |
| 467 | }); |
| 468 | row.append(remove); |
| 469 | wrap.append(row); |
| 470 | }); |
| 471 | |
| 472 | const add = document.createElement("button"); |
| 473 | add.type = "button"; |
| 474 | add.textContent = "Add clause"; |
| 475 | add.addEventListener("click", () => |
| 476 | { |
| 477 | set.all.push({ not: false, condition: defaultCondition(ConditionKind.hasItem) }); |
| 478 | bump(true); |
| 479 | }); |
| 480 | wrap.append(add); |
| 481 | |
| 482 | const jsonFallback = document.createElement("details"); |
| 483 | const summary = document.createElement("summary"); |
| 484 | summary.textContent = "Edit condition set as JSON"; |
| 485 | const area = document.createElement("textarea"); |
| 486 | area.rows = 5; |
| 487 | area.value = JSON.stringify(set, null, 2); |
| 488 | area.addEventListener("change", () => |
| 489 | { |
| 490 | try |
| 491 | { |
| 492 | onChange(normalizeConditionSet(JSON.parse(area.value)), { remount: true }); |
| 493 | } |
| 494 | catch |
| 495 | { /* ignore */ } |
| 496 | }); |
| 497 | jsonFallback.append(summary, area); |
| 498 | wrap.append(jsonFallback); |
| 499 | return wrap; |
| 500 | } |
| 501 | |
| 502 | /** Dialogue picker + step-id dropdown from that tree. */ |
| 503 | function dialogueHeardConditionFields (cond, dialogueList, onChanged) |
| 504 | { |
| 505 | const wrap = document.createElement("div"); |
| 506 | wrap.className = "quest-condition-pair"; |
| 507 | const dialogueSelect = document.createElement("select"); |
| 508 | const blank = document.createElement("option"); |
| 509 | blank.value = ""; |
| 510 | blank.textContent = "Select dialogue…"; |
| 511 | dialogueSelect.append(blank); |
| 512 | for (const tree of dialogueList) |
| 513 | { |
| 514 | const opt = document.createElement("option"); |
| 515 | opt.value = tree.dialogueId; |
| 516 | opt.textContent = tree.label ? `${tree.label} (${tree.dialogueId})` : tree.dialogueId; |
| 517 | if (tree.dialogueId === cond.dialogueId) |
| 518 | { opt.selected = true; } |
| 519 | dialogueSelect.append(opt); |
| 520 | } |
| 521 | if (cond.dialogueId && !dialogueList.some((d) => d.dialogueId === cond.dialogueId)) |
| 522 | { |
| 523 | const missing = document.createElement("option"); |
| 524 | missing.value = cond.dialogueId; |
| 525 | missing.textContent = `${cond.dialogueId} (missing)`; |
| 526 | missing.selected = true; |
| 527 | dialogueSelect.append(missing); |
| 528 | } |
| 529 | |
| 530 | const stepSelect = document.createElement("select"); |
| 531 | const fillSteps = () => |
| 532 | { |
| 533 | const tree = dialogueList.find((d) => d.dialogueId === cond.dialogueId) ?? null; |
| 534 | stepSelect.replaceChildren(); |
| 535 | const steps = tree?.steps ?? []; |
| 536 | if (!steps.length) |
| 537 | { |
| 538 | const empty = document.createElement("option"); |
| 539 | empty.value = cond.stepId ?? ""; |
| 540 | empty.textContent = cond.stepId ? String(cond.stepId) : "No steps"; |
| 541 | empty.selected = true; |
| 542 | stepSelect.append(empty); |
| 543 | return; |
| 544 | } |
| 545 | let matched = false; |
| 546 | for (const step of steps) |
| 547 | { |
| 548 | const opt = document.createElement("option"); |
| 549 | opt.value = step.stepId; |
| 550 | opt.textContent = step.label |
| 551 | ? `${step.label} (${step.stepId})` |
| 552 | : step.stepId; |
| 553 | if (step.stepId === cond.stepId) |
| 554 | { |
| 555 | opt.selected = true; |
| 556 | matched = true; |
| 557 | } |
| 558 | stepSelect.append(opt); |
| 559 | } |
| 560 | if (!matched && cond.stepId) |
| 561 | { |
| 562 | const fallback = document.createElement("option"); |
| 563 | fallback.value = cond.stepId; |
| 564 | fallback.textContent = `${cond.stepId} (missing)`; |
| 565 | fallback.selected = true; |
| 566 | stepSelect.append(fallback); |
| 567 | } |
| 568 | else if (!matched) |
| 569 | { |
| 570 | cond.stepId = steps[0].stepId; |
| 571 | stepSelect.options[0].selected = true; |
| 572 | } |
| 573 | }; |
| 574 | fillSteps(); |
| 575 | |
| 576 | dialogueSelect.addEventListener("change", () => |
| 577 | { |
| 578 | cond.dialogueId = dialogueSelect.value; |
| 579 | const tree = dialogueList.find((d) => d.dialogueId === cond.dialogueId); |
| 580 | cond.stepId = tree?.steps?.[0]?.stepId ?? ""; |
| 581 | fillSteps(); |
| 582 | onChanged(); |
| 583 | }); |
| 584 | stepSelect.addEventListener("change", () => |
| 585 | { |
| 586 | cond.stepId = stepSelect.value; |
| 587 | onChanged(); |
| 588 | }); |
| 589 | wrap.append(dialogueSelect, stepSelect); |
| 590 | return wrap; |
| 591 | } |
| 592 | |
| 593 | /** Quest picker + state dropdown from that quest's steps. */ |
| 594 | function questStateConditionFields (cond, questList, onChanged) |
| 595 | { |
| 596 | const wrap = document.createElement("div"); |
| 597 | wrap.className = "quest-condition-pair"; |
| 598 | const questSelect = document.createElement("select"); |
| 599 | const blank = document.createElement("option"); |
| 600 | blank.value = ""; |
| 601 | blank.textContent = "Select quest…"; |
| 602 | questSelect.append(blank); |
| 603 | for (const quest of questList) |
| 604 | { |
| 605 | const opt = document.createElement("option"); |
| 606 | opt.value = quest.questId; |
| 607 | opt.textContent = quest.title ? `${quest.title} (${quest.questId})` : quest.questId; |
| 608 | if (quest.questId === cond.questId) |
| 609 | { opt.selected = true; } |
| 610 | questSelect.append(opt); |
| 611 | } |
| 612 | if (cond.questId && !questList.some((q) => q.questId === cond.questId)) |
| 613 | { |
| 614 | const missing = document.createElement("option"); |
| 615 | missing.value = cond.questId; |
| 616 | missing.textContent = `${cond.questId} (missing)`; |
| 617 | missing.selected = true; |
| 618 | questSelect.append(missing); |
| 619 | } |
| 620 | |
| 621 | const stateSelect = document.createElement("select"); |
| 622 | const fillStates = () => |
| 623 | { |
| 624 | const quest = questList.find((q) => q.questId === cond.questId) ?? null; |
| 625 | stateSelect.replaceChildren(); |
| 626 | const options = [ |
| 627 | { value: "not-started", label: "not-started" }, |
| 628 | ...((quest?.steps ?? []).map((step, index) => ({ |
| 629 | value: String(index + 1), |
| 630 | label: `Step ${index + 1}: ${step.title || step.stepId}` |
| 631 | }))), |
| 632 | { value: "completed", label: "completed" } |
| 633 | ]; |
| 634 | let matched = false; |
| 635 | for (const entry of options) |
| 636 | { |
| 637 | const opt = document.createElement("option"); |
| 638 | opt.value = entry.value; |
| 639 | opt.textContent = entry.label; |
| 640 | const current = cond.state ?? "not-started"; |
| 641 | const currentKey = current === "not-started" || current === "completed" |
| 642 | ? current |
| 643 | : String(Number(current) || current); |
| 644 | if (entry.value === currentKey) |
| 645 | { |
| 646 | opt.selected = true; |
| 647 | matched = true; |
| 648 | } |
| 649 | stateSelect.append(opt); |
| 650 | } |
| 651 | if (!matched) |
| 652 | { |
| 653 | const fallback = document.createElement("option"); |
| 654 | fallback.value = String(cond.state ?? "not-started"); |
| 655 | fallback.textContent = String(cond.state ?? "not-started"); |
| 656 | fallback.selected = true; |
| 657 | stateSelect.append(fallback); |
| 658 | } |
| 659 | }; |
| 660 | fillStates(); |
| 661 | |
| 662 | questSelect.addEventListener("change", () => |
| 663 | { |
| 664 | cond.questId = questSelect.value; |
| 665 | cond.state = "not-started"; |
| 666 | fillStates(); |
| 667 | onChanged(); |
| 668 | }); |
| 669 | stateSelect.addEventListener("change", () => |
| 670 | { |
| 671 | const raw = stateSelect.value; |
| 672 | if (raw === "not-started" || raw === "completed") |
| 673 | { cond.state = raw; } |
| 674 | else |
| 675 | { cond.state = Math.max(1, Number(raw) | 0); } |
| 676 | onChanged(); |
| 677 | }); |
| 678 | wrap.append(questSelect, stateSelect); |
| 679 | return wrap; |
| 680 | } |
| 681 | |
| 682 | /** Compact searchable item picker for hasItem conditions. */ |
| 683 | function itemConditionPicker (cond, onPicked) |
| 684 | { |
| 685 | const wrap = document.createElement("div"); |
| 686 | wrap.className = "quest-condition-item-picker"; |
| 687 | const pickBtn = document.createElement("button"); |
| 688 | pickBtn.type = "button"; |
| 689 | const refreshLabel = () => |
| 690 | { |
| 691 | const def = itemDefinition(cond.itemId); |
| 692 | pickBtn.textContent = def?.displayName |
| 693 | ?? (cond.itemId ? `Item #${cond.itemId}` : "Pick item…"); |
| 694 | pickBtn.title = cond.itemId |
| 695 | ? `${def?.key ?? "item"} · #${cond.itemId}` |
| 696 | : "Choose an item"; |
| 697 | }; |
| 698 | refreshLabel(); |
| 699 | const searchWrap = document.createElement("div"); |
| 700 | searchWrap.className = "dialogue-reward-item-search"; |
| 701 | searchWrap.hidden = true; |
| 702 | const searchInput = document.createElement("input"); |
| 703 | searchInput.type = "search"; |
| 704 | searchInput.placeholder = "Search items…"; |
| 705 | searchInput.autocomplete = "off"; |
| 706 | const searchResults = document.createElement("div"); |
| 707 | searchResults.className = "dialogue-reward-item-results"; |
| 708 | |
| 709 | const renderMatches = (query) => |
| 710 | { |
| 711 | searchResults.replaceChildren(); |
| 712 | const q = String(query ?? "").trim().toLowerCase(); |
| 713 | const matches = listAllItemPickerEntries().filter((item) => |
| 714 | { |
| 715 | if (!q) |
| 716 | { return true; } |
| 717 | return item.displayName.toLowerCase().includes(q) |
| 718 | || item.key.toLowerCase().includes(q) |
| 719 | || String(item.itemId).includes(q); |
| 720 | }).slice(0, 40); |
| 721 | if (!matches.length) |
| 722 | { |
| 723 | searchResults.textContent = "No matching items."; |
| 724 | return; |
| 725 | } |
| 726 | for (const item of matches) |
| 727 | { |
| 728 | const row = document.createElement("button"); |
| 729 | row.type = "button"; |
| 730 | row.className = "shop-item-search-row"; |
| 731 | const name = document.createElement("span"); |
| 732 | name.className = "shop-item-search-row-name"; |
| 733 | name.textContent = item.displayName; |
| 734 | const key = document.createElement("span"); |
| 735 | key.className = "shop-item-search-row-key"; |
| 736 | key.textContent = `${item.key} · #${item.itemId}`; |
| 737 | row.append(name, key); |
| 738 | row.addEventListener("click", () => |
| 739 | { |
| 740 | cond.itemId = item.itemId; |
| 741 | refreshLabel(); |
| 742 | searchWrap.hidden = true; |
| 743 | onPicked(); |
| 744 | }); |
| 745 | searchResults.append(row); |
| 746 | } |
| 747 | }; |
| 748 | |
| 749 | pickBtn.addEventListener("click", () => |
| 750 | { |
| 751 | searchWrap.hidden = !searchWrap.hidden; |
| 752 | if (!searchWrap.hidden) |
| 753 | { |
| 754 | searchInput.value = ""; |
| 755 | renderMatches(""); |
| 756 | searchInput.focus(); |
| 757 | } |
| 758 | }); |
| 759 | searchInput.addEventListener("input", () => renderMatches(searchInput.value)); |
| 760 | searchWrap.append(searchInput, searchResults); |
| 761 | wrap.append(pickBtn, searchWrap); |
| 762 | return wrap; |
| 763 | } |
| 764 | |
| 765 | function miniField (label, value, onChange) |
| 766 | { |
| 767 | const wrap = document.createElement("label"); |
| 768 | wrap.textContent = `${label} `; |
| 769 | const input = document.createElement("input"); |
| 770 | input.type = "text"; |
| 771 | input.value = String(value ?? ""); |
| 772 | input.style.width = "7rem"; |
| 773 | input.addEventListener("input", () => onChange(input.value)); |
| 774 | wrap.append(input); |
| 775 | return wrap; |
| 776 | } |
| 777 | |
| 778 | function defaultCondition (kind) |
| 779 | { |
| 780 | switch (kind) |
| 781 | { |
| 782 | case ConditionKind.hasItem: |
| 783 | return { kind, itemId: 1, quantity: 1 }; |
| 784 | case ConditionKind.questState: |
| 785 | return { kind, questId: "", state: "not-started" }; |
| 786 | case ConditionKind.dialogueHeard: |
| 787 | return { kind, dialogueId: "", stepId: "" }; |
| 788 | case ConditionKind.killCount: |
| 789 | return { kind, mobTypeId: "deer", count: 1 }; |
| 790 | case ConditionKind.atTile: |
| 791 | return { kind, dimensionId: "overworld", tileX: 0, tileY: 0 }; |
| 792 | case ConditionKind.inDimension: |
| 793 | case ConditionKind.hasVisitedDimension: |
| 794 | return { kind, dimensionId: "overworld" }; |
| 795 | default: |
| 796 | return { kind }; |
| 797 | } |
| 798 | } |