import { normalizeQuestDef, normalizeDialogueTree } from "/shared/quests/normalize.js"; import { emptyConditionSet } from "/shared/quests/conditions.js"; import { renderConditionSet } from "./conditionSetEditor.js"; export function initQuestsModal ({ socket, MessageType, describeWorldTile = null, centerEditorOnTile = null, getDimensionOptions = null }) { const backdrop = document.getElementById("quests-backdrop"); const modal = document.getElementById("quests-modal"); const listEl = document.getElementById("quests-list"); const editorEl = document.getElementById("quests-editor"); const saveBtn = document.getElementById("quests-save"); const deleteBtn = document.getElementById("quests-delete"); let open = false; /** @type {ReturnType[]} */ let quests = []; /** @type {ReturnType[]} */ let dialogues = []; /** @type {ReturnType|null} */ let draft = null; let selectedId = null; function show (visible) { open = visible; backdrop.hidden = !visible; modal.hidden = !visible; if (!visible) { draft = null; selectedId = null; } } function selectQuest (questId) { const found = quests.find((q) => q.questId === questId); if (!found) { draft = null; selectedId = null; render(); return; } selectedId = found.questId; draft = structuredClone(found); render(); } function renderList () { listEl.replaceChildren(); for (const quest of quests) { const btn = document.createElement("button"); btn.type = "button"; btn.textContent = quest.title || quest.questId; if (quest.questId === selectedId) { btn.classList.add("active"); } btn.addEventListener("click", () => selectQuest(quest.questId)); listEl.append(btn); } } function renderEditor () { editorEl.replaceChildren(); saveBtn.disabled = !draft; deleteBtn.disabled = !draft; if (!draft) { const hint = document.createElement("p"); hint.className = "content-editor-hint"; hint.textContent = "Select a quest or create a new one."; editorEl.append(hint); return; } editorEl.append( field("Quest id", textInput(draft.questId, (v) => { draft.questId = v; })), field("Title", textInput(draft.title, (v) => { draft.title = v; })), field("Description", textArea(draft.description, (v) => { draft.description = v; }, 3)) ); const stepsHeader = sectionHeader("Steps", () => { draft.steps.push({ stepId: `step-${draft.steps.length + 1}`, title: "New step", description: "", completionJournalText: "", objectives: [{ objectiveId: "obj-1", text: "Complete this step", when: emptyConditionSet() }], onEnter: [], onComplete: [] }); renderEditor(); }); editorEl.append(stepsHeader); draft.steps.forEach((step, stepIndex) => { const box = document.createElement("div"); box.className = "content-editor-section"; const head = sectionHeader(`Step ${stepIndex + 1}`, null, () => { draft.steps.splice(stepIndex, 1); if (draft.steps.length === 0) { draft.steps.push(normalizeQuestDef({ questId: draft.questId }).steps[0]); } renderEditor(); }); box.append(head); box.append( field("stepId", textInput(step.stepId, (v) => { step.stepId = v; })), field("Title", textInput(step.title, (v) => { step.title = v; })), field("Description", textArea(step.description, (v) => { step.description = v; }, 2)), field("Completion journal", textArea(step.completionJournalText, (v) => { step.completionJournalText = v; }, 2)) ); const objHead = sectionHeader("Objectives", () => { step.objectives.push({ objectiveId: `obj-${step.objectives.length + 1}`, text: "New objective", when: emptyConditionSet() }); renderEditor(); }); box.append(objHead); step.objectives.forEach((obj, objIndex) => { const objBox = document.createElement("div"); objBox.className = "content-editor-section"; objBox.append( sectionHeader(`Objective ${objIndex + 1}`, null, () => { step.objectives.splice(objIndex, 1); if (step.objectives.length === 0) { step.objectives.push({ objectiveId: "obj-1", text: "Complete this step", when: emptyConditionSet() }); } renderEditor(); }), field("Text", textInput(obj.text, (v) => { obj.text = v; })) ); objBox.append(renderConditionSet(obj.when, (next, { remount = true } = {}) => { obj.when = next; if (remount) { renderEditor(); } }, { quests, dialogues, describeWorldTile, centerEditorOnTile, getDimensionOptions })); box.append(objBox); }); editorEl.append(box); }); } function render () { renderList(); renderEditor(); } document.getElementById("open-quests").addEventListener("click", () => { socket.send({ type: MessageType.editorLoadQuests }); socket.send({ type: MessageType.editorLoadDialogues }); show(true); render(); }); document.getElementById("quests-close").addEventListener("click", () => show(false)); backdrop.addEventListener("click", () => show(false)); document.getElementById("quests-new").addEventListener("click", () => { const base = normalizeQuestDef({ questId: `quest-${quests.length + 1}`, title: "New quest", description: "" }); draft = structuredClone(base); selectedId = null; render(); }); saveBtn.addEventListener("click", () => { if (!draft) { return; } socket.send({ type: MessageType.editorSaveQuest, quest: draft }); }); deleteBtn.addEventListener("click", () => { if (!draft?.questId) { return; } if (!window.confirm(`Delete quest "${draft.questId}"?`)) { return; } socket.send({ type: MessageType.editorDeleteQuest, questId: draft.questId }); }); return { isOpen: () => open, getQuests: () => quests, handleMessage (message) { if (message.type === MessageType.editorDialogues) { dialogues = (message.dialogues ?? []).map((d) => normalizeDialogueTree(d)); if (open) { render(); } return; } if (message.type !== MessageType.editorQuests) { return; } quests = (message.quests ?? []).map((q) => normalizeQuestDef(q)); if (draft) { const still = quests.find((q) => q.questId === draft.questId); if (still) { selectedId = still.questId; draft = structuredClone(still); } else if (selectedId) { draft = null; selectedId = null; } } if (open) { render(); } } }; } function field (label, input) { const wrap = document.createElement("label"); wrap.append(document.createTextNode(label), input); return wrap; } function textInput (value, onChange) { const input = document.createElement("input"); input.type = "text"; input.value = value ?? ""; input.addEventListener("input", () => onChange(input.value)); return input; } function textArea (value, onChange, rows = 3) { const input = document.createElement("textarea"); input.rows = rows; input.value = value ?? ""; input.addEventListener("input", () => onChange(input.value)); return input; } function sectionHeader (title, onAdd, onRemove) { const head = document.createElement("div"); head.className = "content-editor-section-header"; const strong = document.createElement("strong"); strong.textContent = title; head.append(strong); const actions = document.createElement("div"); actions.className = "content-editor-row"; if (onAdd) { const add = document.createElement("button"); add.type = "button"; add.textContent = "+"; add.title = "Add"; add.addEventListener("click", onAdd); actions.append(add); } if (onRemove) { const remove = document.createElement("button"); remove.type = "button"; remove.textContent = "×"; remove.title = "Remove"; remove.addEventListener("click", onRemove); actions.append(remove); } head.append(actions); return head; }