import { formatQuantity } from "../definitions/items.js"; import { shopRestockDayKey } from "../shop/pricing.js"; import { nextLocalMidnightMs } from "../deploy/schedule.js"; /** Coins spent at the office to clear 1 coin of bounty on your own head. */ export const BOUNTY_PAYOFF_COINS_PER_POINT = 10; /** @param {number} copper */ export function formatBountyCopper (copper) { const amount = Math.max(0, Math.floor(Number(copper) || 0)); return formatQuantity(amount); } /** * How much bounty clears for a given coin payment (capped at current bounty). * @returns {{ reduce: number, costCopper: number }} */ export function bountyPayoffFromCoins (coinsPaid, currentBounty) { const paid = Math.max(0, Math.floor(Number(coinsPaid) || 0)); const current = Math.max(0, Math.floor(Number(currentBounty) || 0)); const reduce = Math.min(current, Math.floor(paid / BOUNTY_PAYOFF_COINS_PER_POINT)); return { reduce, costCopper: reduce * BOUNTY_PAYOFF_COINS_PER_POINT }; } /** Whole calendar days from `fromDayKey` to `toDayKey` (`YYYY-MM-DD`). */ export function bountyDecayDaysBetween (fromDayKey, toDayKey) { const from = String(fromDayKey ?? ""); const to = String(toDayKey ?? ""); if (!from || !to || from === to) { return 0; } const parse = (key) => { const [y, m, d] = key.split("-").map(Number); if (!y || !m || !d) { return null; } return Date.UTC(y, m - 1, d); }; const a = parse(from); const b = parse(to); if (a == null || b == null || b < a) { return 0; } return Math.round((b - a) / 86_400_000); } /** Apply daily −1 bounty decay for `days` elapsed. */ export function applyBountyDailyDecay (amount, days) { const current = Math.max(0, Math.floor(Number(amount) || 0)); const elapsed = Math.max(0, Math.floor(Number(days) || 0)); return Math.max(0, current - elapsed); } export function bountyDecayDayKey (now = Date.now()) { return shopRestockDayKey(now); } export function bountyNextDecayAt (now = Date.now()) { return nextLocalMidnightMs(now); } /** * @param {{ killerName: string, victimName: string, amountCopper: number }} opts */ export function bountyPayoutLetterBody ({ killerName, victimName, amountCopper }) { const killer = String(killerName ?? "Adventurer").trim() || "Adventurer"; const victim = String(victimName ?? "someone").trim() || "someone"; const amount = formatBountyCopper(amountCopper); return [ `Dear ${killer},`, "", `For defeating ${victim}, a bounty of ${amount} has been paid into your bank vault.` ].join("\n"); }