BlueGit

timfa questward

Files

questward / client / src / play / bounty.js

bounty.js

bounty.js 4.8 KiB Download Raw
1import { mountLayerPreview } from "./characterPreview.js";
2
3export function initBounty ({
4 socket,
5 MessageType,
6 resolveSpriteUrl,
7 resolveImage = null,
8 onSuppressWorldClick
9})
10{
11 const backdrop = document.getElementById("bounty-backdrop");
12 const panel = document.getElementById("bounty-panel");
13 const statusEl = document.getElementById("bounty-status");
14 const myAmountEl = document.getElementById("bounty-my-amount");
15 const previewEl = document.getElementById("bounty-preview");
16 const targetLabel = document.getElementById("bounty-target-label");
17 const targetAmount = document.getElementById("bounty-target-amount");
18 const firstInput = document.getElementById("bounty-search-first");
19 const lastInput = document.getElementById("bounty-search-last");
20 const amountInput = document.getElementById("bounty-amount");
21 const tabs = [...document.querySelectorAll(".bounty-tab")];
22 const panes = [...document.querySelectorAll(".bounty-pane")];
23
24 let open = false;
25 /** @type {null | { characterId: number, firstName: string, lastName: string, amountCopper: number }} */
26 let target = null;
27 let activeTab = "place";
28
29 function suppressWorldClick ()
30 {
31 onSuppressWorldClick?.();
32 }
33
34 function setTab (tab)
35 {
36 activeTab = tab;
37 for (const button of tabs)
38 { button.classList.toggle("active", button.dataset.tab === tab); }
39 for (const pane of panes)
40 { pane.hidden = pane.dataset.pane !== tab; }
41 }
42
43 for (const button of tabs)
44 { button.addEventListener("click", () => setTab(button.dataset.tab)); }
45
46 document.getElementById("bounty-close")?.addEventListener("click", () => close());
47 backdrop?.addEventListener("mousedown", (event) =>
48 {
49 event.preventDefault();
50 suppressWorldClick();
51 });
52 backdrop?.addEventListener("click", () => close());
53 panel?.addEventListener("mousedown", (event) =>
54 {
55 event.stopPropagation();
56 suppressWorldClick();
57 });
58
59 document.getElementById("bounty-search")?.addEventListener("click", () =>
60 {
61 const firstName = firstInput.value.trim();
62 const lastName = lastInput.value.trim();
63 if (!firstName || !lastName)
64 {
65 statusEl.textContent = "Enter a first and last name.";
66 return;
67 }
68 statusEl.textContent = "Searching…";
69 socket.send({ type: MessageType.getCharacterBounty, firstName, lastName });
70 });
71
72 document.getElementById("bounty-raise")?.addEventListener("click", () =>
73 {
74 if (!target)
75 {
76 statusEl.textContent = "Search for a target first.";
77 return;
78 }
79 const amountCopper = Math.floor(Number(amountInput.value) || 0);
80 if (amountCopper <= 0)
81 {
82 statusEl.textContent = "Enter a positive coin amount.";
83 return;
84 }
85 statusEl.textContent = "Placing bounty…";
86 socket.send({
87 type: MessageType.raiseBounty,
88 firstName: target.firstName,
89 lastName: target.lastName,
90 amountCopper
91 });
92 });
93
94 function clearTarget ()
95 {
96 target = null;
97 previewEl.replaceChildren();
98 targetLabel.textContent = "No target selected.";
99 targetAmount.textContent = "";
100 }
101
102 function renderPreview (layers)
103 {
104 previewEl.replaceChildren();
105 previewEl.append(mountLayerPreview(layers, {
106 resolveImage,
107 resolveSpriteUrl,
108 size: 64,
109 className: "char-preview"
110 }));
111 }
112
113 function showTarget (entry)
114 {
115 if (!entry)
116 {
117 clearTarget();
118 statusEl.textContent = "No adventurer by that name.";
119 return;
120 }
121 target = {
122 characterId: entry.characterId,
123 firstName: entry.firstName,
124 lastName: entry.lastName,
125 amountCopper: entry.amountCopper | 0
126 };
127 targetLabel.textContent = `${entry.firstName} ${entry.lastName}`;
128 targetAmount.textContent = (entry.amountCopper | 0) > 0
129 ? `Current bounty: ${entry.amountLabel ?? entry.amountCopper}`
130 : "No bounty yet.";
131 renderPreview(entry.layers);
132 statusEl.textContent = "";
133 }
134
135 function close ()
136 {
137 if (!open)
138 { return; }
139 open = false;
140 panel.hidden = true;
141 backdrop.hidden = true;
142 clearTarget();
143 statusEl.textContent = "";
144 socket.send({ type: MessageType.closeBounty });
145 }
146
147 return {
148 isOpen: () => open,
149 isBlocking: () => open,
150 close,
151 handleSnapshot (message)
152 {
153 open = true;
154 panel.hidden = false;
155 backdrop.hidden = false;
156 setTab(activeTab);
157 myAmountEl.textContent = (message.myAmountCopper | 0) > 0
158 ? message.myAmountLabel || String(message.myAmountCopper)
159 : "None";
160 if (message.raised)
161 {
162 statusEl.textContent = message.raised.message ?? "Bounty updated.";
163 if (target && target.characterId === message.raised.characterId)
164 {
165 target.amountCopper = message.raised.amountCopper | 0;
166 targetAmount.textContent = `Current bounty: ${message.raised.amountLabel}`;
167 }
168 }
169 else if (message.message && !(message.myAmountCopper > 0 && activeTab === "mine"))
170 { statusEl.textContent = ""; }
171 },
172 handleCharacterBounty (message)
173 {
174 if (!open)
175 { return; }
176 showTarget(message.target);
177 },
178 handleClosed ()
179 {
180 open = false;
181 panel.hidden = true;
182 backdrop.hidden = true;
183 }
184 };
185}