Status: Phase 1 Complete ✅ | Architecture Ready for Phase 2
Date: April 4, 2026
Scope: Supply/Demand-driven autonomous NPC trader system
-
Database Schema (5 core tables)
- File:
sql/migrate_traders_system_v1.sql - Deploy:
docker compose exec -T db mysql -uroot -proot galaxyquest < sql/migrate_traders_system_v1.sql
- File:
-
REST API (6 endpoints)
- File:
api/traders.php - Status: All endpoints functional with full JSON responses
- Ready to call from frontend or cron jobs
- File:
-
Market Analysis Engine
- File:
api/market_analysis.php - Functions: Supply/demand calc, opportunity detection, dynamic pricing
- Integration: Standalone, can be called independently
- File:
-
Initialization Script
- File:
scripts/initialize_traders_system.php - Creates NPC traders per faction with realistic capital
- Populates initial market opportunities
- Run once:
docker compose exec -T web php scripts/initialize_traders_system.php
- File:
-
Documentation
TRADERS_SYSTEM_IMPLEMENTATION.md— Full architectureTRADERS_INTEGRATION_GUIDE.md— API reference + testingscripts/deploy_traders_system.sh— One-command deployment
# 1. Deploy DB
docker compose exec -T db mysql -uroot -proot galaxyquest < sql/migrate_traders_system_v1.sql
# 2. Initialize traders
docker compose exec -T web php scripts/initialize_traders_system.php
# 3. List created traders
curl http://localhost/api/traders.php?action=list_traders | jq '.traders[].name'
# 4. See available opportunities
curl "http://localhost/api/traders.php?action=list_opportunities&min_margin=10" | jq '.opportunities[0]'
# 5. Check system supply/demand
curl "http://localhost/api/traders.php?action=market_analysis&system=1" | jq '.resources[]'Expected output: JSON data showing traders, profitable routes, and system balances.
┌─────────────────────────────────────┐
│ 1. MARKET ANALYSIS │
│ (Calculate supply/demand per system)│
│ calculate_system_supply_demand() │
└────────────┬────────────────────────┘
│ Updates hourly
↓
┌──────────────────────────────┐
│ market_supply_demand table │
│ (aggregated, persistent) │
└────────────┬─────────────────┘
│
↓
┌──────────────────────────────────────┐
│ 2. OPPORTUNITY DETECTION │
│ find_and_rank_opportunities() │
│ (Find arbitrage: A cheaper, B pricier)
└────────────┬──────────────────────────┘
│ Ranked by profit %
↓
┌──────────────────────────────────┐
│ trade_opportunities table │
│ (1-hour validity window) │
└────────────┬─────────────────────┘
│
↓
┌─────────────────────────────────────────────┐
│ 3. TRADER DECISIONS (Next Phase) │
│ execute_trader_decisions() │
│ (Allocate capital, create routes) │
└────────────┬────────────────────────────────┘
│ Creates active routes
↓
┌──────────────────────────────────┐
│ trader_routes table │
│ (planning → completed lifecycle) │
└────────────┬─────────────────────┘
│
↓
┌──────────────────────────────────┐
│ 4. PRICE IMPACT (Next Phase) │
│ Update supply/demand on trades │
│ Cycle repeats │
└──────────────────────────────────┘
| Table | Records | Purpose |
|---|---|---|
npc_traders |
10-30 | NPC trader entities per faction |
market_supply_demand |
~60-100 | Hourly aggregated supply/demand per system |
trade_opportunities |
50-200 | Detected profitable routes (1h TTL) |
trader_routes |
0-50 | Active in-flight trading missions |
trader_transactions |
Audit log | every buy/sell/transport action |
GET /api/traders.php?action=list_traders
→ Returns: All traders + capital + profit statsGET /api/traders.php?action=list_opportunities&min_margin=15&limit=20
→ Returns: Profitable routes ranked by margin %GET /api/traders.php?action=market_analysis&system=1&galaxy=1
→ Returns: Supply/demand per resource in systemPOST /api/traders.php?action=process_trader_tick
→ Runs: Trader lifecycle, opportunity updates (stub for Phase 2)Goal: Make traders actually execute trades
-
Implement
process_route_transitions()- File:
api/traders.phpline ~300 - States: planning → acquiring → in_transit → delivering → completed
- Actions: buy goods, dispatch fleet, execute sale
- File:
-
Implement
execute_trader_decisions()- File:
api/traders.phpline ~320 - Algorithm: Per trader, pick best opportunity, allocate capital, create route
- File:
-
Fleet Integration
- Link
trader_routes↔fleetstable - New fleet type: autonomous trader (no player control)
- Buy/sell transactions tied to fleet delivery
- Link
- Create
Traderswindow (GQUI-based) - Show live opportunities with profit projections
- Display trader performance leaderboard
- Market ticker with price changes
- Supply/demand graphs per system
- Trade Director leader role automation
- Passive income from trading network
- Auto-allocation of trader fleets
- Market price influence at high skill
# Check all components initialize correctly
php scripts/initialize_traders_system.php --reset
# Verify API responses
curl http://localhost/api/traders.php?action=list_traders
curl http://localhost/api/traders.php?action=list_opportunities
# Check data consistency
docker compose exec -T db mysql -uroot -proot galaxyquest \
-e "SELECT COUNT(*) FROM npc_traders; SELECT COUNT(*) FROM trade_opportunities;"- Log into game
- Call
/api/traders.php?action=market_analysis - See which systems are buyers/sellers of what
- Check that opportunities are ranked by profit
- Verify traders exist and have capital
- ✅ 10-30 traders created (depending on faction count)
- ✅ 50-200 opportunities detected
- ✅ Prices calculated dynamically
- ✅ All API responses valid JSON
- ✅ No database errors
File: api/market_analysis.php
// Minimum margin to consider a route viable
MIN_MARGIN_PCT = 15.0 // 15% profit
// Transport cost per light-year (estimated)
TRANSPORT_COST_PER_LY = 0.1 // 0.1 credits/LY
// Price elasticity (how much demand/supply affects price)
PRICE_ELASTICITY = 0.4 // Affects price sensitivityFile: scripts/initialize_traders_system.php
// Strategy distribution per faction
'helion' => 3 traders (profit_max)
'vor' => 2 traders (volume)
'myrk' => 2 traders (stabilize)
// Capital allocation per strategy
'helion' => 75,000 Cr
'vor' => 50,000 Cr
'myrk' => 40,000 CrRecommended: Run process_trader_tick every 10-15 minutes
# Via cron:
*/10 * * * * curl -s http://localhost/api/traders.php?action=process_trader_tickapi/
├── traders.php ← API endpoints (fully done)
└── market_analysis.php ← Market analysis engine (fully done)
sql/
└── migrate_traders_system_v1.sql ← DB schema (ready to deploy)
scripts/
├── initialize_traders_system.php ← One-time setup (ready)
└── deploy_traders_system.sh ← Quick deploy (shell)
docs/
├── TRADERS_SYSTEM_IMPLEMENTATION.md ← Full architecture
└── TRADERS_INTEGRATION_GUIDE.md ← API reference
- Traders don't actually trade yet —
process_route_transitions()is a stub - No fleet integration — Routes exist but aren't dispatched
- No price feedback loop — Market doesn't update after trades
- No player interaction — Players can't compete with traders (feature later)
- No UI — Everything API-driven currently
- ✅ Supply/demand correctly calculated per system
- ✅ Profitable opportunities detected and ranked
- ✅ Data persisted in database
- ✅ API fully functional
- ✅ Initialization script works end-to-end
- ✅ Documentation complete
- ✅ No warnings/errors in logs
See:
- Full architecture:
TRADERS_SYSTEM_IMPLEMENTATION.md - API docs:
TRADERS_INTEGRATION_GUIDE.md - Quick start:
scripts/deploy_traders_system.sh
Next session: Implement Phase 2 trader lifecycle → Watch traders execute real trades! 🚀