BlueGit

timfa questward

Files

questward / client / src / play / alchemy.js

alchemy.js

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