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