| 1 | import { normalizeDialogueTree } from "/shared/quests/normalize.js"; |
| 2 | import { emptyConditionSet, normalizeConditionSet } from "/shared/quests/conditions.js"; |
| 3 | import { itemDefinition, listAllItemPickerEntries } from "/shared/definitions/items.js"; |
| 4 | |
| 5 | export function initDialoguesModal ({ socket, MessageType }) |
| 6 | { |
| 7 | const backdrop = document.getElementById("dialogues-backdrop"); |
| 8 | const modal = document.getElementById("dialogues-modal"); |
| 9 | const listEl = document.getElementById("dialogues-list"); |
| 10 | const editorEl = document.getElementById("dialogues-editor"); |
| 11 | const saveBtn = document.getElementById("dialogues-save"); |
| 12 | const deleteBtn = document.getElementById("dialogues-delete"); |
| 13 | let open = false; |
| 14 | /** @type {any[]} */ |
| 15 | let dialogues = []; |
| 16 | /** @type {any|null} */ |
| 17 | let draft = null; |
| 18 | let selectedId = null; |
| 19 | |
| 20 | function show (visible) |
| 21 | { |
| 22 | open = visible; |
| 23 | backdrop.hidden = !visible; |
| 24 | modal.hidden = !visible; |
| 25 | if (!visible) |
| 26 | { |
| 27 | draft = null; |
| 28 | selectedId = null; |
| 29 | closeInlineItemSearch(); |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | /** @type {null | { step: object, resultsEl: HTMLElement, inputEl: HTMLInputElement }} */ |
| 34 | let itemPickContext = null; |
| 35 | |
| 36 | function closeInlineItemSearch () |
| 37 | { |
| 38 | if (itemPickContext?.resultsEl) |
| 39 | { itemPickContext.resultsEl.replaceChildren(); } |
| 40 | itemPickContext = null; |
| 41 | } |
| 42 | |
| 43 | function openInlineItemSearch (step, inputEl, resultsEl) |
| 44 | { |
| 45 | itemPickContext = { step, inputEl, resultsEl }; |
| 46 | inputEl.value = ""; |
| 47 | renderInlineItemSearch(""); |
| 48 | inputEl.focus(); |
| 49 | } |
| 50 | |
| 51 | function renderInlineItemSearch (query) |
| 52 | { |
| 53 | if (!itemPickContext) |
| 54 | { return; } |
| 55 | const { step, resultsEl } = itemPickContext; |
| 56 | resultsEl.replaceChildren(); |
| 57 | const q = String(query ?? "").trim().toLowerCase(); |
| 58 | const taken = new Set((step.itemRewards ?? []).map((row) => row.itemId)); |
| 59 | const matches = listAllItemPickerEntries().filter((item) => |
| 60 | { |
| 61 | if (taken.has(item.itemId)) |
| 62 | { return false; } |
| 63 | if (!q) |
| 64 | { return true; } |
| 65 | return item.displayName.toLowerCase().includes(q) |
| 66 | || item.key.toLowerCase().includes(q) |
| 67 | || String(item.itemId).includes(q); |
| 68 | }).slice(0, 40); |
| 69 | if (!matches.length) |
| 70 | { |
| 71 | resultsEl.textContent = "No matching items."; |
| 72 | return; |
| 73 | } |
| 74 | for (const item of matches) |
| 75 | { |
| 76 | const row = document.createElement("button"); |
| 77 | row.type = "button"; |
| 78 | row.className = "shop-item-search-row"; |
| 79 | const name = document.createElement("span"); |
| 80 | name.className = "shop-item-search-row-name"; |
| 81 | name.textContent = item.displayName; |
| 82 | const key = document.createElement("span"); |
| 83 | key.className = "shop-item-search-row-key"; |
| 84 | key.textContent = `${item.key} · #${item.itemId}`; |
| 85 | row.append(name, key); |
| 86 | row.addEventListener("click", () => |
| 87 | { |
| 88 | if (!Array.isArray(step.itemRewards)) |
| 89 | { step.itemRewards = []; } |
| 90 | step.itemRewards.push({ itemId: item.itemId, quantity: 1 }); |
| 91 | closeInlineItemSearch(); |
| 92 | renderEditor(); |
| 93 | }); |
| 94 | resultsEl.append(row); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | function selectDialogue (dialogueId) |
| 99 | { |
| 100 | const found = dialogues.find((d) => d.dialogueId === dialogueId); |
| 101 | if (!found) |
| 102 | { |
| 103 | draft = null; |
| 104 | selectedId = null; |
| 105 | render(); |
| 106 | return; |
| 107 | } |
| 108 | selectedId = found.dialogueId; |
| 109 | draft = structuredClone(found); |
| 110 | render(); |
| 111 | } |
| 112 | |
| 113 | function renderList () |
| 114 | { |
| 115 | listEl.replaceChildren(); |
| 116 | for (const tree of dialogues) |
| 117 | { |
| 118 | const btn = document.createElement("button"); |
| 119 | btn.type = "button"; |
| 120 | const unique = tree.unique ? " · unique" : ""; |
| 121 | const assigned = tree.assignedSpawnerCount != null |
| 122 | ? ` (${tree.assignedSpawnerCount})` |
| 123 | : ""; |
| 124 | btn.textContent = `${tree.label || tree.dialogueId}${unique}${assigned}`; |
| 125 | if (tree.dialogueId === selectedId) |
| 126 | { btn.classList.add("active"); } |
| 127 | btn.addEventListener("click", () => selectDialogue(tree.dialogueId)); |
| 128 | listEl.append(btn); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | function addStep () |
| 133 | { |
| 134 | draft.steps.push({ |
| 135 | stepId: `step-${draft.steps.length + 1}`, |
| 136 | lines: [{ speaker: "npc", text: "…", effects: [] }], |
| 137 | itemRewards: [], |
| 138 | exit: { kind: "end" } |
| 139 | }); |
| 140 | renderEditor(); |
| 141 | } |
| 142 | |
| 143 | function renderEditor () |
| 144 | { |
| 145 | editorEl.replaceChildren(); |
| 146 | saveBtn.disabled = !draft; |
| 147 | deleteBtn.disabled = !draft; |
| 148 | if (!draft) |
| 149 | { |
| 150 | const hint = document.createElement("p"); |
| 151 | hint.className = "content-editor-hint"; |
| 152 | hint.textContent = "Select a dialogue or create a new one."; |
| 153 | editorEl.append(hint); |
| 154 | return; |
| 155 | } |
| 156 | |
| 157 | editorEl.append( |
| 158 | field("Dialogue id", textInput(draft.dialogueId, (v) => { draft.dialogueId = v; })), |
| 159 | field("Label", textInput(draft.label, (v) => { draft.label = v; })) |
| 160 | ); |
| 161 | |
| 162 | const uniqueLabel = document.createElement("label"); |
| 163 | const uniqueBox = document.createElement("input"); |
| 164 | uniqueBox.type = "checkbox"; |
| 165 | uniqueBox.checked = !!draft.unique; |
| 166 | uniqueBox.addEventListener("change", () => { draft.unique = uniqueBox.checked; }); |
| 167 | uniqueLabel.append(uniqueBox, document.createTextNode(" Unique (at most one spawner)")); |
| 168 | editorEl.append(uniqueLabel); |
| 169 | |
| 170 | const assigned = document.createElement("p"); |
| 171 | assigned.className = "content-editor-hint"; |
| 172 | assigned.textContent = `Assigned spawners: ${draft.assignedSpawnerCount ?? 0}`; |
| 173 | editorEl.append(assigned); |
| 174 | |
| 175 | editorEl.append(field("Entry step id", textInput(draft.entryStepId ?? "", (v) => |
| 176 | { |
| 177 | draft.entryStepId = v; |
| 178 | }))); |
| 179 | |
| 180 | const stepsTitle = document.createElement("div"); |
| 181 | stepsTitle.className = "content-editor-section-header"; |
| 182 | const stepsStrong = document.createElement("strong"); |
| 183 | stepsStrong.textContent = "Steps"; |
| 184 | stepsTitle.append(stepsStrong); |
| 185 | editorEl.append(stepsTitle); |
| 186 | |
| 187 | draft.steps.forEach((step, stepIndex) => |
| 188 | { |
| 189 | const box = document.createElement("div"); |
| 190 | box.className = "content-editor-section"; |
| 191 | box.append(sectionHeader(`Step ${stepIndex + 1}`, null, () => |
| 192 | { |
| 193 | draft.steps.splice(stepIndex, 1); |
| 194 | if (draft.steps.length === 0) |
| 195 | { |
| 196 | draft.steps.push({ |
| 197 | stepId: "greeting", |
| 198 | lines: [{ speaker: "npc", text: "…", effects: [] }], |
| 199 | itemRewards: [], |
| 200 | exit: { kind: "end" } |
| 201 | }); |
| 202 | } |
| 203 | renderEditor(); |
| 204 | })); |
| 205 | box.append(field("stepId", textInput(step.stepId, (v) => { step.stepId = v; }))); |
| 206 | |
| 207 | const linesHead = sectionHeader("Lines", () => |
| 208 | { |
| 209 | step.lines.push({ speaker: "npc", text: "…", effects: [] }); |
| 210 | renderEditor(); |
| 211 | }); |
| 212 | box.append(linesHead); |
| 213 | step.lines.forEach((line, lineIndex) => |
| 214 | { |
| 215 | const row = document.createElement("div"); |
| 216 | row.className = "content-editor-row"; |
| 217 | const speaker = document.createElement("select"); |
| 218 | for (const s of ["npc", "player"]) |
| 219 | { |
| 220 | const opt = document.createElement("option"); |
| 221 | opt.value = s; |
| 222 | opt.textContent = s; |
| 223 | if (line.speaker === s) |
| 224 | { opt.selected = true; } |
| 225 | speaker.append(opt); |
| 226 | } |
| 227 | speaker.addEventListener("change", () => |
| 228 | { |
| 229 | line.speaker = speaker.value; |
| 230 | }); |
| 231 | const text = document.createElement("input"); |
| 232 | text.type = "text"; |
| 233 | text.value = line.text ?? ""; |
| 234 | text.style.flex = "1"; |
| 235 | text.addEventListener("input", () => { line.text = text.value; }); |
| 236 | row.append(speaker, text, removeButton(() => |
| 237 | { |
| 238 | step.lines.splice(lineIndex, 1); |
| 239 | if (step.lines.length === 0) |
| 240 | { step.lines.push({ speaker: "npc", text: "…", effects: [] }); } |
| 241 | renderEditor(); |
| 242 | })); |
| 243 | box.append(row); |
| 244 | }); |
| 245 | |
| 246 | if (!Array.isArray(step.itemRewards)) |
| 247 | { step.itemRewards = []; } |
| 248 | const searchWrap = document.createElement("div"); |
| 249 | searchWrap.className = "dialogue-reward-item-search"; |
| 250 | searchWrap.hidden = true; |
| 251 | const searchInput = document.createElement("input"); |
| 252 | searchInput.type = "search"; |
| 253 | searchInput.placeholder = "Search items…"; |
| 254 | searchInput.autocomplete = "off"; |
| 255 | searchInput.addEventListener("input", () => renderInlineItemSearch(searchInput.value)); |
| 256 | const searchResults = document.createElement("div"); |
| 257 | searchResults.className = "dialogue-reward-item-results"; |
| 258 | searchWrap.append(searchInput, searchResults); |
| 259 | const rewardsHead = sectionHeader("Item rewards", () => |
| 260 | { |
| 261 | searchWrap.hidden = false; |
| 262 | openInlineItemSearch(step, searchInput, searchResults); |
| 263 | }); |
| 264 | box.append(rewardsHead); |
| 265 | const rewardsHint = document.createElement("p"); |
| 266 | rewardsHint.className = "content-editor-hint"; |
| 267 | rewardsHint.textContent = "Granted after all lines, before triggers/branches."; |
| 268 | box.append(rewardsHint); |
| 269 | if (!step.itemRewards.length) |
| 270 | { |
| 271 | const empty = document.createElement("p"); |
| 272 | empty.className = "content-editor-hint"; |
| 273 | empty.textContent = "No item rewards."; |
| 274 | box.append(empty); |
| 275 | } |
| 276 | step.itemRewards.forEach((reward, rewardIndex) => |
| 277 | { |
| 278 | const def = itemDefinition(reward.itemId); |
| 279 | const row = document.createElement("div"); |
| 280 | row.className = "spawner-drops-row"; |
| 281 | const name = document.createElement("span"); |
| 282 | name.className = "spawner-sells-name"; |
| 283 | name.textContent = def?.displayName ?? `Item ${reward.itemId}`; |
| 284 | const qty = document.createElement("input"); |
| 285 | qty.type = "number"; |
| 286 | qty.min = "1"; |
| 287 | qty.step = "1"; |
| 288 | qty.value = String(Math.max(1, reward.quantity | 0 || 1)); |
| 289 | qty.title = "Quantity"; |
| 290 | qty.addEventListener("input", () => |
| 291 | { |
| 292 | step.itemRewards[rewardIndex].quantity = Math.max(1, Number(qty.value) || 1) | 0; |
| 293 | }); |
| 294 | row.append(name, qty, removeButton(() => |
| 295 | { |
| 296 | step.itemRewards.splice(rewardIndex, 1); |
| 297 | renderEditor(); |
| 298 | })); |
| 299 | box.append(row); |
| 300 | }); |
| 301 | box.append(searchWrap); |
| 302 | |
| 303 | const exitKind = document.createElement("select"); |
| 304 | for (const k of ["end", "triggers", "branches"]) |
| 305 | { |
| 306 | const opt = document.createElement("option"); |
| 307 | opt.value = k; |
| 308 | opt.textContent = k; |
| 309 | if ((step.exit?.kind ?? "end") === k) |
| 310 | { opt.selected = true; } |
| 311 | exitKind.append(opt); |
| 312 | } |
| 313 | exitKind.addEventListener("change", () => |
| 314 | { |
| 315 | if (exitKind.value === "triggers") |
| 316 | { |
| 317 | step.exit = { |
| 318 | kind: "triggers", |
| 319 | triggers: step.exit?.triggers?.length |
| 320 | ? step.exit.triggers |
| 321 | : [{ |
| 322 | trigger: "continue", |
| 323 | text: "Continue", |
| 324 | nextStepId: null, |
| 325 | when: emptyConditionSet(), |
| 326 | effects: [] |
| 327 | }] |
| 328 | }; |
| 329 | } |
| 330 | else if (exitKind.value === "branches") |
| 331 | { |
| 332 | step.exit = { |
| 333 | kind: "branches", |
| 334 | branches: step.exit?.branches?.length |
| 335 | ? step.exit.branches |
| 336 | : [{ |
| 337 | when: emptyConditionSet(), |
| 338 | nextStepId: null, |
| 339 | effects: [] |
| 340 | }] |
| 341 | }; |
| 342 | } |
| 343 | else |
| 344 | { step.exit = { kind: "end" }; } |
| 345 | renderEditor(); |
| 346 | }); |
| 347 | box.append(field("Exit kind", exitKind)); |
| 348 | |
| 349 | if (step.exit?.kind === "triggers") |
| 350 | { |
| 351 | const trigHead = sectionHeader("Triggers", () => |
| 352 | { |
| 353 | step.exit.triggers.push({ |
| 354 | trigger: `choice-${step.exit.triggers.length + 1}`, |
| 355 | text: "Choice", |
| 356 | nextStepId: null, |
| 357 | when: emptyConditionSet(), |
| 358 | effects: [] |
| 359 | }); |
| 360 | renderEditor(); |
| 361 | }); |
| 362 | box.append(trigHead); |
| 363 | step.exit.triggers.forEach((trig, trigIndex) => |
| 364 | { |
| 365 | const tBox = document.createElement("div"); |
| 366 | tBox.className = "content-editor-section"; |
| 367 | tBox.append(sectionHeader(`Trigger ${trigIndex + 1}`, null, () => |
| 368 | { |
| 369 | step.exit.triggers.splice(trigIndex, 1); |
| 370 | renderEditor(); |
| 371 | })); |
| 372 | tBox.append( |
| 373 | field("trigger", textInput(trig.trigger, (v) => { trig.trigger = v; })), |
| 374 | field("text (button)", textInput(trig.text, (v) => { trig.text = v; })), |
| 375 | field("nextStepId (blank=end)", textInput(trig.nextStepId ?? "", (v) => |
| 376 | { |
| 377 | trig.nextStepId = v.trim() || null; |
| 378 | })) |
| 379 | ); |
| 380 | tBox.append(whenJson(trig.when, (next) => { trig.when = next; })); |
| 381 | box.append(tBox); |
| 382 | }); |
| 383 | } |
| 384 | else if (step.exit?.kind === "branches") |
| 385 | { |
| 386 | const branchHead = sectionHeader("Branches", () => |
| 387 | { |
| 388 | step.exit.branches.push({ |
| 389 | when: emptyConditionSet(), |
| 390 | nextStepId: null, |
| 391 | effects: [] |
| 392 | }); |
| 393 | renderEditor(); |
| 394 | }); |
| 395 | box.append(branchHead); |
| 396 | step.exit.branches.forEach((branch, branchIndex) => |
| 397 | { |
| 398 | const bBox = document.createElement("div"); |
| 399 | bBox.className = "content-editor-section"; |
| 400 | bBox.append(sectionHeader(`Branch ${branchIndex + 1}`, null, () => |
| 401 | { |
| 402 | step.exit.branches.splice(branchIndex, 1); |
| 403 | renderEditor(); |
| 404 | })); |
| 405 | bBox.append( |
| 406 | field("nextStepId (blank=end)", textInput(branch.nextStepId ?? "", (v) => |
| 407 | { |
| 408 | branch.nextStepId = v.trim() || null; |
| 409 | })) |
| 410 | ); |
| 411 | bBox.append(whenJson(branch.when, (next) => { branch.when = next; })); |
| 412 | box.append(bBox); |
| 413 | }); |
| 414 | } |
| 415 | |
| 416 | editorEl.append(box); |
| 417 | }); |
| 418 | |
| 419 | const addStepBtn = document.createElement("button"); |
| 420 | addStepBtn.type = "button"; |
| 421 | addStepBtn.className = "content-editor-add-step"; |
| 422 | addStepBtn.textContent = "(add step)"; |
| 423 | addStepBtn.addEventListener("click", addStep); |
| 424 | editorEl.append(addStepBtn); |
| 425 | } |
| 426 | |
| 427 | function render () |
| 428 | { |
| 429 | renderList(); |
| 430 | renderEditor(); |
| 431 | } |
| 432 | |
| 433 | document.getElementById("open-dialogues").addEventListener("click", () => |
| 434 | { |
| 435 | socket.send({ type: MessageType.editorLoadDialogues }); |
| 436 | show(true); |
| 437 | render(); |
| 438 | }); |
| 439 | document.getElementById("dialogues-close").addEventListener("click", () => show(false)); |
| 440 | backdrop.addEventListener("click", () => show(false)); |
| 441 | document.getElementById("dialogues-new").addEventListener("click", () => |
| 442 | { |
| 443 | const base = normalizeDialogueTree({ |
| 444 | dialogueId: `dialogue-${dialogues.length + 1}`, |
| 445 | label: "New dialogue", |
| 446 | unique: false |
| 447 | }); |
| 448 | draft = { ...structuredClone(base), assignedSpawnerCount: 0 }; |
| 449 | selectedId = null; |
| 450 | render(); |
| 451 | }); |
| 452 | saveBtn.addEventListener("click", () => |
| 453 | { |
| 454 | if (!draft) |
| 455 | { return; } |
| 456 | socket.send({ type: MessageType.editorSaveDialogue, dialogue: draft }); |
| 457 | }); |
| 458 | deleteBtn.addEventListener("click", () => |
| 459 | { |
| 460 | if (!draft?.dialogueId) |
| 461 | { return; } |
| 462 | if (!window.confirm(`Delete dialogue "${draft.dialogueId}"?`)) |
| 463 | { return; } |
| 464 | socket.send({ type: MessageType.editorDeleteDialogue, dialogueId: draft.dialogueId }); |
| 465 | }); |
| 466 | |
| 467 | return { |
| 468 | isOpen: () => open, |
| 469 | getDialogues: () => dialogues, |
| 470 | handleMessage (message) |
| 471 | { |
| 472 | if (message.type !== MessageType.editorDialogues) |
| 473 | { return; } |
| 474 | dialogues = (message.dialogues ?? []).map((d) => |
| 475 | { |
| 476 | const tree = normalizeDialogueTree(d); |
| 477 | return { |
| 478 | ...tree, |
| 479 | assignedSpawnerCount: d.assignedSpawnerCount ?? 0 |
| 480 | }; |
| 481 | }); |
| 482 | if (draft) |
| 483 | { |
| 484 | const still = dialogues.find((d) => d.dialogueId === draft.dialogueId); |
| 485 | if (still) |
| 486 | { |
| 487 | selectedId = still.dialogueId; |
| 488 | draft = structuredClone(still); |
| 489 | } |
| 490 | else if (selectedId) |
| 491 | { |
| 492 | draft = null; |
| 493 | selectedId = null; |
| 494 | } |
| 495 | } |
| 496 | if (open) |
| 497 | { render(); } |
| 498 | } |
| 499 | }; |
| 500 | } |
| 501 | |
| 502 | function field (label, input) |
| 503 | { |
| 504 | const wrap = document.createElement("label"); |
| 505 | wrap.append(document.createTextNode(label), input); |
| 506 | return wrap; |
| 507 | } |
| 508 | |
| 509 | function textInput (value, onChange) |
| 510 | { |
| 511 | const input = document.createElement("input"); |
| 512 | input.type = "text"; |
| 513 | input.value = value ?? ""; |
| 514 | input.addEventListener("input", () => onChange(input.value)); |
| 515 | return input; |
| 516 | } |
| 517 | |
| 518 | function removeButton (onClick) |
| 519 | { |
| 520 | const remove = document.createElement("button"); |
| 521 | remove.type = "button"; |
| 522 | remove.className = "content-editor-remove"; |
| 523 | remove.textContent = "×"; |
| 524 | remove.title = "Remove"; |
| 525 | remove.addEventListener("click", onClick); |
| 526 | return remove; |
| 527 | } |
| 528 | |
| 529 | function sectionHeader (title, onAdd, onRemove) |
| 530 | { |
| 531 | const head = document.createElement("div"); |
| 532 | head.className = "content-editor-section-header"; |
| 533 | const strong = document.createElement("strong"); |
| 534 | strong.textContent = title; |
| 535 | head.append(strong); |
| 536 | const actions = document.createElement("div"); |
| 537 | actions.className = "content-editor-row"; |
| 538 | if (onAdd) |
| 539 | { |
| 540 | const add = document.createElement("button"); |
| 541 | add.type = "button"; |
| 542 | add.textContent = "+"; |
| 543 | add.title = "Add"; |
| 544 | add.addEventListener("click", onAdd); |
| 545 | actions.append(add); |
| 546 | } |
| 547 | if (onRemove) |
| 548 | { actions.append(removeButton(onRemove)); } |
| 549 | head.append(actions); |
| 550 | return head; |
| 551 | } |
| 552 | |
| 553 | function whenJson (when, onChange) |
| 554 | { |
| 555 | const details = document.createElement("details"); |
| 556 | const summary = document.createElement("summary"); |
| 557 | summary.textContent = "when (condition JSON)"; |
| 558 | const area = document.createElement("textarea"); |
| 559 | area.rows = 4; |
| 560 | area.value = JSON.stringify(normalizeConditionSet(when), null, 2); |
| 561 | area.addEventListener("change", () => |
| 562 | { |
| 563 | try |
| 564 | { |
| 565 | onChange(normalizeConditionSet(JSON.parse(area.value))); |
| 566 | } |
| 567 | catch |
| 568 | { /* ignore invalid */ } |
| 569 | }); |
| 570 | details.append(summary, area); |
| 571 | return details; |
| 572 | } |