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