diff --git a/bot/action_handlers.py b/bot/action_handlers.py index 0379385..57d0708 100644 --- a/bot/action_handlers.py +++ b/bot/action_handlers.py @@ -58,7 +58,7 @@ 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): +async def handle_inspect_area(query, user_id: int, player: dict, data: list): """Handle the inspect area action.""" await query.answer() location_id = player['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): """Return to main menu.""" await query.answer() status_text = await get_player_status_text(user_id) @@ -282,7 +282,7 @@ async def handle_main_menu(query, user_id: int, player: dict): ) -async def handle_move_menu(query, user_id: int, player: dict): +async def handle_move_menu(query, user_id: int, player: dict, data: list): """Show movement options.""" await query.answer() location = game_world.get_location(player['location_id']) diff --git a/bot/combat_handlers.py b/bot/combat_handlers.py index 3d4fd6c..b4e4a5e 100644 --- a/bot/combat_handlers.py +++ b/bot/combat_handlers.py @@ -9,7 +9,7 @@ from data.world_loader import game_world logger = logging.getLogger(__name__) -async def handle_combat_attack(query, user_id: int, player: dict): +async def handle_combat_attack(query, user_id: int, player: dict, data: list): """Handle player attack action in combat.""" from bot import combat await query.answer() @@ -54,7 +54,7 @@ async def handle_combat_attack(query, user_id: int, player: dict): await query.answer(message, show_alert=False) -async def handle_combat_flee(query, user_id: int, player: dict): +async def handle_combat_flee(query, user_id: int, player: dict, data: list): """Handle flee attempt in combat.""" from bot import combat await query.answer() @@ -99,7 +99,7 @@ async def handle_combat_flee(query, user_id: int, player: dict): await query.answer(message, show_alert=False) -async def handle_combat_use_item_menu(query, user_id: int, player: dict): +async def handle_combat_use_item_menu(query, user_id: int, player: dict, data: list): """Show menu of items that can be used in combat.""" await query.answer() keyboard = await keyboards.combat_items_keyboard(user_id) @@ -148,7 +148,7 @@ async def handle_combat_use_item(query, user_id: int, player: dict, data: list): ) -async def handle_combat_back(query, user_id: int, player: dict): +async def handle_combat_back(query, user_id: int, player: dict, data: list): """Return to combat menu from item selection.""" await query.answer() combat_data = await database.get_combat(user_id) diff --git a/bot/handlers.py b/bot/handlers.py index 87e970f..fdbdef0 100644 --- a/bot/handlers.py +++ b/bot/handlers.py @@ -262,6 +262,39 @@ async def spawn_stats(update: Update, context: ContextTypes.DEFAULT_TYPE) -> Non # BUTTON CALLBACK ROUTER # ============================================================================ +# Create handler mapping +ACTION_HANDLERS = { + "inspect_area": handle_inspect_area, + "attack_wandering": handle_attack_wandering, + "inspect": handle_inspect_interactable, + "action": handle_action, + "inspect_area_menu": handle_inspect_area, + "main_menu": handle_main_menu, + "move_menu": handle_move_menu, + "move": handle_move, + "profile": handle_profile, + "spend_points_menu": handle_spend_points_menu, + "spend_point": handle_spend_point, + "inventory_menu": handle_inventory_menu, + "inventory_item": handle_inventory_item, + "inventory_use": handle_inventory_use, + "inventory_drop": handle_inventory_drop, + "inventory_equip": handle_inventory_equip, + "inventory_unequip": handle_inventory_unequip, + "pickup_menu": handle_pickup_menu, + "pickup": handle_pickup, + "combat_attack": handle_combat_attack, + "combat_flee": handle_combat_flee, + "combat_use_item_menu": handle_combat_use_item_menu, + "combat_use_item": handle_combat_use_item, + "combat_back": handle_combat_back, + "loot_player_corpse": handle_loot_player_corpse, + "take_corpse_item": handle_take_corpse_item, + "scavenge_npc_corpse": handle_scavenge_npc_corpse, + "scavenge_corpse_item": handle_scavenge_corpse_item, + "no_op": lambda query, user_id, player, data: query.answer() +} + async def button_handler(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: """ Main router for button callbacks. @@ -294,80 +327,9 @@ async def button_handler(update: Update, context: ContextTypes.DEFAULT_TYPE) -> # Route to appropriate handler based on action type try: - # Inspection & World Interaction - if action_type == "inspect_area": - await handle_inspect_area(query, user_id, player) - elif action_type == "attack_wandering": - await handle_attack_wandering(query, user_id, player, data) - elif action_type == "inspect": - await handle_inspect_interactable(query, user_id, player, data) - elif action_type == "action": - await handle_action(query, user_id, player, data) - elif action_type == "inspect_area_menu": - await handle_inspect_area(query, user_id, player) - - # Navigation & Menu - elif action_type == "main_menu": - await handle_main_menu(query, user_id, player) - elif action_type == "move_menu": - await handle_move_menu(query, user_id, player) - elif action_type == "move": - await handle_move(query, user_id, player, data) - - # Profile & Stats - elif action_type == "profile": - await handle_profile(query, user_id, player) - elif action_type == "spend_points_menu": - await handle_spend_points_menu(query, user_id, player) - elif action_type == "spend_point": - await handle_spend_point(query, user_id, player, data) - - # Inventory Management - elif action_type == "inventory_menu": - await handle_inventory_menu(query, user_id, player) - elif action_type == "inventory_item": - await handle_inventory_item(query, user_id, player, data) - elif action_type == "inventory_use": - await handle_inventory_use(query, user_id, player, data) - elif action_type == "inventory_drop": - await handle_inventory_drop(query, user_id, player, data) - elif action_type == "inventory_equip": - await handle_inventory_equip(query, user_id, player, data) - elif action_type == "inventory_unequip": - await handle_inventory_unequip(query, user_id, player, data) - - # Item Pickup - elif action_type == "pickup_menu": - await handle_pickup_menu(query, user_id, player, data) - elif action_type == "pickup": - await handle_pickup(query, user_id, player, data) - - # Combat Actions - elif action_type == "combat_attack": - await handle_combat_attack(query, user_id, player) - elif action_type == "combat_flee": - await handle_combat_flee(query, user_id, player) - elif action_type == "combat_use_item_menu": - await handle_combat_use_item_menu(query, user_id, player) - elif action_type == "combat_use_item": - await handle_combat_use_item(query, user_id, player, data) - elif action_type == "combat_back": - await handle_combat_back(query, user_id, player) - - # Corpse Looting - elif action_type == "loot_player_corpse": - await handle_loot_player_corpse(query, user_id, player, data) - elif action_type == "take_corpse_item": - await handle_take_corpse_item(query, user_id, player, data) - elif action_type == "scavenge_npc_corpse": - await handle_scavenge_npc_corpse(query, user_id, player, data) - elif action_type == "scavenge_corpse_item": - await handle_scavenge_corpse_item(query, user_id, player, data) - - # No-op (for disabled buttons) - elif action_type == "no_op": - await query.answer() - + handler = ACTION_HANDLERS.get(action_type) + if handler: + await handler(query, user_id, player, data) else: logger.warning(f"Unknown action type: {action_type}") await query.answer("Unknown action", show_alert=False) diff --git a/bot/inventory_handlers.py b/bot/inventory_handlers.py index 51a6624..5cc8901 100644 --- a/bot/inventory_handlers.py +++ b/bot/inventory_handlers.py @@ -10,7 +10,7 @@ from data.items import ITEMS logger = logging.getLogger(__name__) -async def handle_inventory_menu(query, user_id: int, player: dict): +async def handle_inventory_menu(query, user_id: int, player: dict, data: list): """Show player inventory.""" await query.answer() inventory_items = await database.get_inventory(user_id) diff --git a/bot/profile_handlers.py b/bot/profile_handlers.py index bdd7adf..8eec285 100644 --- a/bot/profile_handlers.py +++ b/bot/profile_handlers.py @@ -9,7 +9,7 @@ from data.world_loader import game_world logger = logging.getLogger(__name__) -async def handle_profile(query, user_id: int, player: dict): +async def handle_profile(query, user_id: int, player: dict, data: list): """Show player profile and stats.""" await query.answer() from bot import combat @@ -70,7 +70,7 @@ async def handle_profile(query, user_id: int, player: dict): ) -async def handle_spend_points_menu(query, user_id: int, player: dict): +async def handle_spend_points_menu(query, user_id: int, player: dict, data: list): """Show stat point spending menu.""" await query.answer() unspent = player.get('unspent_points', 0)