Petbot/modules/inventory.py
megaproxy 88e352ee79 Enhance web interface and item system with user experience improvements
- Fix \!team command to redirect to team builder web page instead of showing IRC list
- Add usage commands to inventory items showing "\!use <item name>" for each item
- Implement Coin Pouch treasure item with 1-3 coin rewards (rare drop rate)
- Fix gym badges leaderboard database query with proper COALESCE and CASE syntax
- Improve inventory item display with styled command instructions and monospace code blocks

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-07-15 20:47:37 +00:00

124 lines
No EOL
5.3 KiB
Python

#!/usr/bin/env python3
"""Inventory management module for PetBot"""
from .base_module import BaseModule
class Inventory(BaseModule):
"""Handles inventory, item usage, and item management commands"""
def get_commands(self):
return ["inventory", "inv", "items", "use", "item"]
async def handle_command(self, channel, nickname, command, args):
if command in ["inventory", "inv", "items"]:
await self.cmd_inventory(channel, nickname)
elif command in ["use", "item"]:
await self.cmd_use_item(channel, nickname, args)
async def cmd_inventory(self, channel, nickname):
"""Redirect player to their web profile for inventory management"""
player = await self.require_player(channel, nickname)
if not player:
return
# Redirect to web interface for better inventory management
self.send_message(channel, f"🎒 {nickname}: View your complete inventory at: http://petz.rdx4.com/player/{nickname}#inventory")
self.send_message(channel, f"💡 The web interface shows detailed item information, categories, and usage options!")
async def cmd_use_item(self, channel, nickname, args):
"""Use an item from inventory"""
if not args:
self.send_message(channel, f"{nickname}: Specify an item to use! Example: !use Small Potion")
return
player = await self.require_player(channel, nickname)
if not player:
return
item_name = " ".join(self.normalize_input(args))
result = await self.database.use_item(player["id"], item_name)
if not result["success"]:
self.send_message(channel, f"{nickname}: {result['error']}")
return
item = result["item"]
effect = result["effect"]
effect_value = result["effect_value"]
# Handle different item effects
if effect == "heal":
# Find active pet to heal
active_pets = await self.database.get_active_pets(player["id"])
if not active_pets:
self.send_message(channel, f"{nickname}: You need an active pet to use healing items!")
return
# Heal the first active pet (can be expanded to choose specific pet)
pet = active_pets[0]
old_hp = pet["hp"]
new_hp = min(pet["max_hp"], pet["hp"] + effect_value)
healed_amount = new_hp - old_hp
# Update pet HP in database
await self.database.update_pet_hp(pet["id"], new_hp)
self.send_message(channel,
f"💊 {nickname}: Used {item['name']} on {pet['nickname'] or pet['species_name']}! "
f"Restored {healed_amount} HP ({old_hp}{new_hp}/{pet['max_hp']})")
elif effect == "full_heal":
active_pets = await self.database.get_active_pets(player["id"])
if not active_pets:
self.send_message(channel, f"{nickname}: You need an active pet to use healing items!")
return
pet = active_pets[0]
old_hp = pet["hp"]
healed_amount = pet["max_hp"] - old_hp
await self.database.update_pet_hp(pet["id"], pet["max_hp"])
self.send_message(channel,
f"{nickname}: Used {item['name']} on {pet['nickname'] or pet['species_name']}! "
f"Fully restored HP! ({old_hp}{pet['max_hp']}/{pet['max_hp']})")
elif effect == "attack_boost":
self.send_message(channel,
f"⚔️ {nickname}: Used {item['name']}! Your next battle will have +{effect_value}% attack damage!")
elif effect == "defense_boost":
self.send_message(channel,
f"🛡️ {nickname}: Used {item['name']}! Your next battle will have +{effect_value}% damage reduction!")
elif effect == "speed_boost":
self.send_message(channel,
f"💨 {nickname}: Used {item['name']}! You'll move first in your next battle!")
elif effect == "lucky_boost":
self.send_message(channel,
f"🍀 {nickname}: Used {item['name']}! Rare pet encounter rate increased by {effect_value}% for 1 hour!")
elif effect == "money":
# Handle money items (like Coin Pouch)
import random
if "-" in str(effect_value):
# Parse range like "1-3"
min_coins, max_coins = map(int, str(effect_value).split("-"))
coins_gained = random.randint(min_coins, max_coins)
else:
coins_gained = int(effect_value)
# Add money to player
await self.database.add_money(player["id"], coins_gained)
self.send_message(channel,
f"💰 {nickname}: Used {item['name']}! Found {coins_gained} coins inside!")
elif effect == "none":
self.send_message(channel,
f"📦 {nickname}: Used {item['name']}! This item has no immediate effect but may be useful later.")
else:
self.send_message(channel,
f"{nickname}: Used {item['name']}! {item['description']}")