Unify all handler signatures and simplify router

- Standardize all handlers to signature: (query, user_id, player, data=None)
- Replace 125-line if/elif chain with HANDLER_MAP dictionary
- Reduce router code by 90 lines (72% reduction)
- Add HANDLER_MAP registry for cleaner organization
- Enable future auto-discovery and decorator patterns
- Maintain full backward compatibility
- Document changes in HANDLER_REFACTORING_V2.md

Benefits:
- O(1) handler lookup vs O(n) if/elif chain
- Add new handlers by just updating the map
- Consistent signature makes code easier to understand
- Opens doors for middleware and hooks
This commit is contained in:
Joan
2025-10-20 12:22:07 +02:00
parent 39f3be6980
commit c0783340b0
6 changed files with 307 additions and 109 deletions

View File

@@ -58,8 +58,8 @@ async def get_player_status_text(telegram_id: int) -> str:
# INSPECTION & WORLD INTERACTION HANDLERS
# ============================================================================
async def handle_inspect_area(query, user_id: int, player: dict):
"""Handle the inspect area action."""
async def handle_inspect_area(query, user_id: int, player: dict, data: list = None):
"""Handle inspect area action - show NPCs and interactables in current location."""
await query.answer()
location_id = player['location_id']
location = game_world.get_location(location_id)
@@ -266,7 +266,7 @@ async def handle_action(query, user_id: int, player: dict, data: list):
# NAVIGATION & MOVEMENT HANDLERS
# ============================================================================
async def handle_main_menu(query, user_id: int, player: dict):
async def handle_main_menu(query, user_id: int, player: dict, data: list = None):
"""Return to main menu."""
await query.answer()
status_text = await get_player_status_text(user_id)
@@ -282,8 +282,8 @@ async def handle_main_menu(query, user_id: int, player: dict):
)
async def handle_move_menu(query, user_id: int, player: dict):
"""Show movement options."""
async def handle_move_menu(query, user_id: int, player: dict, data: list = None):
"""Show movement options menu."""
await query.answer()
location = game_world.get_location(player['location_id'])
location_image = location.image_path if location else None