Refresh repository documentation
Some checks failed
Deploy to Production / test (push) Failing after 33s

- Rewrite AGENTS and README files to match the current app layout
- Document API routes, trivia UI, and the active panel design language
This commit is contained in:
syntaxbullet
2026-04-09 21:10:10 +02:00
parent 8369d10bab
commit 6abbd4652a
17 changed files with 893 additions and 639 deletions

View File

@@ -1,9 +1,32 @@
# Trade Module
# Trade module
- Trade sessions are stored **in-memory only** (a `Map` keyed by thread ID). Sessions are lost on restart. There is no persistence or recovery mechanism.
- The trade uses a **two-phase lock** pattern: both users must `toggleLock` (accept) before `executeTrade` can proceed. Any offer modification (add/remove item, change money) automatically **unlocks both users**, forcing re-confirmation. This prevents bait-and-switch.
- `executeTrade` wraps both directions of transfer in a single DB transaction. If any part fails (e.g., insufficient funds, inventory full), the entire trade rolls back.
- Money and item transfers go through `economyService.modifyUserBalance` and `inventoryService.addItem`/`removeItem`, which means all their validation (balance checks, stack limits, slot limits) and side effects (transaction logging, quest events) apply.
- Item transactions are logged separately in the `itemTransactions` table (distinct from currency `transactions`), with `TRADE_IN`/`TRADE_OUT` types.
- The trade types are defined in `bot/modules/trade/trade.types.ts` but the service lives in `shared/modules/trade/`. The import uses `@/modules/trade/trade.types` (bot alias). This cross-boundary import works because both run in the same process.
- `_sessions` is exposed on the service object for testing purposes only.
## Model
- trade sessions are in-memory only
- sessions are keyed by Discord thread ID
- there is no persistence or restart recovery
## Main methods
- `createSession()`
- `getSession()`
- `endSession()`
- `updateMoney()`
- `addItem()`
- `removeItem()`
- `toggleLock()`
- `executeTrade()`
- `clearSessions()`
## Rules
- any change to money or items unlocks both participants
- `executeTrade()` requires both users to be locked
- money transfer goes through `economyService.modifyUserBalance()`
- item transfer goes through `inventoryService.removeItem()` and `addItem()`
- item transfers are also logged in `item_transactions`
## Notes
- `_sessions` is intentionally exposed for tests
- type definitions live under `bot/modules/trade/trade.types.ts`, while the service stays in `shared/modules/trade`