BlueGit

timfa questward

Files

questward / client / src / play / statusEffects.js

statusEffects.js

statusEffects.js 4.8 KiB Download Raw
1import {
2 statusEffectDisplay,
3 statusEffectIconPath,
4 formatStatusRemaining
5} from "/shared/statusEffects/display.js";
6
7export function initStatusEffects ()
8{
9 let vitals = document.getElementById("vitals");
10 if (!vitals)
11 {
12 vitals = document.createElement("div");
13 vitals.id = "vitals";
14 vitals.hidden = true;
15 document.body.append(vitals);
16 }
17
18 let rail = document.getElementById("status-effects");
19 if (!rail)
20 {
21 rail = document.createElement("div");
22 rail.id = "status-effects";
23 rail.setAttribute("aria-label", "Active effects");
24 vitals.prepend(rail);
25 }
26 else if (rail.parentElement !== vitals)
27 { vitals.prepend(rail); }
28
29 let tip = document.getElementById("status-effect-tooltip");
30 if (!tip)
31 {
32 tip = document.createElement("div");
33 tip.id = "status-effect-tooltip";
34 tip.hidden = true;
35 document.body.append(tip);
36 }
37
38 /** @type {Array<{ type: string, expiresAt: number, amount?: number, amountPerSecond?: number, scale?: number }>} */
39 let effects = [];
40 let hoverType = null;
41 let tickId = 0;
42
43 function remainingMs (entry, now = Date.now())
44 {
45 return Math.max(0, Number(entry.expiresAt) - now);
46 }
47
48 function prune (now = Date.now())
49 {
50 effects = effects.filter((entry) => remainingMs(entry, now) > 0);
51 }
52
53 function hideTip ()
54 {
55 hoverType = null;
56 tip.hidden = true;
57 }
58
59 function showTipFor (entry, anchor)
60 {
61 const display = statusEffectDisplay(entry.type);
62 if (!display)
63 {
64 hideTip();
65 return;
66 }
67 hoverType = entry.type;
68 const left = remainingMs(entry);
69 tip.innerHTML = "";
70 const title = document.createElement("strong");
71 title.textContent = display.title;
72 const body = document.createElement("p");
73 body.textContent = display.describe(entry);
74 const time = document.createElement("p");
75 time.className = "status-effect-tooltip-time";
76 time.textContent = left > 0 ? `${formatStatusRemaining(left)} remaining` : "Expired";
77 tip.append(title, body, time);
78 tip.hidden = false;
79 tip.dataset.polarity = display.polarity;
80 const rect = anchor.getBoundingClientRect();
81 const tipW = tip.offsetWidth || 180;
82 const tipH = tip.offsetHeight || 60;
83 let leftPx = rect.left - tipW - 8;
84 if (leftPx < 8)
85 { leftPx = rect.right + 8; }
86 let topPx = rect.bottom - tipH;
87 if (topPx < 8)
88 { topPx = 8; }
89 if (topPx + tipH > window.innerHeight - 8)
90 { topPx = window.innerHeight - tipH - 8; }
91 tip.style.left = `${leftPx}px`;
92 tip.style.top = `${topPx}px`;
93 }
94
95 function refreshTip ()
96 {
97 if (!hoverType)
98 { return; }
99 const entry = effects.find((row) => row.type === hoverType);
100 const btn = rail.querySelector(`[data-effect-type="${hoverType}"]`);
101 if (!entry || !btn)
102 {
103 hideTip();
104 return;
105 }
106 showTipFor(entry, btn);
107 }
108
109 function render ()
110 {
111 const now = Date.now();
112 prune(now);
113 rail.replaceChildren();
114 for (const entry of effects)
115 {
116 const display = statusEffectDisplay(entry.type);
117 if (!display)
118 { continue; }
119 const btn = document.createElement("button");
120 btn.type = "button";
121 btn.className = `status-effect-icon status-effect-${display.polarity}`;
122 btn.dataset.effectType = entry.type;
123 btn.setAttribute("aria-label", display.title);
124 const img = document.createElement("img");
125 img.src = statusEffectIconPath(entry.type);
126 img.alt = "";
127 img.draggable = false;
128 img.addEventListener("error", () =>
129 {
130 img.remove();
131 btn.classList.add("status-effect-icon-fallback");
132 btn.textContent = display.title.slice(0, 1);
133 }, { once: true });
134 btn.append(img);
135 btn.addEventListener("mouseenter", () => showTipFor(entry, btn));
136 btn.addEventListener("mouseleave", hideTip);
137 btn.addEventListener("focus", () => showTipFor(entry, btn));
138 btn.addEventListener("blur", hideTip);
139 rail.append(btn);
140 }
141 rail.hidden = effects.length === 0;
142 refreshTip();
143 }
144
145 function stopTick ()
146 {
147 if (tickId)
148 {
149 clearInterval(tickId);
150 tickId = 0;
151 }
152 }
153
154 function ensureTick ()
155 {
156 if (tickId || effects.length === 0)
157 { return; }
158 tickId = setInterval(() =>
159 {
160 const before = effects.length;
161 prune();
162 if (effects.length !== before)
163 { render(); }
164 else
165 { refreshTip(); }
166 if (effects.length === 0)
167 { stopTick(); }
168 }, 250);
169 }
170
171 return {
172 apply (message)
173 {
174 const list = Array.isArray(message?.effects) ? message.effects : [];
175 effects = list
176 .filter((entry) => entry?.type && Number(entry.expiresAt) > Date.now())
177 .map((entry) => ({
178 type: entry.type,
179 expiresAt: Number(entry.expiresAt) || 0,
180 amount: entry.amount | 0,
181 amountPerSecond: Number(entry.amountPerSecond) || 0,
182 scale: entry.scale ?? null,
183 lightRadiusTiles: entry.lightRadiusTiles ?? null
184 }));
185 render();
186 if (effects.length)
187 { ensureTick(); }
188 else
189 {
190 stopTick();
191 hideTip();
192 }
193 },
194 clear ()
195 {
196 effects = [];
197 stopTick();
198 hideTip();
199 render();
200 },
201 show ()
202 {
203 vitals.hidden = false;
204 render();
205 },
206 hide ()
207 {
208 this.clear();
209 }
210 };
211}