BlueGit

timfa questward

Files

questward / client / src / play / tipArrows.js

tipArrows.js

tipArrows.js 6.6 KiB Download Raw
1import { ItemId, itemDefinition, itemDisplayName } from "/shared/definitions/items.js";
2import { EquipmentKind } from "/shared/definitions/equipmentKinds.js";
3import { ARROW_COATING_ARROWS_PER_USE, alchemyPotionDefinition } from "/shared/definitions/alchemy.js";
4import { stackQuantity } from "/shared/items/inventory.js";
5import { applyItemIcon } from "../render/potionIcon.js";
6
7function coatingEffect (stack)
8{
9 if (!stack || stack.itemId !== ItemId.potion || stack.instance?.kind !== "potion")
10 { return null; }
11 if (stack.instance.effect?.type === "arrow-coating")
12 { return stack.instance.effect; }
13 const def = alchemyPotionDefinition(stack.instance.potionKey);
14 return def?.effect?.type === "arrow-coating" ? def.effect : null;
15}
16
17/**
18 * Modal: apply arrow coating potion to untipped arrows in the bag.
19 */
20export function initTipArrows ({
21 socket,
22 MessageType,
23 getInventoryBag,
24 root = document.body
25})
26{
27 const backdrop = document.createElement("div");
28 backdrop.className = "modal-backdrop";
29 backdrop.hidden = true;
30 backdrop.id = "tip-arrows-backdrop";
31
32 const panel = document.createElement("div");
33 panel.id = "tip-arrows-panel";
34 panel.innerHTML = `
35 <h2>Apply to arrows</h2>
36 <p class="tip-arrows-hint"></p>
37 <div class="tip-arrows-row">
38 <div class="tip-arrows-slot" data-role="coating"></div>
39 <span class="tip-arrows-arrow">→</span>
40 <button type="button" class="tip-arrows-slot tip-arrows-pick" data-role="arrow" title="Choose arrows"></button>
41 </div>
42 <label class="tip-arrows-qty-label">Quantity
43 <input type="number" class="tip-arrows-qty" min="1" value="1" />
44 </label>
45 <p class="tip-arrows-capacity"></p>
46 <div class="tip-arrows-actions">
47 <button type="button" class="tip-arrows-apply">Apply</button>
48 <button type="button" class="tip-arrows-cancel">Cancel</button>
49 </div>
50 <div class="tip-arrows-picker" hidden></div>
51 `;
52 backdrop.append(panel);
53 root.append(backdrop);
54
55 const hintEl = panel.querySelector(".tip-arrows-hint");
56 const coatingEl = panel.querySelector('[data-role="coating"]');
57 const arrowEl = panel.querySelector('[data-role="arrow"]');
58 const qtyInput = panel.querySelector(".tip-arrows-qty");
59 const capacityEl = panel.querySelector(".tip-arrows-capacity");
60 const pickerEl = panel.querySelector(".tip-arrows-picker");
61 const applyBtn = panel.querySelector(".tip-arrows-apply");
62 const cancelBtn = panel.querySelector(".tip-arrows-cancel");
63
64 let coatingSlotIndex = -1;
65 let arrowSlotIndex = -1;
66
67 function bag ()
68 {
69 return getInventoryBag?.() ?? null;
70 }
71
72 function coatingStack ()
73 {
74 return bag()?.slots?.[coatingSlotIndex] ?? null;
75 }
76
77 function arrowStack ()
78 {
79 return bag()?.slots?.[arrowSlotIndex] ?? null;
80 }
81
82 function coatingCharges ()
83 {
84 const stack = coatingStack();
85 if (!stack)
86 { return 0; }
87 const fill = Math.max(1, Math.min(5, stack.instance?.fillLevel | 0 || 4));
88 return stackQuantity(stack.quantity) * fill;
89 }
90
91 function maxTipWithCoating ()
92 {
93 return coatingCharges() * ARROW_COATING_ARROWS_PER_USE;
94 }
95
96 function maxTipForSelection ()
97 {
98 const arrows = arrowStack();
99 const arrowQty = arrows ? stackQuantity(arrows.quantity) : 0;
100 return Math.min(arrowQty, ARROW_COATING_ARROWS_PER_USE, maxTipWithCoating());
101 }
102
103 function renderSlot (el, stack)
104 {
105 el.replaceChildren();
106 if (!stack)
107 {
108 el.textContent = "+";
109 return;
110 }
111 const img = document.createElement("img");
112 img.alt = "";
113 applyItemIcon(img, stack);
114 el.append(img);
115 const qty = stackQuantity(stack.quantity);
116 if (qty > 1)
117 {
118 const span = document.createElement("span");
119 span.className = "qty";
120 span.textContent = String(qty);
121 el.append(span);
122 }
123 }
124
125 function refresh ()
126 {
127 const coating = coatingStack();
128 const name = coating?.instance?.displayName ?? "Coating";
129 hintEl.textContent = `${name} tips up to ${ARROW_COATING_ARROWS_PER_USE} arrows per use.`;
130 renderSlot(coatingEl, coating);
131 renderSlot(arrowEl, arrowStack());
132 const max = maxTipForSelection();
133 qtyInput.max = String(Math.max(1, max));
134 if ((qtyInput.value | 0) > max || (qtyInput.value | 0) < 1)
135 { qtyInput.value = String(Math.max(1, max)); }
136 capacityEl.textContent = coating
137 ? `This coating can tip up to ${maxTipWithCoating()} arrows in total.`
138 : "";
139 applyBtn.disabled = !coating || !arrowStack() || max <= 0;
140 }
141
142 function listUntippedArrowSlots ()
143 {
144 const slots = bag()?.slots ?? [];
145 const out = [];
146 for (let i = 0; i < slots.length; i++)
147 {
148 const slot = slots[i];
149 if (!slot)
150 { continue; }
151 const def = itemDefinition(slot.itemId);
152 if (def?.equipment?.kind !== EquipmentKind.arrowAmmo)
153 { continue; }
154 if (slot.instance?.kind === "tipped-arrow")
155 { continue; }
156 out.push({ slotIndex: i, stack: slot });
157 }
158 return out;
159 }
160
161 function openPicker ()
162 {
163 pickerEl.hidden = false;
164 pickerEl.replaceChildren();
165 const options = listUntippedArrowSlots();
166 if (!options.length)
167 {
168 pickerEl.textContent = "No untipped arrows in your bag.";
169 return;
170 }
171 for (const opt of options)
172 {
173 const btn = document.createElement("button");
174 btn.type = "button";
175 btn.className = "tip-arrows-pick-option";
176 const img = document.createElement("img");
177 img.alt = "";
178 applyItemIcon(img, opt.stack);
179 btn.append(img);
180 const label = document.createElement("span");
181 label.textContent = `${itemDisplayName(opt.stack)} ×${stackQuantity(opt.stack.quantity)}`;
182 btn.append(label);
183 btn.addEventListener("click", () =>
184 {
185 arrowSlotIndex = opt.slotIndex;
186 pickerEl.hidden = true;
187 const max = maxTipForSelection();
188 qtyInput.value = String(Math.max(1, max));
189 refresh();
190 });
191 pickerEl.append(btn);
192 }
193 }
194
195 function close ()
196 {
197 backdrop.hidden = true;
198 pickerEl.hidden = true;
199 coatingSlotIndex = -1;
200 arrowSlotIndex = -1;
201 }
202
203 function open (slotIndex)
204 {
205 coatingSlotIndex = slotIndex | 0;
206 arrowSlotIndex = -1;
207 pickerEl.hidden = true;
208 backdrop.hidden = false;
209 qtyInput.value = "1";
210 refresh();
211 }
212
213 arrowEl.addEventListener("click", () => openPicker());
214 cancelBtn.addEventListener("click", () => close());
215 backdrop.addEventListener("click", (event) =>
216 {
217 if (event.target === backdrop)
218 { close(); }
219 });
220 panel.addEventListener("click", (event) => event.stopPropagation());
221 applyBtn.addEventListener("click", () =>
222 {
223 const qty = Math.max(1, qtyInput.value | 0 || 1);
224 if (coatingSlotIndex < 0 || arrowSlotIndex < 0)
225 { return; }
226 socket.send({
227 type: MessageType.tipArrows,
228 coatingSlotIndex,
229 arrowSlotIndex,
230 quantity: qty
231 });
232 close();
233 });
234 qtyInput.addEventListener("change", () => refresh());
235
236 return {
237 open,
238 close,
239 isOpen: () => !backdrop.hidden,
240 isArrowCoatingPotion (stack)
241 {
242 return !!coatingEffect(stack);
243 }
244 };
245}