BlueGit

timfa questward

Files

questward / shared / market / exchange.js

exchange.js

exchange.js 3.8 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 if (side === "buy")
65 {
66 return [
67 `Unfortunately your buy order for ${quantity}× ${itemName} could not be filled within one month.`,
68 "",
69 "Your reserved coins have been returned to your bank."
70 ].join("\n");
71 }
72 if (escrowLabel === "written cheque")
73 {
74 return [
75 `Unfortunately your sell order for ${quantity}× ${itemName} could not be filled within one month.`,
76 "",
77 "Your reserved written cheque has been returned to your bank."
78 ].join("\n");
79 }
80 return [
81 `Unfortunately your sell order for ${quantity}× ${itemName} could not be filled within one month.`,
82 "",
83 "The listed goods have been returned to your bank."
84 ].join("\n");
85}
86
87export function orderCancelledLetterBody ({ side, itemName, quantity, escrowLabel, clerkName })
88{
89 void clerkName;
90 if (side === "buy")
91 {
92 return [
93 `Your buy order for ${quantity}× ${itemName} was cancelled.`,
94 "",
95 "Your reserved coins have been returned to your bank."
96 ].join("\n");
97 }
98 if (escrowLabel === "written cheque")
99 {
100 return [
101 `Your sell order for ${quantity}× ${itemName} was cancelled.`,
102 "",
103 "Your reserved written cheque has been returned to your bank."
104 ].join("\n");
105 }
106 return [
107 `Your sell order for ${quantity}× ${itemName} was cancelled.`,
108 "",
109 "The listed goods have been returned to your bank."
110 ].join("\n");
111}
112
113/** Deterministic midpoint unit price for integer coins (half rounds away from zero via Math.round). */
114export function midpointTradePrice (buyerMaximum, sellerMinimum)
115{
116 const high = Math.max(0, Math.floor(Number(buyerMaximum) || 0));
117 const low = Math.max(0, Math.floor(Number(sellerMinimum) || 0));
118 return Math.max(0, Math.round((high + low) / 2));
119}