BlueGit

timfa questward

Files

questward / client / src / play / bounty.js

bounty.js

bounty.js 7.2 KiB Download Raw
1import { mountLayerPreview } from "./characterPreview.js";
2import { formatRestartCountdown } from "/shared/deploy/schedule.js";
3import { BOUNTY_PAYOFF_COINS_PER_POINT } from "/shared/bounty/pricing.js";
4
5export function initBounty ({
6 socket,
7 MessageType,
8 resolveSpriteUrl,
9 resolveImage = null,
10 onSuppressWorldClick
11})
12{
13 const backdrop = document.getElementById("bounty-backdrop");
14 const panel = document.getElementById("bounty-panel");
15 const statusEl = document.getElementById("bounty-status");
16 const myAmountEl = document.getElementById("bounty-my-amount");
17 const payoffBox = document.getElementById("bounty-payoff");
18 const payoffRateEl = document.getElementById("bounty-payoff-rate");
19 const payAmountInput = document.getElementById("bounty-pay-amount");
20 const decayHintEl = document.getElementById("bounty-decay-hint");
21 const previewEl = document.getElementById("bounty-preview");
22 const targetLabel = document.getElementById("bounty-target-label");
23 const targetAmount = document.getElementById("bounty-target-amount");
24 const firstInput = document.getElementById("bounty-search-first");
25 const lastInput = document.getElementById("bounty-search-last");
26 const amountInput = document.getElementById("bounty-amount");
27 const tabs = [...document.querySelectorAll(".bounty-tab")];
28 const panes = [...document.querySelectorAll(".bounty-pane")];
29
30 let open = false;
31 /** @type {null | { characterId: number, firstName: string, lastName: string, amountCopper: number }} */
32 let target = null;
33 let activeTab = "place";
34 let myAmountCopper = 0;
35 let payoffCoinsPerPoint = BOUNTY_PAYOFF_COINS_PER_POINT;
36 let nextDecayAt = null;
37 let decayTick = null;
38
39 function suppressWorldClick ()
40 {
41 onSuppressWorldClick?.();
42 }
43
44 function setTab (tab)
45 {
46 activeTab = tab;
47 for (const button of tabs)
48 { button.classList.toggle("active", button.dataset.tab === tab); }
49 for (const pane of panes)
50 { pane.hidden = pane.dataset.pane !== tab; }
51 }
52
53 function syncPayoffUi ()
54 {
55 const rate = Math.max(1, payoffCoinsPerPoint | 0);
56 if (payoffRateEl)
57 {
58 payoffRateEl.textContent = `Every ${rate} coins paid clears 1 coin of bounty.`;
59 }
60 if (payAmountInput)
61 {
62 payAmountInput.min = String(rate);
63 payAmountInput.step = String(rate);
64 }
65 if (payoffBox)
66 { payoffBox.hidden = !(myAmountCopper > 0); }
67 syncDecayHint();
68 }
69
70 function syncDecayHint ()
71 {
72 if (!decayHintEl)
73 { return; }
74 if (!(myAmountCopper > 0) || !nextDecayAt)
75 {
76 decayHintEl.textContent = "Your bounty drops by 1 coin each day at midnight.";
77 return;
78 }
79 const remaining = Math.max(0, nextDecayAt - Date.now());
80 decayHintEl.textContent = remaining > 0
81 ? `Drops by 1 coin in ${formatRestartCountdown(remaining)} (daily at midnight).`
82 : "Drops by 1 coin at midnight.";
83 }
84
85 function startDecayTick ()
86 {
87 stopDecayTick();
88 syncDecayHint();
89 if (!(myAmountCopper > 0) || !nextDecayAt)
90 { return; }
91 decayTick = window.setInterval(syncDecayHint, 1000);
92 }
93
94 function stopDecayTick ()
95 {
96 if (decayTick != null)
97 {
98 clearInterval(decayTick);
99 decayTick = null;
100 }
101 }
102
103 for (const button of tabs)
104 { button.addEventListener("click", () => setTab(button.dataset.tab)); }
105
106 document.getElementById("bounty-close")?.addEventListener("click", () => close());
107 backdrop?.addEventListener("mousedown", (event) =>
108 {
109 event.preventDefault();
110 suppressWorldClick();
111 });
112 backdrop?.addEventListener("click", () => close());
113 panel?.addEventListener("mousedown", (event) =>
114 {
115 event.stopPropagation();
116 suppressWorldClick();
117 });
118
119 document.getElementById("bounty-search")?.addEventListener("click", () =>
120 {
121 const firstName = firstInput.value.trim();
122 const lastName = lastInput.value.trim();
123 if (!firstName || !lastName)
124 {
125 statusEl.textContent = "Enter a first and last name.";
126 return;
127 }
128 statusEl.textContent = "Searching…";
129 socket.send({ type: MessageType.getCharacterBounty, firstName, lastName });
130 });
131
132 document.getElementById("bounty-raise")?.addEventListener("click", () =>
133 {
134 if (!target)
135 {
136 statusEl.textContent = "Search for a target first.";
137 return;
138 }
139 const amountCopper = Math.floor(Number(amountInput.value) || 0);
140 if (amountCopper <= 0)
141 {
142 statusEl.textContent = "Enter a positive coin amount.";
143 return;
144 }
145 statusEl.textContent = "Placing bounty…";
146 socket.send({
147 type: MessageType.raiseBounty,
148 firstName: target.firstName,
149 lastName: target.lastName,
150 amountCopper
151 });
152 });
153
154 document.getElementById("bounty-pay")?.addEventListener("click", () =>
155 {
156 if (!(myAmountCopper > 0))
157 {
158 statusEl.textContent = "There is no bounty on your head.";
159 return;
160 }
161 const amountCopper = Math.floor(Number(payAmountInput?.value) || 0);
162 if (amountCopper <= 0)
163 {
164 statusEl.textContent = "Enter a positive coin amount.";
165 return;
166 }
167 statusEl.textContent = "Paying bounty…";
168 socket.send({ type: MessageType.payBounty, amountCopper });
169 });
170
171 function clearTarget ()
172 {
173 target = null;
174 previewEl.replaceChildren();
175 targetLabel.textContent = "No target selected.";
176 targetAmount.textContent = "";
177 }
178
179 function renderPreview (layers)
180 {
181 previewEl.replaceChildren();
182 previewEl.append(mountLayerPreview(layers, {
183 resolveImage,
184 resolveSpriteUrl,
185 size: 64,
186 className: "char-preview"
187 }));
188 }
189
190 function showTarget (entry)
191 {
192 if (!entry)
193 {
194 clearTarget();
195 statusEl.textContent = "No adventurer by that name.";
196 return;
197 }
198 target = {
199 characterId: entry.characterId,
200 firstName: entry.firstName,
201 lastName: entry.lastName,
202 amountCopper: entry.amountCopper | 0
203 };
204 targetLabel.textContent = `${entry.firstName} ${entry.lastName}`;
205 targetAmount.textContent = (entry.amountCopper | 0) > 0
206 ? `Current bounty: ${entry.amountLabel ?? entry.amountCopper}`
207 : "No bounty yet.";
208 renderPreview(entry.layers);
209 statusEl.textContent = "";
210 }
211
212 function close ()
213 {
214 if (!open)
215 { return; }
216 open = false;
217 panel.hidden = true;
218 backdrop.hidden = true;
219 clearTarget();
220 statusEl.textContent = "";
221 stopDecayTick();
222 socket.send({ type: MessageType.closeBounty });
223 }
224
225 return {
226 isOpen: () => open,
227 isBlocking: () => open,
228 close,
229 handleSnapshot (message)
230 {
231 open = true;
232 panel.hidden = false;
233 backdrop.hidden = false;
234 setTab(activeTab);
235 myAmountCopper = message.myAmountCopper | 0;
236 payoffCoinsPerPoint = message.payoffCoinsPerPoint | 0 || BOUNTY_PAYOFF_COINS_PER_POINT;
237 nextDecayAt = Number(message.nextDecayAt) || null;
238 myAmountEl.textContent = myAmountCopper > 0
239 ? message.myAmountLabel || String(myAmountCopper)
240 : "None";
241 syncPayoffUi();
242 startDecayTick();
243 if (message.paidOff)
244 { statusEl.textContent = message.paidOff.message ?? "Bounty updated."; }
245 else if (message.raised)
246 {
247 statusEl.textContent = message.raised.message ?? "Bounty updated.";
248 if (target && target.characterId === message.raised.characterId)
249 {
250 target.amountCopper = message.raised.amountCopper | 0;
251 targetAmount.textContent = `Current bounty: ${message.raised.amountLabel}`;
252 }
253 }
254 else if (message.message && !(message.myAmountCopper > 0 && activeTab === "mine"))
255 { statusEl.textContent = ""; }
256 },
257 handleCharacterBounty (message)
258 {
259 if (!open)
260 { return; }
261 showTarget(message.target);
262 },
263 handleClosed ()
264 {
265 open = false;
266 panel.hidden = true;
267 backdrop.hidden = true;
268 stopDecayTick();
269 }
270 };
271}