BlueGit

timfa questward

Files

questward / shared / market / exchange.js

exchange.js

rweaks
timfa Tim Falken · pushed by timfa · 2 months ago
2b34e2dd History
exchange.js 3.2 KiB Download Raw
1/** Central Exchange (marketplace plaza) staff + letter copy. */
2
3export const MARKET_ORDER_MAX_AGE_MS = 30 * 24 * 60 * 60 * 1000;
4
5/** Named clerks; sex drives humanoid body. */
6export const CENTRAL_EXCHANGE_STAFF = Object.freeze([
7 Object.freeze({ name: "Bobby Pennies", sex: "male" }),
8 Object.freeze({ name: "Ada Ledger", sex: "female" }),
9 Object.freeze({ name: "Clive Quill", sex: "male" }),
10 Object.freeze({ name: "Marge Tally", sex: "female" }),
11 Object.freeze({ name: "Nigel Coinwright", sex: "male" }),
12 Object.freeze({ name: "Pearl Balance", sex: "female" }),
13 Object.freeze({ name: "Otto Vault", sex: "male" }),
14 Object.freeze({ name: "Iris Counter", sex: "female" })
15]);
16
17export function centralExchangeStaffCount ()
18{
19 return CENTRAL_EXCHANGE_STAFF.length;
20}
21
22export function pickExchangeClerk (random = Math.random)
23{
24 const index = Math.floor(random() * CENTRAL_EXCHANGE_STAFF.length) % CENTRAL_EXCHANGE_STAFF.length;
25 return CENTRAL_EXCHANGE_STAFF[index].name;
26}
27
28/** Listing fee charged when placing a buy or sell order (non-refundable). */
29export const MARKET_ORDER_FEE_RATE = 0.03;
30
31/** Fee = ceil(3% of unitPrice × quantity), minimum 1 when the notional is positive. */
32export function marketOrderListingFee (unitPrice, quantity)
33{
34 const notional = Math.max(0, Math.floor(Number(unitPrice) || 0))
35 * Math.max(0, Math.floor(Number(quantity) || 0));
36 if (notional <= 0)
37 { return 0; }
38 return Math.max(1, Math.ceil(notional * MARKET_ORDER_FEE_RATE));
39}
40
41export function exchangeSignature (clerkName)
42{
43 return `Signed,\n${clerkName}\nCentral Exchange`;
44}
45
46export function soldLetterBody ({ itemName, quantity, unitPrice, clerkName })
47{
48 return [
49 `Good news — your sell order for ${quantity}× ${itemName} has been filled at ${unitPrice} coins each.`,
50 "",
51 "The proceeds have been deposited in your bank vault.",
52 "",
53 exchangeSignature(clerkName)
54 ].join("\n");
55}
56
57export function buyFilledLetterBody ({ itemName, quantity, unitPrice, clerkName })
58{
59 return [
60 `Your buy order for ${quantity}× ${itemName} has been filled at ${unitPrice} coins each.`,
61 "",
62 "The goods have been deposited in your bank vault.",
63 "",
64 exchangeSignature(clerkName)
65 ].join("\n");
66}
67
68export function orderExpiredLetterBody ({ side, itemName, quantity, escrowLabel, clerkName })
69{
70 const kind = side === "buy" ? "buy" : "sell";
71 return [
72 `Unfortunately your ${kind} order for ${quantity}× ${itemName} could not be filled within one month.`,
73 "",
74 `Your reserved ${escrowLabel} has been returned to your bank.`,
75 "",
76 exchangeSignature(clerkName)
77 ].join("\n");
78}
79
80export function orderCancelledLetterBody ({ side, itemName, quantity, escrowLabel, clerkName })
81{
82 const kind = side === "buy" ? "buy" : "sell";
83 return [
84 `Your ${kind} order for ${quantity}× ${itemName} was cancelled.`,
85 "",
86 `Your reserved ${escrowLabel} has been returned to your bank.`,
87 "",
88 exchangeSignature(clerkName)
89 ].join("\n");
90}
91
92/** Deterministic midpoint unit price for integer coins (half rounds away from zero via Math.round). */
93export function midpointTradePrice (buyerMaximum, sellerMinimum)
94{
95 const high = Math.max(0, Math.floor(Number(buyerMaximum) || 0));
96 const low = Math.max(0, Math.floor(Number(sellerMinimum) || 0));
97 return Math.max(0, Math.round((high + low) / 2));
98}