import { mountLayerPreview } from "./characterPreview.js"; export function initBounty ({ socket, MessageType, resolveSpriteUrl, resolveImage = null, onSuppressWorldClick }) { const backdrop = document.getElementById("bounty-backdrop"); const panel = document.getElementById("bounty-panel"); const statusEl = document.getElementById("bounty-status"); const myAmountEl = document.getElementById("bounty-my-amount"); const previewEl = document.getElementById("bounty-preview"); const targetLabel = document.getElementById("bounty-target-label"); const targetAmount = document.getElementById("bounty-target-amount"); const firstInput = document.getElementById("bounty-search-first"); const lastInput = document.getElementById("bounty-search-last"); const amountInput = document.getElementById("bounty-amount"); const tabs = [...document.querySelectorAll(".bounty-tab")]; const panes = [...document.querySelectorAll(".bounty-pane")]; let open = false; /** @type {null | { characterId: number, firstName: string, lastName: string, amountCopper: number }} */ let target = null; let activeTab = "place"; function suppressWorldClick () { onSuppressWorldClick?.(); } function setTab (tab) { activeTab = tab; for (const button of tabs) { button.classList.toggle("active", button.dataset.tab === tab); } for (const pane of panes) { pane.hidden = pane.dataset.pane !== tab; } } for (const button of tabs) { button.addEventListener("click", () => setTab(button.dataset.tab)); } document.getElementById("bounty-close")?.addEventListener("click", () => close()); backdrop?.addEventListener("mousedown", (event) => { event.preventDefault(); suppressWorldClick(); }); backdrop?.addEventListener("click", () => close()); panel?.addEventListener("mousedown", (event) => { event.stopPropagation(); suppressWorldClick(); }); document.getElementById("bounty-search")?.addEventListener("click", () => { const firstName = firstInput.value.trim(); const lastName = lastInput.value.trim(); if (!firstName || !lastName) { statusEl.textContent = "Enter a first and last name."; return; } statusEl.textContent = "Searching…"; socket.send({ type: MessageType.getCharacterBounty, firstName, lastName }); }); document.getElementById("bounty-raise")?.addEventListener("click", () => { if (!target) { statusEl.textContent = "Search for a target first."; return; } const amountCopper = Math.floor(Number(amountInput.value) || 0); if (amountCopper <= 0) { statusEl.textContent = "Enter a positive coin amount."; return; } statusEl.textContent = "Placing bounty…"; socket.send({ type: MessageType.raiseBounty, firstName: target.firstName, lastName: target.lastName, amountCopper }); }); function clearTarget () { target = null; previewEl.replaceChildren(); targetLabel.textContent = "No target selected."; targetAmount.textContent = ""; } function renderPreview (layers) { previewEl.replaceChildren(); previewEl.append(mountLayerPreview(layers, { resolveImage, resolveSpriteUrl, size: 64, className: "char-preview" })); } function showTarget (entry) { if (!entry) { clearTarget(); statusEl.textContent = "No adventurer by that name."; return; } target = { characterId: entry.characterId, firstName: entry.firstName, lastName: entry.lastName, amountCopper: entry.amountCopper | 0 }; targetLabel.textContent = `${entry.firstName} ${entry.lastName}`; targetAmount.textContent = (entry.amountCopper | 0) > 0 ? `Current bounty: ${entry.amountLabel ?? entry.amountCopper}` : "No bounty yet."; renderPreview(entry.layers); statusEl.textContent = ""; } function close () { if (!open) { return; } open = false; panel.hidden = true; backdrop.hidden = true; clearTarget(); statusEl.textContent = ""; socket.send({ type: MessageType.closeBounty }); } return { isOpen: () => open, isBlocking: () => open, close, handleSnapshot (message) { open = true; panel.hidden = false; backdrop.hidden = false; setTab(activeTab); myAmountEl.textContent = (message.myAmountCopper | 0) > 0 ? message.myAmountLabel || String(message.myAmountCopper) : "None"; if (message.raised) { statusEl.textContent = message.raised.message ?? "Bounty updated."; if (target && target.characterId === message.raised.characterId) { target.amountCopper = message.raised.amountCopper | 0; targetAmount.textContent = `Current bounty: ${message.raised.amountLabel}`; } } else if (message.message && !(message.myAmountCopper > 0 && activeTab === "mine")) { statusEl.textContent = ""; } }, handleCharacterBounty (message) { if (!open) { return; } showTarget(message.target); }, handleClosed () { open = false; panel.hidden = true; backdrop.hidden = true; } }; }