| 1 | import { |
| 2 | itemDefinition, |
| 3 | itemDisplayName, |
| 4 | itemAssetPathForStack, |
| 5 | formatQuantity, |
| 6 | ItemId |
| 7 | } from "/shared/definitions/items.js"; |
| 8 | import { AlchemyStepKind, CAULDRON_SLOT_COUNT } from "/shared/definitions/alchemy.js"; |
| 9 | import { applyItemIcon } from "../render/potionIcon.js"; |
| 10 | |
| 11 | export function initAlchemy ({ |
| 12 | socket, |
| 13 | MessageType, |
| 14 | getInventorySlots |
| 15 | }) |
| 16 | { |
| 17 | const backdrop = document.getElementById("cauldron-backdrop"); |
| 18 | const panel = document.getElementById("cauldron-panel"); |
| 19 | const slotsEl = document.getElementById("cauldron-slots"); |
| 20 | const insertsEl = document.getElementById("cauldron-inserts"); |
| 21 | const stepsEl = document.getElementById("cauldron-steps"); |
| 22 | const statusEl = document.getElementById("cauldron-status"); |
| 23 | const saveBtn = document.getElementById("cauldron-save-recipe"); |
| 24 | const pickBackdrop = document.getElementById("cauldron-pick-backdrop"); |
| 25 | const pickPanel = document.getElementById("cauldron-pick-panel"); |
| 26 | const pickList = document.getElementById("cauldron-pick-list"); |
| 27 | const pickQty = document.getElementById("cauldron-pick-qty"); |
| 28 | |
| 29 | let open = false; |
| 30 | let state = null; |
| 31 | let pickSlotIndex = null; |
| 32 | let pickItemId = null; |
| 33 | |
| 34 | document.getElementById("cauldron-close")?.addEventListener("click", () => close()); |
| 35 | backdrop?.addEventListener("click", () => close()); |
| 36 | document.getElementById("cauldron-stir")?.addEventListener("click", () => |
| 37 | { socket.send({ type: MessageType.cauldronAction, action: AlchemyStepKind.stir }); }); |
| 38 | document.getElementById("cauldron-heat")?.addEventListener("click", () => |
| 39 | { socket.send({ type: MessageType.cauldronAction, action: AlchemyStepKind.heat }); }); |
| 40 | document.getElementById("cauldron-cool")?.addEventListener("click", () => |
| 41 | { socket.send({ type: MessageType.cauldronAction, action: AlchemyStepKind.cool }); }); |
| 42 | document.getElementById("cauldron-bottle")?.addEventListener("click", () => |
| 43 | { socket.send({ type: MessageType.cauldronBottle }); }); |
| 44 | document.getElementById("cauldron-clear")?.addEventListener("click", () => |
| 45 | { socket.send({ type: MessageType.cauldronClear }); }); |
| 46 | saveBtn?.addEventListener("click", () => |
| 47 | { socket.send({ type: MessageType.cauldronSaveRecipe }); }); |
| 48 | document.getElementById("cauldron-pick-cancel")?.addEventListener("click", () => closePick()); |
| 49 | document.getElementById("cauldron-pick-confirm")?.addEventListener("click", () => confirmPick()); |
| 50 | pickBackdrop?.addEventListener("click", () => closePick()); |
| 51 | |
| 52 | function render () |
| 53 | { |
| 54 | if (!open || !panel) |
| 55 | { return; } |
| 56 | renderSlots(); |
| 57 | renderSteps(); |
| 58 | if (saveBtn) |
| 59 | { saveBtn.hidden = !state?.canSaveRecipe; } |
| 60 | } |
| 61 | |
| 62 | function renderSlots () |
| 63 | { |
| 64 | slotsEl.replaceChildren(); |
| 65 | insertsEl.replaceChildren(); |
| 66 | for (let i = 0; i < CAULDRON_SLOT_COUNT; i++) |
| 67 | { |
| 68 | const reserved = state?.slots?.[i] ?? null; |
| 69 | const slotBtn = document.createElement("button"); |
| 70 | slotBtn.type = "button"; |
| 71 | slotBtn.className = "cauldron-slot"; |
| 72 | if (reserved) |
| 73 | { |
| 74 | const img = document.createElement("img"); |
| 75 | const stack = { itemId: reserved.itemId, quantity: reserved.quantity }; |
| 76 | applyItemIcon(img, stack, itemAssetPathForStack(stack)); |
| 77 | img.alt = itemDisplayName(stack); |
| 78 | const qty = document.createElement("span"); |
| 79 | qty.className = "qty"; |
| 80 | qty.textContent = formatQuantity(reserved.quantity); |
| 81 | slotBtn.append(img, qty); |
| 82 | slotBtn.title = `${itemDisplayName(stack)} ×${reserved.quantity}`; |
| 83 | } |
| 84 | else |
| 85 | { slotBtn.textContent = "+"; } |
| 86 | slotBtn.addEventListener("click", () => openPick(i)); |
| 87 | slotsEl.append(slotBtn); |
| 88 | |
| 89 | const insertBtn = document.createElement("button"); |
| 90 | insertBtn.type = "button"; |
| 91 | insertBtn.className = "cauldron-insert"; |
| 92 | insertBtn.textContent = "↓"; |
| 93 | insertBtn.disabled = !reserved; |
| 94 | insertBtn.title = "Insert into cauldron"; |
| 95 | insertBtn.addEventListener("click", () => |
| 96 | { socket.send({ type: MessageType.cauldronInsert, slotIndex: i }); }); |
| 97 | insertsEl.append(insertBtn); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | function renderSteps () |
| 102 | { |
| 103 | stepsEl.replaceChildren(); |
| 104 | for (const step of state?.steps ?? []) |
| 105 | { |
| 106 | const li = document.createElement("li"); |
| 107 | if (step.kind === AlchemyStepKind.insert) |
| 108 | { |
| 109 | const def = itemDefinition(step.itemId); |
| 110 | li.textContent = `insert ${def?.displayName ?? step.itemId}`; |
| 111 | } |
| 112 | else |
| 113 | { li.textContent = step.kind; } |
| 114 | stepsEl.append(li); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | function openPick (slotIndex) |
| 119 | { |
| 120 | pickSlotIndex = slotIndex; |
| 121 | pickItemId = null; |
| 122 | pickList.replaceChildren(); |
| 123 | const counts = new Map(); |
| 124 | for (const slot of getInventorySlots?.() ?? []) |
| 125 | { |
| 126 | if (!slot) |
| 127 | { continue; } |
| 128 | const def = itemDefinition(slot.itemId); |
| 129 | if (!def || def.uniqueInstance || def.equipment |
| 130 | || slot.itemId === ItemId.potion || slot.itemId === ItemId.glassBottle) |
| 131 | { continue; } |
| 132 | counts.set(slot.itemId, (counts.get(slot.itemId) ?? 0) + (slot.quantity | 0)); |
| 133 | } |
| 134 | for (const [itemId, quantity] of counts) |
| 135 | { |
| 136 | const stack = { itemId, quantity }; |
| 137 | const btn = document.createElement("button"); |
| 138 | btn.type = "button"; |
| 139 | btn.className = "cauldron-pick-item"; |
| 140 | const img = document.createElement("img"); |
| 141 | applyItemIcon(img, stack, itemAssetPathForStack(stack)); |
| 142 | const label = document.createElement("span"); |
| 143 | label.textContent = `${itemDisplayName(stack)} (${formatQuantity(quantity)})`; |
| 144 | btn.append(img, label); |
| 145 | btn.addEventListener("click", () => |
| 146 | { |
| 147 | pickItemId = itemId; |
| 148 | pickQty.max = String(quantity); |
| 149 | pickQty.value = String(Math.min(quantity, Number(pickQty.value) || 1)); |
| 150 | for (const child of pickList.children) |
| 151 | { child.classList.toggle("selected", child === btn); } |
| 152 | }); |
| 153 | pickList.append(btn); |
| 154 | } |
| 155 | pickQty.value = "1"; |
| 156 | pickBackdrop.hidden = false; |
| 157 | pickPanel.hidden = false; |
| 158 | } |
| 159 | |
| 160 | function closePick () |
| 161 | { |
| 162 | pickBackdrop.hidden = true; |
| 163 | pickPanel.hidden = true; |
| 164 | pickSlotIndex = null; |
| 165 | pickItemId = null; |
| 166 | } |
| 167 | |
| 168 | function confirmPick () |
| 169 | { |
| 170 | if (pickSlotIndex == null || pickItemId == null) |
| 171 | { return; } |
| 172 | const quantity = Math.max(1, pickQty.value | 0); |
| 173 | socket.send({ |
| 174 | type: MessageType.cauldronSetSlot, |
| 175 | slotIndex: pickSlotIndex, |
| 176 | itemId: pickItemId, |
| 177 | quantity |
| 178 | }); |
| 179 | closePick(); |
| 180 | } |
| 181 | |
| 182 | function openFromServer (nextState) |
| 183 | { |
| 184 | open = true; |
| 185 | state = nextState; |
| 186 | backdrop.hidden = false; |
| 187 | panel.hidden = false; |
| 188 | statusEl.textContent = ""; |
| 189 | render(); |
| 190 | } |
| 191 | |
| 192 | function close () |
| 193 | { |
| 194 | if (!open) |
| 195 | { return; } |
| 196 | open = false; |
| 197 | backdrop.hidden = true; |
| 198 | panel.hidden = true; |
| 199 | closePick(); |
| 200 | socket.send({ type: MessageType.cauldronClose }); |
| 201 | } |
| 202 | |
| 203 | return { |
| 204 | isBlocking () |
| 205 | { return open || !pickPanel?.hidden; }, |
| 206 | reset () |
| 207 | { |
| 208 | open = false; |
| 209 | state = null; |
| 210 | if (backdrop) |
| 211 | { backdrop.hidden = true; } |
| 212 | if (panel) |
| 213 | { panel.hidden = true; } |
| 214 | closePick(); |
| 215 | }, |
| 216 | handleSnapshot (message) |
| 217 | { |
| 218 | if (!message.open) |
| 219 | { |
| 220 | open = false; |
| 221 | if (backdrop) |
| 222 | { backdrop.hidden = true; } |
| 223 | if (panel) |
| 224 | { panel.hidden = true; } |
| 225 | state = null; |
| 226 | return; |
| 227 | } |
| 228 | if (!open) |
| 229 | { openFromServer(message.state); } |
| 230 | else |
| 231 | { |
| 232 | state = message.state; |
| 233 | render(); |
| 234 | } |
| 235 | if (message.alchemyResult) |
| 236 | { |
| 237 | const result = message.alchemyResult; |
| 238 | const name = result.potion?.displayName ?? "Potion"; |
| 239 | const lost = result.lost > 0 ? ` (${result.lost} lost — not enough bottles)` : ""; |
| 240 | statusEl.textContent = result.success |
| 241 | ? `Bottled ${result.produced}× ${name}${lost}.` |
| 242 | : `Unstable brew: ${result.produced}× ${name}${lost}.`; |
| 243 | } |
| 244 | }, |
| 245 | refresh () |
| 246 | { render(); } |
| 247 | }; |
| 248 | } |