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