BlueGit

timfa questward

Files

questward / shared / bounty / pricing.js

pricing.js

pricing.js 2.4 KiB Download Raw
1import { formatQuantity } from "../definitions/items.js";
2import { shopRestockDayKey } from "../shop/pricing.js";
3import { nextLocalMidnightMs } from "../deploy/schedule.js";
4
5/** Coins spent at the office to clear 1 coin of bounty on your own head. */
6export const BOUNTY_PAYOFF_COINS_PER_POINT = 10;
7
8/** @param {number} copper */
9export 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 */
19export 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`). */
28export 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. */
49export 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
56export function bountyDecayDayKey (now = Date.now())
57{
58 return shopRestockDayKey(now);
59}
60
61export function bountyNextDecayAt (now = Date.now())
62{
63 return nextLocalMidnightMs(now);
64}
65
66/**
67 * @param {{ killerName: string, victimName: string, amountCopper: number }} opts
68 */
69export 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}