5.4 KiB
5.4 KiB
Project Refactoring Plan
Current Status
✅ Completed
-
Moved unused files to
old/folder:bot/- Old Telegram bot code (no longer used)web-map/- Old map editor- All
.mddocumentation files - Old migration scripts
- Old Dockerfiles
-
Created new API module structure:
api/ ├── core/ # Core functionality (config, security, websockets) ├── routers/ # API route handlers ├── services/ # Business logic services └── ...existing files... -
Created core modules:
- ✅
api/core/config.py- All configuration and constants - ✅
api/core/security.py- JWT, auth, password hashing - ✅
api/core/websockets.py- WebSocket ConnectionManager
- ✅
🔄 Next Steps
Backend API Refactoring
Router Files to Create (in api/routers/):
auth.py-/api/auth/*endpoints (register, login, me)characters.py-/api/characters/*endpoints (list, create, select, delete)game.py-/api/game/*endpoints (state, location, profile, move, inspect, interact, pickup, use_item)combat.py-/api/game/combat/*endpoints (initiate, action) + PvP combatequipment.py-/api/game/equip/*endpoints (equip, unequip, repair)crafting.py-/api/game/craft/*endpoints (craftable, craft_item)corpses.py-/api/game/corpses/*and/api/internal/corpses/*endpointswebsocket.py-/ws/game/*WebSocket endpoint
Helper Files to Create (in api/services/):
helpers.py- Utility functions (distance calculation, stamina cost, armor durability, etc.)models.py- Pydantic models (all request/response models)
Final api/main.py will contain ONLY:
- FastAPI app initialization
- Middleware setup (CORS)
- Static file mounting
- Router registration
- Lifespan context (startup/shutdown)
- ~100 lines instead of 5500+
Frontend Refactoring
Components to Extract from Game.tsx:
In pwa/src/components/game/:
Compass.tsx- Navigation compass with stamina costsLocationView.tsx- Location description and imageSurroundings.tsx- NPCs, players, items, corpses, interactablesInventoryPanel.tsx- Inventory managementEquipmentPanel.tsx- Equipment slotsCombatView.tsx- Combat interface (PvE and PvP)ProfilePanel.tsx- Player stats and infoCraftingPanel.tsx- Crafting interfaceDeathOverlay.tsx- Death screen
Shared hooks (in pwa/src/hooks/):
useWebSocket.ts- WebSocket connection and message handlinguseGameState.ts- Game state managementuseCombat.ts- Combat state and actions
Type definitions (in pwa/src/types/):
game.ts- Game entities (Player, Location, Item, NPC, etc.)combat.ts- Combat-related typeswebsocket.ts- WebSocket message types
Final Game.tsx will contain ONLY:
- Component composition
- State management coordination
- WebSocket message routing
- ~300-400 lines instead of 3300+
📋 Estimated File Count
Before:
- Backend: 1 massive file (5574 lines)
- Frontend: 1 massive file (3315 lines)
- Total: 2 files, ~9000 lines
After:
- Backend: ~15 files, average ~200-400 lines each
- Frontend: ~15 files, average ~100-300 lines each
- Total: ~30 files, all maintainable and focused
🎯 Benefits
- Easier to navigate - Each file has a single responsibility
- Easier to test - Isolated components and functions
- Easier to maintain - Changes don't affect unrelated code
- Easier to understand - Clear module boundaries
- Better IDE support - Faster autocomplete, better error detection
- Team-friendly - Multiple developers can work without conflicts
Implementation Strategy
Phase 1: Backend (4-5 hours)
- Create all router files with endpoints
- Create service/helper files
- Extract Pydantic models
- Refactor main.py to just registration
- Test all endpoints still work
Phase 2: Frontend (3-4 hours)
- Create type definitions
- Extract hooks
- Create component files
- Refactor Game.tsx to use components
- Test all functionality still works
Phase 3: TypeScript Configuration (30 minutes)
- Create/update
tsconfig.json - Add proper type definitions
- Fix VSCode errors
Phase 4: Testing & Documentation (1 hour)
- Verify all features work
- Update README with new structure
- Create architecture diagram
Questions Before Proceeding
-
Should I continue with the full refactoring now?
- This will take significant time (8-10 hours of work)
- Will create 30+ new files
- Will require thorough testing
-
Do you want me to do it all at once or in phases?
- All at once: Complete transformation
- Phases: Backend first, then frontend, then testing
-
Any specific preferences for file organization?
- Current plan follows standard FastAPI/React best practices
- Open to adjustments
Recommendation
I recommend doing this in phases with testing after each:
- Phase 1: Backend refactoring (today) - Most critical, easier to test
- Phase 2: Frontend refactoring (next session) - Can verify backend works first
- Phase 3: TypeScript fixes (quick win)
- Phase 4: Final testing and documentation
This approach:
- Allows for testing and validation at each step
- Reduces risk of breaking everything at once
- Gives you time to review and provide feedback
- Easier to roll back if issues arise
Would you like me to proceed with Phase 1: Backend Refactoring now?