| 1 | import { itemDefinition } from "../definitions/items.js"; |
| 2 | import { canTransferLetterToOtherPlayers, isLetterStack } from "./letters.js"; |
| 3 | |
| 4 | /** |
| 5 | * Player-to-player trade eligibility. |
| 6 | * Rule of thumb: if it can be dropped and another player can pick it up, it is tradeable. |
| 7 | * Letters use their own authorship rules (same as ground privacy); do not set `untradeable` on letters. |
| 8 | * Future quest/bound items may set `def.untradeable = true`. |
| 9 | * |
| 10 | * @param {object|null|undefined} stack |
| 11 | * @param {number} characterId offering player |
| 12 | */ |
| 13 | export function canTradeStack (stack, characterId) |
| 14 | { |
| 15 | if (!stack) |
| 16 | { return false; } |
| 17 | const def = itemDefinition(stack.itemId); |
| 18 | if (!def) |
| 19 | { return false; } |
| 20 | if (def.untradeable === true) |
| 21 | { return false; } |
| 22 | if (!canTransferLetterToOtherPlayers(stack, characterId)) |
| 23 | { return false; } |
| 24 | return true; |
| 25 | } |
| 26 | |
| 27 | /** @throws {Error & { userFacing?: boolean }} */ |
| 28 | export function assertTradeable (stack, characterId) |
| 29 | { |
| 30 | if (canTradeStack(stack, characterId)) |
| 31 | { return; } |
| 32 | const def = itemDefinition(stack?.itemId); |
| 33 | const error = new Error( |
| 34 | def?.untradeable === true |
| 35 | ? (def.untradeableBlockedReason || "That item cannot be traded.") |
| 36 | : (isLetterStack(stack) |
| 37 | ? "You can only trade letters you wrote." |
| 38 | : "That item cannot be traded.") |
| 39 | ); |
| 40 | error.userFacing = true; |
| 41 | throw error; |
| 42 | } |