import { mountLayerPreview } from "./characterPreview.js"; import { formatRestartCountdown } from "/shared/deploy/schedule.js"; import { BOUNTY_PAYOFF_COINS_PER_POINT } from "/shared/bounty/pricing.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 payoffBox = document.getElementById("bounty-payoff"); const payoffRateEl = document.getElementById("bounty-payoff-rate"); const payAmountInput = document.getElementById("bounty-pay-amount"); const decayHintEl = document.getElementById("bounty-decay-hint"); 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"; let myAmountCopper = 0; let payoffCoinsPerPoint = BOUNTY_PAYOFF_COINS_PER_POINT; let nextDecayAt = null; let decayTick = null; 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; } } function syncPayoffUi () { const rate = Math.max(1, payoffCoinsPerPoint | 0); if (payoffRateEl) { payoffRateEl.textContent = `Every ${rate} coins paid clears 1 coin of bounty.`; } if (payAmountInput) { payAmountInput.min = String(rate); payAmountInput.step = String(rate); } if (payoffBox) { payoffBox.hidden = !(myAmountCopper > 0); } syncDecayHint(); } function syncDecayHint () { if (!decayHintEl) { return; } if (!(myAmountCopper > 0) || !nextDecayAt) { decayHintEl.textContent = "Your bounty drops by 1 coin each day at midnight."; return; } const remaining = Math.max(0, nextDecayAt - Date.now()); decayHintEl.textContent = remaining > 0 ? `Drops by 1 coin in ${formatRestartCountdown(remaining)} (daily at midnight).` : "Drops by 1 coin at midnight."; } function startDecayTick () { stopDecayTick(); syncDecayHint(); if (!(myAmountCopper > 0) || !nextDecayAt) { return; } decayTick = window.setInterval(syncDecayHint, 1000); } function stopDecayTick () { if (decayTick != null) { clearInterval(decayTick); decayTick = null; } } 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 }); }); document.getElementById("bounty-pay")?.addEventListener("click", () => { if (!(myAmountCopper > 0)) { statusEl.textContent = "There is no bounty on your head."; return; } const amountCopper = Math.floor(Number(payAmountInput?.value) || 0); if (amountCopper <= 0) { statusEl.textContent = "Enter a positive coin amount."; return; } statusEl.textContent = "Paying bounty…"; socket.send({ type: MessageType.payBounty, 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 = ""; stopDecayTick(); socket.send({ type: MessageType.closeBounty }); } return { isOpen: () => open, isBlocking: () => open, close, handleSnapshot (message) { open = true; panel.hidden = false; backdrop.hidden = false; setTab(activeTab); myAmountCopper = message.myAmountCopper | 0; payoffCoinsPerPoint = message.payoffCoinsPerPoint | 0 || BOUNTY_PAYOFF_COINS_PER_POINT; nextDecayAt = Number(message.nextDecayAt) || null; myAmountEl.textContent = myAmountCopper > 0 ? message.myAmountLabel || String(myAmountCopper) : "None"; syncPayoffUi(); startDecayTick(); if (message.paidOff) { statusEl.textContent = message.paidOff.message ?? "Bounty updated."; } else 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; stopDecayTick(); } }; }