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