- Removed 11 duplicate router files (auth.py, users.py, decks.py, card_router.py, interactions.py, user_data.py, rooms.py, games/router.py, refresh.py, card_import.py, ws.py) - Removed duplicate core files (logging_config.py, settings.py) - Removed unused services (card_mirror_service.py, mtgjson_loader.py, mtgjson_uploader.py) - Removed unused schemas (proto_messages.py, protocol_constants.py) - Created run.py startup script for simple execution - Created app/core/config.py for unified configuration management - Created app/core/logging.py for logging configuration - Consolidated routers into 6 focused files (auth_router.py, users_router.py, decks_router.py, cards_router.py, user_data_router.py, games_router.py) - Updated all imports to use consolidated model paths - Added backward compatibility aliases in database.py - Created SIMPLIFICATION_SUMMARY.md documentation Architecture improvements: - Single unified database engine with auto-detection (SQLite/PostgreSQL) - Consolidated models into 2 files (models.py, mtg_models.py) - Clean router structure with one file per domain - Simplified configuration with sensible defaults - SQLite by default for development, PostgreSQL for production Testing: - All imports verified working - App loads successfully - Ready for development and testing
7.1 KiB
MTG Online Architecture Simplification Plan
Current State Analysis
Backend Structure
backend/
├── app/
│ ├── core/ # Settings, database engines, Redis client, logging
│ ├── models/ # 10+ SQLAlchemy model files
│ ├── routers/ # 10+ router files
│ ├── schemas/ # 13+ Pydantic schema files
│ ├── services/ # 15+ service files (some massive)
│ └── main.py # FastAPI app with 10+ router mounts
├── mtg_rules_engine/ # 10+ files for game rules engine
├── alembic/ # Database migrations
├── Dockerfile
├── docker-compose.yml
└── requirements.txt
Key Issues Identified
- Fragmented models - 10+ separate model files that should be consolidated
- Massive services -
mtgjson_manager.pyis 53KB,mtgjson_loader.pyis 32KB - Docker complexity - Multi-database setup (PostgreSQL x2, Redis) with Docker Compose
- Async everywhere - Async SQLAlchemy, asyncpg, async Redis - adds complexity
- Redundant schemas - 13+ schema files with potential duplication
- Empty frontend - Frontend directory exists but is empty
- Multiple entry points - Scripts, migrations, Docker, direct Python execution
Router Analysis
- auth.py (162 lines) - Authentication endpoints
- users.py (168 lines) - User profile management
- decks.py (721 lines) - Deck management (largest router)
- rooms.py (158 lines) - Room CRUD
- admin.py (220 lines) - Admin operations
- card_router.py (292 lines) - MTG card search
- interactions.py (674 lines) - Card interactions
- refresh.py (101 lines) - MTGJSON refresh
- user_data.py (1714 lines) - User data (massive)
- card_import.py (368 lines) - Card import
- games/router.py (135 lines) - Game stubs
- ws.py (18 lines) - WebSocket
Model Analysis
- models.py (228 lines) - Core models (User, DecklistFile, Room, etc.)
- mtg_models.py (71 lines) - MTG card/set models
- user_data.py (325 lines) - User sessions, decks, replays, etc.
- user_deck.py (153 lines) - User deck building
- mirror_models.py (97 lines) - Card mirrors
- card_import_batch.py (74 lines) - Import tracking
- relationships.py (24 lines) - Cross-model relationships
Service Analysis
- mtgjson_manager.py (53KB) - MTGJSON data management
- mtgjson_loader.py (32KB) - Data loading
- mtgjson_uploader.py (34KB) - Data uploading
- deck_manager.py (7.8KB) - Deck management
- card_database.py (9.6KB) - Card database
- game_server.py (10KB) - Game server
Simplification Strategy
Phase 1: Core Layer Simplification
Goal: Simplify configuration and database layer
-
config.py - Replace pydantic-settings with simple config class
- SQLite by default, PostgreSQL optional
- Single DATABASE_URL setting
- Sensible defaults for all settings
-
database.py - Single database engine
- Remove dual database setup
- Support SQLite and PostgreSQL via same interface
- Keep async engine for compatibility
-
logging.py - Simplified logging
- Remove Docker-specific complexity
- Keep structured logging
-
security.py - Keep existing functionality
- JWT token management
- Password hashing
- Authentication dependencies
Phase 2: Model Consolidation
Goal: Reduce 10+ model files to 2-3 consolidated files
-
models.py - All application models
- User, DecklistFile, Room, Ban, GameLog, AuditLog
- MtgonlineCard (local card mirror)
- UserDeck, UserDeckCard, DeckPrecedent, CardSuggestion
- UserSession, DeckVersion, GameReplay, etc.
- CardImportBatch, UserCardImportRecord
-
mtg_models.py - All MTG card data models
- MtgSet, MtgCard
- MtgCardMirror, DeckCardLink
-
Remove: relationships.py (integrate into models)
Phase 3: Router Consolidation
Goal: Reduce 12 router files to 6 focused routers
-
auth_router.py - Authentication (from auth.py)
- Login, register, token refresh, current user
-
users_router.py - User management + admin (merge users.py + admin.py)
- User profile, ban management, user listing, audit logs
-
decks_router.py - Deck building (from decks.py)
- Deck CRUD, card management, precedents, suggestions
-
cards_router.py - Card data (merge card_router.py + interactions.py)
- Card search, sets, types, rarities, interactions
-
user_data_router.py - User data + imports (merge user_data.py + card_import.py)
- Sessions, replays, collection, wishlist, groups, networks
- Card imports
-
games_router.py - Games + rooms + refresh (merge rooms.py + games + refresh.py)
- Room management, game stubs, MTGJSON refresh
Phase 4: Schema Consolidation
Goal: Reduce 13+ schema files to 6 matching routers
- auth_schemas.py - Auth request/response
- user_schemas.py - User data schemas
- deck_schemas.py - Deck schemas
- card_schemas.py - Card schemas
- user_data_schemas.py - User data schemas
- game_schemas.py - Game schemas
Phase 5: Service Simplification
Goal: Break large services into focused modules
- card_service.py - Card search and lookup
- deck_service.py - Deck CRUD and validation
- user_service.py - User management
- mtgjson_service.py - MTGJSON data management (split from 53KB file)
- game_service.py - Game state management
Phase 6: Startup Script
Goal: Single entry point
- run.py - Simple startup script
import uvicorn from app.core.database import create_tables from app.main import app if __name__ == "__main__": create_tables() uvicorn.run(app, host="0.0.0.0", port=8000)
Phase 7: Documentation
Goal: Update all documentation
- README.md - New setup instructions
- Remove Docker files - Keep only for production if needed
- Update state.json - Document changes
Implementation Order
- Core Layer (config, database, logging, security)
- Models (consolidate into 2-3 files)
- Schemas (consolidate into 6 files)
- Services (simplify and split)
- Routers (consolidate into 6 files)
- Main (update to use new structure)
- Run script (create startup script)
- Documentation (update README)
- Testing (verify all endpoints work)
Success Criteria
- Single SQLite database works out of the box
- All API endpoints functional
- No Docker required for development
- File count reduced by 50%
- Code lines reduced by 30%
- All tests passing
- Documentation updated
python run.pystarts everything
Risk Mitigation
- Data integrity - Keep all database migrations compatible
- Backward compatibility - Maintain existing endpoint paths
- Testing - Run comprehensive tests after each phase
- Rollback plan - Keep original files as backup until verified
Timeline
- Phase 1-2: Core and Models (1-2 hours)
- Phase 3-4: Routers and Schemas (2-3 hours)
- Phase 5-6: Services and Startup (1-2 hours)
- Phase 7: Documentation and Testing (1-2 hours)
- Total: 5-9 hours