| 1 | import { formatQuantity } from "../definitions/items.js"; |
| 2 | import { shopRestockDayKey } from "../shop/pricing.js"; |
| 3 | import { nextLocalMidnightMs } from "../deploy/schedule.js"; |
| 4 | |
| 5 | /** Coins spent at the office to clear 1 coin of bounty on your own head. */ |
| 6 | export const BOUNTY_PAYOFF_COINS_PER_POINT = 10; |
| 7 | |
| 8 | /** @param {number} copper */ |
| 9 | export function formatBountyCopper (copper) |
| 10 | { |
| 11 | const amount = Math.max(0, Math.floor(Number(copper) || 0)); |
| 12 | return formatQuantity(amount); |
| 13 | } |
| 14 | |
| 15 | /** |
| 16 | * How much bounty clears for a given coin payment (capped at current bounty). |
| 17 | * @returns {{ reduce: number, costCopper: number }} |
| 18 | */ |
| 19 | export function bountyPayoffFromCoins (coinsPaid, currentBounty) |
| 20 | { |
| 21 | const paid = Math.max(0, Math.floor(Number(coinsPaid) || 0)); |
| 22 | const current = Math.max(0, Math.floor(Number(currentBounty) || 0)); |
| 23 | const reduce = Math.min(current, Math.floor(paid / BOUNTY_PAYOFF_COINS_PER_POINT)); |
| 24 | return { reduce, costCopper: reduce * BOUNTY_PAYOFF_COINS_PER_POINT }; |
| 25 | } |
| 26 | |
| 27 | /** Whole calendar days from `fromDayKey` to `toDayKey` (`YYYY-MM-DD`). */ |
| 28 | export function bountyDecayDaysBetween (fromDayKey, toDayKey) |
| 29 | { |
| 30 | const from = String(fromDayKey ?? ""); |
| 31 | const to = String(toDayKey ?? ""); |
| 32 | if (!from || !to || from === to) |
| 33 | { return 0; } |
| 34 | const parse = (key) => |
| 35 | { |
| 36 | const [y, m, d] = key.split("-").map(Number); |
| 37 | if (!y || !m || !d) |
| 38 | { return null; } |
| 39 | return Date.UTC(y, m - 1, d); |
| 40 | }; |
| 41 | const a = parse(from); |
| 42 | const b = parse(to); |
| 43 | if (a == null || b == null || b < a) |
| 44 | { return 0; } |
| 45 | return Math.round((b - a) / 86_400_000); |
| 46 | } |
| 47 | |
| 48 | /** Apply daily −1 bounty decay for `days` elapsed. */ |
| 49 | export function applyBountyDailyDecay (amount, days) |
| 50 | { |
| 51 | const current = Math.max(0, Math.floor(Number(amount) || 0)); |
| 52 | const elapsed = Math.max(0, Math.floor(Number(days) || 0)); |
| 53 | return Math.max(0, current - elapsed); |
| 54 | } |
| 55 | |
| 56 | export function bountyDecayDayKey (now = Date.now()) |
| 57 | { |
| 58 | return shopRestockDayKey(now); |
| 59 | } |
| 60 | |
| 61 | export function bountyNextDecayAt (now = Date.now()) |
| 62 | { |
| 63 | return nextLocalMidnightMs(now); |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * @param {{ killerName: string, victimName: string, amountCopper: number }} opts |
| 68 | */ |
| 69 | export function bountyPayoutLetterBody ({ killerName, victimName, amountCopper }) |
| 70 | { |
| 71 | const killer = String(killerName ?? "Adventurer").trim() || "Adventurer"; |
| 72 | const victim = String(victimName ?? "someone").trim() || "someone"; |
| 73 | const amount = formatBountyCopper(amountCopper); |
| 74 | return [ |
| 75 | `Dear ${killer},`, |
| 76 | "", |
| 77 | `For defeating ${victim}, a bounty of ${amount} has been paid into your bank vault.` |
| 78 | ].join("\n"); |
| 79 | } |