BlueGit

timfa questward

Files

questward / shared / market / exchange.js

exchange.js

exchange.js 3.0 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 soldLetterBody ({ itemName, quantity, unitPrice, clerkName })
42{
43 void clerkName;
44 return [
45 `Good news — your sell order for ${quantity}× ${itemName} has been filled at ${unitPrice} coins each.`,
46 "",
47 "The proceeds have been deposited in your bank vault."
48 ].join("\n");
49}
50
51export function buyFilledLetterBody ({ itemName, quantity, unitPrice, clerkName })
52{
53 void clerkName;
54 return [
55 `Your buy order for ${quantity}× ${itemName} has been filled at ${unitPrice} coins each.`,
56 "",
57 "The goods have been deposited in your bank vault."
58 ].join("\n");
59}
60
61export function orderExpiredLetterBody ({ side, itemName, quantity, escrowLabel, clerkName })
62{
63 void clerkName;
64 const kind = side === "buy" ? "buy" : "sell";
65 return [
66 `Unfortunately your ${kind} order for ${quantity}× ${itemName} could not be filled within one month.`,
67 "",
68 `Your reserved ${escrowLabel} has been returned to your bank.`
69 ].join("\n");
70}
71
72export function orderCancelledLetterBody ({ side, itemName, quantity, escrowLabel, clerkName })
73{
74 void clerkName;
75 const kind = side === "buy" ? "buy" : "sell";
76 return [
77 `Your ${kind} order for ${quantity}× ${itemName} was cancelled.`,
78 "",
79 `Your reserved ${escrowLabel} has been returned to your bank.`
80 ].join("\n");
81}
82
83/** Deterministic midpoint unit price for integer coins (half rounds away from zero via Math.round). */
84export function midpointTradePrice (buyerMaximum, sellerMinimum)
85{
86 const high = Math.max(0, Math.floor(Number(buyerMaximum) || 0));
87 const low = Math.max(0, Math.floor(Number(sellerMinimum) || 0));
88 return Math.max(0, Math.round((high + low) / 2));
89}