| 1 | import test from "node:test"; |
| 2 | import assert from "node:assert/strict"; |
| 3 | import { ItemId } from "../../shared/definitions/items.js"; |
| 4 | import { CraftingItemId, tinderboxItemInstance } from "../../shared/definitions/craftingItems.js"; |
| 5 | import { dyeItemInstance } from "../../shared/definitions/dyes.js"; |
| 6 | import { potionItemInstance } from "../../shared/alchemy/recipes.js"; |
| 7 | import { BlockType } from "../../shared/definitions/tiles.js"; |
| 8 | import { canSellOnMarketplace, assertMarketplaceSellable } from "../../shared/items/marketplace.js"; |
| 9 | import { midpointTradePrice, MARKET_ORDER_MAX_AGE_MS, soldLetterBody, marketOrderListingFee, orderCancelledLetterBody, orderExpiredLetterBody } from "../../shared/market/exchange.js"; |
| 10 | import { formatLetterSignature, CENTRAL_EXCHANGE_ORG } from "../../shared/courier/systemLetters.js"; |
| 11 | import { writtenOrigin, originLabel, CraftOriginVerb } from "../../shared/items/origin.js"; |
| 12 | import { takeMarketFunds, resolveMarketSellListing } from "../../shared/market/funds.js"; |
| 13 | import { |
| 14 | buildMarketCatalogEntries, |
| 15 | marketOrdersCompatible, |
| 16 | recipeListingKey, |
| 17 | recipePaperDisplayName, |
| 18 | potionListingKey, |
| 19 | flowerListingKey, |
| 20 | arrowListingKey, |
| 21 | UNSTABLE_POTION_LISTING_KEY, |
| 22 | listingKeyFromStack |
| 23 | } from "../../shared/market/listingKey.js"; |
| 24 | import { alchemyPotionDefinition } from "../../shared/definitions/alchemy.js"; |
| 25 | import { MagicElement } from "../../shared/definitions/attackTypes.js"; |
| 26 | import { RangedItemId } from "../../shared/definitions/rangedItems.js"; |
| 27 | import { ClothItemId } from "../../shared/definitions/clothItems.js"; |
| 28 | import { FlowerColorId, flowerItemInstance } from "../../shared/definitions/dyes.js"; |
| 29 | import { emptyEquipment, emptySlots, addCoinsToSlots, countCoinsInSlots } from "../../shared/items/inventory.js"; |
| 30 | import { makeCharacter, makeSimulation } from "./helpers/simulationHarness.js"; |
| 31 | import { createEmptyChunkData, encodeChunkBlob } from "../src/world/chunkData.js"; |
| 32 | import { CHUNK_SIZE } from "../../shared/protocol/constants.js"; |
| 33 | |
| 34 | test("blank cheques cannot be listed on the marketplace", () => |
| 35 | { |
| 36 | const stack = { itemId: ItemId.blankCheque, quantity: 1 }; |
| 37 | assert.equal(canSellOnMarketplace(stack), false); |
| 38 | assert.throws( |
| 39 | () => assertMarketplaceSellable(stack), |
| 40 | (err) => err.message === "Blank cheques cannot be sold on the marketplace." |
| 41 | ); |
| 42 | }); |
| 43 | |
| 44 | test("written cheques can be listed on the marketplace", () => |
| 45 | { |
| 46 | const stack = { |
| 47 | itemId: ItemId.filledCheque, |
| 48 | quantity: 1, |
| 49 | instance: { kind: "filled-cheque", chequeId: "x", issuerCharacterId: 1, itemId: ItemId.apple, quantity: 2 } |
| 50 | }; |
| 51 | assert.equal(canSellOnMarketplace(stack), true); |
| 52 | assert.doesNotThrow(() => assertMarketplaceSellable(stack)); |
| 53 | }); |
| 54 | |
| 55 | test("normal items are marketplace sellable by default", () => |
| 56 | { |
| 57 | assert.equal(canSellOnMarketplace({ itemId: ItemId.apple, quantity: 3 }), true); |
| 58 | }); |
| 59 | |
| 60 | test("fully charged tinderbox can list on CE; worn cannot", () => |
| 61 | { |
| 62 | const fresh = { |
| 63 | itemId: CraftingItemId.tinderbox, |
| 64 | quantity: 1, |
| 65 | instance: tinderboxItemInstance(6) |
| 66 | }; |
| 67 | const worn = { |
| 68 | itemId: CraftingItemId.tinderbox, |
| 69 | quantity: 1, |
| 70 | instance: tinderboxItemInstance(1) |
| 71 | }; |
| 72 | assert.equal(canSellOnMarketplace(fresh), true); |
| 73 | assert.equal(canSellOnMarketplace(worn), false); |
| 74 | assert.throws( |
| 75 | () => assertMarketplaceSellable(worn), |
| 76 | (err) => /partially used/i.test(err.message) |
| 77 | ); |
| 78 | }); |
| 79 | |
| 80 | test("full potions and dyes can list on CE; sipped or used cannot", () => |
| 81 | { |
| 82 | const fullPotion = { |
| 83 | itemId: ItemId.potion, |
| 84 | quantity: 1, |
| 85 | instance: potionItemInstance({ |
| 86 | potionKey: "lesserHealthTonic", |
| 87 | displayName: "Lesser health tonic", |
| 88 | color: { r: 0.95, g: 0.38, b: 0.32 }, |
| 89 | fillLevel: 4, |
| 90 | effect: { type: "health-regen", amountPerSecond: 1, durationMs: 10_000 }, |
| 91 | effectKey: "health-regen" |
| 92 | }) |
| 93 | }; |
| 94 | const sipped = { |
| 95 | ...fullPotion, |
| 96 | instance: { ...fullPotion.instance, fillLevel: 3 } |
| 97 | }; |
| 98 | const fullDye = { |
| 99 | itemId: ItemId.dye, |
| 100 | quantity: 1, |
| 101 | instance: dyeItemInstance({ r: 0.8, g: 0.1, b: 0.1 }, 4) |
| 102 | }; |
| 103 | const usedDye = { |
| 104 | itemId: ItemId.dye, |
| 105 | quantity: 1, |
| 106 | instance: dyeItemInstance({ r: 0.8, g: 0.1, b: 0.1 }, 2) |
| 107 | }; |
| 108 | const unstableFull = { |
| 109 | itemId: ItemId.potion, |
| 110 | quantity: 1, |
| 111 | instance: potionItemInstance({ |
| 112 | potionKey: "unstable:glow", |
| 113 | unstable: true, |
| 114 | displayName: "Unstable draught", |
| 115 | color: { r: 0.95, g: 0.9, b: 0.45 }, |
| 116 | fillLevel: 3, |
| 117 | effectKey: "glow", |
| 118 | effect: { type: "glow", lightRadiusTiles: 2, durationMs: 10_000 } |
| 119 | }) |
| 120 | }; |
| 121 | const unstableSipped = { |
| 122 | ...unstableFull, |
| 123 | instance: { ...unstableFull.instance, fillLevel: 2 } |
| 124 | }; |
| 125 | assert.equal(canSellOnMarketplace(fullPotion), true); |
| 126 | assert.equal(canSellOnMarketplace(sipped), false); |
| 127 | assert.equal(canSellOnMarketplace(fullDye), true); |
| 128 | assert.equal(canSellOnMarketplace(usedDye), false); |
| 129 | assert.equal(canSellOnMarketplace(unstableFull), true); |
| 130 | assert.equal(canSellOnMarketplace(unstableSipped), false); |
| 131 | }); |
| 132 | |
| 133 | test("letters cannot be listed on the marketplace", () => |
| 134 | { |
| 135 | const own = { |
| 136 | itemId: ItemId.letter, |
| 137 | quantity: 1, |
| 138 | instance: { |
| 139 | kind: "letter", |
| 140 | letterId: "x", |
| 141 | authorCharacterId: 1, |
| 142 | authorDisplayName: "Ada", |
| 143 | body: "Hi", |
| 144 | opened: true |
| 145 | } |
| 146 | }; |
| 147 | const received = { |
| 148 | ...own, |
| 149 | instance: { ...own.instance, authorCharacterId: 2, authorDisplayName: "Bob" } |
| 150 | }; |
| 151 | assert.equal(canSellOnMarketplace(own, 1), false); |
| 152 | assert.equal(canSellOnMarketplace(received, 1), false); |
| 153 | assert.throws( |
| 154 | () => assertMarketplaceSellable(own, 1), |
| 155 | (err) => /letters cannot be sold on the marketplace/i.test(err.message) |
| 156 | ); |
| 157 | }); |
| 158 | |
| 159 | test("midpoint trade price rounds half units deterministically", () => |
| 160 | { |
| 161 | assert.equal(midpointTradePrice(100, 80), 90); |
| 162 | assert.equal(midpointTradePrice(101, 80), 91); |
| 163 | assert.equal(midpointTradePrice(80, 100), 90); |
| 164 | }); |
| 165 | |
| 166 | test("listing fee is 3% of notional rounded up", () => |
| 167 | { |
| 168 | assert.equal(marketOrderListingFee(100, 1), 3); |
| 169 | assert.equal(marketOrderListingFee(10, 1), 1); |
| 170 | assert.equal(marketOrderListingFee(8, 2), 1); |
| 171 | assert.equal(marketOrderListingFee(0, 5), 0); |
| 172 | }); |
| 173 | |
| 174 | test("cancel and expiry letters use side-specific escrow copy", () => |
| 175 | { |
| 176 | const buyCancel = orderCancelledLetterBody({ |
| 177 | side: "buy", |
| 178 | itemName: "Flower", |
| 179 | quantity: 1, |
| 180 | escrowLabel: "coins", |
| 181 | clerkName: "Ada Ledger" |
| 182 | }); |
| 183 | assert.match(buyCancel, /buy order for 1× Flower was cancelled/); |
| 184 | assert.match(buyCancel, /coins have been returned/); |
| 185 | assert.doesNotMatch(buyCancel, /coins has been returned/); |
| 186 | |
| 187 | const sellCancel = orderCancelledLetterBody({ |
| 188 | side: "sell", |
| 189 | itemName: "Apple", |
| 190 | quantity: 2, |
| 191 | escrowLabel: "Apple", |
| 192 | clerkName: "Ada Ledger" |
| 193 | }); |
| 194 | assert.match(sellCancel, /sell order for 2× Apple was cancelled/); |
| 195 | assert.match(sellCancel, /listed goods have been returned/); |
| 196 | |
| 197 | const buyExpired = orderExpiredLetterBody({ |
| 198 | side: "buy", |
| 199 | itemName: "Flower", |
| 200 | quantity: 1, |
| 201 | escrowLabel: "coins", |
| 202 | clerkName: "Ada Ledger" |
| 203 | }); |
| 204 | assert.match(buyExpired, /coins have been returned/); |
| 205 | }); |
| 206 | |
| 207 | test("market funds spend whole coin cheques and return change", () => |
| 208 | { |
| 209 | const slots = emptySlots(); |
| 210 | slots[0] = { |
| 211 | itemId: ItemId.filledCheque, |
| 212 | quantity: 1, |
| 213 | instance: { kind: "filled-cheque", chequeId: "c1", issuerCharacterId: 1, itemId: ItemId.coins, quantity: 40 } |
| 214 | }; |
| 215 | const paid = takeMarketFunds(slots, 25); |
| 216 | assert.equal(paid.ok, true); |
| 217 | assert.equal(paid.change, 15); |
| 218 | assert.equal(paid.redeemedCheques.length, 1); |
| 219 | assert.equal(paid.slots.every((slot) => !slot || slot.itemId !== ItemId.filledCheque), true); |
| 220 | }); |
| 221 | |
| 222 | test("written cheque sell listing locks quantity to reserved goods", () => |
| 223 | { |
| 224 | const listing = resolveMarketSellListing({ |
| 225 | itemId: ItemId.filledCheque, |
| 226 | quantity: 1, |
| 227 | instance: { kind: "filled-cheque", chequeId: "x", issuerCharacterId: 1, itemId: ItemId.apple, quantity: 7 } |
| 228 | }); |
| 229 | assert.equal(listing.isCheque, true); |
| 230 | assert.equal(listing.itemId, ItemId.apple); |
| 231 | assert.equal(listing.quantity, 7); |
| 232 | assert.equal(listing.quantityLocked, true); |
| 233 | }); |
| 234 | |
| 235 | function placePlaza (worldDb) |
| 236 | { |
| 237 | const data = createEmptyChunkData(); |
| 238 | data.open = true; |
| 239 | data.blocks[8 * CHUNK_SIZE + 9] = { typeId: BlockType.marketplace, state: null }; |
| 240 | data.blocks[8 * CHUNK_SIZE + 10] = { typeId: BlockType.bank, state: null }; |
| 241 | worldDb.saveChunkVersion(0, 0, worldDb.getActiveRevisionId(), true, encodeChunkBlob(data)); |
| 242 | } |
| 243 | |
| 244 | function recipePaperStack (potionKey, displayName) |
| 245 | { |
| 246 | return { |
| 247 | itemId: ItemId.recipePaper, |
| 248 | quantity: 1, |
| 249 | instance: { |
| 250 | kind: "recipe-paper", |
| 251 | recipeId: `test-${potionKey}`, |
| 252 | potionKey, |
| 253 | displayName: displayName ?? recipePaperDisplayName(potionKey) |
| 254 | } |
| 255 | }; |
| 256 | } |
| 257 | |
| 258 | test("market catalog lists each alchemy recipe paper separately", () => |
| 259 | { |
| 260 | const catalog = buildMarketCatalogEntries({ |
| 261 | lastSaleFor: () => null, |
| 262 | undiscountedMarketCoins: (itemId) => (itemId === ItemId.recipePaper ? 5 : 10), |
| 263 | now: Date.now(), |
| 264 | lerpDurationMs: 0, |
| 265 | settings: {} |
| 266 | }); |
| 267 | const recipes = catalog.filter((entry) => entry.itemId === ItemId.recipePaper); |
| 268 | assert.ok(recipes.length > 1); |
| 269 | assert.ok(recipes.some((entry) => entry.listingKey === recipeListingKey("lesserHealthTonic"))); |
| 270 | assert.ok(recipes.some((entry) => entry.displayName.toLowerCase().includes("lesser health tonic"))); |
| 271 | assert.equal(recipes.some((entry) => !entry.listingKey), false); |
| 272 | }); |
| 273 | |
| 274 | test("market catalog lists each drinkable potion brew separately", () => |
| 275 | { |
| 276 | const catalog = buildMarketCatalogEntries({ |
| 277 | lastSaleFor: () => null, |
| 278 | undiscountedMarketCoins: (itemId) => (itemId === ItemId.potion ? 7 : 10), |
| 279 | now: Date.now(), |
| 280 | lerpDurationMs: 0, |
| 281 | settings: {} |
| 282 | }); |
| 283 | const potions = catalog.filter((entry) => entry.itemId === ItemId.potion); |
| 284 | assert.ok(potions.length > 1); |
| 285 | assert.ok(potions.some((entry) => entry.listingKey === potionListingKey("lesserHealthTonic"))); |
| 286 | assert.ok(potions.some((entry) => entry.displayName === "Lesser health tonic")); |
| 287 | assert.ok(potions.some((entry) => entry.listingKey === UNSTABLE_POTION_LISTING_KEY)); |
| 288 | assert.equal(potions.filter((entry) => entry.listingKey === UNSTABLE_POTION_LISTING_KEY).length, 1); |
| 289 | assert.equal(potions.some((entry) => !entry.listingKey), false); |
| 290 | assert.equal(potions.some((entry) => entry.displayName === "Potion"), false); |
| 291 | assert.equal( |
| 292 | potions.some((entry) => entry.listingKey === potionListingKey("tanningBrew")), |
| 293 | true, |
| 294 | "tanning brew is listed as a drinkable potion" |
| 295 | ); |
| 296 | }); |
| 297 | |
| 298 | test("market catalog expands flowers and tipped arrows", () => |
| 299 | { |
| 300 | const catalog = buildMarketCatalogEntries({ |
| 301 | lastSaleFor: () => null, |
| 302 | undiscountedMarketCoins: () => 4, |
| 303 | now: Date.now(), |
| 304 | lerpDurationMs: 0, |
| 305 | settings: {} |
| 306 | }); |
| 307 | const flowers = catalog.filter((entry) => entry.itemId === ItemId.flower); |
| 308 | assert.ok(flowers.length >= 9); |
| 309 | assert.ok(flowers.every((entry) => entry.listingKey)); |
| 310 | assert.ok(flowers.some((entry) => entry.listingKey === flowerListingKey(FlowerColorId.red))); |
| 311 | assert.ok(flowers.some((entry) => entry.displayName === "Red flower")); |
| 312 | |
| 313 | const bronze = catalog.filter((entry) => entry.itemId === RangedItemId.bronzeArrow); |
| 314 | assert.ok(bronze.some((entry) => entry.listingKey == null && entry.displayName)); |
| 315 | assert.ok(bronze.some((entry) => entry.listingKey === arrowListingKey(MagicElement.fire))); |
| 316 | assert.ok(bronze.some((entry) => /fire/i.test(entry.displayName))); |
| 317 | }); |
| 318 | |
| 319 | test("unstable and tipped-arrow stacks carry market listing keys", () => |
| 320 | { |
| 321 | const unstable = { |
| 322 | itemId: ItemId.potion, |
| 323 | quantity: 1, |
| 324 | instance: potionItemInstance({ |
| 325 | potionKey: "unstable:glow", |
| 326 | unstable: true, |
| 327 | displayName: "Unstable draught", |
| 328 | color: { r: 0.95, g: 0.9, b: 0.45 }, |
| 329 | fillLevel: 3, |
| 330 | effectKey: "glow", |
| 331 | effect: { type: "glow", lightRadiusTiles: 2, durationMs: 10_000 } |
| 332 | }) |
| 333 | }; |
| 334 | assert.equal(listingKeyFromStack(unstable), UNSTABLE_POTION_LISTING_KEY); |
| 335 | assert.equal( |
| 336 | listingKeyFromStack({ |
| 337 | itemId: RangedItemId.bronzeArrow, |
| 338 | quantity: 5, |
| 339 | instance: { kind: "tipped-arrow", element: MagicElement.fire } |
| 340 | }), |
| 341 | arrowListingKey(MagicElement.fire) |
| 342 | ); |
| 343 | assert.equal( |
| 344 | listingKeyFromStack({ |
| 345 | itemId: ItemId.flower, |
| 346 | quantity: 1, |
| 347 | instance: flowerItemInstance(FlowerColorId.blue) |
| 348 | }), |
| 349 | flowerListingKey(FlowerColorId.blue) |
| 350 | ); |
| 351 | assert.equal( |
| 352 | listingKeyFromStack({ |
| 353 | itemId: ClothItemId.cottonShirt, |
| 354 | quantity: 1, |
| 355 | instance: { kind: "clothing", dyeColor: { r: 0.2, g: 0.4, b: 0.8 } } |
| 356 | }), |
| 357 | null, |
| 358 | "dyed clothing stays unkeyed so buyers get a colour surprise" |
| 359 | ); |
| 360 | }); |
| 361 | |
| 362 | test("unstable buy orders match any unstable sell effect", () => |
| 363 | { |
| 364 | const buy = { itemId: ItemId.potion, listingKey: UNSTABLE_POTION_LISTING_KEY }; |
| 365 | const glowSell = { |
| 366 | itemId: ItemId.potion, |
| 367 | listingKey: UNSTABLE_POTION_LISTING_KEY, |
| 368 | escrowStack: { |
| 369 | itemId: ItemId.potion, |
| 370 | quantity: 1, |
| 371 | instance: potionItemInstance({ |
| 372 | potionKey: "unstable:glow", |
| 373 | unstable: true, |
| 374 | displayName: "Unstable draught", |
| 375 | fillLevel: 3, |
| 376 | effectKey: "glow" |
| 377 | }) |
| 378 | } |
| 379 | }; |
| 380 | const shrinkSell = { |
| 381 | itemId: ItemId.potion, |
| 382 | listingKey: UNSTABLE_POTION_LISTING_KEY, |
| 383 | escrowStack: { |
| 384 | itemId: ItemId.potion, |
| 385 | quantity: 1, |
| 386 | instance: potionItemInstance({ |
| 387 | potionKey: "unstable:shrink", |
| 388 | unstable: true, |
| 389 | displayName: "Unstable draught", |
| 390 | fillLevel: 2, |
| 391 | effectKey: "shrink" |
| 392 | }) |
| 393 | } |
| 394 | }; |
| 395 | assert.equal(marketOrdersCompatible(buy, glowSell), true); |
| 396 | assert.equal(marketOrdersCompatible(buy, shrinkSell), true); |
| 397 | assert.equal( |
| 398 | marketOrdersCompatible(buy, { |
| 399 | itemId: ItemId.potion, |
| 400 | listingKey: potionListingKey("lesserHealthTonic") |
| 401 | }), |
| 402 | false |
| 403 | ); |
| 404 | }); |
| 405 | |
| 406 | test("dyed clothing sell matches an undyed clothing buy (colour surprise)", () => |
| 407 | { |
| 408 | const buy = { itemId: ClothItemId.cottonShirt, listingKey: null }; |
| 409 | const dyedSell = { |
| 410 | itemId: ClothItemId.cottonShirt, |
| 411 | listingKey: null, |
| 412 | escrowStack: { |
| 413 | itemId: ClothItemId.cottonShirt, |
| 414 | quantity: 1, |
| 415 | instance: { kind: "clothing", dyeColor: { r: 0.9, g: 0.1, b: 0.2 } } |
| 416 | } |
| 417 | }; |
| 418 | const plainSell = { |
| 419 | itemId: ClothItemId.cottonShirt, |
| 420 | listingKey: null, |
| 421 | escrowStack: { itemId: ClothItemId.cottonShirt, quantity: 1, instance: null } |
| 422 | }; |
| 423 | assert.equal(marketOrdersCompatible(buy, dyedSell), true); |
| 424 | assert.equal(marketOrdersCompatible(buy, plainSell), true); |
| 425 | }); |
| 426 | |
| 427 | test("recipe paper sell listings carry listing keys", () => |
| 428 | { |
| 429 | const stack = recipePaperStack("lesserHealthTonic"); |
| 430 | const listing = resolveMarketSellListing(stack); |
| 431 | assert.ok(listing); |
| 432 | assert.equal(listing.listingKey, recipeListingKey("lesserHealthTonic")); |
| 433 | assert.match(listing.displayName.toLowerCase(), /lesser health tonic recipe/); |
| 434 | assert.equal(listing.quantityLocked, true); |
| 435 | }); |
| 436 | |
| 437 | test("potion sell listings carry brew listing keys", () => |
| 438 | { |
| 439 | const potion = alchemyPotionDefinition("lesserHealthTonic"); |
| 440 | const stack = { |
| 441 | itemId: ItemId.potion, |
| 442 | quantity: 3, |
| 443 | instance: potionItemInstance({ |
| 444 | potionKey: potion.potionKey, |
| 445 | displayName: potion.displayName, |
| 446 | color: potion.color, |
| 447 | fillLevel: potion.fillLevel, |
| 448 | effect: potion.effect, |
| 449 | effectKey: potion.effect?.type |
| 450 | }) |
| 451 | }; |
| 452 | const listing = resolveMarketSellListing(stack); |
| 453 | assert.ok(listing); |
| 454 | assert.equal(listing.listingKey, potionListingKey("lesserHealthTonic")); |
| 455 | assert.equal(listing.displayName, "Lesser health tonic"); |
| 456 | assert.equal(listing.quantityLocked, false); |
| 457 | }); |
| 458 | |
| 459 | test("recipe paper buy and sell orders only match the same recipe", () => |
| 460 | { |
| 461 | const lesserBuy = { |
| 462 | itemId: ItemId.recipePaper, |
| 463 | listingKey: recipeListingKey("lesserHealthTonic") |
| 464 | }; |
| 465 | const lesserSell = { |
| 466 | itemId: ItemId.recipePaper, |
| 467 | listingKey: recipeListingKey("lesserHealthTonic"), |
| 468 | escrowStack: recipePaperStack("lesserHealthTonic") |
| 469 | }; |
| 470 | const claritySell = { |
| 471 | itemId: ItemId.recipePaper, |
| 472 | listingKey: recipeListingKey("clarityPotion"), |
| 473 | escrowStack: recipePaperStack("clarityPotion") |
| 474 | }; |
| 475 | assert.equal(marketOrdersCompatible(lesserBuy, lesserSell), true); |
| 476 | assert.equal(marketOrdersCompatible(lesserBuy, claritySell), false); |
| 477 | }); |
| 478 | |
| 479 | test("matching recipe paper buy and sell orders trade the specific recipe", () => |
| 480 | { |
| 481 | const harness = makeSimulation({ workerCount: 1 }); |
| 482 | try |
| 483 | { |
| 484 | placePlaza(harness.worldDb); |
| 485 | harness.simulation.reloadWorld(); |
| 486 | |
| 487 | const seller = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8, firstName: "Scribe", lastName: "One" }); |
| 488 | const buyer = makeCharacter(harness.stateDb, { tileX: 8, tileY: 9, firstName: "Buyer", lastName: "Two" }); |
| 489 | const sellerBag = addCoinsToSlots(emptySlots(), 5); |
| 490 | assert.equal(sellerBag.ok, true); |
| 491 | sellerBag.slots[1] = recipePaperStack("lesserHealthTonic"); |
| 492 | harness.stateDb.saveBag(seller.character_id, { |
| 493 | inventory: sellerBag.slots, |
| 494 | equipment: emptyEquipment(), |
| 495 | vault: [], |
| 496 | vaultReserved: [] |
| 497 | }); |
| 498 | const buyerBag = addCoinsToSlots(emptySlots(), 100); |
| 499 | assert.equal(buyerBag.ok, true); |
| 500 | harness.stateDb.saveBag(buyer.character_id, { |
| 501 | inventory: buyerBag.slots, |
| 502 | equipment: emptyEquipment(), |
| 503 | vault: [], |
| 504 | vaultReserved: [] |
| 505 | }); |
| 506 | |
| 507 | harness.simulation.spawnCharacter(1, seller); |
| 508 | harness.simulation.spawnCharacter(2, buyer); |
| 509 | harness.simulation.step(); |
| 510 | |
| 511 | harness.simulation.enqueue(1, { |
| 512 | type: "place-market-sell", |
| 513 | characterId: seller.character_id, |
| 514 | slotIndex: 1, |
| 515 | quantity: 1, |
| 516 | minUnitPrice: 8 |
| 517 | }); |
| 518 | const sellResult = harness.simulation.step(); |
| 519 | assert.equal(sellResult.events.some((event) => event.type === "command-error"), false, JSON.stringify(sellResult.events)); |
| 520 | |
| 521 | harness.simulation.enqueue(2, { |
| 522 | type: "place-market-buy", |
| 523 | characterId: buyer.character_id, |
| 524 | itemId: ItemId.recipePaper, |
| 525 | listingKey: recipeListingKey("lesserHealthTonic"), |
| 526 | quantity: 1, |
| 527 | maxUnitPrice: 12 |
| 528 | }); |
| 529 | const buyResult = harness.simulation.step(); |
| 530 | assert.equal(buyResult.events.some((event) => event.type === "command-error"), false, JSON.stringify(buyResult.events)); |
| 531 | const trade = buyResult.events.find((event) => event.type === "market-trade"); |
| 532 | assert.ok(trade, JSON.stringify(buyResult.events)); |
| 533 | assert.equal(trade.quantity, 1); |
| 534 | |
| 535 | const buyerBagNow = harness.simulation.bagOf(2); |
| 536 | const recipe = (buyerBagNow.inventory ?? []).find((slot) => slot?.itemId === ItemId.recipePaper); |
| 537 | assert.ok(recipe); |
| 538 | assert.equal(recipe.instance?.potionKey, "lesserHealthTonic"); |
| 539 | } |
| 540 | finally |
| 541 | { harness.close(); } |
| 542 | }); |
| 543 | |
| 544 | test("matching potion buy and sell orders trade the specific brew", () => |
| 545 | { |
| 546 | const harness = makeSimulation({ workerCount: 1 }); |
| 547 | try |
| 548 | { |
| 549 | placePlaza(harness.worldDb); |
| 550 | harness.simulation.reloadWorld(); |
| 551 | |
| 552 | const seller = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8, firstName: "Alchem", lastName: "One" }); |
| 553 | const buyer = makeCharacter(harness.stateDb, { tileX: 8, tileY: 9, firstName: "Buyer", lastName: "Two" }); |
| 554 | const potion = alchemyPotionDefinition("lesserHealthTonic"); |
| 555 | const sellerBag = addCoinsToSlots(emptySlots(), 5); |
| 556 | assert.equal(sellerBag.ok, true); |
| 557 | sellerBag.slots[1] = { |
| 558 | itemId: ItemId.potion, |
| 559 | quantity: 2, |
| 560 | instance: potionItemInstance({ |
| 561 | potionKey: potion.potionKey, |
| 562 | displayName: potion.displayName, |
| 563 | color: potion.color, |
| 564 | fillLevel: potion.fillLevel, |
| 565 | effect: potion.effect, |
| 566 | effectKey: potion.effect?.type |
| 567 | }) |
| 568 | }; |
| 569 | harness.stateDb.saveBag(seller.character_id, { |
| 570 | inventory: sellerBag.slots, |
| 571 | equipment: emptyEquipment(), |
| 572 | vault: [], |
| 573 | vaultReserved: [] |
| 574 | }); |
| 575 | const buyerBag = addCoinsToSlots(emptySlots(), 100); |
| 576 | assert.equal(buyerBag.ok, true); |
| 577 | harness.stateDb.saveBag(buyer.character_id, { |
| 578 | inventory: buyerBag.slots, |
| 579 | equipment: emptyEquipment(), |
| 580 | vault: [], |
| 581 | vaultReserved: [] |
| 582 | }); |
| 583 | |
| 584 | harness.simulation.spawnCharacter(1, seller); |
| 585 | harness.simulation.spawnCharacter(2, buyer); |
| 586 | harness.simulation.step(); |
| 587 | |
| 588 | harness.simulation.enqueue(1, { |
| 589 | type: "place-market-sell", |
| 590 | characterId: seller.character_id, |
| 591 | slotIndex: 1, |
| 592 | quantity: 2, |
| 593 | minUnitPrice: 8 |
| 594 | }); |
| 595 | const sellResult = harness.simulation.step(); |
| 596 | assert.equal(sellResult.events.some((event) => event.type === "command-error"), false, JSON.stringify(sellResult.events)); |
| 597 | |
| 598 | harness.simulation.enqueue(2, { |
| 599 | type: "place-market-buy", |
| 600 | characterId: buyer.character_id, |
| 601 | itemId: ItemId.potion, |
| 602 | listingKey: potionListingKey("lesserHealthTonic"), |
| 603 | quantity: 2, |
| 604 | maxUnitPrice: 12 |
| 605 | }); |
| 606 | const buyResult = harness.simulation.step(); |
| 607 | assert.equal(buyResult.events.some((event) => event.type === "command-error"), false, JSON.stringify(buyResult.events)); |
| 608 | const trade = buyResult.events.find((event) => event.type === "market-trade"); |
| 609 | assert.ok(trade, JSON.stringify(buyResult.events)); |
| 610 | assert.equal(trade.quantity, 2); |
| 611 | |
| 612 | const buyerBagNow = harness.simulation.bagOf(2); |
| 613 | const brew = (buyerBagNow.inventory ?? []).find((slot) => |
| 614 | slot?.itemId === ItemId.potion && slot.instance?.potionKey === "lesserHealthTonic" |
| 615 | ); |
| 616 | assert.ok(brew); |
| 617 | assert.equal(brew.quantity, 2); |
| 618 | } |
| 619 | finally |
| 620 | { harness.close(); } |
| 621 | }); |
| 622 | |
| 623 | test("recipe paper buy orders for a different recipe do not match", () => |
| 624 | { |
| 625 | const harness = makeSimulation({ workerCount: 1 }); |
| 626 | try |
| 627 | { |
| 628 | placePlaza(harness.worldDb); |
| 629 | harness.simulation.reloadWorld(); |
| 630 | |
| 631 | const seller = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 632 | const buyer = makeCharacter(harness.stateDb, { tileX: 8, tileY: 9 }); |
| 633 | const sellerBag = addCoinsToSlots(emptySlots(), 5); |
| 634 | assert.equal(sellerBag.ok, true); |
| 635 | sellerBag.slots[1] = recipePaperStack("lesserHealthTonic"); |
| 636 | harness.stateDb.saveBag(seller.character_id, { |
| 637 | inventory: sellerBag.slots, |
| 638 | equipment: emptyEquipment(), |
| 639 | vault: [], |
| 640 | vaultReserved: [] |
| 641 | }); |
| 642 | const buyerBag = addCoinsToSlots(emptySlots(), 100); |
| 643 | assert.equal(buyerBag.ok, true); |
| 644 | harness.stateDb.saveBag(buyer.character_id, { |
| 645 | inventory: buyerBag.slots, |
| 646 | equipment: emptyEquipment(), |
| 647 | vault: [], |
| 648 | vaultReserved: [] |
| 649 | }); |
| 650 | |
| 651 | harness.simulation.spawnCharacter(1, seller); |
| 652 | harness.simulation.spawnCharacter(2, buyer); |
| 653 | harness.simulation.step(); |
| 654 | |
| 655 | harness.simulation.enqueue(1, { |
| 656 | type: "place-market-sell", |
| 657 | characterId: seller.character_id, |
| 658 | slotIndex: 1, |
| 659 | quantity: 1, |
| 660 | minUnitPrice: 8 |
| 661 | }); |
| 662 | harness.simulation.step(); |
| 663 | |
| 664 | harness.simulation.enqueue(2, { |
| 665 | type: "place-market-buy", |
| 666 | characterId: buyer.character_id, |
| 667 | itemId: ItemId.recipePaper, |
| 668 | listingKey: recipeListingKey("clarityPotion"), |
| 669 | quantity: 1, |
| 670 | maxUnitPrice: 12 |
| 671 | }); |
| 672 | const buyResult = harness.simulation.step(); |
| 673 | assert.equal(buyResult.events.some((event) => event.type === "command-error"), false, JSON.stringify(buyResult.events)); |
| 674 | assert.equal(buyResult.events.some((event) => event.type === "market-trade"), false); |
| 675 | assert.equal(harness.stateDb.listOpenMarketOrdersForCharacter(seller.character_id).length, 1); |
| 676 | assert.equal(harness.stateDb.listOpenMarketOrdersForCharacter(buyer.character_id).length, 1); |
| 677 | } |
| 678 | finally |
| 679 | { harness.close(); } |
| 680 | }); |
| 681 | |
| 682 | test("sell then buy at Central Exchange matches at midpoint and pays seller by pigeon letter", () => |
| 683 | { |
| 684 | const harness = makeSimulation({ workerCount: 1 }); |
| 685 | try |
| 686 | { |
| 687 | placePlaza(harness.worldDb); |
| 688 | harness.simulation.reloadWorld(); |
| 689 | |
| 690 | const seller = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8, firstName: "Sel", lastName: "Ler" }); |
| 691 | const buyer = makeCharacter(harness.stateDb, { tileX: 8, tileY: 9, firstName: "Buy", lastName: "Er" }); |
| 692 | const sellerBag = addCoinsToSlots(emptySlots(), 5); |
| 693 | assert.equal(sellerBag.ok, true); |
| 694 | sellerBag.slots[1] = { itemId: ItemId.apple, quantity: 2 }; |
| 695 | harness.stateDb.saveBag(seller.character_id, { |
| 696 | inventory: sellerBag.slots, |
| 697 | equipment: emptyEquipment(), |
| 698 | vault: [], |
| 699 | vaultReserved: [] |
| 700 | }); |
| 701 | const buyerBag = addCoinsToSlots(emptySlots(), 100); |
| 702 | assert.equal(buyerBag.ok, true); |
| 703 | harness.stateDb.saveBag(buyer.character_id, { |
| 704 | inventory: buyerBag.slots, |
| 705 | equipment: emptyEquipment(), |
| 706 | vault: [], |
| 707 | vaultReserved: [] |
| 708 | }); |
| 709 | |
| 710 | const sellerId = harness.simulation.spawnCharacter(1, seller); |
| 711 | const buyerId = harness.simulation.spawnCharacter(2, buyer); |
| 712 | harness.simulation.step(); |
| 713 | |
| 714 | harness.simulation.enqueue(1, { |
| 715 | type: "place-market-sell", |
| 716 | characterId: seller.character_id, |
| 717 | slotIndex: 1, |
| 718 | quantity: 2, |
| 719 | minUnitPrice: 8 |
| 720 | }); |
| 721 | const sellResult = harness.simulation.step(); |
| 722 | assert.equal(sellResult.events.some((event) => event.type === "command-error"), false, JSON.stringify(sellResult.events)); |
| 723 | assert.ok(sellResult.events.some((event) => event.type === "market-open")); |
| 724 | |
| 725 | harness.simulation.enqueue(2, { |
| 726 | type: "place-market-buy", |
| 727 | characterId: buyer.character_id, |
| 728 | itemId: ItemId.apple, |
| 729 | quantity: 2, |
| 730 | maxUnitPrice: 12 |
| 731 | }); |
| 732 | const buyResult = harness.simulation.step(); |
| 733 | assert.equal(buyResult.events.some((event) => event.type === "command-error"), false, JSON.stringify(buyResult.events)); |
| 734 | const trade = buyResult.events.find((event) => event.type === "market-trade"); |
| 735 | assert.ok(trade, JSON.stringify(buyResult.events)); |
| 736 | assert.equal(trade.unitPrice, 10); |
| 737 | assert.equal(trade.quantity, 2); |
| 738 | |
| 739 | const buyerBagNow = harness.simulation.bagOf(buyerId); |
| 740 | assert.equal((buyerBagNow.inventory ?? []).some((slot) => slot?.itemId === ItemId.apple), true); |
| 741 | |
| 742 | // Coins go to bank; letter goes to the online seller's inventory. |
| 743 | for (let i = 0; i < 500; i++) |
| 744 | { |
| 745 | harness.advance(1000); |
| 746 | harness.simulation.step(); |
| 747 | const sellerVault = harness.stateDb.getBag(seller.character_id); |
| 748 | const sellerBag = harness.simulation.bagOf(sellerId); |
| 749 | const coins = countCoinsInSlots(sellerVault.vault ?? []); |
| 750 | const letter = (sellerBag?.inventory ?? []).find((slot) => slot?.itemId === ItemId.letter) |
| 751 | ?? (sellerVault.vault ?? []).find((slot) => slot?.itemId === ItemId.letter); |
| 752 | if (coins >= 20 && letter) |
| 753 | { |
| 754 | assert.match(letter.instance.body, /sold|filled|proceeds/i); |
| 755 | assert.doesNotMatch(letter.instance.body, /Signed,/); |
| 756 | assert.equal(letter.instance.authorOrganization, "Central Exchange"); |
| 757 | assert.ok(letter.instance.authorDisplayName); |
| 758 | assert.equal(letter.origins?.[0]?.origin?.verb, "Written"); |
| 759 | assert.equal(letter.origins?.[0]?.origin?.displayName, letter.instance.authorDisplayName); |
| 760 | assert.equal(harness.simulation.market.lastSaleFor(ItemId.apple)?.lastTradePrice, 10); |
| 761 | return; |
| 762 | } |
| 763 | } |
| 764 | assert.fail("Seller did not receive coins in vault and letter"); |
| 765 | } |
| 766 | finally |
| 767 | { harness.close(); } |
| 768 | }); |
| 769 | |
| 770 | test("orders older than one month auto-cancel and return escrow to bank", () => |
| 771 | { |
| 772 | const harness = makeSimulation({ workerCount: 1 }); |
| 773 | try |
| 774 | { |
| 775 | placePlaza(harness.worldDb); |
| 776 | harness.simulation.reloadWorld(); |
| 777 | |
| 778 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 779 | const bag = addCoinsToSlots(emptySlots(), 50); |
| 780 | assert.equal(bag.ok, true); |
| 781 | harness.stateDb.saveBag(character.character_id, { |
| 782 | inventory: bag.slots, |
| 783 | equipment: emptyEquipment(), |
| 784 | vault: [], |
| 785 | vaultReserved: [] |
| 786 | }); |
| 787 | harness.simulation.spawnCharacter(1, character); |
| 788 | harness.simulation.step(); |
| 789 | |
| 790 | harness.simulation.enqueue(1, { |
| 791 | type: "place-market-buy", |
| 792 | characterId: character.character_id, |
| 793 | itemId: ItemId.apple, |
| 794 | quantity: 1, |
| 795 | maxUnitPrice: 10 |
| 796 | }); |
| 797 | const placed = harness.simulation.step(); |
| 798 | assert.equal(placed.events.some((event) => event.type === "command-error"), false, JSON.stringify(placed.events)); |
| 799 | const open = harness.stateDb.listOpenMarketOrdersForCharacter(character.character_id); |
| 800 | assert.equal(open.length, 1); |
| 801 | |
| 802 | const order = open[0]; |
| 803 | harness.stateDb.db.prepare("UPDATE market_orders SET created_at = ? WHERE order_id = ?") |
| 804 | .run(Date.now() - MARKET_ORDER_MAX_AGE_MS - 1000, order.orderId); |
| 805 | |
| 806 | const expired = harness.simulation.market.step(Date.now()); |
| 807 | assert.ok(expired.some((event) => event.type === "market-order-expired")); |
| 808 | assert.equal(harness.stateDb.listOpenMarketOrdersForCharacter(character.character_id).length, 0); |
| 809 | |
| 810 | for (let i = 0; i < 500; i++) |
| 811 | { |
| 812 | harness.advance(1000); |
| 813 | harness.simulation.step(); |
| 814 | const vault = harness.stateDb.getBag(character.character_id); |
| 815 | const bag = harness.simulation.bagOf(1); |
| 816 | const coins = countCoinsInSlots(vault.vault ?? []); |
| 817 | const letter = (bag?.inventory ?? []).find((slot) => slot?.itemId === ItemId.letter) |
| 818 | ?? (vault.vault ?? []).find((slot) => slot?.itemId === ItemId.letter); |
| 819 | if (coins >= 10 && letter) |
| 820 | { |
| 821 | assert.match(letter.instance.body, /one month/i); |
| 822 | assert.doesNotMatch(letter.instance.body, /Signed,/); |
| 823 | assert.equal(letter.instance.authorOrganization, "Central Exchange"); |
| 824 | return; |
| 825 | } |
| 826 | } |
| 827 | assert.fail("Expired order did not return escrow letter"); |
| 828 | } |
| 829 | finally |
| 830 | { harness.close(); } |
| 831 | }); |
| 832 | |
| 833 | test("exchange letter instance carries Central Exchange signature fields", () => |
| 834 | { |
| 835 | const body = soldLetterBody({ |
| 836 | itemName: "Apple", |
| 837 | quantity: 1, |
| 838 | unitPrice: 5, |
| 839 | clerkName: "Bobby Pennies" |
| 840 | }); |
| 841 | assert.doesNotMatch(body, /Signed,/); |
| 842 | assert.doesNotMatch(body, /Bobby Pennies/); |
| 843 | assert.doesNotMatch(body, /Central Exchange/); |
| 844 | assert.match(body, /filled at 5 coins/); |
| 845 | assert.equal( |
| 846 | formatLetterSignature({ |
| 847 | authorDisplayName: "Bobby Pennies", |
| 848 | authorOrganization: CENTRAL_EXCHANGE_ORG |
| 849 | }), |
| 850 | "Signed:\nBobby Pennies\nCentral Exchange" |
| 851 | ); |
| 852 | assert.equal( |
| 853 | formatLetterSignature({ authorDisplayName: "The Bank" }), |
| 854 | "Signed:\nThe Bank" |
| 855 | ); |
| 856 | }); |
| 857 | |
| 858 | test("written origin labels match letter author", () => |
| 859 | { |
| 860 | assert.equal(originLabel(writtenOrigin("Marge Tally")), "Written by Marge Tally"); |
| 861 | assert.equal(CraftOriginVerb.written, "Written"); |
| 862 | }); |