Add \!heal command and automatic pet recovery background system

- Implement \!heal command with 1-hour cooldown available to all users
- Add comprehensive cooldown tracking with database last_heal_time validation
- Heal command restores all active pets to full health
- Add background pet recovery system to game engine:
  - Automatic 30-minute recovery timer for fainted pets
  - Background task checks every 5 minutes for eligible pets
  - Auto-recovery restores pets to 1 HP after 30 minutes
  - Proper startup/shutdown integration with game engine
- Add pet_recovery_task to game engine with graceful shutdown
- Include detailed logging for recovery operations
- Ensure system resilience with error handling and task cancellation

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

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
megaproxy 2025-07-16 11:32:25 +00:00
parent 72c1098a22
commit d758d6b924
2 changed files with 246 additions and 3 deletions

View file

@ -16,6 +16,7 @@ class GameEngine:
self.type_chart = {}
self.weather_patterns = {}
self.weather_task = None
self.pet_recovery_task = None
self.shutdown_event = asyncio.Event()
async def load_game_data(self):
@ -28,6 +29,7 @@ class GameEngine:
await self.database.initialize_gyms()
await self.init_weather_system()
await self.battle_engine.load_battle_data()
await self.start_pet_recovery_system()
async def load_pet_species(self):
try:
@ -705,7 +707,58 @@ class GameEngine:
else:
return f"🐾 {pet_name}"
async def start_pet_recovery_system(self):
"""Start the background pet recovery task"""
if self.pet_recovery_task is None or self.pet_recovery_task.done():
print("🏥 Starting pet recovery background task...")
self.pet_recovery_task = asyncio.create_task(self._pet_recovery_loop())
async def stop_pet_recovery_system(self):
"""Stop the background pet recovery task"""
print("🏥 Stopping pet recovery background task...")
if self.pet_recovery_task and not self.pet_recovery_task.done():
self.pet_recovery_task.cancel()
try:
await self.pet_recovery_task
except asyncio.CancelledError:
pass
async def _pet_recovery_loop(self):
"""Background task that checks for pets eligible for auto-recovery"""
try:
while not self.shutdown_event.is_set():
try:
# Check every 5 minutes for pets that need recovery
await asyncio.sleep(300) # 5 minutes
if self.shutdown_event.is_set():
break
# Get pets eligible for auto-recovery (fainted for 30+ minutes)
eligible_pets = await self.database.get_pets_for_auto_recovery()
if eligible_pets:
print(f"🏥 Auto-recovering {len(eligible_pets)} pet(s) after 30 minutes...")
for pet in eligible_pets:
success = await self.database.auto_recover_pet(pet["id"])
if success:
print(f" 🏥 Auto-recovered {pet['nickname'] or pet['species_name']} (ID: {pet['id']}) to 1 HP")
else:
print(f" ❌ Failed to auto-recover pet ID: {pet['id']}")
except asyncio.CancelledError:
break
except Exception as e:
print(f"Error in pet recovery loop: {e}")
# Continue the loop even if there's an error
await asyncio.sleep(60) # Wait a minute before retrying
except asyncio.CancelledError:
print("Pet recovery task cancelled")
async def shutdown(self):
"""Gracefully shutdown the game engine"""
print("🔄 Shutting down game engine...")
await self.stop_weather_system()
await self.stop_weather_system()
await self.stop_pet_recovery_system()