| 1 | import { |
| 2 | itemDefinition, |
| 3 | itemDisplayName, |
| 4 | itemAssetPathForStack, |
| 5 | formatQuantity, |
| 6 | ItemId |
| 7 | } from "/shared/definitions/items.js"; |
| 8 | import { |
| 9 | AlchemyStepKind, |
| 10 | CAULDRON_SLOT_COUNT, |
| 11 | DYE_CAULDRON_SLOT_COUNT, |
| 12 | CAULDRON_EMPTY_FLAVOR |
| 13 | } from "/shared/definitions/alchemy.js"; |
| 14 | import { applyItemIcon } from "../render/potionIcon.js"; |
| 15 | |
| 16 | export function initAlchemy ({ |
| 17 | socket, |
| 18 | MessageType, |
| 19 | getInventorySlots |
| 20 | }) |
| 21 | { |
| 22 | const backdrop = document.getElementById("cauldron-backdrop"); |
| 23 | const panel = document.getElementById("cauldron-panel"); |
| 24 | const alchemyPane = document.getElementById("cauldron-alchemy-pane"); |
| 25 | const dyesPane = document.getElementById("cauldron-dyes-pane"); |
| 26 | const tabAlchemy = document.getElementById("cauldron-tab-alchemy"); |
| 27 | const tabDyes = document.getElementById("cauldron-tab-dyes"); |
| 28 | const slotsEl = document.getElementById("cauldron-slots"); |
| 29 | const dyeSlotsEl = document.getElementById("cauldron-dye-slots"); |
| 30 | const insertsEl = document.getElementById("cauldron-inserts"); |
| 31 | const stepsEl = document.getElementById("cauldron-steps"); |
| 32 | const flavorEl = document.getElementById("cauldron-flavor"); |
| 33 | const statusEl = document.getElementById("cauldron-status"); |
| 34 | const dyeStatusEl = document.getElementById("cauldron-dye-status"); |
| 35 | const saveBtn = document.getElementById("cauldron-save-recipe"); |
| 36 | const stirBtn = document.getElementById("cauldron-stir"); |
| 37 | const heatBtn = document.getElementById("cauldron-heat"); |
| 38 | const coolBtn = document.getElementById("cauldron-cool"); |
| 39 | const pickBackdrop = document.getElementById("cauldron-pick-backdrop"); |
| 40 | const pickPanel = document.getElementById("cauldron-pick-panel"); |
| 41 | const pickTitle = document.getElementById("cauldron-pick-title"); |
| 42 | const pickList = document.getElementById("cauldron-pick-list"); |
| 43 | const pickQty = document.getElementById("cauldron-pick-qty"); |
| 44 | const confirmBackdrop = document.getElementById("cauldron-dye-confirm-backdrop"); |
| 45 | const confirmPanel = document.getElementById("cauldron-dye-confirm-panel"); |
| 46 | const confirmText = document.getElementById("cauldron-dye-confirm-text"); |
| 47 | |
| 48 | let open = false; |
| 49 | let state = null; |
| 50 | let activeTab = "alchemy"; |
| 51 | let pickMode = "alchemy"; |
| 52 | let pickSlotIndex = null; |
| 53 | let pickItemId = null; |
| 54 | let pickInstance = null; |
| 55 | let pendingDyeConfirm = null; |
| 56 | |
| 57 | document.getElementById("cauldron-close")?.addEventListener("click", () => close()); |
| 58 | backdrop?.addEventListener("click", () => close()); |
| 59 | tabAlchemy?.addEventListener("click", () => setTab("alchemy")); |
| 60 | tabDyes?.addEventListener("click", () => setTab("dyes")); |
| 61 | stirBtn?.addEventListener("click", () => |
| 62 | { socket.send({ type: MessageType.cauldronAction, action: AlchemyStepKind.stir }); }); |
| 63 | heatBtn?.addEventListener("click", () => |
| 64 | { socket.send({ type: MessageType.cauldronAction, action: AlchemyStepKind.heat }); }); |
| 65 | coolBtn?.addEventListener("click", () => |
| 66 | { socket.send({ type: MessageType.cauldronAction, action: AlchemyStepKind.cool }); }); |
| 67 | document.getElementById("cauldron-bottle")?.addEventListener("click", () => |
| 68 | { socket.send({ type: MessageType.cauldronBottle }); }); |
| 69 | document.getElementById("cauldron-clear")?.addEventListener("click", () => |
| 70 | { socket.send({ type: MessageType.cauldronClear }); }); |
| 71 | document.getElementById("cauldron-create-dye")?.addEventListener("click", () => |
| 72 | { socket.send({ type: MessageType.cauldronCreateDye, confirmOverflow: false }); }); |
| 73 | document.getElementById("cauldron-clear-dye")?.addEventListener("click", () => |
| 74 | { socket.send({ type: MessageType.cauldronClearDye }); }); |
| 75 | saveBtn?.addEventListener("click", () => |
| 76 | { socket.send({ type: MessageType.cauldronSaveRecipe }); }); |
| 77 | document.getElementById("cauldron-pick-cancel")?.addEventListener("click", () => closePick()); |
| 78 | document.getElementById("cauldron-pick-confirm")?.addEventListener("click", () => confirmPick()); |
| 79 | pickBackdrop?.addEventListener("click", () => closePick()); |
| 80 | document.getElementById("cauldron-dye-confirm-cancel")?.addEventListener("click", () => closeDyeConfirm()); |
| 81 | confirmBackdrop?.addEventListener("click", () => closeDyeConfirm()); |
| 82 | document.getElementById("cauldron-dye-confirm-ok")?.addEventListener("click", () => |
| 83 | { |
| 84 | closeDyeConfirm(); |
| 85 | socket.send({ type: MessageType.cauldronCreateDye, confirmOverflow: true }); |
| 86 | }); |
| 87 | |
| 88 | function setTab (tab) |
| 89 | { |
| 90 | activeTab = tab; |
| 91 | const alchemy = tab === "alchemy"; |
| 92 | if (tabAlchemy) |
| 93 | { |
| 94 | tabAlchemy.classList.toggle("active", alchemy); |
| 95 | tabAlchemy.setAttribute("aria-selected", alchemy ? "true" : "false"); |
| 96 | } |
| 97 | if (tabDyes) |
| 98 | { |
| 99 | tabDyes.classList.toggle("active", !alchemy); |
| 100 | tabDyes.setAttribute("aria-selected", alchemy ? "false" : "true"); |
| 101 | } |
| 102 | if (alchemyPane) |
| 103 | { alchemyPane.hidden = !alchemy; } |
| 104 | if (dyesPane) |
| 105 | { dyesPane.hidden = alchemy; } |
| 106 | } |
| 107 | |
| 108 | function render () |
| 109 | { |
| 110 | if (!open || !panel) |
| 111 | { return; } |
| 112 | renderAlchemySlots(); |
| 113 | renderDyeSlots(); |
| 114 | renderSteps(); |
| 115 | if (flavorEl) |
| 116 | { flavorEl.textContent = state?.flavorText ?? CAULDRON_EMPTY_FLAVOR; } |
| 117 | const canAct = !!state?.canProcessActions; |
| 118 | if (stirBtn) |
| 119 | { stirBtn.disabled = !canAct; } |
| 120 | if (heatBtn) |
| 121 | { heatBtn.disabled = !canAct; } |
| 122 | if (coolBtn) |
| 123 | { coolBtn.disabled = !canAct; } |
| 124 | if (saveBtn) |
| 125 | { saveBtn.hidden = !state?.canSaveRecipe; } |
| 126 | } |
| 127 | |
| 128 | function renderSlotButton (reserved, onClick) |
| 129 | { |
| 130 | const slotBtn = document.createElement("button"); |
| 131 | slotBtn.type = "button"; |
| 132 | slotBtn.className = "cauldron-slot"; |
| 133 | if (reserved) |
| 134 | { |
| 135 | const stack = { |
| 136 | itemId: reserved.itemId, |
| 137 | quantity: reserved.quantity, |
| 138 | instance: reserved.instance ?? null |
| 139 | }; |
| 140 | const img = document.createElement("img"); |
| 141 | applyItemIcon(img, stack, itemAssetPathForStack(stack)); |
| 142 | img.alt = itemDisplayName(stack); |
| 143 | const qty = document.createElement("span"); |
| 144 | qty.className = "qty"; |
| 145 | qty.textContent = formatQuantity(reserved.quantity); |
| 146 | slotBtn.append(img, qty); |
| 147 | slotBtn.title = `${itemDisplayName(stack)} ×${reserved.quantity}`; |
| 148 | } |
| 149 | else |
| 150 | { slotBtn.textContent = "+"; } |
| 151 | slotBtn.addEventListener("click", onClick); |
| 152 | return slotBtn; |
| 153 | } |
| 154 | |
| 155 | function renderAlchemySlots () |
| 156 | { |
| 157 | slotsEl.replaceChildren(); |
| 158 | insertsEl.replaceChildren(); |
| 159 | for (let i = 0; i < CAULDRON_SLOT_COUNT; i++) |
| 160 | { |
| 161 | const reserved = state?.slots?.[i] ?? null; |
| 162 | slotsEl.append(renderSlotButton(reserved, () => openPick("alchemy", i))); |
| 163 | const insertBtn = document.createElement("button"); |
| 164 | insertBtn.type = "button"; |
| 165 | insertBtn.className = "cauldron-insert"; |
| 166 | insertBtn.textContent = "↓"; |
| 167 | insertBtn.disabled = !reserved; |
| 168 | insertBtn.title = "Insert into cauldron"; |
| 169 | insertBtn.addEventListener("click", () => |
| 170 | { socket.send({ type: MessageType.cauldronInsert, slotIndex: i }); }); |
| 171 | insertsEl.append(insertBtn); |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | function renderDyeSlots () |
| 176 | { |
| 177 | if (!dyeSlotsEl) |
| 178 | { return; } |
| 179 | dyeSlotsEl.replaceChildren(); |
| 180 | for (let i = 0; i < DYE_CAULDRON_SLOT_COUNT; i++) |
| 181 | { |
| 182 | const reserved = state?.dyeSlots?.[i] ?? null; |
| 183 | dyeSlotsEl.append(renderSlotButton(reserved, () => openPick("dye", i))); |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | function renderSteps () |
| 188 | { |
| 189 | stepsEl.replaceChildren(); |
| 190 | for (const step of state?.steps ?? []) |
| 191 | { |
| 192 | const li = document.createElement("li"); |
| 193 | if (step.kind === AlchemyStepKind.insert) |
| 194 | { |
| 195 | const def = itemDefinition(step.itemId); |
| 196 | li.textContent = `insert ${def?.displayName ?? step.itemId}`; |
| 197 | } |
| 198 | else |
| 199 | { li.textContent = step.kind; } |
| 200 | stepsEl.append(li); |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | function openPick (mode, slotIndex) |
| 205 | { |
| 206 | pickMode = mode; |
| 207 | pickSlotIndex = slotIndex; |
| 208 | pickItemId = null; |
| 209 | pickInstance = null; |
| 210 | if (pickTitle) |
| 211 | { pickTitle.textContent = mode === "dye" ? "Choose flower" : "Choose ingredient"; } |
| 212 | pickList.replaceChildren(); |
| 213 | const counts = new Map(); |
| 214 | for (const slot of getInventorySlots?.() ?? []) |
| 215 | { |
| 216 | if (!slot) |
| 217 | { continue; } |
| 218 | const def = itemDefinition(slot.itemId); |
| 219 | if (!def || def.uniqueInstance || def.equipment |
| 220 | || slot.itemId === ItemId.potion || slot.itemId === ItemId.glassBottle) |
| 221 | { continue; } |
| 222 | if (mode === "dye") |
| 223 | { |
| 224 | if (slot.itemId !== ItemId.flower) |
| 225 | { continue; } |
| 226 | } |
| 227 | else if (slot.itemId === ItemId.flower || slot.itemId === ItemId.dye || def.flower || def.dye) |
| 228 | { continue; } |
| 229 | |
| 230 | const identity = slot.itemId === ItemId.flower |
| 231 | ? `${slot.itemId}|${JSON.stringify(slot.instance ?? null)}` |
| 232 | : String(slot.itemId); |
| 233 | const prev = counts.get(identity); |
| 234 | if (prev) |
| 235 | { prev.quantity += slot.quantity | 0; } |
| 236 | else |
| 237 | { |
| 238 | counts.set(identity, { |
| 239 | itemId: slot.itemId, |
| 240 | quantity: slot.quantity | 0, |
| 241 | instance: slot.instance ? structuredClone(slot.instance) : null |
| 242 | }); |
| 243 | } |
| 244 | } |
| 245 | for (const entry of counts.values()) |
| 246 | { |
| 247 | const stack = { |
| 248 | itemId: entry.itemId, |
| 249 | quantity: entry.quantity, |
| 250 | instance: entry.instance |
| 251 | }; |
| 252 | const btn = document.createElement("button"); |
| 253 | btn.type = "button"; |
| 254 | btn.className = "cauldron-pick-item"; |
| 255 | const img = document.createElement("img"); |
| 256 | applyItemIcon(img, stack, itemAssetPathForStack(stack)); |
| 257 | const label = document.createElement("span"); |
| 258 | label.textContent = `${itemDisplayName(stack)} (${formatQuantity(entry.quantity)})`; |
| 259 | btn.append(img, label); |
| 260 | btn.addEventListener("click", () => |
| 261 | { |
| 262 | pickItemId = entry.itemId; |
| 263 | pickInstance = entry.instance; |
| 264 | pickQty.max = String(entry.quantity); |
| 265 | pickQty.value = String(Math.min(entry.quantity, Number(pickQty.value) || 1)); |
| 266 | for (const child of pickList.children) |
| 267 | { child.classList.toggle("selected", child === btn); } |
| 268 | }); |
| 269 | pickList.append(btn); |
| 270 | } |
| 271 | pickQty.value = "1"; |
| 272 | pickBackdrop.hidden = false; |
| 273 | pickPanel.hidden = false; |
| 274 | } |
| 275 | |
| 276 | function closePick () |
| 277 | { |
| 278 | pickBackdrop.hidden = true; |
| 279 | pickPanel.hidden = true; |
| 280 | pickSlotIndex = null; |
| 281 | pickItemId = null; |
| 282 | pickInstance = null; |
| 283 | } |
| 284 | |
| 285 | function confirmPick () |
| 286 | { |
| 287 | if (pickSlotIndex == null || pickItemId == null) |
| 288 | { return; } |
| 289 | const quantity = Math.max(1, pickQty.value | 0); |
| 290 | if (pickMode === "dye") |
| 291 | { |
| 292 | socket.send({ |
| 293 | type: MessageType.cauldronSetDyeSlot, |
| 294 | slotIndex: pickSlotIndex, |
| 295 | itemId: pickItemId, |
| 296 | quantity, |
| 297 | instance: pickInstance |
| 298 | }); |
| 299 | } |
| 300 | else |
| 301 | { |
| 302 | socket.send({ |
| 303 | type: MessageType.cauldronSetSlot, |
| 304 | slotIndex: pickSlotIndex, |
| 305 | itemId: pickItemId, |
| 306 | quantity, |
| 307 | instance: pickInstance |
| 308 | }); |
| 309 | } |
| 310 | closePick(); |
| 311 | } |
| 312 | |
| 313 | function openDyeConfirm (confirm) |
| 314 | { |
| 315 | pendingDyeConfirm = confirm; |
| 316 | const wanted = confirm.wanted | 0; |
| 317 | const bottles = confirm.bottles | 0; |
| 318 | confirmText.textContent = `Je staat op het punt ${wanted} units aan dye te maken, maar je hebt maar genoeg lege flesjes om ${bottles} op te slaan. De rest gaat verloren. Weet je het zeker?`; |
| 319 | confirmBackdrop.hidden = false; |
| 320 | confirmPanel.hidden = false; |
| 321 | } |
| 322 | |
| 323 | function closeDyeConfirm () |
| 324 | { |
| 325 | pendingDyeConfirm = null; |
| 326 | confirmBackdrop.hidden = true; |
| 327 | confirmPanel.hidden = true; |
| 328 | } |
| 329 | |
| 330 | function openFromServer (nextState) |
| 331 | { |
| 332 | open = true; |
| 333 | state = nextState; |
| 334 | backdrop.hidden = false; |
| 335 | panel.hidden = false; |
| 336 | if (statusEl) |
| 337 | { statusEl.textContent = ""; } |
| 338 | if (dyeStatusEl) |
| 339 | { dyeStatusEl.textContent = ""; } |
| 340 | setTab(activeTab); |
| 341 | render(); |
| 342 | } |
| 343 | |
| 344 | function close () |
| 345 | { |
| 346 | if (!open) |
| 347 | { return; } |
| 348 | open = false; |
| 349 | backdrop.hidden = true; |
| 350 | panel.hidden = true; |
| 351 | closePick(); |
| 352 | closeDyeConfirm(); |
| 353 | socket.send({ type: MessageType.cauldronClose }); |
| 354 | } |
| 355 | |
| 356 | return { |
| 357 | isBlocking () |
| 358 | { |
| 359 | return open |
| 360 | || !pickPanel?.hidden |
| 361 | || !confirmPanel?.hidden; |
| 362 | }, |
| 363 | reset () |
| 364 | { |
| 365 | open = false; |
| 366 | state = null; |
| 367 | if (backdrop) |
| 368 | { backdrop.hidden = true; } |
| 369 | if (panel) |
| 370 | { panel.hidden = true; } |
| 371 | closePick(); |
| 372 | closeDyeConfirm(); |
| 373 | }, |
| 374 | handleSnapshot (message) |
| 375 | { |
| 376 | if (!message.open) |
| 377 | { |
| 378 | open = false; |
| 379 | if (backdrop) |
| 380 | { backdrop.hidden = true; } |
| 381 | if (panel) |
| 382 | { panel.hidden = true; } |
| 383 | state = null; |
| 384 | closeDyeConfirm(); |
| 385 | return; |
| 386 | } |
| 387 | if (!open) |
| 388 | { openFromServer(message.state); } |
| 389 | else |
| 390 | { |
| 391 | state = message.state; |
| 392 | render(); |
| 393 | } |
| 394 | if (message.dyeConfirm) |
| 395 | { openDyeConfirm(message.dyeConfirm); } |
| 396 | if (message.alchemyResult) |
| 397 | { |
| 398 | const result = message.alchemyResult; |
| 399 | const name = result.potion?.displayName ?? "Potion"; |
| 400 | const lost = result.lost > 0 ? ` (${result.lost} lost — not enough bottles)` : ""; |
| 401 | const line = result.success |
| 402 | ? `Bottled ${result.produced}× ${name}${lost}.` |
| 403 | : `Unstable brew: ${result.produced}× ${name}${lost}.`; |
| 404 | if (result.potion?.potionKey === "dye") |
| 405 | { |
| 406 | if (dyeStatusEl) |
| 407 | { dyeStatusEl.textContent = line; } |
| 408 | } |
| 409 | else if (statusEl) |
| 410 | { statusEl.textContent = line; } |
| 411 | } |
| 412 | }, |
| 413 | refresh () |
| 414 | { render(); } |
| 415 | }; |
| 416 | } |