| 1 | /** Central Exchange (marketplace plaza) staff + letter copy. */ |
| 2 | |
| 3 | export const MARKET_ORDER_MAX_AGE_MS = 30 * 24 * 60 * 60 * 1000; |
| 4 | |
| 5 | export const CENTRAL_EXCHANGE_STAFF = Object.freeze([ |
| 6 | "Bobby Pennies", |
| 7 | "Ada Ledger", |
| 8 | "Clive Quill", |
| 9 | "Marge Tally", |
| 10 | "Nigel Coinwright", |
| 11 | "Pearl Balance", |
| 12 | "Otto Vault", |
| 13 | "Iris Counter" |
| 14 | ]); |
| 15 | |
| 16 | export function pickExchangeClerk (random = Math.random) |
| 17 | { |
| 18 | const index = Math.floor(random() * CENTRAL_EXCHANGE_STAFF.length) % CENTRAL_EXCHANGE_STAFF.length; |
| 19 | return CENTRAL_EXCHANGE_STAFF[index]; |
| 20 | } |
| 21 | |
| 22 | export function exchangeSignature (clerkName) |
| 23 | { |
| 24 | return `Signed,\n${clerkName}\nCentral Exchange`; |
| 25 | } |
| 26 | |
| 27 | export function soldLetterBody ({ itemName, quantity, unitPrice, clerkName }) |
| 28 | { |
| 29 | return [ |
| 30 | `Good news — your sell order for ${quantity}× ${itemName} has been filled at ${unitPrice} coins each.`, |
| 31 | "", |
| 32 | "The proceeds have been deposited in your bank vault.", |
| 33 | "", |
| 34 | exchangeSignature(clerkName) |
| 35 | ].join("\n"); |
| 36 | } |
| 37 | |
| 38 | export function buyFilledLetterBody ({ itemName, quantity, unitPrice, clerkName }) |
| 39 | { |
| 40 | return [ |
| 41 | `Your buy order for ${quantity}× ${itemName} has been filled at ${unitPrice} coins each.`, |
| 42 | "", |
| 43 | "The goods have been deposited in your bank vault.", |
| 44 | "", |
| 45 | exchangeSignature(clerkName) |
| 46 | ].join("\n"); |
| 47 | } |
| 48 | |
| 49 | export function orderExpiredLetterBody ({ side, itemName, quantity, escrowLabel, clerkName }) |
| 50 | { |
| 51 | const kind = side === "buy" ? "buy" : "sell"; |
| 52 | return [ |
| 53 | `Unfortunately your ${kind} order for ${quantity}× ${itemName} could not be filled within one month.`, |
| 54 | "", |
| 55 | `Your reserved ${escrowLabel} has been returned to your bank.`, |
| 56 | "", |
| 57 | exchangeSignature(clerkName) |
| 58 | ].join("\n"); |
| 59 | } |
| 60 | |
| 61 | export function orderCancelledLetterBody ({ side, itemName, quantity, escrowLabel, clerkName }) |
| 62 | { |
| 63 | const kind = side === "buy" ? "buy" : "sell"; |
| 64 | return [ |
| 65 | `Your ${kind} order for ${quantity}× ${itemName} was cancelled.`, |
| 66 | "", |
| 67 | `Your reserved ${escrowLabel} has been returned to your bank.`, |
| 68 | "", |
| 69 | exchangeSignature(clerkName) |
| 70 | ].join("\n"); |
| 71 | } |
| 72 | |
| 73 | /** Deterministic midpoint unit price for integer coins (half rounds away from zero via Math.round). */ |
| 74 | export function midpointTradePrice (buyerMaximum, sellerMinimum) |
| 75 | { |
| 76 | const high = Math.max(0, Math.floor(Number(buyerMaximum) || 0)); |
| 77 | const low = Math.max(0, Math.floor(Number(sellerMinimum) || 0)); |
| 78 | return Math.max(0, Math.round((high + low) / 2)); |
| 79 | } |