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