| 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 { FLOWER_MAX_STACK } from "/shared/definitions/dyes.js"; |
| 15 | import { applyItemIcon } from "../render/potionIcon.js"; |
| 16 | |
| 17 | export function initAlchemy ({ |
| 18 | socket, |
| 19 | MessageType, |
| 20 | getInventorySlots |
| 21 | }) |
| 22 | { |
| 23 | const backdrop = document.getElementById("cauldron-backdrop"); |
| 24 | const panel = document.getElementById("cauldron-panel"); |
| 25 | const alchemyPane = document.getElementById("cauldron-alchemy-pane"); |
| 26 | const dyesPane = document.getElementById("cauldron-dyes-pane"); |
| 27 | const tabAlchemy = document.getElementById("cauldron-tab-alchemy"); |
| 28 | const tabDyes = document.getElementById("cauldron-tab-dyes"); |
| 29 | const slotsEl = document.getElementById("cauldron-slots"); |
| 30 | const dyeSlotsEl = document.getElementById("cauldron-dye-slots"); |
| 31 | const insertsEl = document.getElementById("cauldron-inserts"); |
| 32 | const stepsEl = document.getElementById("cauldron-steps"); |
| 33 | const flavorEl = document.getElementById("cauldron-flavor"); |
| 34 | const statusEl = document.getElementById("cauldron-status"); |
| 35 | const dyeStatusEl = document.getElementById("cauldron-dye-status"); |
| 36 | const saveBtn = document.getElementById("cauldron-save-recipe"); |
| 37 | const stirBtn = document.getElementById("cauldron-stir"); |
| 38 | const heatBtn = document.getElementById("cauldron-heat"); |
| 39 | const coolBtn = document.getElementById("cauldron-cool"); |
| 40 | const pickBackdrop = document.getElementById("cauldron-pick-backdrop"); |
| 41 | const pickPanel = document.getElementById("cauldron-pick-panel"); |
| 42 | const pickTitle = document.getElementById("cauldron-pick-title"); |
| 43 | const pickList = document.getElementById("cauldron-pick-list"); |
| 44 | const pickQty = document.getElementById("cauldron-pick-qty"); |
| 45 | const confirmBackdrop = document.getElementById("cauldron-dye-confirm-backdrop"); |
| 46 | const confirmPanel = document.getElementById("cauldron-dye-confirm-panel"); |
| 47 | const confirmText = document.getElementById("cauldron-dye-confirm-text"); |
| 48 | |
| 49 | let open = false; |
| 50 | let state = null; |
| 51 | let localSlots = emptyAlchemySlots(); |
| 52 | let localDyeSlots = emptyDyeSlots(); |
| 53 | let activeTab = "alchemy"; |
| 54 | let pickMode = "alchemy"; |
| 55 | let pickSlotIndex = null; |
| 56 | let pickItemId = null; |
| 57 | let pickInstance = null; |
| 58 | let pendingDyeConfirm = null; |
| 59 | |
| 60 | function emptyAlchemySlots () |
| 61 | { |
| 62 | return Array.from({ length: CAULDRON_SLOT_COUNT }, () => null); |
| 63 | } |
| 64 | |
| 65 | function emptyDyeSlots () |
| 66 | { |
| 67 | return Array.from({ length: DYE_CAULDRON_SLOT_COUNT }, () => null); |
| 68 | } |
| 69 | |
| 70 | function flowerPayload () |
| 71 | { |
| 72 | return localDyeSlots |
| 73 | .filter((slot) => slot?.itemId === ItemId.flower && slot.quantity > 0) |
| 74 | .map((slot) => ({ |
| 75 | itemId: slot.itemId, |
| 76 | quantity: slot.quantity | 0, |
| 77 | instance: slot.instance ? structuredClone(slot.instance) : null |
| 78 | })); |
| 79 | } |
| 80 | |
| 81 | document.getElementById("cauldron-close")?.addEventListener("click", () => close()); |
| 82 | backdrop?.addEventListener("click", () => close()); |
| 83 | tabAlchemy?.addEventListener("click", () => setTab("alchemy")); |
| 84 | tabDyes?.addEventListener("click", () => setTab("dyes")); |
| 85 | stirBtn?.addEventListener("click", () => |
| 86 | { socket.send({ type: MessageType.cauldronAction, action: AlchemyStepKind.stir }); }); |
| 87 | heatBtn?.addEventListener("click", () => |
| 88 | { socket.send({ type: MessageType.cauldronAction, action: AlchemyStepKind.heat }); }); |
| 89 | coolBtn?.addEventListener("click", () => |
| 90 | { socket.send({ type: MessageType.cauldronAction, action: AlchemyStepKind.cool }); }); |
| 91 | document.getElementById("cauldron-bottle")?.addEventListener("click", () => |
| 92 | { socket.send({ type: MessageType.cauldronBottle }); }); |
| 93 | document.getElementById("cauldron-clear")?.addEventListener("click", () => |
| 94 | { |
| 95 | localSlots = emptyAlchemySlots(); |
| 96 | socket.send({ type: MessageType.cauldronClear }); |
| 97 | render(); |
| 98 | }); |
| 99 | document.getElementById("cauldron-create-dye")?.addEventListener("click", () => |
| 100 | { |
| 101 | socket.send({ |
| 102 | type: MessageType.cauldronCreateDye, |
| 103 | confirmOverflow: false, |
| 104 | flowers: flowerPayload() |
| 105 | }); |
| 106 | }); |
| 107 | document.getElementById("cauldron-clear-dye")?.addEventListener("click", () => |
| 108 | { |
| 109 | localDyeSlots = emptyDyeSlots(); |
| 110 | socket.send({ type: MessageType.cauldronClearDye }); |
| 111 | render(); |
| 112 | }); |
| 113 | saveBtn?.addEventListener("click", () => |
| 114 | { socket.send({ type: MessageType.cauldronSaveRecipe }); }); |
| 115 | document.getElementById("cauldron-pick-cancel")?.addEventListener("click", () => closePick()); |
| 116 | document.getElementById("cauldron-pick-confirm")?.addEventListener("click", () => confirmPick()); |
| 117 | pickBackdrop?.addEventListener("click", () => closePick()); |
| 118 | document.getElementById("cauldron-dye-confirm-cancel")?.addEventListener("click", () => closeDyeConfirm()); |
| 119 | confirmBackdrop?.addEventListener("click", () => closeDyeConfirm()); |
| 120 | document.getElementById("cauldron-dye-confirm-ok")?.addEventListener("click", () => |
| 121 | { |
| 122 | closeDyeConfirm(); |
| 123 | socket.send({ |
| 124 | type: MessageType.cauldronCreateDye, |
| 125 | confirmOverflow: true, |
| 126 | flowers: flowerPayload() |
| 127 | }); |
| 128 | }); |
| 129 | |
| 130 | function setTab (tab) |
| 131 | { |
| 132 | activeTab = tab; |
| 133 | const alchemy = tab === "alchemy"; |
| 134 | if (tabAlchemy) |
| 135 | { |
| 136 | tabAlchemy.classList.toggle("active", alchemy); |
| 137 | tabAlchemy.setAttribute("aria-selected", alchemy ? "true" : "false"); |
| 138 | } |
| 139 | if (tabDyes) |
| 140 | { |
| 141 | tabDyes.classList.toggle("active", !alchemy); |
| 142 | tabDyes.setAttribute("aria-selected", alchemy ? "false" : "true"); |
| 143 | } |
| 144 | if (alchemyPane) |
| 145 | { alchemyPane.hidden = !alchemy; } |
| 146 | if (dyesPane) |
| 147 | { dyesPane.hidden = alchemy; } |
| 148 | } |
| 149 | |
| 150 | function render () |
| 151 | { |
| 152 | if (!open || !panel) |
| 153 | { return; } |
| 154 | renderAlchemySlots(); |
| 155 | renderDyeSlots(); |
| 156 | renderSteps(); |
| 157 | if (flavorEl) |
| 158 | { flavorEl.textContent = state?.flavorText ?? CAULDRON_EMPTY_FLAVOR; } |
| 159 | const canAct = !!state?.canProcessActions; |
| 160 | if (stirBtn) |
| 161 | { stirBtn.disabled = !canAct; } |
| 162 | if (heatBtn) |
| 163 | { heatBtn.disabled = !canAct; } |
| 164 | if (coolBtn) |
| 165 | { coolBtn.disabled = !canAct; } |
| 166 | if (saveBtn) |
| 167 | { saveBtn.hidden = !state?.canSaveRecipe; } |
| 168 | } |
| 169 | |
| 170 | function alchemySlotsLocked () |
| 171 | { |
| 172 | return state?.canAddIngredients === false |
| 173 | || ((state?.steps?.length ?? 0) > 0); |
| 174 | } |
| 175 | |
| 176 | function renderSlotButton (reserved, { onClick, locked = false } = {}) |
| 177 | { |
| 178 | const slotBtn = document.createElement("button"); |
| 179 | slotBtn.type = "button"; |
| 180 | slotBtn.className = "cauldron-slot"; |
| 181 | if (locked) |
| 182 | { slotBtn.classList.add("locked"); } |
| 183 | if (reserved) |
| 184 | { |
| 185 | const stack = { |
| 186 | itemId: reserved.itemId, |
| 187 | quantity: reserved.quantity, |
| 188 | instance: reserved.instance ?? null |
| 189 | }; |
| 190 | const img = document.createElement("img"); |
| 191 | applyItemIcon(img, stack, itemAssetPathForStack(stack)); |
| 192 | img.alt = itemDisplayName(stack); |
| 193 | const qty = document.createElement("span"); |
| 194 | qty.className = "qty"; |
| 195 | qty.textContent = formatQuantity(reserved.quantity); |
| 196 | slotBtn.append(img, qty); |
| 197 | slotBtn.title = locked |
| 198 | ? `${itemDisplayName(stack)} ×${reserved.quantity} (locked until Bottle)` |
| 199 | : `${itemDisplayName(stack)} ×${reserved.quantity}`; |
| 200 | } |
| 201 | else |
| 202 | { |
| 203 | slotBtn.textContent = "+"; |
| 204 | if (locked) |
| 205 | { slotBtn.title = "Ingredient slots lock after the first brew step"; } |
| 206 | } |
| 207 | slotBtn.disabled = locked && !reserved; |
| 208 | if (!locked) |
| 209 | { slotBtn.addEventListener("click", onClick); } |
| 210 | else if (reserved) |
| 211 | { |
| 212 | // Filled + locked: still show contents, no replace/clear via click. |
| 213 | slotBtn.disabled = true; |
| 214 | } |
| 215 | return slotBtn; |
| 216 | } |
| 217 | |
| 218 | function renderAlchemySlots () |
| 219 | { |
| 220 | slotsEl.replaceChildren(); |
| 221 | insertsEl.replaceChildren(); |
| 222 | const locked = alchemySlotsLocked(); |
| 223 | for (let i = 0; i < CAULDRON_SLOT_COUNT; i++) |
| 224 | { |
| 225 | const reserved = localSlots[i] ?? null; |
| 226 | slotsEl.append(renderSlotButton(reserved, { |
| 227 | locked, |
| 228 | onClick: () => openPick("alchemy", i) |
| 229 | })); |
| 230 | const insertBtn = document.createElement("button"); |
| 231 | insertBtn.type = "button"; |
| 232 | insertBtn.className = "cauldron-insert"; |
| 233 | insertBtn.textContent = "↓"; |
| 234 | insertBtn.disabled = !reserved; |
| 235 | insertBtn.title = "Insert into cauldron"; |
| 236 | insertBtn.addEventListener("click", () => |
| 237 | { |
| 238 | const slot = localSlots[i]; |
| 239 | if (!slot) |
| 240 | { return; } |
| 241 | socket.send({ |
| 242 | type: MessageType.cauldronInsert, |
| 243 | itemId: slot.itemId, |
| 244 | quantity: slot.quantity | 0, |
| 245 | instance: slot.instance ?? null |
| 246 | }); |
| 247 | localSlots[i] = null; |
| 248 | render(); |
| 249 | }); |
| 250 | insertsEl.append(insertBtn); |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | function renderDyeSlots () |
| 255 | { |
| 256 | if (!dyeSlotsEl) |
| 257 | { return; } |
| 258 | dyeSlotsEl.replaceChildren(); |
| 259 | for (let i = 0; i < DYE_CAULDRON_SLOT_COUNT; i++) |
| 260 | { |
| 261 | const reserved = localDyeSlots[i] ?? null; |
| 262 | dyeSlotsEl.append(renderSlotButton(reserved, { |
| 263 | onClick: () => openPick("dye", i) |
| 264 | })); |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | function renderSteps () |
| 269 | { |
| 270 | stepsEl.replaceChildren(); |
| 271 | for (const step of state?.steps ?? []) |
| 272 | { |
| 273 | const li = document.createElement("li"); |
| 274 | if (step.kind === AlchemyStepKind.insert) |
| 275 | { |
| 276 | const def = itemDefinition(step.itemId); |
| 277 | li.textContent = `insert ${def?.displayName ?? step.itemId}`; |
| 278 | } |
| 279 | else |
| 280 | { li.textContent = step.kind; } |
| 281 | stepsEl.append(li); |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | function openPick (mode, slotIndex) |
| 286 | { |
| 287 | if (mode === "alchemy" && alchemySlotsLocked()) |
| 288 | { return; } |
| 289 | pickMode = mode; |
| 290 | pickSlotIndex = slotIndex; |
| 291 | pickItemId = null; |
| 292 | pickInstance = null; |
| 293 | if (pickTitle) |
| 294 | { pickTitle.textContent = mode === "dye" ? "Choose flower" : "Choose ingredient"; } |
| 295 | pickList.replaceChildren(); |
| 296 | const counts = new Map(); |
| 297 | for (const slot of getInventorySlots?.() ?? []) |
| 298 | { |
| 299 | if (!slot) |
| 300 | { continue; } |
| 301 | const def = itemDefinition(slot.itemId); |
| 302 | if (!def || def.uniqueInstance || def.equipment |
| 303 | || slot.itemId === ItemId.potion || slot.itemId === ItemId.glassBottle) |
| 304 | { continue; } |
| 305 | if (mode === "dye") |
| 306 | { |
| 307 | if (slot.itemId !== ItemId.flower) |
| 308 | { continue; } |
| 309 | } |
| 310 | else if (slot.itemId === ItemId.flower || slot.itemId === ItemId.dye || def.flower || def.dye) |
| 311 | { continue; } |
| 312 | |
| 313 | const identity = slot.itemId === ItemId.flower |
| 314 | ? `${slot.itemId}|${JSON.stringify(slot.instance ?? null)}` |
| 315 | : String(slot.itemId); |
| 316 | const prev = counts.get(identity); |
| 317 | if (prev) |
| 318 | { prev.quantity += slot.quantity | 0; } |
| 319 | else |
| 320 | { |
| 321 | counts.set(identity, { |
| 322 | itemId: slot.itemId, |
| 323 | quantity: slot.quantity | 0, |
| 324 | instance: slot.instance ? structuredClone(slot.instance) : null |
| 325 | }); |
| 326 | } |
| 327 | } |
| 328 | for (const entry of counts.values()) |
| 329 | { |
| 330 | const stack = { |
| 331 | itemId: entry.itemId, |
| 332 | quantity: entry.quantity, |
| 333 | instance: entry.instance |
| 334 | }; |
| 335 | const btn = document.createElement("button"); |
| 336 | btn.type = "button"; |
| 337 | btn.className = "cauldron-pick-item"; |
| 338 | const img = document.createElement("img"); |
| 339 | applyItemIcon(img, stack, itemAssetPathForStack(stack)); |
| 340 | const label = document.createElement("span"); |
| 341 | label.textContent = `${itemDisplayName(stack)} (${formatQuantity(entry.quantity)})`; |
| 342 | btn.append(img, label); |
| 343 | btn.addEventListener("click", () => |
| 344 | { |
| 345 | pickItemId = entry.itemId; |
| 346 | pickInstance = entry.instance; |
| 347 | pickQty.max = String(pickMode === "dye" |
| 348 | ? Math.min(entry.quantity, FLOWER_MAX_STACK) |
| 349 | : entry.quantity); |
| 350 | pickQty.value = String(Math.min( |
| 351 | entry.quantity, |
| 352 | Number(pickQty.value) || 1, |
| 353 | pickMode === "dye" ? FLOWER_MAX_STACK : entry.quantity |
| 354 | )); |
| 355 | for (const child of pickList.children) |
| 356 | { child.classList.toggle("selected", child === btn); } |
| 357 | }); |
| 358 | pickList.append(btn); |
| 359 | } |
| 360 | pickQty.value = "1"; |
| 361 | pickBackdrop.hidden = false; |
| 362 | pickPanel.hidden = false; |
| 363 | } |
| 364 | |
| 365 | function closePick () |
| 366 | { |
| 367 | pickBackdrop.hidden = true; |
| 368 | pickPanel.hidden = true; |
| 369 | pickSlotIndex = null; |
| 370 | pickItemId = null; |
| 371 | pickInstance = null; |
| 372 | } |
| 373 | |
| 374 | function confirmPick () |
| 375 | { |
| 376 | if (pickSlotIndex == null || pickItemId == null) |
| 377 | { return; } |
| 378 | const rawQty = Math.max(1, pickQty.value | 0); |
| 379 | const quantity = pickMode === "dye" ? Math.min(rawQty, FLOWER_MAX_STACK) : rawQty; |
| 380 | const entry = { |
| 381 | itemId: pickItemId, |
| 382 | quantity, |
| 383 | instance: pickInstance ? structuredClone(pickInstance) : null |
| 384 | }; |
| 385 | if (pickMode === "dye") |
| 386 | { localDyeSlots[pickSlotIndex] = entry; } |
| 387 | else |
| 388 | { localSlots[pickSlotIndex] = entry; } |
| 389 | closePick(); |
| 390 | render(); |
| 391 | } |
| 392 | |
| 393 | function openDyeConfirm (confirm) |
| 394 | { |
| 395 | pendingDyeConfirm = confirm; |
| 396 | const wanted = confirm.wanted | 0; |
| 397 | const bottles = confirm.bottles | 0; |
| 398 | 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?`; |
| 399 | confirmBackdrop.hidden = false; |
| 400 | confirmPanel.hidden = false; |
| 401 | } |
| 402 | |
| 403 | function closeDyeConfirm () |
| 404 | { |
| 405 | pendingDyeConfirm = null; |
| 406 | confirmBackdrop.hidden = true; |
| 407 | confirmPanel.hidden = true; |
| 408 | } |
| 409 | |
| 410 | function openFromServer (nextState) |
| 411 | { |
| 412 | open = true; |
| 413 | state = nextState; |
| 414 | localSlots = emptyAlchemySlots(); |
| 415 | localDyeSlots = emptyDyeSlots(); |
| 416 | backdrop.hidden = false; |
| 417 | panel.hidden = false; |
| 418 | if (statusEl) |
| 419 | { statusEl.textContent = ""; } |
| 420 | if (dyeStatusEl) |
| 421 | { dyeStatusEl.textContent = ""; } |
| 422 | setTab(activeTab); |
| 423 | render(); |
| 424 | } |
| 425 | |
| 426 | function close () |
| 427 | { |
| 428 | if (!open) |
| 429 | { return; } |
| 430 | open = false; |
| 431 | backdrop.hidden = true; |
| 432 | panel.hidden = true; |
| 433 | localSlots = emptyAlchemySlots(); |
| 434 | localDyeSlots = emptyDyeSlots(); |
| 435 | closePick(); |
| 436 | closeDyeConfirm(); |
| 437 | socket.send({ type: MessageType.cauldronClose }); |
| 438 | } |
| 439 | |
| 440 | return { |
| 441 | isBlocking () |
| 442 | { |
| 443 | return open |
| 444 | || !pickPanel?.hidden |
| 445 | || !confirmPanel?.hidden; |
| 446 | }, |
| 447 | isOpen () |
| 448 | { |
| 449 | return open; |
| 450 | }, |
| 451 | close, |
| 452 | reset () |
| 453 | { |
| 454 | open = false; |
| 455 | state = null; |
| 456 | localSlots = emptyAlchemySlots(); |
| 457 | localDyeSlots = emptyDyeSlots(); |
| 458 | if (backdrop) |
| 459 | { backdrop.hidden = true; } |
| 460 | if (panel) |
| 461 | { panel.hidden = true; } |
| 462 | closePick(); |
| 463 | closeDyeConfirm(); |
| 464 | }, |
| 465 | handleSnapshot (message) |
| 466 | { |
| 467 | if (!message.open) |
| 468 | { |
| 469 | open = false; |
| 470 | if (backdrop) |
| 471 | { backdrop.hidden = true; } |
| 472 | if (panel) |
| 473 | { panel.hidden = true; } |
| 474 | state = null; |
| 475 | localSlots = emptyAlchemySlots(); |
| 476 | localDyeSlots = emptyDyeSlots(); |
| 477 | closeDyeConfirm(); |
| 478 | return; |
| 479 | } |
| 480 | if (!open) |
| 481 | { openFromServer(message.state); } |
| 482 | else |
| 483 | { |
| 484 | state = message.state; |
| 485 | render(); |
| 486 | } |
| 487 | if (message.dyeConfirm) |
| 488 | { openDyeConfirm(message.dyeConfirm); } |
| 489 | if (message.alchemyResult) |
| 490 | { |
| 491 | const result = message.alchemyResult; |
| 492 | const name = result.potion?.displayName ?? "Potion"; |
| 493 | const lost = result.lost > 0 ? ` (${result.lost} lost — not enough bottles)` : ""; |
| 494 | const line = result.success |
| 495 | ? `Bottled ${result.produced}× ${name}${lost}.` |
| 496 | : `Unstable brew: ${result.produced}× ${name}${lost}.`; |
| 497 | if (result.potion?.potionKey === "dye") |
| 498 | { |
| 499 | localDyeSlots = emptyDyeSlots(); |
| 500 | if (dyeStatusEl) |
| 501 | { dyeStatusEl.textContent = line; } |
| 502 | render(); |
| 503 | } |
| 504 | else |
| 505 | { |
| 506 | localSlots = emptyAlchemySlots(); |
| 507 | if (statusEl) |
| 508 | { statusEl.textContent = line; } |
| 509 | render(); |
| 510 | } |
| 511 | } |
| 512 | }, |
| 513 | refresh () |
| 514 | { render(); } |
| 515 | }; |
| 516 | } |