diff --git a/README.md b/README.md deleted file mode 100644 index 5e2c7ca..0000000 --- a/README.md +++ /dev/null @@ -1,156 +0,0 @@ -# DuckHunt IRC Bot - -A competitive IRC game bot implementing the classic DuckHunt mechanics with modern features. Players compete to shoot ducks that spawn in channels, managing ammunition, accuracy, and collecting items in a persistent progression system. - -## Features - -### Core Gameplay -- **Duck Spawning**: Ducks appear randomly in configured channels with ASCII art -- **Shooting Mechanics**: Players use `!bang` to shoot ducks with limited ammunition -- **Accuracy System**: Hit chances based on player skill that improves with successful shots -- **Gun Jamming**: Weapons can jam and require reloading based on reliability stats -- **Wild Shots**: Shooting without targets results in gun confiscation - -### Progression System -- **Experience Points**: Earned from successful duck kills and befriending -- **Level System**: 40 levels with titles and increasing XP requirements -- **Statistics Tracking**: Comprehensive stats including accuracy, best times, and shot counts -- **Leaderboards**: Top player rankings and personal statistics - -### Item System -- **Shop**: 8 different items available for purchase with earned money -- **Inventory**: Persistent item storage with quantity tracking -- **Item Effects**: Consumable and permanent items affecting gameplay -- **Competitive Drops**: Items drop to the ground for any player to grab with `!snatch` - -### Gun Mechanics -- **Ammunition Management**: Limited shots per magazine with reloading required -- **Charger System**: Multiple magazines with reload mechanics -- **Gun Confiscation**: Administrative punishment system for wild shooting -- **Reliability**: Weapon condition affecting jam probability - -## Installation - -### Requirements -- Python 3.7 or higher -- asyncio support -- SSL/TLS support for secure IRC connections - -### Setup -1. Clone the repository -2. Install Python dependencies (none required beyond standard library) -3. Copy and configure `config.json` -4. Run the bot - -```bash -python3 duckhunt.py -``` - -## Configuration - -The bot uses `config.json` for all configuration. Key sections include: - -### IRC Connection -```json -{ - "server": "irc.example.net", - "port": 6697, - "nick": "DuckHunt", - "channels": ["#games"], - "ssl": true -} -``` - -### SASL Authentication -```json -{ - "sasl": { - "enabled": true, - "username": "bot_username", - "password": "bot_password" - } -} -``` - -### Game Settings -- Duck spawn intervals and timing -- Sleep hours when ducks don't spawn -- Duck type probabilities and rewards -- Shop item prices and effects - -## Commands - -### Player Commands -- `!bang` - Shoot at a duck -- `!reload` - Reload your weapon -- `!bef` / `!befriend` - Attempt to befriend a duck instead of shooting -- `!shop` - View available items for purchase -- `!duckstats` - View your personal statistics -- `!topduck` - View the leaderboard -- `!snatch` - Grab items dropped by other players -- `!use [target]` - Use an item from inventory -- `!sell ` - Sell an item for half price - -### Admin Commands -- `!rearm [player]` - Restore confiscated guns -- `!disarm ` - Confiscate a player's gun -- `!ducklaunch` - Force spawn a duck -- `!reset [confirm]` - Reset player statistics - -## Architecture - -### Modular Design -- `duckhuntbot.py` - Main bot class and IRC handling -- `game.py` - Duck spawning and game mechanics -- `db.py` - Player data persistence and management -- `utils.py` - Input validation and IRC message parsing -- `sasl.py` - SASL authentication implementation -- `logging_utils.py` - Enhanced logging with rotation - -### Database -Player data is stored in JSON format with automatic backups. The system handles: -- Player statistics and progression -- Inventory and item management -- Configuration and preferences -- Historical data and records - -### Concurrency -Built on Python's asyncio framework for handling: -- IRC message processing -- Duck spawning timers -- Background cleanup tasks -- Multiple simultaneous players - -## Duck Types - -- **Normal Duck**: Standard rewards and difficulty -- **Fast Duck**: Higher XP but harder to hit -- **Rare Duck**: Bonus rewards and special drops -- **Boss Duck**: Challenging encounters with significant rewards - -## Item Types - -1. **Extra Shots** - Temporary ammunition boost -2. **Faster Reload** - Reduced reload time -3. **Accuracy Charm** - Permanent accuracy improvement -4. **Lucky Charm** - Increased rare duck encounters -5. **Friendship Bracelet** - Better befriending success rates -6. **Duck Caller** - Faster duck spawning -7. **Camouflage** - Temporary stealth mode -8. **Energy Drink** - Energy restoration - -## Development - -The codebase follows clean architecture principles with: -- Separation of concerns between IRC, game logic, and data persistence -- Comprehensive error handling and logging -- Input validation and sanitization -- Graceful shutdown handling -- Signal-based process management - -### Adding Features -New features can be added by: -1. Extending the command processing in `duckhuntbot.py` -2. Adding game mechanics to `game.py` -3. Updating data structures in `db.py` -4. Configuring behavior in `config.json` \ No newline at end of file diff --git a/__pycache__/duckhunt.cpython-312.pyc b/__pycache__/duckhunt.cpython-312.pyc deleted file mode 100644 index 0008df4..0000000 Binary files a/__pycache__/duckhunt.cpython-312.pyc and /dev/null differ diff --git a/backup/simple_duckhunt.py b/backup/simple_duckhunt.py deleted file mode 100644 index 3e1359d..0000000 --- a/backup/simple_duckhunt.py +++ /dev/null @@ -1,2839 +0,0 @@ -#!/usr/bin/env python3 -""" -Standalone DuckHunt IRC Bot with JSON Database Storage -""" - -import asyncio -import ssl -import json -import random -import logging -import logging.handlers -import sys -import os -import base64 -import subprocess -import time -import uuid -import signal -import traceback -import re -from functools import partial -from typing import Optional, Dict, Any - -# Import SASL handler -from src.sasl import SASLHandler - -# Enhanced logger with detailed formatting -class DetailedColorFormatter(logging.Formatter): - COLORS = { - 'DEBUG': '\033[94m', # Blue - 'INFO': '\033[92m', # Green - 'WARNING': '\033[93m', # Yellow - 'ERROR': '\033[91m', # Red - 'CRITICAL': '\033[95m', # Magenta - 'ENDC': '\033[0m' # End color - } - - def format(self, record): - color = self.COLORS.get(record.levelname, '') - endc = self.COLORS['ENDC'] - msg = super().format(record) - return f"{color}{msg}{endc}" - -class DetailedFileFormatter(logging.Formatter): - """File formatter with extra context but no colors""" - def format(self, record): - return super().format(record) - -def setup_logger(): - logger = logging.getLogger('DuckHuntBot') - logger.setLevel(logging.DEBUG) - - # Clear any existing handlers - logger.handlers.clear() - - # Console handler with colors - console_handler = logging.StreamHandler() - console_handler.setLevel(logging.INFO) - console_formatter = DetailedColorFormatter( - '%(asctime)s [%(levelname)s] %(name)s: %(message)s' - ) - console_handler.setFormatter(console_formatter) - logger.addHandler(console_handler) - - # File handler with rotation for detailed logs - try: - file_handler = logging.handlers.RotatingFileHandler( - 'duckhunt.log', - maxBytes=10*1024*1024, # 10MB per file - backupCount=5 # Keep 5 backup files - ) - file_handler.setLevel(logging.DEBUG) - file_formatter = DetailedFileFormatter( - '%(asctime)s [%(levelname)-8s] %(name)s - %(funcName)s:%(lineno)d: %(message)s' - ) - file_handler.setFormatter(file_formatter) - logger.addHandler(file_handler) - - logger.info("Enhanced logging system initialized with file rotation") - except Exception as e: - logger.error(f"Failed to setup file logging: {e}") - - return logger - -# Input validation functions -class InputValidator: - @staticmethod - def validate_nickname(nick: str) -> bool: - """Validate IRC nickname format""" - if not nick or len(nick) > 30: - return False - # RFC 2812 nickname pattern - pattern = r'^[a-zA-Z\[\]\\`_^{|}][a-zA-Z0-9\[\]\\`_^{|}\-]*$' - return bool(re.match(pattern, nick)) - - @staticmethod - def validate_channel(channel: str) -> bool: - """Validate IRC channel format""" - if not channel or len(channel) > 50: - return False - return channel.startswith('#') and ' ' not in channel - - @staticmethod - def validate_numeric_input(value: str, min_val: Optional[int] = None, max_val: Optional[int] = None) -> Optional[int]: - """Safely parse and validate numeric input""" - try: - num = int(value) - if min_val is not None and num < min_val: - return None - if max_val is not None and num > max_val: - return None - return num - except (ValueError, TypeError): - return None - - @staticmethod - def sanitize_message(message: str) -> str: - """Sanitize user input message""" - if not message: - return "" - # Remove control characters and limit length - sanitized = ''.join(char for char in message if ord(char) >= 32 or char in '\t\n') - return sanitized[:500] # Limit message length - -# Simple IRC message parser -def parse_message(line): - prefix = '' - trailing = '' - if line.startswith(':'): - prefix, line = line[1:].split(' ', 1) - if ' :' in line: - line, trailing = line.split(' :', 1) - parts = line.split() - command = parts[0] if parts else '' - params = parts[1:] if len(parts) > 1 else [] - return prefix, command, params, trailing - -class SimpleIRCBot: - def __init__(self, config): - self.config = config - self.logger = setup_logger() - self.reader: Optional[asyncio.StreamReader] = None - self.writer: Optional[asyncio.StreamWriter] = None - self.registered = False - self.channels_joined = set() - self.players = {} # Memory cache for speed - self.ducks = {} # Format: {channel: [{'alive': True, 'spawn_time': time, 'id': uuid}, ...]} - self.db_file = "duckhunt.json" - self.admins = [admin.lower() for admin in self.config.get('admins', ['colby'])] # Load from config only, case insensitive - self.ignored_nicks = set() # Nicks to ignore commands from - self.duck_timeout_min = self.config.get('duck_timeout_min', 45) # Minimum duck timeout - self.duck_timeout_max = self.config.get('duck_timeout_max', 75) # Maximum duck timeout - self.duck_spawn_min = self.config.get('duck_spawn_min', 1800) # Minimum duck spawn time (30 min) - self.duck_spawn_max = self.config.get('duck_spawn_max', 5400) # Maximum duck spawn time (90 min) - self.shutdown_requested = False # Graceful shutdown flag - self.running_tasks = set() # Track running tasks for cleanup - - # Duck intelligence and records tracking - self.channel_records = {} # Channel-specific records {channel: {'fastest_shot': {}, 'last_duck': {}, 'total_ducks': 0}} - self.duck_difficulty = {} # Per-channel duck difficulty {channel: multiplier} - self.next_duck_spawn = {} # Track next spawn time per channel - self.channel_bread = {} # Track deployed bread per channel {channel: [{'time': timestamp, 'owner': nick}]} - - # Initialize SASL handler - self.sasl_handler = SASLHandler(self, config) - - # IRC Color codes - self.colors = { - 'red': '\x0304', - 'green': '\x0303', - 'blue': '\x0302', - 'yellow': '\x0308', - 'orange': '\x0307', - 'purple': '\x0306', - 'magenta': '\x0313', - 'cyan': '\x0311', - 'white': '\x0300', - 'black': '\x0301', - 'gray': '\x0314', - 'reset': '\x03' - } - - # 40-level progression system with titles - self.levels = [ - {'xp': 0, 'title': 'Duck Harasser'}, - {'xp': 10, 'title': 'Unemployed'}, - {'xp': 25, 'title': 'Hunter Apprentice'}, - {'xp': 45, 'title': 'Duck Tracker'}, - {'xp': 70, 'title': 'Sharp Shooter'}, - {'xp': 100, 'title': 'Hunter'}, - {'xp': 135, 'title': 'Experienced Hunter'}, - {'xp': 175, 'title': 'Skilled Hunter'}, - {'xp': 220, 'title': 'Expert Hunter'}, - {'xp': 270, 'title': 'Master Hunter'}, - {'xp': 325, 'title': 'Duck Slayer'}, - {'xp': 385, 'title': 'Duck Terminator'}, - {'xp': 450, 'title': 'Duck Destroyer'}, - {'xp': 520, 'title': 'Duck Exterminator'}, - {'xp': 595, 'title': 'Duck Assassin'}, - {'xp': 675, 'title': 'Legendary Hunter'}, - {'xp': 760, 'title': 'Elite Hunter'}, - {'xp': 850, 'title': 'Supreme Hunter'}, - {'xp': 945, 'title': 'Ultimate Hunter'}, - {'xp': 1045, 'title': 'Godlike Hunter'}, - {'xp': 1150, 'title': 'Duck Nightmare'}, - {'xp': 1260, 'title': 'Duck Executioner'}, - {'xp': 1375, 'title': 'Duck Eliminator'}, - {'xp': 1495, 'title': 'Duck Obliterator'}, - {'xp': 1620, 'title': 'Duck Annihilator'}, - {'xp': 1750, 'title': 'Duck Devastator'}, - {'xp': 1885, 'title': 'Duck Vanquisher'}, - {'xp': 2025, 'title': 'Duck Conqueror'}, - {'xp': 2170, 'title': 'Duck Dominator'}, - {'xp': 2320, 'title': 'Duck Emperor'}, - {'xp': 2475, 'title': 'Duck Overlord'}, - {'xp': 2635, 'title': 'Duck Deity'}, - {'xp': 2800, 'title': 'Duck God'}, - {'xp': 2970, 'title': 'Duck Nemesis'}, - {'xp': 3145, 'title': 'Duck Apocalypse'}, - {'xp': 3325, 'title': 'Duck Armageddon'}, - {'xp': 3510, 'title': 'Duck Ragnarok'}, - {'xp': 3700, 'title': 'Duck Cataclysm'}, - {'xp': 3895, 'title': 'Duck Holocaust'}, - {'xp': 4095, 'title': 'Duck Genesis'} - ] - - # Sleep hours configuration (when ducks don't spawn) - self.sleep_hours = self.config.get('sleep_hours', []) # Format: [[22, 30], [8, 0]] for 22:30 to 08:00 - - # Duck planning system - self.daily_duck_plan = {} # Format: {channel: [(hour, minute), ...]} - - # Karma system - self.karma_events = ['teamkill', 'miss', 'wild_shot', 'hit', 'golden_hit'] - - self.load_database() - - def get_config(self, path, default=None): - """Get nested configuration value with fallback to default""" - keys = path.split('.') - value = self.config - for key in keys: - if isinstance(value, dict) and key in value: - value = value[key] - else: - return default - return value - - async def attempt_nickserv_auth(self): - """Delegate to SASL handler for NickServ auth""" - # For simple bot, we'll implement NickServ auth here - sasl_config = self.config.get('sasl', {}) - username = sasl_config.get('username', '') - password = sasl_config.get('password', '') - - if username and password: - self.logger.info(f"Attempting NickServ identification for {username}") - # Try both common NickServ commands - self.send_raw(f'PRIVMSG NickServ :IDENTIFY {username} {password}') - # Some networks use just the password if nick matches - await asyncio.sleep(1) - self.send_raw(f'PRIVMSG NickServ :IDENTIFY {password}') - self.logger.info("NickServ identification commands sent") - else: - self.logger.debug("No SASL credentials available for NickServ fallback") - - async def handle_nickserv_response(self, message): - """Handle responses from NickServ""" - message_lower = message.lower() - - if any(phrase in message_lower for phrase in [ - 'you are now identified', 'password accepted', 'you are already identified', - 'authentication successful', 'you have been identified' - ]): - self.logger.info("NickServ identification successful!") - - elif any(phrase in message_lower for phrase in [ - 'invalid password', 'incorrect password', 'access denied', - 'authentication failed', 'not registered', 'nickname is not registered' - ]): - self.logger.error(f"NickServ identification failed: {message}") - - else: - self.logger.debug(f"NickServ message: {message}") - - def get_player_level(self, xp): - """Get player level and title based on XP""" - for i in range(len(self.levels) - 1, -1, -1): - if xp >= self.levels[i]['xp']: - return i + 1, self.levels[i]['title'] - return 1, self.levels[0]['title'] - - def get_xp_for_next_level(self, xp): - """Get XP needed for next level""" - level, _ = self.get_player_level(xp) - if level < len(self.levels): - return self.levels[level]['xp'] - xp - return 0 # Max level reached - - def calculate_penalty_by_level(self, base_penalty, xp): - """Calculate penalty based on player level""" - level, _ = self.get_player_level(xp) - # Higher levels get higher penalties - return base_penalty + (level - 1) * 0.5 - - def update_karma(self, player, event): - """Update player karma based on event""" - if 'karma' not in player: - player['karma'] = 0 - - karma_changes = { - 'hit': 2, - 'golden_hit': 5, - 'teamkill': -10, - 'wild_shot': -3, - 'miss': -1 - } - - player['karma'] += karma_changes.get(event, 0) - - def is_sleep_time(self): - """Check if current time is within sleep hours""" - if not self.sleep_hours: - return False - - import datetime - now = datetime.datetime.now() - current_time = now.hour * 60 + now.minute - - for sleep_start, sleep_end in self.sleep_hours: - start_minutes = sleep_start[0] * 60 + sleep_start[1] - end_minutes = sleep_end[0] * 60 + sleep_end[1] - - if start_minutes <= end_minutes: # Same day - if start_minutes <= current_time <= end_minutes: - return True - else: # Crosses midnight - if current_time >= start_minutes or current_time <= end_minutes: - return True - return False - - def calculate_gun_reliability(self, player): - """Calculate gun reliability percentage""" - base_reliability = player.get('reliability', 70) - grease_bonus = 10 if player.get('grease', 0) > 0 else 0 - brush_bonus = 5 if player.get('brush', 0) > 0 else 0 - return min(base_reliability + grease_bonus + brush_bonus, 95) - - def gun_jams(self, player): - """Check if gun jams when firing""" - reliability = self.calculate_gun_reliability(player) - return random.randint(1, 100) > reliability - - async def scare_other_ducks(self, channel, shot_duck_id): - """Successful shots can scare other ducks away""" - if not self.config.get('successful_shots_scare_ducks', True): - return - - channel_ducks = self.ducks.get(channel, []) - for duck in channel_ducks: - if duck.get('alive') and duck['id'] != shot_duck_id: - # 30% chance to scare each remaining duck - if random.randint(1, 100) <= 30: - duck['scared'] = True - duck['alive'] = False - - async def scare_duck_on_miss(self, channel, target_duck): - """Duck may be scared by missed shots""" - if target_duck.get('hit_attempts', 0) >= 2: # Duck gets scared after 2+ attempts - if random.randint(1, 100) <= 40: # 40% chance to scare - target_duck['scared'] = True - target_duck['alive'] = False - self.send_message(channel, f"The duck got scared and flew away! (\\_o<) *flap flap*") - - async def find_bushes_items(self, nick, channel, player): - """Find items in bushes after killing a duck""" - if random.randint(1, 100) <= 12: # 12% chance to find something - found_items = [ - "Handful of sand", "Water bucket", "Four-leaf clover", "Mirror", - "Grease", "Brush for gun", "Spare clothes", "Sunglasses", - "Piece of bread", "Life insurance" - ] - found_item = random.choice(found_items) - - # Add item to player inventory - item_key = found_item.lower().replace(' ', '_').replace("'", "") - if 'four_leaf_clover' in item_key: - item_key = 'luck' - player['luck'] = player.get('luck', 0) + 1 - elif item_key in player: - player[item_key] = player.get(item_key, 0) + 1 - - self.send_message(channel, f"{nick} > {self.colors['cyan']}You found {found_item} in the bushes!{self.colors['reset']}") - # Player data will be saved by the calling function - - def load_database(self): - """Load player data from JSON file""" - if os.path.exists(self.db_file): - try: - with open(self.db_file, 'r') as f: - data = json.load(f) - self.players = data.get('players', {}) - self.logger.info(f"Loaded {len(self.players)} players from {self.db_file}") - except (json.JSONDecodeError, IOError) as e: - self.logger.error(f"Error loading database: {e}") - self.players = {} - else: - self.players = {} - self.logger.info(f"Created new database: {self.db_file}") - - def save_database(self): - """Save all player data to JSON file with error handling""" - try: - # Atomic write to prevent corruption - temp_file = f"{self.db_file}.tmp" - data = { - 'players': self.players, - 'last_save': str(time.time()) - } - with open(temp_file, 'w') as f: - json.dump(data, f, indent=2) - - # Atomic rename to replace old file - import os - os.replace(temp_file, self.db_file) - - except IOError as e: - self.logger.error(f"Error saving database: {e}") - except Exception as e: - self.logger.error(f"Unexpected database save error: {e}") - - def is_admin(self, user): - """Check if user is admin by nick only""" - if '!' not in user: - return False - nick = user.split('!')[0].lower() - return nick in self.admins - - async def send_user_message(self, nick, channel, message, message_type='default'): - """Send message to user respecting their output mode preferences and config overrides""" - player = self.get_player(f"{nick}!*@*") - - # Check if this message type should be forced to public - force_public_key = f'message_output.force_public.{message_type}' - if self.get_config(force_public_key, False): - self.send_message(channel, message) - return - - # Default to config setting if player not found or no settings - default_mode = self.get_config('message_output.default_user_mode', 'PUBLIC') - output_mode = default_mode - if player and 'settings' in player: - output_mode = player['settings'].get('output_mode', default_mode) - # Handle legacy 'notices' setting for backwards compatibility - if 'output_mode' not in player['settings'] and 'notices' in player['settings']: - output_mode = 'NOTICE' if player['settings']['notices'] else 'PRIVMSG' - - if output_mode == 'PUBLIC': - # Send as regular channel message - self.send_message(channel, message) - elif output_mode == 'NOTICE': - # Send as notice to user - notice_msg = message.replace(f"{nick} > ", "") # Remove nick prefix for notice - self.send_raw(f'NOTICE {nick} :{notice_msg}') - else: # PRIVMSG - # Send as private message - private_msg = message.replace(f"{nick} > ", "") # Remove nick prefix for PM - self.send_message(nick, private_msg) - - def get_random_player_for_friendly_fire(self, shooter_nick): - """Get a random player (except shooter) for friendly fire""" - eligible_players = [] - shooter_lower = shooter_nick.lower() - - for nick in self.players.keys(): - if nick != shooter_lower: # Don't hit yourself - eligible_players.append(nick) - - if eligible_players: - return random.choice(eligible_players) - return None - - def _get_starting_accuracy(self): - """Get starting accuracy for new player - either fixed or random""" - if self.get_config('new_players.random_stats.enabled', False): - accuracy_range = self.get_config('new_players.random_stats.accuracy_range', [60, 80]) - if accuracy_range and len(accuracy_range) >= 2: - return random.randint(accuracy_range[0], accuracy_range[1]) - return self.get_config('new_players.starting_accuracy', 65) - - def _get_starting_reliability(self): - """Get starting reliability for new player - either fixed or random""" - if self.get_config('new_players.random_stats.enabled', False): - reliability_range = self.get_config('new_players.random_stats.reliability_range', [65, 85]) - if reliability_range and len(reliability_range) >= 2: - return random.randint(reliability_range[0], reliability_range[1]) - return self.get_config('new_players.starting_reliability', 70) - - async def auto_rearm_confiscated_guns(self, channel, shooter_nick): - """Auto-rearm all players with confiscated guns when someone shoots a duck""" - if not self.get_config('weapons.auto_rearm_on_duck_shot', False): - return - - rearmed_players = [] - for user_host, player_data in self.players.items(): - if player_data.get('gun_confiscated', False): - player_data['gun_confiscated'] = False - player_data['ammo'] = player_data.get('ammo', 0) + 1 # Give them 1 ammo - - # Get just the nickname from user!host format - nick = user_host.split('!')[0] if '!' in user_host else user_host - rearmed_players.append(nick) - - if rearmed_players: - self.save_database() - # Send notification to channel - rearmed_list = ', '.join(rearmed_players) - self.send_message(channel, f"🔫 {self.colors['green']}Auto-rearm:{self.colors['reset']} {rearmed_list} got their guns back! (Thanks to {shooter_nick}'s duck shot)") - self.logger.info(f"Auto-rearmed {len(rearmed_players)} players after {shooter_nick} shot duck in {channel}") - - async def update_channel_records(self, channel, hunter, shot_time, duck_type): - """Update channel records and duck difficulty after a successful shot""" - if not self.get_config('records_tracking.enabled', True): - return - - # Initialize channel records if needed - if channel not in self.channel_records: - self.channel_records[channel] = { - 'fastest_shot': None, - 'last_duck': None, - 'total_ducks': 0, - 'total_shots': 0 - } - - records = self.channel_records[channel] - - # Update totals - records['total_ducks'] += 1 - - # Update fastest shot record - if not records['fastest_shot'] or shot_time < records['fastest_shot']['time']: - records['fastest_shot'] = { - 'time': shot_time, - 'hunter': hunter, - 'duck_type': duck_type, - 'timestamp': time.time() - } - # Announce new record - self.send_message(channel, f"🏆 {self.colors['yellow']}NEW RECORD!{self.colors['reset']} {hunter} set fastest shot: {shot_time:.3f}s!") - - # Update last duck info - records['last_duck'] = { - 'hunter': hunter, - 'type': duck_type, - 'shot_time': shot_time, - 'timestamp': time.time() - } - - # Increase duck difficulty (smartness) - if self.get_config('duck_smartness.enabled', True): - if channel not in self.duck_difficulty: - self.duck_difficulty[channel] = 1.0 - - learning_rate = self.get_config('duck_smartness.learning_rate', 0.1) - max_difficulty = self.get_config('duck_smartness.max_difficulty_multiplier', 2.0) - - # Ensure max_difficulty has a valid value - if max_difficulty is None: - max_difficulty = 2.0 - - # Increase difficulty slightly with each duck shot - self.duck_difficulty[channel] = min( - max_difficulty, - self.duck_difficulty[channel] + learning_rate - ) - - # Save records to database periodically - self.save_database() - - async def connect(self): - try: - server = self.config['server'] - port = self.config['port'] - ssl_context = ssl.create_default_context() if self.config.get('ssl', True) else None - - self.logger.info(f"Connecting to {server}:{port} (SSL: {ssl_context is not None})") - - self.reader, self.writer = await asyncio.open_connection( - server, port, ssl=ssl_context - ) - self.logger.info("Connected successfully!") - - # Start SASL negotiation if enabled - if await self.sasl_handler.start_negotiation(): - return True - else: - # Standard registration without SASL - await self.register_user() - return True - except Exception as e: - self.logger.error(f"Connection failed: {e}") - return False - - async def register_user(self): - """Register the user with the IRC server""" - # Send password FIRST if configured (for I-line exemption) - if self.config.get('password'): - self.send_raw(f'PASS {self.config["password"]}') - - self.logger.info(f"Registering as {self.config['nick']}") - self.send_raw(f'NICK {self.config["nick"]}') - self.send_raw(f'USER {self.config["nick"]} 0 * :DuckHunt Bot') - - def send_raw(self, msg): - # Skip debug logging for speed - # self.logger.debug(f"-> {msg}") - if self.writer: - self.writer.write((msg + '\r\n').encode()) - - def send_message(self, target, msg): - # Skip logging during gameplay for speed (uncomment for debugging) - # self.logger.info(f"Sending to {target}: {msg}") - self.send_raw(f'PRIVMSG {target} :{msg}') - # Remove drain() for faster responses - let TCP handle buffering - - def get_player(self, user): - """Get player data by nickname only (case insensitive)""" - if '!' not in user: - return None - - nick = user.split('!')[0].lower() # Case insensitive - - # Use nick as database key - if nick in self.players: - player = self.players[nick] - # Backward compatibility: ensure all required fields exist - if 'missed' not in player: - player['missed'] = 0 - if 'inventory' not in player: - player['inventory'] = {} - return player - - # Create new player with configurable defaults - player_data = { - 'xp': self.get_config('new_players.starting_xp', 0), - 'caught': 0, - 'befriended': 0, # Separate counter for befriended ducks - 'missed': 0, - 'ammo': self.get_config('weapons.starting_ammo', 6), - 'max_ammo': self.get_config('weapons.max_ammo_base', 6), - 'chargers': self.get_config('weapons.starting_chargers', 2), - 'max_chargers': self.get_config('weapons.max_chargers_base', 2), - 'accuracy': self._get_starting_accuracy(), - 'reliability': self._get_starting_reliability(), - 'weapon': self.get_config('weapons.starting_weapon', 'pistol'), - 'gun_confiscated': False, - 'explosive_ammo': False, - 'settings': { - 'output_mode': self.get_config('message_output.default_user_mode', 'PUBLIC'), - 'notices': False, # Legacy setting for backwards compatibility - False means public - 'private_messages': False - }, - # Inventory system - 'inventory': {}, - # New advanced stats - 'golden_ducks': 0, - 'karma': self.get_config('new_players.starting_karma', 0), - 'deflection': self.get_config('new_players.starting_deflection', 0), - 'defense': self.get_config('new_players.starting_defense', 0), - 'jammed': False, - 'jammed_count': 0, - 'deaths': 0, - 'neutralized': 0, - 'deflected': 0, - 'best_time': 999.9, - 'total_reflex_time': 0.0, - 'reflex_shots': 0, - 'wild_shots': 0, - 'accidents': 0, - 'total_ammo_used': 0, - 'shot_at': 0, - 'lucky_shots': 0, - # Shop items - 'luck': 0, - 'detector': 0, - 'silencer': 0, - 'sunglasses': 0, - 'clothes': 0, - 'grease': 0, - 'brush': 0, - 'mirror': 0, - 'sand': 0, - 'water': 0, - 'sabotage': 0, - 'life_insurance': 0, - 'liability': 0, - 'decoy': 0, - 'bread': 0, - 'duck_detector': 0, - 'mechanical': 0 - } - - self.players[nick] = player_data - self.save_database() # Auto-save new players - return player_data - - def save_player(self, user): - """Save player data - batch saves for performance""" - if not hasattr(self, '_save_pending'): - self._save_pending = False - - if not self._save_pending: - self._save_pending = True - # Schedule delayed save to batch multiple writes - asyncio.create_task(self._delayed_save()) - - async def _delayed_save(self): - """Batch save to reduce disk I/O""" - await asyncio.sleep(0.5) # Small delay to batch saves - try: - self.save_database() - self.logger.debug("Database batch save completed") - except Exception as e: - self.logger.error(f"Database batch save failed: {e}") - finally: - self._save_pending = False - - def setup_signal_handlers(self): - """Setup signal handlers for graceful shutdown""" - def signal_handler(signum): - signal_name = signal.Signals(signum).name - self.logger.info(f"Received {signal_name}, initiating graceful shutdown...") - self.shutdown_requested = True - - # Handle common shutdown signals - if hasattr(signal, 'SIGTERM'): - signal.signal(signal.SIGTERM, lambda s, f: signal_handler(s)) - if hasattr(signal, 'SIGINT'): - signal.signal(signal.SIGINT, lambda s, f: signal_handler(s)) - if hasattr(signal, 'SIGHUP'): - signal.signal(signal.SIGHUP, lambda s, f: signal_handler(s)) - - async def handle_command(self, user, channel, message): - """Enhanced command handler with logging, validation, and graceful degradation""" - if not user: - self.logger.warning("Received command with no user information") - return - - try: - nick = user.split('!')[0] - nick_lower = nick.lower() - - # Input validation - if not InputValidator.validate_nickname(nick): - self.logger.warning(f"Invalid nickname format: {nick}") - return - - if not InputValidator.validate_channel(channel) and not channel == self.config['nick']: - self.logger.warning(f"Invalid channel format: {channel}") - return - - # Sanitize message input - message = InputValidator.sanitize_message(message) - if not message: - return - - # Enhanced logging with context - self.logger.debug(f"Processing command from {nick} in {channel}: {message[:100]}") - - # Check if user is ignored - if nick_lower in self.ignored_nicks: - self.logger.debug(f"Ignoring command from ignored user: {nick}") - return - - # Determine if this is a private message to the bot - is_private = channel == self.config['nick'] - - # For private messages, use the nick as the target for responses - response_target = nick if is_private else channel - - # Handle private messages (no ! prefix needed) - if is_private: - cmd = message.strip().lower() - self.logger.info(f"Private command from {nick}: {cmd}") - - # Private message admin commands - if self.is_admin(user): - if cmd == 'restart': - await self.handle_restart(nick, response_target) - return - elif cmd == 'quit': - await self.handle_quit(nick, response_target) - return - elif cmd == 'launch' or cmd == 'ducklaunch': - # For private messages, launch in all channels - for chan in self.channels_joined: - await self.spawn_duck_now(chan) - self.send_message(response_target, f"{nick} > Launched ducks in all channels!") - return - elif cmd == 'golden' or cmd == 'goldenduck': - # Launch golden ducks - for chan in self.channels_joined: - await self.spawn_duck_now(chan, force_golden=True) - self.send_message(response_target, f"{nick} > Launched {self.colors['yellow']}GOLDEN DUCKS{self.colors['reset']} in all channels!") - return - elif cmd.startswith('ignore '): - target_nick = cmd[7:].strip().lower() - await self.handle_ignore(nick, response_target, target_nick) - return - elif cmd.startswith('unignore '): - target_nick = cmd[9:].strip().lower() - await self.handle_delignore(nick, response_target, target_nick) - return - else: - # Unknown private command - self.send_message(response_target, f"{nick} > Admin commands: restart, quit, launch, golden, ignore , unignore ") - return - else: - # Non-admin private message - self.send_message(response_target, f"{nick} > Private commands are admin-only. Use !duckhelp in a channel for game commands.") - return - - # Handle channel messages (must start with !) - if not message.startswith('!'): - return - - # Extract just the command part (first word) to handle emojis and extra text - cmd = message.strip().lower().split()[0] - # Keep the original message for commands that need arguments - full_cmd = message.strip().lower() - - # Regular game commands (channel only) - # Inline common commands for speed - if cmd == '!bang': - player = self.get_player(user) - if not player: - return - - # Check if gun is confiscated - if player.get('gun_confiscated', False): - self.send_message(channel, f"{nick} > {self.colors['red']}Gun confiscated! Buy item #5{self.colors['reset']}") - return - - # Check if gun is jammed - if player.get('jammed', False): - self.send_message(channel, f"{nick} > {self.colors['red']}Gun jammed! Use !reload{self.colors['reset']}") - return - - # Check ammo - if player['ammo'] <= 0: - self.send_message(channel, f"{nick} > Empty! | {player['ammo']}/{player['max_ammo']} | {player['chargers']}/{player['max_chargers']}") - return - - # Check for gun jamming before shooting - if self.gun_jams(player): - player['jammed'] = True - player['jammed_count'] = player.get('jammed_count', 0) + 1 - jam_sound = "•click• •click•" if player.get('silencer', 0) > 0 else "*CLICK* *CLICK*" - self.send_message(channel, f"{nick} > {jam_sound} Your gun jammed! Use !reload to unjam it.") - self.save_player(user) - return - - # Get ducks in this channel - channel_ducks = self.ducks.get(channel, []) - alive_ducks = [duck for duck in channel_ducks if duck.get('alive')] - - # Consume ammo - player['ammo'] -= 1 - player['total_ammo_used'] = player.get('total_ammo_used', 0) + 1 - - if alive_ducks: - # Target the oldest duck (first in, first out) - target_duck = alive_ducks[0] - shot_time = time.time() - target_duck['spawn_time'] - is_golden = target_duck.get('type') == 'golden' - - # Calculate hit chance (golden ducks are harder to hit) - base_accuracy = player['accuracy'] - if is_golden: - base_accuracy = max(base_accuracy - 30, 10) # Golden ducks much harder - - # Apply bonuses - if player.get('sunglasses', 0) > 0: - base_accuracy += 5 # Sunglasses help - if player.get('mirror', 0) > 0: - base_accuracy += 3 # Mirror helps - - # Apply duck smartness penalty - duck_difficulty = self.duck_difficulty.get(channel, 1.0) - if duck_difficulty > 1.0: - # Smarter ducks are harder to hit - difficulty_penalty = (duck_difficulty - 1.0) * 20 # Up to 20% penalty at max difficulty - base_accuracy = max(base_accuracy - difficulty_penalty, 10) # Never go below 10% - - hit_chance = min(base_accuracy, 95) # Cap at 95% - - # Record shot attempt - player['shot_at'] = player.get('shot_at', 0) + 1 - target_duck['hit_attempts'] = target_duck.get('hit_attempts', 0) + 1 - - # Track total shots for channel statistics - if channel not in self.channel_records: - self.channel_records[channel] = { - 'fastest_shot': None, - 'last_duck': None, - 'total_ducks': 0, - 'total_shots': 0 - } - self.channel_records[channel]['total_shots'] += 1 - - # Check for hit - if random.randint(1, 100) <= hit_chance: - # HIT! - player['caught'] += 1 - target_duck['alive'] = False - - # Update reflex time stats - player['reflex_shots'] = player.get('reflex_shots', 0) + 1 - player['total_reflex_time'] = player.get('total_reflex_time', 0) + shot_time - if shot_time < player.get('best_time', 999.9): - player['best_time'] = shot_time - - # Calculate XP and rewards - if is_golden: - player['golden_ducks'] = player.get('golden_ducks', 0) + 1 - base_xp = 50 # Golden ducks give much more XP - self.update_karma(player, 'golden_hit') - else: - base_xp = 15 # Normal XP - self.update_karma(player, 'hit') - - # Lucky shot bonus - luck_multiplier = 1 + (player.get('luck', 0) * 0.1) # 10% per luck point - is_lucky = random.randint(1, 100) <= (5 + player.get('luck', 0)) - if is_lucky: - player['lucky_shots'] = player.get('lucky_shots', 0) + 1 - luck_multiplier *= 1.5 # 50% bonus for lucky shot - - xp_earned = int(base_xp * luck_multiplier) - player['xp'] += xp_earned - - # Sound effects based on ammo type - if player.get('explosive_ammo', False): - shot_sound = "•BOUM•" if player.get('silencer', 0) > 0 else "*BOUM*" - explosive_text = f" {self.colors['orange']}[explosive ammo]{self.colors['reset']}" - else: - shot_sound = "•bang•" if player.get('silencer', 0) > 0 else "*BANG*" - explosive_text = "" - - # Lucky shot text - lucky_text = f" {self.colors['yellow']}[lucky shot!]{self.colors['reset']}" if is_lucky else "" - - # Build hit message - level, title = self.get_player_level(player['xp']) - - if is_golden: - golden_count = player.get('golden_ducks', 0) - hit_msg = f"{nick} > {self.colors['yellow']}{shot_sound} ★ GOLDEN DUCK ★{self.colors['reset']} shot in {shot_time:.3f}s! | Ducks: {player['caught']} ({self.colors['yellow']}{golden_count} golden{self.colors['reset']}) | Level {level} | +{xp_earned} xp{explosive_text}{lucky_text}" - else: - hit_msg = f"{nick} > {self.colors['green']}{shot_sound}{self.colors['reset']} Duck shot in {shot_time:.3f}s! | Ducks: {player['caught']} | Level {level} | +{xp_earned} xp{explosive_text}{lucky_text}" - - self.send_message(channel, hit_msg) - - # Scare other ducks if enabled (successful shots can scare ducks) - await self.scare_other_ducks(channel, target_duck['id']) - - # Find items in bushes (rare chance) - await self.find_bushes_items(nick, channel, player) - - # Auto-rearm confiscated guns if enabled - await self.auto_rearm_confiscated_guns(channel, nick) - - # Track records and increase duck difficulty - await self.update_channel_records(channel, nick, shot_time, target_duck['type']) - - else: - # MISS! - player['missed'] += 1 - self.update_karma(player, 'miss') - - # Calculate miss penalty based on level - miss_penalty = int(self.calculate_penalty_by_level(-2, player['xp'])) - player['xp'] += miss_penalty - - # Bullet ricochet chance (can hit other players) - ricochet_chance = 8 # 8% base chance - if player.get('explosive_ammo', False): - ricochet_chance = 15 # Higher with explosive - - ricochet_msg = "" - if random.randint(1, 100) <= ricochet_chance: - ricochet_target = self.get_random_player_for_friendly_fire(nick) - if ricochet_target: - target_player = self.players[ricochet_target] - ricochet_dmg = -3 - target_player['xp'] += ricochet_dmg - target_player['shot_at'] = target_player.get('shot_at', 0) + 1 - ricochet_msg = f" [HIT:{ricochet_target}:{ricochet_dmg}xp]" - - # Scare duck on miss - await self.scare_duck_on_miss(channel, target_duck) - - miss_sound = "•click•" if player.get('silencer', 0) > 0 else "*CLICK*" - await self.send_user_message(nick, channel, f"{nick} > {miss_sound} You missed the duck! | {miss_penalty} xp{ricochet_msg}", 'duck_miss') - - else: - # No duck present - wild fire! - player['wild_shots'] = player.get('wild_shots', 0) + 1 - self.update_karma(player, 'wild_shot') - - # Track wild shots in channel statistics - if channel not in self.channel_records: - self.channel_records[channel] = { - 'fastest_shot': None, - 'last_duck': None, - 'total_ducks': 0, - 'total_shots': 0 - } - self.channel_records[channel]['total_shots'] += 1 - - # Calculate penalties based on level - miss_penalty = int(self.calculate_penalty_by_level(-2, player['xp'])) - wild_penalty = int(self.calculate_penalty_by_level(-3, player['xp'])) - player['xp'] += miss_penalty + wild_penalty - - # Confiscate gun - player['gun_confiscated'] = True - - # Higher chance of hitting other players when no duck - friendly_fire_chance = 25 # 25% when no duck - friendly_fire_msg = "" - - if random.randint(1, 100) <= friendly_fire_chance: - ff_target = self.get_random_player_for_friendly_fire(nick) - if ff_target: - target_player = self.players[ff_target] - ff_dmg = int(self.calculate_penalty_by_level(-4, target_player['xp'])) - target_player['xp'] += ff_dmg - target_player['shot_at'] = target_player.get('shot_at', 0) + 1 - player['accidents'] = player.get('accidents', 0) + 1 - self.update_karma(player, 'teamkill') - friendly_fire_msg = f" [HIT:{ff_target}:{ff_dmg}xp]" - - wild_sound = "•BOUM•" if player.get('explosive_ammo', False) else "*BANG*" - if player.get('silencer', 0) > 0: - wild_sound = "•" + wild_sound[1:-1] + "•" - - await self.send_user_message(nick, channel, f"{nick} > {wild_sound} You shot at nothing! What were you aiming at? | {miss_penalty+wild_penalty} xp | GUN CONFISCATED{friendly_fire_msg}", 'wild_shot') - - # Save after each shot - self.save_player(user) - - elif cmd == '!bef': - # Check if befriending is enabled - if not self.get_config('befriending.enabled', True): - self.send_message(channel, f"{nick} > Duck befriending is currently disabled!") - return - - player = self.get_player(user) - if not player: - return - - # Get ducks in this channel - channel_ducks = self.ducks.get(channel, []) - alive_ducks = [duck for duck in channel_ducks if duck.get('alive')] - - if alive_ducks: - # Target the oldest duck (first in, first out) - target_duck = alive_ducks[0] - bef_time = time.time() - target_duck['spawn_time'] - - # Calculate befriend success chance using config values - level, _ = self.get_player_level(player['xp']) - base_success = self.get_config('befriending.base_success_rate', 65) or 65 - max_success = self.get_config('befriending.max_success_rate', 90) or 90 - level_bonus_per_level = self.get_config('befriending.level_bonus_per_level', 2) or 2 - level_bonus_cap = self.get_config('befriending.level_bonus_cap', 20) or 20 - luck_bonus_per_point = self.get_config('befriending.luck_bonus_per_point', 3) or 3 - - level_bonus = min(level * level_bonus_per_level, level_bonus_cap) - luck_bonus = player.get('luck', 0) * luck_bonus_per_point - success_chance = min(base_success + level_bonus + luck_bonus, max_success) - - # Check if befriend attempt succeeds - if random.randint(1, 100) <= success_chance: - # Successful befriend - player['befriended'] = player.get('befriended', 0) + 1 - - # XP rewards from config - xp_min = self.get_config('befriending.xp_reward_min', 1) or 1 - xp_max = self.get_config('befriending.xp_reward_max', 3) or 3 - - xp_earned = random.randint(xp_min, xp_max) - player['xp'] += xp_earned - - # Mark duck as befriended (dead) - target_duck['alive'] = False - - # Lucky items with configurable chance - if self.get_config('items.lucky_items_enabled', True): - lucky_items = ["four-leaf clover", "rabbit's foot", "horseshoe", "lucky penny", "magic feather"] - base_luck_chance = self.get_config('befriending.lucky_item_chance', 5) + player.get('luck', 0) - lucky_item = random.choice(lucky_items) if random.randint(1, 100) <= base_luck_chance else None - lucky_text = f" [{lucky_item}]" if lucky_item else "" - else: - lucky_text = "" - - remaining_ducks = len([d for d in channel_ducks if d.get('alive')]) - duck_count_text = f" | {remaining_ducks} ducks remain" if remaining_ducks > 0 else "" - - self.send_message(channel, f"{nick} > \\_o< You befriended the duck in {bef_time:.3f}s! | Friends: {player['befriended']} ducks | +{xp_earned} xp{lucky_text}{duck_count_text}") - - # Update karma for successful befriend - if self.get_config('karma.enabled', True): - karma_bonus = self.get_config('karma.befriend_success_bonus', 2) - player['karma'] = player.get('karma', 0) + karma_bonus - - # Save to database after befriending - self.save_player(user) - else: - # Duck refuses to be befriended - refusal_messages = [ - f"{nick} > The duck looks at you suspiciously and waddles away! \\_o< *suspicious quack*", - f"{nick} > The duck refuses to be friends and flaps away angrily! \\_O< *angry quack*", - f"{nick} > The duck ignores your friendship attempts and goes back to swimming! \\_o< *indifferent quack*", - f"{nick} > The duck seems shy and hides behind some reeds! \\_o< *shy quack*", - f"{nick} > The duck is too busy looking for food to be your friend! \\_o< *hungry quack*", - f"{nick} > The duck gives you a cold stare and swims to the other side! \\_O< *cold quack*", - f"{nick} > The duck prefers to stay wild and free! \\_o< *wild quack*", - f"{nick} > The duck thinks you're trying too hard and keeps its distance! \\_o< *skeptical quack*" - ] - - # Small chance the duck gets scared and flies away (configurable) - scared_chance = self.get_config('befriending.scared_away_chance', 10) or 10 - if random.randint(1, 100) <= scared_chance: - target_duck['alive'] = False - scared_messages = [ - f"{nick} > Your friendship attempt scared the duck away! It flies off into the sunset! \\_o< *departing quack*", - f"{nick} > The duck panics at your approach and escapes! \\_O< *panicked quack* *flap flap*" - ] - self.send_message(channel, random.choice(scared_messages)) - else: - self.send_message(channel, random.choice(refusal_messages)) - - # XP penalty for failed befriend attempt (configurable) - xp_penalty = self.get_config('befriending.failure_xp_penalty', 1) - player['xp'] = max(0, player['xp'] - xp_penalty) - - # Update karma for failed befriend - if self.get_config('karma.enabled', True): - karma_penalty = self.get_config('karma.befriend_fail_penalty', 1) - player['karma'] = player.get('karma', 0) - karma_penalty - - # Save player data - self.save_player(user) - else: - self.send_message(channel, f"{nick} > There is no duck to befriend!") - - elif cmd == '!reload': - player = self.get_player(user) - if not player: - return - - # Check if gun is jammed (reload unjams it) - if player.get('jammed', False): - player['jammed'] = False - unjam_sound = "•click click•" if player.get('silencer', 0) > 0 else "*click click*" - self.send_message(channel, f"{nick} > {unjam_sound} You unjammed your gun! | Ammo: {player['ammo']}/{player['max_ammo']} | Chargers: {player['chargers']}/{player['max_chargers']}") - self.save_player(user) - return - - if player['ammo'] == player['max_ammo']: - self.send_message(channel, f"{nick} > Already loaded | {player['ammo']}/{player['max_ammo']} | {player['chargers']}/{player['max_chargers']}") - return - - if player['chargers'] <= 0: - self.send_message(channel, f"{nick} > No chargers! | {player['ammo']}/{player['max_ammo']} | 0/{player['max_chargers']}") - return - - # Calculate reload reliability - reload_reliability = self.calculate_gun_reliability(player) - - if random.randint(1, 100) <= reload_reliability: - player['chargers'] -= 1 - player['ammo'] = player['max_ammo'] - reload_sound = "•click•" if player.get('silencer', 0) > 0 else "*click*" - self.send_message(channel, f"{nick} > {reload_sound} You reloaded your gun! | Ammo: {player['ammo']}/{player['max_ammo']} | Chargers: {player['chargers']}/{player['max_chargers']}") - else: - # Gun jams during reload - player['jammed'] = True - player['jammed_count'] = player.get('jammed_count', 0) + 1 - jam_sound = "•CLACK• •click click•" if player.get('silencer', 0) > 0 else "*CLACK* *click click*" - self.send_message(channel, f"{nick} > {jam_sound} Your gun jammed while reloading! Use !reload again to unjam it.") - - # Save to database after reload - self.save_player(user) - - elif cmd == '!duckstats': - await self.handle_stats(nick, channel, user) - elif cmd == '!duckhelp': - await self.handle_help(nick, channel) - elif full_cmd.startswith('!shop'): - # Handle !shop or !shop - parts = full_cmd.split() - if len(parts) == 1: - # Just !shop - show shop listing - await self.handle_shop(nick, channel, user) - elif len(parts) >= 2: - # !shop - purchase item - item_id = parts[1] - await self.handle_buy(nick, channel, item_id, user) - elif full_cmd.startswith('!use '): - parts = full_cmd[5:].split() - if len(parts) >= 1: - item_id = parts[0] - target_nick = parts[1] if len(parts) >= 2 else None - await self.handle_use(nick, channel, item_id, user, target_nick) - else: - self.send_message(channel, f"{nick} > Usage: !use [target_nick]") - elif full_cmd.startswith('!give '): - parts = full_cmd[6:].split() - if len(parts) >= 2: - target_nick, item_id = parts[0], parts[1] - await self.handle_give(nick, channel, user, target_nick, item_id) - else: - self.send_message(channel, f"{nick} > Usage: !give ") - elif full_cmd.startswith('!sell '): - item_id = full_cmd[6:].strip() - await self.handle_sell(nick, channel, item_id, user) - elif full_cmd.startswith('!trade '): - parts = full_cmd[7:].split() - if len(parts) >= 3: - target_nick, item, amount = parts[0], parts[1], parts[2] - await self.handle_trade(nick, channel, user, target_nick, item, amount) - else: - self.send_message(channel, f"{nick} > Usage: !trade ") - elif full_cmd.startswith('!rearm ') and self.is_admin(user): # Admin only - # Allow rearming other players or self - target_nick = full_cmd[7:].strip() - await self.handle_rearm(nick, channel, user, target_nick) - elif cmd == '!rearm' and self.is_admin(user): # Admin only - # Rearm self - await self.handle_rearm(nick, channel, user, nick) - elif cmd == '!duck' and self.is_admin(user): # Admin only - await self.spawn_duck_now(channel) - elif cmd == '!golden' and self.is_admin(user): # Admin only - await self.spawn_duck_now(channel, force_golden=True) - elif cmd == '!listplayers' and self.is_admin(user): # Admin only - await self.handle_listplayers(nick, channel) - elif full_cmd.startswith('!ban ') and self.is_admin(user): # Admin only - target_nick = full_cmd[5:].strip() - await self.handle_ban(nick, channel, target_nick) - elif full_cmd.startswith('!reset ') and self.is_admin(user): # Admin only - target_nick = full_cmd[7:].strip() - await self.handle_reset(nick, channel, target_nick) - elif cmd == '!resetdb' and self.is_admin(user): # Admin only - await self.handle_reset_database(nick, channel, user) - elif full_cmd.startswith('!resetdb confirm ') and self.is_admin(user): # Admin only - confirmation = full_cmd[17:].strip() - await self.handle_reset_database_confirm(nick, channel, user, confirmation) - elif cmd == '!restart' and self.is_admin(user): # Admin only - await self.handle_restart(nick, channel) - elif cmd == '!quit' and self.is_admin(user): # Admin only - await self.handle_quit(nick, channel) - elif cmd == '!ducklaunch' and self.is_admin(user): # Admin only - await self.spawn_duck_now(channel) - elif cmd == '!ducks': - # Show duck count for all users - channel_ducks = self.ducks.get(channel, []) - alive_ducks = [duck for duck in channel_ducks if duck.get('alive')] - dead_ducks = [duck for duck in channel_ducks if not duck.get('alive')] - - if alive_ducks: - duck_list = [] - for duck in alive_ducks: - duck_type = duck.get('type', 'normal') - spawn_time = time.time() - duck['spawn_time'] - if duck_type == 'golden': - duck_list.append(f"{self.colors['yellow']}Golden Duck{self.colors['reset']} ({spawn_time:.1f}s)") - else: - duck_list.append(f"Duck ({spawn_time:.1f}s)") - self.send_message(channel, f"{nick} > Active ducks: {', '.join(duck_list)}") - else: - self.send_message(channel, f"{nick} > No ducks currently active.") - - elif cmd == '!top' or cmd == '!leaderboard' or cmd == '!topduck': - # Show top players by XP - if not self.players: - self.send_message(channel, f"{nick} > No players found!") - return - - # Sort players by XP - sorted_players = sorted(self.players.items(), key=lambda x: x[1]['xp'], reverse=True) - top_5 = sorted_players[:5] - - self.send_message(channel, f"{self.colors['cyan']}🏆 TOP HUNTERS LEADERBOARD 🏆{self.colors['reset']}") - for i, (player_nick, player_data) in enumerate(top_5, 1): - level, title = self.get_player_level(player_data['xp']) - total_ducks = player_data.get('caught', 0) + player_data.get('befriended', 0) - golden = player_data.get('golden_ducks', 0) - golden_text = f" ({self.colors['yellow']}{golden} golden{self.colors['reset']})" if golden > 0 else "" - - if i == 1: - rank_color = self.colors['yellow'] # Gold - elif i == 2: - rank_color = self.colors['gray'] # Silver - elif i == 3: - rank_color = self.colors['orange'] # Bronze - else: - rank_color = self.colors['white'] - - self.send_message(channel, f"{rank_color}#{i}{self.colors['reset']} {player_nick} - Level {level}: {title} | XP: {player_data['xp']} | Ducks: {total_ducks}{golden_text}") - - elif cmd == '!levels': - # Show level progression table - self.send_message(channel, f"{self.colors['cyan']}🎯 LEVEL PROGRESSION SYSTEM 🎯{self.colors['reset']}") - - # Show first 10 levels as example - for i in range(min(10, len(self.levels))): - level_data = self.levels[i] - next_xp = self.levels[i + 1]['xp'] if i + 1 < len(self.levels) else "MAX" - self.send_message(channel, f"Level {i + 1}: {level_data['title']} (XP: {level_data['xp']} - {next_xp})") - - if len(self.levels) > 10: - self.send_message(channel, f"... and {len(self.levels) - 10} more levels up to Level {len(self.levels)}: {self.levels[-1]['title']}") - - elif full_cmd.startswith('!level '): - # Show specific player's level info - target_nick = full_cmd[7:].strip().lower() - if target_nick in self.players: - target_player = self.players[target_nick] - level, title = self.get_player_level(target_player['xp']) - xp_for_next = self.get_xp_for_next_level(target_player['xp']) - - if xp_for_next > 0: - next_info = f"Next level in {xp_for_next} XP" - else: - next_info = "MAX LEVEL REACHED!" - - self.send_message(channel, f"{nick} > {target_nick}: Level {level} - {self.colors['cyan']}{title}{self.colors['reset']} | {next_info}") - else: - self.send_message(channel, f"{nick} > Player {target_nick} not found!") - - elif cmd == '!karma': - # Show karma leaderboard - if not self.players: - self.send_message(channel, f"{nick} > No players found!") - return - - # Sort by karma - karma_players = [(nick, data) for nick, data in self.players.items() if data.get('karma', 0) != 0] - karma_players.sort(key=lambda x: x[1].get('karma', 0), reverse=True) - - if not karma_players: - self.send_message(channel, f"{nick} > No karma data available!") - return - - self.send_message(channel, f"{self.colors['purple']}☯ KARMA LEADERBOARD ☯{self.colors['reset']}") - for i, (player_nick, player_data) in enumerate(karma_players[:5], 1): - karma = player_data.get('karma', 0) - karma_color = self.colors['green'] if karma >= 0 else self.colors['red'] - karma_text = "Saint" if karma >= 50 else "Good" if karma >= 10 else "Evil" if karma <= -10 else "Chaotic" if karma <= -50 else "Neutral" - - self.send_message(channel, f"#{i} {player_nick} - {karma_color}Karma: {karma}{self.colors['reset']} ({karma_text})") - - elif cmd == '!ducks': - # Show duck count for all users - channel_ducks = self.ducks.get(channel, []) - alive_ducks = [duck for duck in channel_ducks if duck.get('alive')] - dead_ducks = [duck for duck in channel_ducks if not duck.get('alive')] - - if alive_ducks: - oldest_time = min(time.time() - duck['spawn_time'] for duck in alive_ducks) - self.send_message(channel, f"{nick} > {len(alive_ducks)} ducks in {channel} | Oldest: {oldest_time:.1f}s | Dead: {len(dead_ducks)} | Timeout: {self.duck_timeout_min}-{self.duck_timeout_max}s") - else: - self.send_message(channel, f"{nick} > No ducks in {channel} | Dead: {len(dead_ducks)}") - elif cmd == '!output' or full_cmd.startswith('!output '): - parts = cmd.split(maxsplit=1) - output_type = parts[1] if len(parts) > 1 else '' - await self.handle_output(nick, channel, user, output_type) - - elif cmd == '!ducktime': - # Show time until next duck spawn - await self.handle_ducktime(nick, channel) - - elif cmd == '!lastduck': - # Show information about the last duck shot - await self.handle_lastduck(nick, channel) - - elif cmd == '!records': - # Show channel records - await self.handle_records(nick, channel) - elif full_cmd.startswith('!ignore ') and self.is_admin(user): # Admin only - target_nick = full_cmd[8:].strip().lower() - await self.handle_ignore(nick, channel, target_nick) - elif full_cmd.startswith('!unignore ') and self.is_admin(user): # Admin only - target_nick = full_cmd[10:].strip().lower() - await self.handle_delignore(nick, channel, target_nick) - elif full_cmd.startswith('!giveitem ') and self.is_admin(user): # Admin only - parts = full_cmd[10:].split() - if len(parts) >= 2: - target_nick, item = parts[0], parts[1] - await self.handle_admin_giveitem(nick, channel, target_nick, item) - else: - self.send_message(channel, f"{nick} > Usage: !giveitem ") - elif full_cmd.startswith('!givexp ') and self.is_admin(user): # Admin only - parts = full_cmd[8:].split() - if len(parts) >= 2: - target_nick, amount = parts[0], parts[1] - await self.handle_admin_givexp(nick, channel, target_nick, amount) - else: - self.send_message(channel, f"{nick} > Usage: !givexp ") - # No else clause - ignore unknown commands to avoid interfering with other bots - - except Exception as e: - # Graceful degradation - log error but don't crash - self.logger.error(f"Command handling error for {user} in {channel}: {e}") - import traceback - self.logger.error(f"Full traceback: {traceback.format_exc()}") - - # Send a gentle error message to user - try: - nick = user.split('!')[0] if user and '!' in user else "Unknown" - error_msg = f"{nick} > Sorry, there was an error processing your command. Please try again." - if channel == self.config['nick']: # Private message - self.send_message(nick, error_msg) - else: # Channel message - self.send_message(channel, error_msg) - except Exception as send_error: - self.logger.error(f"Failed to send error message: {send_error}") - - async def handle_stats(self, nick, channel, user): - player = self.get_player(user) - if not player: - self.send_message(channel, f"{nick} > Player data not found!") - return - - # Get level and title - level, title = self.get_player_level(player['xp']) - xp_for_next = self.get_xp_for_next_level(player['xp']) - - # Calculate advanced stats - total_shots = player.get('caught', 0) + player.get('missed', 0) - effective_accuracy = (player.get('caught', 0) / total_shots * 100) if total_shots > 0 else 0 - average_time = (player.get('total_reflex_time', 0) / player.get('reflex_shots', 1)) if player.get('reflex_shots', 0) > 0 else 0 - - # Gun status - gun_status = "" - if player.get('gun_confiscated', False): - gun_status += f" {self.colors['red']}[CONFISCATED]{self.colors['reset']}" - if player.get('jammed', False): - gun_status += f" {self.colors['yellow']}[JAMMED]{self.colors['reset']}" - if player.get('explosive_ammo', False): - gun_status += f" {self.colors['orange']}[EXPLOSIVE]{self.colors['reset']}" - - # Compact stats display - combine into fewer lines - duck_display = f"D:{player.get('caught', 0)}" - if player.get('befriended', 0) > 0: - duck_display += f"/B:{player['befriended']}" - if player.get('golden_ducks', 0) > 0: - duck_display += f"/{self.colors['yellow']}G:{player['golden_ducks']}{self.colors['reset']}" - - # Main stats line - karma_color = self.colors['green'] if player.get('karma', 0) >= 0 else self.colors['red'] - stats_line1 = f"{nick} > {duck_display} | L{level} | XP:{player['xp']}" - if xp_for_next > 0: - stats_line1 += f"(+{xp_for_next})" - stats_line1 += f" | {karma_color}K:{player.get('karma', 0)}{self.colors['reset']}" - - # Equipment line with compact gun status - weapon_name = player.get('weapon', 'pistol')[:6] # Shorten weapon names - compact_gun_status = "" - if player.get('gun_confiscated', False): - compact_gun_status += "[CONF]" - if player.get('jammed', False): - compact_gun_status += "[JAM]" - if player.get('explosive_ammo', False): - compact_gun_status += "[EXP]" - - stats_line2 = f"{nick} > {weapon_name.title()}{compact_gun_status} | Ammo: {player['ammo']}/{player['max_ammo']} | Chargers: {player['chargers']}/{player['max_chargers']} | Accuracy: {player['accuracy']}% (effective: {effective_accuracy:.0f}%) | Reliability: {self.calculate_gun_reliability(player)}%" - - # Optional advanced stats line (if requested) - best_time = player.get('best_time', 999.9) - best_display = f"{best_time:.3f}s" if best_time < 999 else "none" - - stats_line3 = f"{nick} > Best:{best_display} | Avg:{average_time:.3f}s | Jams:{player.get('jammed_count', 0)} | Accidents:{player.get('accidents', 0)} | Lucky:{player.get('lucky_shots', 0)}" - - # Send compact stats (just 2-3 lines instead of 4+) - await self.send_user_message(nick, channel, stats_line1, 'player_stats') - await self.send_user_message(nick, channel, stats_line2, 'player_stats') - - # Display inventory compactly if player has items - if player.get('inventory'): - inventory_items = [] - shop_items = { - '1': 'Bullet', '2': 'Clip', '3': 'AP', '4': 'Explosive', - '5': 'Gun', '6': 'Grease', '7': 'Sight', '8': 'Detector', '9': 'Silencer', - '10': 'Clover', '11': 'Shotgun', '12': 'Rifle', '13': 'Sniper', '14': 'Auto', - '15': 'Sand', '16': 'Water', '17': 'Sabotage', '18': 'Life Ins', - '19': 'Liability', '20': 'Decoy', '21': 'Bread', '22': 'Detector', '23': 'Mech Duck' - } - - for item_id, count in player['inventory'].items(): - item_name = shop_items.get(item_id, f"#{item_id}") - inventory_items.append(f"{item_id}:{item_name}({count})") - - if inventory_items: - max_slots = self.get_config('economy.max_inventory_slots', 20) - total_items = sum(player['inventory'].values()) - inventory_display = f"{nick} > {self.colors['magenta']}Inventory ({total_items}/{max_slots}):{self.colors['reset']} {' | '.join(inventory_items[:8])}" - if len(inventory_items) > 8: - inventory_display += f" ... +{len(inventory_items) - 8} more" - await self.send_user_message(nick, channel, inventory_display, 'player_stats') - # Only show advanced stats if player has significant activity - if player.get('reflex_shots', 0) > 5: - await self.send_user_message(nick, channel, stats_line3, 'player_stats') - - # Inventory display - if player.get('inventory'): - inventory_items = [] - shop_items = { - '1': 'Extra bullet', '2': 'Extra clip', '3': 'AP ammo', '4': 'Explosive ammo', - '5': 'Gun restore', '6': 'Grease', '7': 'Sight', '8': 'Detector', '9': 'Silencer', - '10': 'Clover', '11': 'Shotgun', '12': 'Rifle', '13': 'Sniper', '14': 'Auto shotgun', - '15': 'Sand', '16': 'Water', '17': 'Sabotage', '18': 'Life insurance', - '19': 'Liability insurance', '20': 'Decoy', '21': 'Bread', '22': 'Duck detector', '23': 'Mechanical duck' - } - - for item_id, count in player['inventory'].items(): - inventory_items.append(f"{item_id}({count})") - - if inventory_items: - max_slots = self.get_config('economy.max_inventory_slots', 20) - total_items = sum(player['inventory'].values()) - inventory_display = f"{nick} > {self.colors['magenta']}Items({total_items}/{max_slots}):{self.colors['reset']} {' '.join(inventory_items[:15])}" - if len(inventory_items) > 15: - inventory_display += f" +{len(inventory_items) - 15}" - await self.send_user_message(nick, channel, inventory_display, 'player_stats') - - async def handle_rearm(self, nick, channel, user, target_nick): - """Rearm a player whose gun was confiscated""" - player = self.get_player(user) - target_nick_lower = target_nick.lower() - - if not player: - self.send_message(channel, f"{nick} > Player data not found!") - return - - # Check if target exists - if target_nick_lower not in self.players: - self.send_message(channel, f"{nick} > Player {target_nick} not found!") - return - - target_player = self.players[target_nick_lower] - - # Check if target's gun is confiscated - if not target_player.get('gun_confiscated', False): - self.send_message(channel, f"{nick} > {target_nick}'s gun is not confiscated!") - return - - # Admins can rearm anyone for free - is_admin = self.is_admin(user) - - if is_admin: - # Admin rearm - no cost, configurable restoration - target_player['gun_confiscated'] = False - - # Configure ammo restoration - if self.get_config('moderation.admin_rearm_gives_full_ammo', True): - target_player['ammo'] = target_player['max_ammo'] # Full ammo - ammo_text = "full ammo" - else: - target_player['ammo'] = min(target_player['ammo'] + 1, target_player['max_ammo']) # Just +1 ammo - ammo_text = "+1 ammo" - - # Configure charger restoration - if self.get_config('moderation.admin_rearm_gives_full_chargers', True): - target_player['chargers'] = target_player.get('max_chargers', 2) # Full chargers - charger_text = ", full chargers" - else: - charger_text = "" - - if target_nick_lower == nick.lower(): - self.send_message(channel, f"{nick} > {self.colors['green']}Admin command: Gun restored with {ammo_text}{charger_text}.{self.colors['reset']}") - else: - self.send_message(channel, f"{nick} > {self.colors['green']}Admin command: {target_nick}'s gun restored with {ammo_text}{charger_text}.{self.colors['reset']}") - self.save_database() - elif target_nick_lower == nick.lower(): - # Regular player rearming self - costs XP - rearm_cost = 40 - if player['xp'] < rearm_cost: - self.send_message(channel, f"{nick} > You need {rearm_cost} XP to rearm yourself (you have {player['xp']} XP)") - return - - player['xp'] -= rearm_cost - player['gun_confiscated'] = False - player['ammo'] = player['max_ammo'] # Full ammo when rearmed - self.send_message(channel, f"{nick} > {self.colors['green']}You rearmed yourself! [-{rearm_cost} XP] Gun restored with full ammo.{self.colors['reset']}") - self.save_player(user) - else: - # Regular player rearming someone else - costs XP (friendly gesture) - rearm_cost_xp = 5 - if player['xp'] < rearm_cost_xp: - self.send_message(channel, f"{nick} > You need {rearm_cost_xp} XP to rearm {target_nick} (you have {player['xp']} XP)") - return - - player['xp'] -= rearm_cost_xp - target_player['gun_confiscated'] = False - target_player['ammo'] = target_player['max_ammo'] # Full ammo when rearmed - self.send_message(channel, f"{nick} > {self.colors['green']}You rearmed {target_nick}! [-{rearm_cost_xp} XP] {target_nick}'s gun restored with full ammo.{self.colors['reset']}") - self.save_player(user) - self.save_database() - - async def handle_help(self, nick, channel): - help_lines = [ - f"{nick} > {self.colors['cyan']}🦆 DUCKHUNT COMMANDS 🦆{self.colors['reset']}", - f"{nick} > {self.colors['green']}Game:{self.colors['reset']} !bang !bef !reload !duckstats !topduck !shop !buy !use !give ", - f"{nick} > {self.colors['blue']}Settings:{self.colors['reset']} !output " - ] - if self.is_admin(f"{nick}!*@*"): # Check if admin - help_lines.append(f"{nick} > {self.colors['red']}Admin:{self.colors['reset']} !duck !golden !ban !reset !resetdb !rearm !giveitem !givexp !ignore !unignore | /msg {self.config['nick']} restart|quit") - for line in help_lines: - self.send_message(channel, line) - - async def handle_output(self, nick, channel, user, output_type): - """Handle output mode setting (PUBLIC, NOTICE, or PRIVMSG)""" - player = self.get_player(user) - if not player: - self.send_message(channel, f"{nick} > Player data not found!") - return - - # Ensure player has settings (for existing players) - if 'settings' not in player: - default_mode = self.get_config('message_output.default_user_mode', 'PUBLIC') - player['settings'] = { - 'output_mode': default_mode - } - - output_type = output_type.upper() - - if output_type == 'PUBLIC': - player['settings']['output_mode'] = 'PUBLIC' - self.save_database() - self.send_message(channel, f"{nick} > Output mode set to {self.colors['cyan']}PUBLIC{self.colors['reset']} (channel messages)") - - elif output_type == 'NOTICE': - player['settings']['output_mode'] = 'NOTICE' - self.save_database() - self.send_message(channel, f"{nick} > Output mode set to {self.colors['cyan']}NOTICE{self.colors['reset']} (channel notices)") - - elif output_type == 'PRIVMSG': - player['settings']['output_mode'] = 'PRIVMSG' - self.save_database() - self.send_message(channel, f"{nick} > Output mode set to {self.colors['cyan']}PRIVMSG{self.colors['reset']} (private messages)") - - else: - current_mode = player['settings'].get('output_mode', 'NOTICE') - self.send_message(channel, f"{nick} > Current output mode: {self.colors['cyan']}{current_mode}{self.colors['reset']} | Usage: !output PUBLIC, !output NOTICE, or !output PRIVMSG") - - async def handle_ducktime(self, nick, channel): - """Show time until next duck spawn""" - current_time = time.time() - - # Check if there are active ducks - channel_ducks = self.ducks.get(channel, []) - alive_ducks = [duck for duck in channel_ducks if duck.get('alive')] - - if alive_ducks: - self.send_message(channel, f"{nick} > {len(alive_ducks)} duck(s) currently active! Go hunt them!") - return - - # Show next spawn time if we have it - if channel in self.next_duck_spawn: - next_spawn = self.next_duck_spawn[channel] - time_until = max(0, next_spawn - current_time) - - if time_until > 0: - minutes = int(time_until // 60) - seconds = int(time_until % 60) - difficulty = self.duck_difficulty.get(channel, 1.0) - difficulty_text = f" (Difficulty: {difficulty:.1f}x)" if difficulty > 1.0 else "" - self.send_message(channel, f"{nick} > Next duck spawn in {self.colors['cyan']}{minutes}m {seconds}s{self.colors['reset']}{difficulty_text}") - else: - self.send_message(channel, f"{nick} > Duck should spawn any moment now...") - else: - # Estimate based on spawn range - min_min = self.duck_spawn_min // 60 - max_min = self.duck_spawn_max // 60 - self.send_message(channel, f"{nick} > Ducks spawn every {min_min}-{max_min} minutes (spawn time varies)") - - async def handle_lastduck(self, nick, channel): - """Show information about the last duck shot in this channel""" - if channel not in self.channel_records: - self.send_message(channel, f"{nick} > No duck records found for {channel}") - return - - last_duck = self.channel_records[channel].get('last_duck') - if not last_duck: - self.send_message(channel, f"{nick} > No ducks have been shot in {channel} yet") - return - - # Format the last duck info - hunter = last_duck['hunter'] - duck_type = last_duck['type'] - shot_time = last_duck['shot_time'] - time_ago = time.time() - last_duck['timestamp'] - - # Format time ago - if time_ago < 60: - time_ago_str = f"{int(time_ago)}s ago" - elif time_ago < 3600: - time_ago_str = f"{int(time_ago // 60)}m ago" - else: - time_ago_str = f"{int(time_ago // 3600)}h ago" - - duck_emoji = "🥇" if duck_type == "golden" else "🦆" - self.send_message(channel, f"{nick} > Last duck: {duck_emoji} {duck_type} duck shot by {self.colors['cyan']}{hunter}{self.colors['reset']} in {shot_time:.3f}s ({time_ago_str})") - - async def handle_records(self, nick, channel): - """Show channel records and statistics""" - if channel not in self.channel_records: - self.send_message(channel, f"{nick} > No records found for {channel}") - return - - records = self.channel_records[channel] - - # Header - self.send_message(channel, f"{nick} > {self.colors['yellow']}📊 {channel.upper()} RECORDS 📊{self.colors['reset']}") - - # Fastest shot - fastest = records.get('fastest_shot') - if fastest: - self.send_message(channel, f"🏆 Fastest shot: {self.colors['green']}{fastest['time']:.3f}s{self.colors['reset']} by {self.colors['cyan']}{fastest['hunter']}{self.colors['reset']} ({fastest['duck_type']} duck)") - - # Total stats - total_ducks = records.get('total_ducks', 0) - total_shots = records.get('total_shots', 0) - accuracy = (total_ducks / total_shots * 100) if total_shots > 0 else 0 - - self.send_message(channel, f"📈 Total: {total_ducks} ducks shot, {total_shots} shots fired ({accuracy:.1f}% accuracy)") - - # Current difficulty - difficulty = self.duck_difficulty.get(channel, 1.0) - if difficulty > 1.0: - self.send_message(channel, f"🧠 Duck intelligence: {self.colors['red']}{difficulty:.2f}x harder{self.colors['reset']} (they're learning!)") - else: - self.send_message(channel, f"🧠 Duck intelligence: Normal (fresh ducks)") - - async def handle_shop(self, nick, channel, user): - player = self.get_player(user) - if not player: - self.send_message(channel, f"{nick} > Player data not found!") - return - - # Create organized shop display - shop_items = { - 'ammo': [ - {'id': '1', 'name': 'Extra bullet', 'cost': 7}, - {'id': '2', 'name': 'Extra clip', 'cost': 20}, - {'id': '3', 'name': 'AP ammo', 'cost': 15}, - {'id': '4', 'name': 'Explosive ammo', 'cost': 25} - ], - 'weapons': [ - {'id': '11', 'name': 'Shotgun', 'cost': 100}, - {'id': '12', 'name': 'Assault rifle', 'cost': 200}, - {'id': '13', 'name': 'Sniper rifle', 'cost': 350} - ], - 'upgrades': [ - {'id': '6', 'name': 'Grease', 'cost': 8}, - {'id': '7', 'name': 'Sight', 'cost': 6}, - {'id': '8', 'name': 'Infrared detector', 'cost': 15}, - {'id': '9', 'name': 'Silencer', 'cost': 5}, - {'id': '10', 'name': 'Four-leaf clover', 'cost': 13} - ], - 'special': [ - {'id': '5', 'name': 'Repurchase gun', 'cost': 40}, - {'id': '15', 'name': 'Sand', 'cost': 7}, - {'id': '16', 'name': 'Water bucket', 'cost': 10}, - {'id': '17', 'name': 'Sabotage', 'cost': 14}, - {'id': '20', 'name': 'Decoy', 'cost': 80}, - {'id': '21', 'name': 'Bread', 'cost': 10}, - {'id': '23', 'name': 'Mechanical duck', 'cost': 50} - ], - 'insurance': [ - {'id': '18', 'name': 'Life insurance', 'cost': 10}, - {'id': '19', 'name': 'Liability insurance', 'cost': 5} - ] - } - - # Format each category - def format_items(items, color): - formatted = [] - for item in items: - formatted.append(f"{item['id']}.{item['name']}({item['cost']})") - return ' '.join(formatted) - - # Send compact shop header - self.send_message(channel, f"{nick} > {self.colors['yellow']}SHOP{self.colors['reset']} XP:{self.colors['green']}{player['xp']}{self.colors['reset']}") - - # Send categorized items in compact format - self.send_message(channel, f"{self.colors['cyan']}Ammo:{self.colors['reset']} {format_items(shop_items['ammo'], self.colors['cyan'])}") - self.send_message(channel, f"{self.colors['red']}Weapons:{self.colors['reset']} {format_items(shop_items['weapons'], self.colors['red'])}") - self.send_message(channel, f"{self.colors['green']}Upgrades:{self.colors['reset']} {format_items(shop_items['upgrades'], self.colors['green'])}") - self.send_message(channel, f"{self.colors['yellow']}Special:{self.colors['reset']} {format_items(shop_items['special'], self.colors['yellow'])}") - self.send_message(channel, f"{self.colors['magenta']}Insurance:{self.colors['reset']} {format_items(shop_items['insurance'], self.colors['magenta'])}") - - # Compact footer - self.send_message(channel, f"Use !shop to buy") - - async def handle_buy(self, nick, channel, item, user): - """Buy items and add to inventory""" - player = self.get_player(user) - if not player: - self.send_message(channel, f"{nick} > Player data not found!") - return - - # Check if inventory system is enabled - if not self.get_config('economy.inventory_system_enabled', True): - self.send_message(channel, f"{nick} > Inventory system is disabled!") - return - - # Initialize inventory if not exists - if 'inventory' not in player: - player['inventory'] = {} - - # Eggdrop-style shop items with XP costs - shop_items = { - '1': {'name': 'Extra bullet', 'cost': 7}, - '2': {'name': 'Extra clip', 'cost': 20}, - '3': {'name': 'AP ammo', 'cost': 15}, - '4': {'name': 'Explosive ammo', 'cost': 25}, - '5': {'name': 'Repurchase confiscated gun', 'cost': 40}, - '6': {'name': 'Grease', 'cost': 8}, - '7': {'name': 'Sight', 'cost': 6}, - '8': {'name': 'Infrared detector', 'cost': 15}, - '9': {'name': 'Silencer', 'cost': 5}, - '10': {'name': 'Four-leaf clover', 'cost': 13}, - '11': {'name': 'Shotgun', 'cost': 100}, - '12': {'name': 'Assault rifle', 'cost': 200}, - '13': {'name': 'Sniper rifle', 'cost': 350}, - '14': {'name': 'Automatic shotgun', 'cost': 500}, - '15': {'name': 'Handful of sand', 'cost': 7}, - '16': {'name': 'Water bucket', 'cost': 10}, - '17': {'name': 'Sabotage', 'cost': 14}, - '18': {'name': 'Life insurance', 'cost': 10}, - '19': {'name': 'Liability insurance', 'cost': 5}, - '20': {'name': 'Decoy', 'cost': 80}, - '21': {'name': 'Piece of bread', 'cost': 10}, - '22': {'name': 'Ducks detector', 'cost': 50}, - '23': {'name': 'Mechanical duck', 'cost': 50} - } - - if item not in shop_items: - self.send_message(channel, f"{nick} > Invalid item ID. Use !shop to see available items.") - return - - shop_item = shop_items[item] - - # Check for bread channel limit - if item == '21': # Bread - # Initialize and clean up old bread - if channel not in self.channel_bread: - self.channel_bread[channel] = [] - - # Clean up expired bread (30 minutes) - import time - current_time = time.time() - self.channel_bread[channel] = [b for b in self.channel_bread[channel] if current_time - b['time'] < 1800] - - # Check limit after cleanup - channel_bread_count = len(self.channel_bread[channel]) - if channel_bread_count >= 3: - self.send_message(channel, f"{nick} > Maximum 3 bread items allowed in channel! Current: {channel_bread_count}") - return - cost = shop_item['cost'] - - if player['xp'] < cost: - self.send_message(channel, f"{nick} > Not enough XP! You need {cost} XP but only have {player['xp']}.") - return - - # Check inventory space - max_slots = self.get_config('economy.max_inventory_slots', 20) - if max_slots is None: - max_slots = 20 - total_items = sum(player['inventory'].values()) - if total_items >= max_slots: - self.send_message(channel, f"{nick} > Inventory full! ({total_items}/{max_slots}) Use items or increase capacity.") - return - - # Purchase the item and add to inventory - player['xp'] -= cost - if item in player['inventory']: - player['inventory'][item] += 1 - else: - player['inventory'][item] = 1 - - self.send_message(channel, f"{nick} > Purchased {shop_item['name']}! Added to inventory ({total_items + 1}/{max_slots})") - - # Save to database after purchase - self.save_player(user) - - async def handle_sell(self, nick, channel, item_id, user): - """Sell items from inventory for 70% of original cost""" - player = self.get_player(user) - if not player: - self.send_message(channel, f"{nick} > Player data not found!") - return - - # Check if inventory system is enabled - if not self.get_config('economy.inventory_system_enabled', True): - self.send_message(channel, f"{nick} > Inventory system is disabled!") - return - - # Initialize inventory if not exists - if 'inventory' not in player: - player['inventory'] = {} - - # Check if item is in inventory - if item_id not in player['inventory'] or player['inventory'][item_id] <= 0: - self.send_message(channel, f"{nick} > You don't have that item! Check !stats to see your inventory.") - return - - # Get shop item data for pricing - shop_items = { - '1': {'name': 'Extra bullet', 'cost': 7}, - '2': {'name': 'Extra clip', 'cost': 20}, - '3': {'name': 'AP ammo', 'cost': 15}, - '4': {'name': 'Explosive ammo', 'cost': 25}, - '5': {'name': 'Repurchase confiscated gun', 'cost': 40}, - '6': {'name': 'Grease', 'cost': 8}, - '7': {'name': 'Sight', 'cost': 6}, - '8': {'name': 'Infrared detector', 'cost': 15}, - '9': {'name': 'Silencer', 'cost': 5}, - '10': {'name': 'Four-leaf clover', 'cost': 13}, - '11': {'name': 'Shotgun', 'cost': 100}, - '12': {'name': 'Assault rifle', 'cost': 200}, - '13': {'name': 'Sniper rifle', 'cost': 350}, - '14': {'name': 'Automatic shotgun', 'cost': 500}, - '15': {'name': 'Handful of sand', 'cost': 7}, - '16': {'name': 'Water bucket', 'cost': 10}, - '17': {'name': 'Sabotage', 'cost': 14}, - '18': {'name': 'Life insurance', 'cost': 10}, - '19': {'name': 'Liability insurance', 'cost': 5}, - '20': {'name': 'Decoy', 'cost': 80}, - '21': {'name': 'Piece of bread', 'cost': 10}, - '22': {'name': 'Ducks detector', 'cost': 50}, - '23': {'name': 'Mechanical duck', 'cost': 50} - } - - if item_id not in shop_items: - self.send_message(channel, f"{nick} > Invalid item ID!") - return - - shop_item = shop_items[item_id] - original_cost = shop_item['cost'] - sell_price = int(original_cost * 0.7) # 70% of original cost - - # Remove item from inventory - player['inventory'][item_id] -= 1 - if player['inventory'][item_id] <= 0: - del player['inventory'][item_id] - - # Give XP back - player['xp'] += sell_price - - total_items = sum(player['inventory'].values()) - max_slots = self.get_config('economy.max_inventory_slots', 20) - - self.send_message(channel, f"{nick} > Sold {shop_item['name']} for {sell_price}xp! Inventory: ({total_items}/{max_slots})") - - # Save to database after sale - self.save_player(user) - - async def handle_use(self, nick, channel, item_id, user, target_nick=None): - """Use an item from inventory""" - player = self.get_player(user) - if not player: - self.send_message(channel, f"{nick} > Player data not found!") - return - - # Check if item is in inventory - if item_id not in player['inventory'] or player['inventory'][item_id] <= 0: - self.send_message(channel, f"{nick} > You don't have that item! Check !stats to see your inventory.") - return - - # Get shop item data for reference - shop_items = { - '1': {'name': 'Extra bullet', 'effect': 'ammo'}, - '2': {'name': 'Extra clip', 'effect': 'max_ammo'}, - '3': {'name': 'AP ammo', 'effect': 'accuracy'}, - '4': {'name': 'Explosive ammo', 'effect': 'explosive'}, - '5': {'name': 'Repurchase confiscated gun', 'effect': 'gun'}, - '6': {'name': 'Grease', 'effect': 'reliability'}, - '7': {'name': 'Sight', 'effect': 'accuracy'}, - '8': {'name': 'Infrared detector', 'effect': 'detector'}, - '9': {'name': 'Silencer', 'effect': 'silencer'}, - '10': {'name': 'Four-leaf clover', 'effect': 'luck'}, - '11': {'name': 'Shotgun', 'effect': 'shotgun'}, - '12': {'name': 'Assault rifle', 'effect': 'rifle'}, - '13': {'name': 'Sniper rifle', 'effect': 'sniper'}, - '14': {'name': 'Automatic shotgun', 'effect': 'auto_shotgun'}, - '15': {'name': 'Handful of sand', 'effect': 'sand'}, - '16': {'name': 'Water bucket', 'effect': 'water'}, - '17': {'name': 'Sabotage', 'effect': 'sabotage'}, - '18': {'name': 'Life insurance', 'effect': 'life_insurance'}, - '19': {'name': 'Liability insurance', 'effect': 'liability'}, - '20': {'name': 'Decoy', 'effect': 'decoy'}, - '21': {'name': 'Piece of bread', 'effect': 'bread'}, - '22': {'name': 'Ducks detector', 'effect': 'duck_detector'}, - '23': {'name': 'Mechanical duck', 'effect': 'mechanical'} - } - - if item_id not in shop_items: - self.send_message(channel, f"{nick} > Invalid item ID!") - return - - shop_item = shop_items[item_id] - effect = shop_item['effect'] - - # Determine target player - if target_nick and target_nick.lower() != nick.lower(): - # Using on someone else - target_nick_lower = target_nick.lower() - if target_nick_lower not in self.players: - self.send_message(channel, f"{nick} > Player {target_nick} not found!") - return - target_player = self.players[target_nick_lower] - using_on_other = True - else: - # Using on self - target_player = player - target_nick = nick - using_on_other = False - - # Remove item from inventory - player['inventory'][item_id] -= 1 - if player['inventory'][item_id] <= 0: - del player['inventory'][item_id] - - # Apply item effects - if effect == 'ammo': - target_player['ammo'] = min(target_player['max_ammo'], target_player['ammo'] + 1) - if using_on_other: - self.send_message(channel, f"{nick} > Used {shop_item['name']} on {target_nick}! +1 ammo") - else: - self.send_message(channel, f"{nick} > Used {shop_item['name']}! +1 ammo") - elif effect == 'water': - # Water bucket - splash attack on target player - if using_on_other: - # Reduce target's accuracy temporarily - target_player['accuracy'] = max(10, target_player['accuracy'] - 15) - self.send_message(channel, f"{nick} > *SPLASH!* You soaked {target_nick} with water! Their accuracy reduced by 15%!") - else: - self.send_message(channel, f"{nick} > You splashed yourself with water... why?") - elif effect == 'sand': - # Handful of sand - blind target temporarily - if using_on_other: - target_player['accuracy'] = max(5, target_player['accuracy'] - 20) - self.send_message(channel, f"{nick} > *POCKET SAND!* You threw sand in {target_nick}'s eyes! Their accuracy reduced by 20%!") - else: - self.send_message(channel, f"{nick} > You threw sand in your own eyes... brilliant strategy!") - elif effect == 'bread': - # Bread - deploy in channel to attract ducks faster - if using_on_other: - self.send_message(channel, f"{nick} > You can't use bread on other players! Deploy it in the channel.") - return - - # Initialize channel bread if needed - if channel not in self.channel_bread: - self.channel_bread[channel] = [] - - # Check limit (should have been checked in buy, but double-check) - if len(self.channel_bread[channel]) >= 3: - self.send_message(channel, f"{nick} > Maximum 3 bread items already deployed in this channel!") - return - - # Deploy bread - import time - bread_info = { - 'time': time.time(), - 'owner': nick - } - self.channel_bread[channel].append(bread_info) - - self.send_message(channel, f"{nick} > *CRUMBLE CRUMBLE* You scattered bread crumbs around the channel! Ducks will be attracted faster. ({len(self.channel_bread[channel])}/3 bread deployed)") - # Add more effects as needed... - else: - # Default effects for other items - self.send_message(channel, f"{nick} > Used {shop_item['name']}! (Effect: {effect})") - - # Save changes - self.save_player(user) - if using_on_other: - # Save target player too if different - target_user = f"{target_nick.lower()}!user@host" # Simplified - would need real user data - self.save_database() - - async def handle_give(self, nick, channel, user, target_nick, item_id): - """Give an item from inventory to another player""" - # Get giver's player data - player = self.get_player(user) - if not player: - self.send_message(channel, f"{nick} > Player data not found!") - return - - # Check if giver has the item - if item_id not in player['inventory'] or player['inventory'][item_id] <= 0: - self.send_message(channel, f"{nick} > You don't have that item! Check !duckstats to see your inventory.") - return - - # Find target player - target_nick_lower = target_nick.lower() - if target_nick_lower not in self.players: - self.send_message(channel, f"{nick} > Player {target_nick} not found!") - return - - # Can't give to yourself - if target_nick_lower == nick.lower(): - self.send_message(channel, f"{nick} > You can't give items to yourself!") - return - - target_player = self.players[target_nick_lower] - - # Get shop item data for reference - shop_items = { - '1': {'name': 'Extra bullet', 'effect': 'ammo'}, - '2': {'name': 'Extra clip', 'effect': 'max_ammo'}, - '3': {'name': 'AP ammo', 'effect': 'accuracy'}, - '4': {'name': 'Explosive ammo', 'effect': 'explosive'}, - '5': {'name': 'Repurchase confiscated gun', 'effect': 'gun'}, - '6': {'name': 'Grease', 'effect': 'reliability'}, - '7': {'name': 'Sight', 'effect': 'accuracy'}, - '8': {'name': 'Infrared detector', 'effect': 'detector'}, - '9': {'name': 'Silencer', 'effect': 'silencer'}, - '10': {'name': 'Four-leaf clover', 'effect': 'luck'}, - '11': {'name': 'Shotgun', 'effect': 'shotgun'}, - '12': {'name': 'Assault rifle', 'effect': 'rifle'}, - '13': {'name': 'Sniper rifle', 'effect': 'sniper'}, - '14': {'name': 'Automatic shotgun', 'effect': 'auto_shotgun'}, - '15': {'name': 'Handful of sand', 'effect': 'sand'}, - '16': {'name': 'Water bucket', 'effect': 'water'}, - '17': {'name': 'Sabotage', 'effect': 'sabotage'}, - '18': {'name': 'Life insurance', 'effect': 'life_insurance'}, - '19': {'name': 'Liability insurance', 'effect': 'liability'}, - '20': {'name': 'Decoy', 'effect': 'decoy'}, - '21': {'name': 'Piece of bread', 'effect': 'bread'}, - '22': {'name': 'Ducks detector', 'effect': 'duck_detector'}, - '23': {'name': 'Mechanical duck', 'effect': 'mechanical'} - } - - if item_id not in shop_items: - self.send_message(channel, f"{nick} > Invalid item ID! Use shop numbers 1-23.") - return - - shop_item = shop_items[item_id] - - # Remove item from giver - player['inventory'][item_id] -= 1 - if player['inventory'][item_id] <= 0: - del player['inventory'][item_id] - - # Add item to target player - if 'inventory' not in target_player: - target_player['inventory'] = {} - if item_id not in target_player['inventory']: - target_player['inventory'][item_id] = 0 - target_player['inventory'][item_id] += 1 - - # Announce the gift - self.send_message(channel, f"{nick} > Gave {shop_item['name']} to {target_nick}!") - - # Save both players - self.save_player(user) - self.save_database() - - async def handle_trade(self, nick, channel, user, target_nick, item, amount): - """Trade items with other players""" - player = self.get_player(user) - if not player: - return - - try: - amount = int(amount) - except ValueError: - self.send_message(channel, f"{nick} > Amount must be a number!") - return - - if amount <= 0: - self.send_message(channel, f"{nick} > Amount must be positive!") - return - - if amount > 10000: # Prevent excessive amounts - self.send_message(channel, f"{nick} > Amount too large! Maximum: 10,000") - return - - # Find target player (simplified - would need to track online users in real implementation) - if item == 'coins': - if player['coins'] < amount: - self.send_message(channel, f"{nick} > You don't have {amount} coins!") - return - player['coins'] -= amount - self.send_message(channel, f"{nick} > Offering {amount} coins to {target_nick}. They can !accept or !decline.") - # In real implementation, store pending trade - - elif item == 'ammo': - if player['ammo'] < amount: - self.send_message(channel, f"{nick} > You don't have {amount} ammo!") - return - self.send_message(channel, f"{nick} > Offering {amount} ammo to {target_nick}.") - - elif item == 'chargers': - if player['chargers'] < amount: - self.send_message(channel, f"{nick} > You don't have {amount} chargers!") - return - self.send_message(channel, f"{nick} > Offering {amount} chargers to {target_nick}.") - - else: - self.send_message(channel, f"{nick} > Can't trade '{item}'. Use: coins, ammo, or chargers") - - self.save_player(user) - - async def handle_listplayers(self, nick, channel): - """Admin command to list all players""" - if not self.players: - self.send_message(channel, f"{nick} > No players in database.") - return - - player_list = [] - for nick_key, data in self.players.items(): - shot_count = data['caught'] - befriended_count = data.get('befriended', 0) - total_ducks = shot_count + befriended_count - player_list.append(f"{nick_key}(Ducks:{total_ducks},Shot:{shot_count},Befriended:{befriended_count})") - - players_str = " | ".join(player_list[:10]) # Limit to first 10 - if len(self.players) > 10: - players_str += f" ... and {len(self.players) - 10} more" - - self.send_message(channel, f"{nick} > Players: {players_str}") - - async def handle_ban(self, nick, channel, target_nick): - """Admin command to ban a player""" - target_nick_lower = target_nick.lower() - if target_nick_lower in self.players: - del self.players[target_nick_lower] - self.send_message(channel, f"{nick} > Banned and reset {target_nick}") - self.save_database() - else: - self.send_message(channel, f"{nick} > Player {target_nick} not found!") - - async def handle_reset(self, nick, channel, target_nick): - """Admin command to reset a player's stats""" - target_nick_lower = target_nick.lower() - if target_nick_lower in self.players: - # Reset to defaults - self.players[target_nick_lower] = { - 'caught': 0, 'ammo': 10, 'max_ammo': 10, - 'chargers': 2, 'max_chargers': 2, 'xp': 0, - 'accuracy': 85, 'reliability': 90, 'gun_level': 1, - 'luck': 0, 'gun_type': 'pistol' - } - self.send_message(channel, f"{nick} > Reset {target_nick}'s stats to defaults") - self.save_database() - else: - self.send_message(channel, f"{nick} > Player {target_nick} not found!") - - async def handle_reset_database(self, nick, channel, user): - """Admin command to reset entire database - requires confirmation""" - self.send_message(channel, f"{nick} > {self.colors['red']}⚠️ DATABASE RESET WARNING ⚠️{self.colors['reset']}") - self.send_message(channel, f"{nick} > This will DELETE ALL player data, statistics, and progress!") - self.send_message(channel, f"{nick} > {self.colors['yellow']}Players affected: {len(self.players)}{self.colors['reset']}") - self.send_message(channel, f"{nick} > To confirm, type: {self.colors['cyan']}!resetdb confirm DESTROY_ALL_DATA{self.colors['reset']}") - self.send_message(channel, f"{nick} > {self.colors['red']}This action CANNOT be undone!{self.colors['reset']}") - - async def handle_reset_database_confirm(self, nick, channel, user, confirmation): - """Confirm and execute database reset""" - if confirmation != "DESTROY_ALL_DATA": - self.send_message(channel, f"{nick} > {self.colors['red']}Incorrect confirmation code. Database reset cancelled.{self.colors['reset']}") - return - - # Log the reset action - self.logger.warning(f"DATABASE RESET initiated by admin {nick} - All player data will be destroyed") - - # Backup current database - import shutil - backup_name = f"duckhunt_backup_{int(time.time())}.json" - try: - shutil.copy2(self.db_file, backup_name) - self.send_message(channel, f"{nick} > {self.colors['cyan']}Database backed up to: {backup_name}{self.colors['reset']}") - except Exception as e: - self.logger.error(f"Failed to create backup: {e}") - self.send_message(channel, f"{nick} > {self.colors['red']}Warning: Could not create backup!{self.colors['reset']}") - - # Clear all data - player_count = len(self.players) - self.players.clear() - self.ducks.clear() - self.ignored_nicks.clear() - - # Save empty database - self.save_database() - - # Confirmation messages - self.send_message(channel, f"{nick} > {self.colors['green']}✅ DATABASE RESET COMPLETE{self.colors['reset']}") - self.send_message(channel, f"{nick} > {self.colors['yellow']}{player_count} player records deleted{self.colors['reset']}") - self.send_message(channel, f"{nick} > All ducks cleared, fresh start initiated") - self.logger.warning(f"Database reset completed by {nick} - {player_count} players deleted") - - async def handle_restart(self, nick, channel): - """Admin command to restart the bot""" - self.send_message(channel, f"{nick} > Restarting bot...") - self.logger.info(f"Bot restart requested by {nick}") - - # Close connections gracefully - if self.writer: - self.writer.close() - await self.writer.wait_closed() - - # Save any pending data - self.save_database() - - # Restart the Python process - self.logger.info("Restarting Python process...") - python = sys.executable - script = sys.argv[0] - args = sys.argv[1:] - - # Use subprocess to restart - subprocess.Popen([python, script] + args) - - # Exit current process - sys.exit(0) - - async def handle_quit(self, nick, channel): - """Admin command to quit the bot""" - self.send_message(channel, f"{nick} > Shutting down bot...") - self.logger.info(f"Bot shutdown requested by {nick}") - # Close connections gracefully - if self.writer: - self.writer.close() - await self.writer.wait_closed() - # Exit with code 0 for normal shutdown - import sys - sys.exit(0) - - async def handle_ignore(self, nick, channel, target_nick): - """Admin command to ignore a user""" - if target_nick in self.ignored_nicks: - self.send_message(channel, f"{nick} > {target_nick} is already ignored!") - return - - self.ignored_nicks.add(target_nick) - self.send_message(channel, f"{nick} > Now ignoring {target_nick}. Total ignored: {len(self.ignored_nicks)}") - self.logger.info(f"{nick} added {target_nick} to ignore list") - - async def handle_delignore(self, nick, channel, target_nick): - """Admin command to stop ignoring a user""" - if target_nick not in self.ignored_nicks: - self.send_message(channel, f"{nick} > {target_nick} is not ignored!") - return - - self.ignored_nicks.remove(target_nick) - self.send_message(channel, f"{nick} > No longer ignoring {target_nick}. Total ignored: {len(self.ignored_nicks)}") - self.logger.info(f"{nick} removed {target_nick} from ignore list") - - async def handle_admin_giveitem(self, nick, channel, target_nick, item): - """Admin command to give an item to a player""" - target_nick_lower = target_nick.lower() - - # Check if target exists - if target_nick_lower not in self.players: - self.send_message(channel, f"{nick} > Player {target_nick} not found!") - return - - # Shop items reference for item names - shop_items = { - '1': {'name': 'Extra bullet', 'effect': 'ammo'}, - '2': {'name': 'Extra clip', 'effect': 'max_ammo'}, - '3': {'name': 'AP ammo', 'effect': 'accuracy'}, - '4': {'name': 'Explosive ammo', 'effect': 'explosive'}, - '5': {'name': 'Repurchase confiscated gun', 'effect': 'gun'}, - '6': {'name': 'Grease', 'effect': 'reliability'}, - '7': {'name': 'Sight', 'effect': 'accuracy'}, - '8': {'name': 'Infrared detector', 'effect': 'detector'}, - '9': {'name': 'Silencer', 'effect': 'silencer'}, - '10': {'name': 'Four-leaf clover', 'effect': 'luck'}, - '11': {'name': 'Sunglasses', 'effect': 'sunglasses'}, - '12': {'name': 'Spare clothes', 'effect': 'clothes'}, - '13': {'name': 'Brush for gun', 'effect': 'brush'}, - '14': {'name': 'Mirror', 'effect': 'mirror'}, - '15': {'name': 'Handful of sand', 'effect': 'sand'}, - '16': {'name': 'Water bucket', 'effect': 'water'}, - '17': {'name': 'Sabotage', 'effect': 'sabotage'}, - '18': {'name': 'Life insurance', 'effect': 'life_insurance'}, - '19': {'name': 'Liability insurance', 'effect': 'liability'}, - '20': {'name': 'Decoy', 'effect': 'decoy'}, - '21': {'name': 'Piece of bread', 'effect': 'bread'}, - '22': {'name': 'Ducks detector', 'effect': 'duck_detector'}, - '23': {'name': 'Mechanical duck', 'effect': 'mechanical'} - } - - if item not in shop_items: - self.send_message(channel, f"{nick} > Invalid item ID '{item}'. Use item IDs 1-23.") - return - - target_player = self.players[target_nick_lower] - shop_item = shop_items[item] - effect = shop_item['effect'] - - # Apply the item effect - if effect == 'ammo': - target_player['ammo'] = min(target_player['ammo'] + 1, target_player['max_ammo']) - elif effect == 'max_ammo': - target_player['max_ammo'] += 1 - target_player['ammo'] = target_player['max_ammo'] # Fill ammo - elif effect == 'accuracy': - target_player['accuracy'] = min(target_player['accuracy'] + 5, 100) - elif effect == 'explosive': - target_player['explosive_ammo'] = True - elif effect == 'gun': - target_player['gun_confiscated'] = False - target_player['ammo'] = target_player['max_ammo'] - elif effect == 'reliability': - target_player['reliability'] = min(target_player['reliability'] + 5, 100) - elif effect == 'luck': - target_player['luck'] = target_player.get('luck', 0) + 1 - # Add other effects as needed - - self.send_message(channel, f"{nick} > {self.colors['green']}Gave {shop_item['name']} to {target_nick}!{self.colors['reset']}") - self.save_database() - - async def handle_admin_givexp(self, nick, channel, target_nick, amount): - """Admin command to give XP to a player""" - target_nick_lower = target_nick.lower() - - # Check if target exists - if target_nick_lower not in self.players: - self.send_message(channel, f"{nick} > Player {target_nick} not found!") - return - - try: - xp_amount = int(amount) - except ValueError: - self.send_message(channel, f"{nick} > Amount must be a number!") - return - - if abs(xp_amount) > 50000: # Prevent excessive XP changes - self.send_message(channel, f"{nick} > XP amount too large! Maximum: ±50,000") - return - - target_player = self.players[target_nick_lower] - old_xp = target_player['xp'] - target_player['xp'] = max(0, target_player['xp'] + xp_amount) # Prevent negative XP - - color = self.colors['green'] if xp_amount >= 0 else self.colors['red'] - sign = '+' if xp_amount >= 0 else '' - self.send_message(channel, f"{nick} > {color}Gave {sign}{xp_amount} XP to {target_nick}! (Total: {target_player['xp']} XP){self.colors['reset']}") - self.save_database() - - def get_duck_spawn_message(self): - """Get a random duck spawn message with different types""" - duck_types = [ - {"msg": "-.,¸¸.-·°'`'°·-.,¸¸.-·°'`'°· \\_O< QUACK", "type": "normal"}, # Normal duck - {"msg": "-._..-'`'°-,_,.-'`'°-,_,.-'`'°-,_,.-° \\_o< A duck waddles by! QUACK QUACK", "type": "normal"}, # Waddling duck - {"msg": "~~~°*°~~~°*°~~~°*°~~~ \\_O< SPLASH! A duck lands in the water! QUACK!", "type": "normal"}, # Water duck - {"msg": "***GOLDEN*** \\_O< *** A golden duck appears! *** QUACK QUACK! ***GOLDEN***", "type": "golden"}, # Golden duck (rare) - {"msg": "°~°*°~°*°~° \\_o< Brrr! A winter duck appears! QUACK!", "type": "normal"}, # Winter duck - {"msg": ".,¸¸.-·°'`'°·-.,¸¸.-·°'`'°· \\_O< A spring duck blooms into view! QUACK!", "type": "normal"}, # Spring duck - {"msg": "***ZAP*** \\_O< BZZT! An electric duck sparks to life! QUACK! ***ZAP***", "type": "normal"}, # Electric duck - {"msg": "~*~*~*~ \\_o< A sleepy night duck appears... *yawn* quack...", "type": "normal"}, # Night duck - ] - - # Golden duck is rare (5% chance) - if random.random() < 0.05: - golden_duck = [d for d in duck_types if d["type"] == "golden"][0] - return golden_duck - else: - # Choose from normal duck types - normal_ducks = [d for d in duck_types if d["type"] == "normal"] - return random.choice(normal_ducks) - - async def spawn_duck_now(self, channel, force_golden=False): - """Admin command to spawn a duck immediately""" - # Create duck with unique ID and type - duck_id = str(uuid.uuid4())[:8] # Short ID for easier tracking - - if force_golden: - # Force spawn a golden duck - duck_info = { - "msg": f"{self.colors['yellow']}***GOLDEN***{self.colors['reset']} \\_$< {self.colors['yellow']}*** A golden duck appears! ***{self.colors['reset']} QUACK QUACK! {self.colors['yellow']}***GOLDEN***{self.colors['reset']}", - "type": "golden" - } - else: - duck_info = self.get_duck_spawn_message() - - duck_timeout = random.randint(self.duck_timeout_min, self.duck_timeout_max) - duck = { - 'alive': True, - 'spawn_time': time.time(), - 'id': duck_id, - 'type': duck_info['type'], - 'message': duck_info['msg'], - 'timeout': duck_timeout - } - - # Initialize channel duck list if needed - if channel not in self.ducks: - self.ducks[channel] = [] - - # Add duck to channel - self.ducks[channel].append(duck) - - # Consume bread when duck spawns (bread gets eaten!) - if channel in self.channel_bread and self.channel_bread[channel]: - consumed_bread = self.channel_bread[channel].pop(0) # Remove oldest bread - self.logger.info(f"Duck consumed bread from {consumed_bread['owner']} in {channel}") - - # Send spawn message - self.send_message(channel, duck_info['msg']) - self.logger.info(f"Admin spawned {duck_info['type']} duck {duck_id} in {channel}") - return True - return True # Return True to indicate duck was spawned - - async def spawn_ducks(self): - # Spawn first duck immediately after joining - await asyncio.sleep(5) # Brief delay for players to see the bot joined - for channel in self.channels_joined: - await self.spawn_duck_now(channel) - - # Start duck timeout checker - asyncio.create_task(self.duck_timeout_checker()) - - while not self.shutdown_requested: - wait_time = random.randint(self.duck_spawn_min, self.duck_spawn_max) - - # Apply bread effects - reduce spawn time - for channel in self.channels_joined: - if channel in self.channel_bread and self.channel_bread[channel]: - # Clean up old bread (expires after 30 minutes) - current_time = time.time() - self.channel_bread[channel] = [b for b in self.channel_bread[channel] if current_time - b['time'] < 1800] - - if self.channel_bread[channel]: # If any bread remains after cleanup - # Each bread reduces spawn time by 20%, max 60% reduction - bread_count = len(self.channel_bread[channel]) - reduction = min(0.6, bread_count * 0.2) # Max 60% reduction - wait_time = int(wait_time * (1 - reduction)) - self.logger.info(f"Bread effect: {bread_count} bread in {channel}, {reduction*100:.0f}% spawn time reduction") - break # Apply effect based on first channel with bread - - self.logger.info(f"Waiting {wait_time//60}m {wait_time%60}s for next duck") - - # Set next spawn time for all channels - next_spawn_time = time.time() + wait_time - for channel in self.channels_joined: - self.next_duck_spawn[channel] = next_spawn_time - - # Sleep in chunks to check shutdown flag - for _ in range(wait_time): - if self.shutdown_requested: - self.logger.info("Duck spawning stopped due to shutdown request") - return - await asyncio.sleep(1) - - # Check each channel for possible duck spawning - for channel in self.channels_joined: - if self.shutdown_requested: - return - - # Check if there are any alive ducks in this channel - channel_ducks = self.ducks.get(channel, []) - alive_ducks = [duck for duck in channel_ducks if duck.get('alive')] - - # Only consider spawning if no ducks are alive - if not alive_ducks: - # Calculate spawn chance based on bread - base_chance = 0.3 # 30% base chance per spawn cycle - bread_count = 0 - - # Count and clean up bread - if channel in self.channel_bread and self.channel_bread[channel]: - current_time = time.time() - self.channel_bread[channel] = [b for b in self.channel_bread[channel] if current_time - b['time'] < 1800] - bread_count = len(self.channel_bread[channel]) - - # Each bread adds 25% spawn chance - spawn_chance = base_chance + (bread_count * 0.25) - spawn_chance = min(0.95, spawn_chance) # Cap at 95% - - # Roll for spawn - if random.random() < spawn_chance: - await self.spawn_duck_now(channel) - bread_msg = f" (bread boosted: {bread_count} bread = {spawn_chance*100:.0f}% chance)" if bread_count > 0 else "" - self.logger.info(f"Duck spawned in {channel}{bread_msg}") - break # Only spawn in one channel per cycle - - async def duck_timeout_checker(self): - """Remove ducks that have been around too long""" - while not self.shutdown_requested: - await asyncio.sleep(10) # Check every 10 seconds - current_time = time.time() - - for channel in list(self.ducks.keys()): - if channel in self.ducks: - ducks_to_remove = [] - for i, duck in enumerate(self.ducks[channel]): - duck_timeout = duck.get('timeout', 60) # Use individual timeout or default to 60 - if duck['alive'] and (current_time - duck['spawn_time']) > duck_timeout: - # Duck wandered off - ducks_to_remove.append(i) - self.send_message(channel, f"A duck wandered off... *quack quack* (timeout after {duck_timeout}s)") - self.logger.info(f"Duck {duck['id']} timed out in {channel}") - - # Remove timed out ducks (in reverse order to maintain indices) - for i in reversed(ducks_to_remove): - del self.ducks[channel][i] - - async def listen(self): - """Listen for IRC messages with shutdown handling""" - while not self.shutdown_requested: - try: - if not self.reader: - self.logger.error("No reader available") - break - - # Use timeout to allow checking shutdown flag - try: - line = await asyncio.wait_for(self.reader.readline(), timeout=1.0) - except asyncio.TimeoutError: - continue # Check shutdown flag - - if not line: - self.logger.warning("Connection closed by server") - break - - line = line.decode(errors='ignore').strip() - if not line: - continue - - self.logger.debug(f"<- {line}") - - if line.startswith('PING'): - self.send_raw('PONG ' + line.split()[1]) - continue - - prefix, command, params, trailing = parse_message(line) - - except Exception as e: - self.logger.error(f"Error in listen loop: {e}") - await asyncio.sleep(1) # Brief pause before retry - continue - - # Handle SASL authentication responses - if command == 'CAP': - await self.sasl_handler.handle_cap_response(params, trailing) - - elif command == 'AUTHENTICATE': - await self.sasl_handler.handle_authenticate_response(params) - - elif command in ['903', '904', '905', '906', '907', '908']: # SASL responses - await self.sasl_handler.handle_sasl_result(command, params, trailing) - - elif command == '001': # Welcome - self.registered = True - auth_status = " (SASL authenticated)" if self.sasl_handler.is_authenticated() else "" - self.logger.info(f"Successfully registered!{auth_status}") - - # If SASL failed, try NickServ identification - if not self.sasl_handler.is_authenticated(): - await self.attempt_nickserv_auth() - - for chan in self.config['channels']: - self.logger.info(f"Joining {chan}") - self.send_raw(f'JOIN {chan}') - - elif command == 'JOIN' and prefix and prefix.startswith(self.config['nick']): - channel = trailing or (params[0] if params else '') - if channel: - self.channels_joined.add(channel) - self.logger.info(f"Successfully joined {channel}") - - elif command == 'PRIVMSG' and trailing: - target = params[0] if params else '' - sender = prefix.split('!')[0] if prefix else '' - - # Handle NickServ responses - if sender.lower() == 'nickserv': - await self.handle_nickserv_response(trailing) - elif trailing == 'VERSION': - self.send_raw(f'NOTICE {sender} :VERSION DuckHunt Bot v1.0') - else: - await self.handle_command(prefix, target, trailing) - - async def cleanup(self): - """Enhanced cleanup with graceful shutdown""" - self.logger.info("Starting cleanup process...") - - try: - # Cancel all running tasks - for task in self.running_tasks.copy(): - if not task.done(): - self.logger.debug(f"Cancelling task: {task.get_name()}") - task.cancel() - try: - await task - except asyncio.CancelledError: - pass - except Exception as e: - self.logger.error(f"Error cancelling task: {e}") - - # Send goodbye message to all channels - if self.writer and not self.writer.is_closing(): - for channel in self.channels_joined: - self.send_message(channel, "🦆 DuckHunt Bot shutting down. Thanks for playing! 🦆") - await asyncio.sleep(0.1) # Brief delay between messages - - self.send_raw('QUIT :DuckHunt Bot shutting down gracefully') - await asyncio.sleep(1.0) # Give time for QUIT and messages to send - - self.writer.close() - await self.writer.wait_closed() - self.logger.info("IRC connection closed") - - # Final database save with verification - self.save_database() - self.logger.info(f"Final database save completed - {len(self.players)} players saved") - - # Clear in-memory data - self.players.clear() - self.ducks.clear() - - self.logger.info("Cleanup completed successfully") - - except Exception as e: - self.logger.error(f"Error during cleanup: {e}") - import traceback - traceback.print_exc() - - async def run(self): - """Main bot entry point with enhanced shutdown handling""" - try: - # Setup signal handlers - self.setup_signal_handlers() - - self.logger.info("Starting DuckHunt Bot...") - self.load_database() - await self.connect() - - # Create and track main tasks - listen_task = asyncio.create_task(self.listen(), name="listen") - duck_task = asyncio.create_task(self.wait_and_spawn_ducks(), name="duck_spawner") - - self.running_tasks.add(listen_task) - self.running_tasks.add(duck_task) - - # Main execution loop with shutdown monitoring - done, pending = await asyncio.wait( - [listen_task, duck_task], - return_when=asyncio.FIRST_COMPLETED - ) - - # If we get here, one task completed (likely due to error or shutdown) - if self.shutdown_requested: - self.logger.info("Shutdown requested, stopping all tasks...") - else: - self.logger.warning("A main task completed unexpectedly") - - # Cancel remaining tasks - for task in pending: - task.cancel() - try: - await task - except asyncio.CancelledError: - pass - - except KeyboardInterrupt: - self.logger.info("Keyboard interrupt received") - self.shutdown_requested = True - except Exception as e: - self.logger.error(f"Fatal error in main loop: {e}") - import traceback - traceback.print_exc() - finally: - await self.cleanup() - - async def wait_and_spawn_ducks(self): - """Duck spawning with shutdown handling""" - # Wait for registration and channel joins - while not self.registered or not self.channels_joined and not self.shutdown_requested: - await asyncio.sleep(1) - - if self.shutdown_requested: - return - - self.logger.info("Starting duck spawning...") - await self.spawn_ducks() - -def main(): - """Enhanced main entry point with better shutdown handling""" - bot = None - try: - # Load configuration - with open('config.json') as f: - config = json.load(f) - - # Create bot instance - bot = SimpleIRCBot(config) - bot.logger.info("DuckHunt Bot initializing...") - - # Run bot with graceful shutdown - try: - asyncio.run(bot.run()) - except KeyboardInterrupt: - bot.logger.info("Keyboard interrupt received in main") - except Exception as e: - bot.logger.error(f"Runtime error: {e}") - import traceback - traceback.print_exc() - - bot.logger.info("DuckHunt Bot shutdown complete") - - except KeyboardInterrupt: - print("\n🦆 DuckHunt Bot stopped by user") - except FileNotFoundError: - print("❌ Error: config.json not found") - print("Please create a config.json file with your IRC server settings") - except json.JSONDecodeError as e: - print(f"❌ Error: Invalid config.json - {e}") - print("Please check your config.json file syntax") - except Exception as e: - print(f"💥 Unexpected error: {e}") - import traceback - traceback.print_exc() - finally: - # Ensure final message - print("🦆 Thanks for using DuckHunt Bot!") - -if __name__ == '__main__': - main() diff --git a/config.json b/config.json index 0c74b5f..d5620de 100644 --- a/config.json +++ b/config.json @@ -10,246 +10,9 @@ "password": "duckhunt//789//" }, "password": "your_iline_password_here", - "_comment_password": "Server password for I-line exemption (PASS command)", "admins": ["peorth", "computertech", "colby"], - "_comment_message_output": "Default output modes for different message types", - "message_output": { - "default_user_mode": "PUBLIC", - "_comment_default_user_mode": "Default output mode for new users: PUBLIC, NOTICE, or PRIVMSG", - "force_public": { - "duck_spawn": true, - "duck_shot": true, - "duck_befriend": true, - "duck_miss": true, - "wild_shot": true, - "player_stats": true, - "leaderboard": true, - "admin_commands": true - }, - "_comment_force_public": "Message types that always go to channel regardless of user preference" - }, - - "_comment_duck_spawning": "Duck spawning configuration", - "duck_spawn_min": 1800, - "duck_spawn_max": 5400, - "duck_timeout_min": 45, - "duck_timeout_max": 75, - "duck_smartness": { - "enabled": true, - "learning_rate": 0.1, - "max_difficulty_multiplier": 2.0, - "_comment": "Ducks become harder to hit as more are shot in channel" - }, - "records_tracking": { - "enabled": true, - "track_fastest_shots": true, - "track_channel_records": true, - "max_records_stored": 10 - }, - "duck_types": { - "normal": { - "spawn_chance": 0.6, - "xp_reward": 10, - "difficulty": 1.0, - "flee_time": 15, - "messages": ["・゜゜・。。・゜゜\\\\_o< QUACK!"] - }, - "fast": { - "spawn_chance": 0.25, - "xp_reward": 15, - "difficulty": 1.5, - "flee_time": 8, - "messages": ["・゜゜・。。・゜゜\\\\_o< QUACK! (Fast duck!)"] - }, - "rare": { - "spawn_chance": 0.1, - "xp_reward": 30, - "difficulty": 2.0, - "flee_time": 12, - "messages": ["・゜゜・。。・゜゜\\\\_o< QUACK! (Rare duck!)"] - }, - "golden": { - "spawn_chance": 0.05, - "xp_reward": 75, - "difficulty": 3.0, - "flee_time": 10, - "messages": ["・゜゜・。。・゜゜\\\\_✪< ★ GOLDEN DUCK ★"] - } - }, - "sleep_hours": [], - "max_ducks_per_channel": 3, - - "_comment_befriending": "Duck befriending configuration", - "befriending": { - "enabled": true, - "success_chance": 0.7, - "failure_messages": [ - "The duck looked at you suspiciously and flew away!", - "The duck didn't trust you and escaped!", - "The duck was too scared and ran off!" - ], - "scared_away_chance": 0.1, - "scared_away_messages": [ - "You scared the duck away with your approach!", - "The duck was terrified and fled immediately!" - ], - "xp_reward_min": 1, - "xp_reward_max": 3 - }, - - "_comment_shooting": "Shooting mechanics configuration", - "shooting": { - "enabled": true, - "base_accuracy": 85, - "base_reliability": 90, - "jam_chance_base": 10, - "friendly_fire_enabled": true, - "friendly_fire_chance": 5, - "reflex_shot_bonus": 5, - "miss_xp_penalty": 5, - "wild_shot_xp_penalty": 10, - "teamkill_xp_penalty": 20 - }, - - "_comment_weapons": "Weapon system configuration", - "weapons": { - "enabled": true, - "starting_weapon": "pistol", - "starting_ammo": 6, - "max_ammo_base": 6, - "starting_chargers": 2, - "max_chargers_base": 2, - "durability_enabled": true, - "confiscation_enabled": true, - "auto_rearm_on_duck_shot": true, - "_comment_auto_rearm": "Automatically restore confiscated guns when anyone shoots a duck" - }, - - "_comment_new_players": "Starting stats for new hunters", - "new_players": { - "starting_xp": 0, - "starting_accuracy": 65, - "starting_reliability": 70, - "starting_karma": 0, - "starting_deflection": 0, - "starting_defense": 0, - "luck_chance": 5, - "_comment_luck_chance": "Base percentage chance for lucky events", - "random_stats": { - "enabled": false, - "accuracy_range": [60, 80], - "reliability_range": [65, 85], - "_comment_ranges": "If enabled, new players get random stats within these ranges" - } - }, - - "_comment_economy": "Economy and shop configuration", - "economy": { - "enabled": true, - "starting_coins": 100, - "shop_enabled": true, - "trading_enabled": true, - "theft_enabled": true, - "theft_success_rate": 30, - "theft_penalty": 50, - "banking_enabled": true, - "interest_rate": 5, - "loan_enabled": true, - "inventory_system_enabled": true, - "max_inventory_slots": 20 - }, - - "_comment_progression": "Player progression configuration", - "progression": { - "enabled": true, - "max_level": 40, - "xp_multiplier": 1.0, - "level_benefits_enabled": true, - "titles_enabled": true, - "prestige_enabled": false - }, - - "_comment_karma": "Karma system configuration", - "karma": { - "enabled": true, - "hit_bonus": 2, - "golden_hit_bonus": 5, - "teamkill_penalty": 10, - "wild_shot_penalty": 3, - "miss_penalty": 1, - "befriend_success_bonus": 2, - "befriend_fail_penalty": 1 - }, - - "_comment_items": "Items and powerups configuration", - "items": { - "enabled": true, - "lucky_items_enabled": true, - "lucky_item_base_chance": 5, - "detector_enabled": true, - "silencer_enabled": true, - "sunglasses_enabled": true, - "explosive_ammo_enabled": true, - "sabotage_enabled": true, - "insurance_enabled": true, - "decoy_enabled": true - }, - - "_comment_social": "Social features configuration", - "social": { - "leaderboards_enabled": true, - "duck_alerts_enabled": true, - "private_messages_enabled": true, - "statistics_sharing_enabled": true, - "achievements_enabled": false - }, - - "_comment_moderation": "Moderation and admin features", - "moderation": { - "ignore_system_enabled": true, - "rate_limiting_enabled": true, - "rate_limit_cooldown": 2.0, - "admin_commands_enabled": true, - "ban_system_enabled": true, - "database_reset_enabled": true, - "admin_rearm_gives_full_ammo": false, - "admin_rearm_gives_full_chargers":false - }, - - "_comment_advanced": "Advanced game mechanics", - "advanced": { - "gun_jamming_enabled": true, - "weather_effects_enabled": false, - "seasonal_events_enabled": false, - "daily_challenges_enabled": false, - "guild_system_enabled": false, - "pvp_enabled": false - }, - - "_comment_messages": "Message customization", - "messages": { - "custom_duck_messages_enabled": true, - "color_enabled": true, - "emoji_enabled": true, - "verbose_messages": true, - "success_sound_effects": true - }, - - "_comment_database": "Database and persistence", - "database": { - "auto_save_enabled": true, - "auto_save_interval": 300, - "backup_enabled": true, - "backup_interval": 3600, - "compression_enabled": false - }, - - "_comment_debug": "Debug and logging options", - "debug": { - "debug_mode": false, - "verbose_logging": false, - "command_logging": false, - "performance_monitoring": false - } -} + "duck_spawn_min": 10, + "duck_spawn_max": 30, + "duck_timeout": 60 +} \ No newline at end of file diff --git a/duckhunt.json b/duckhunt.json index b34f2ee..3f9b65e 100644 --- a/duckhunt.json +++ b/duckhunt.json @@ -1,1105 +1,16 @@ { "players": { - "guest44288": { - "current_nick": "Guest44288", - "hostmask": "~Colby@Rizon-FFE0901B.ipcom.comunitel.net", - "coins": 102, - "caught": 1, - "ammo": 9, - "max_ammo": 10, - "chargers": 2, - "max_chargers": 2, - "xp": 15, - "accuracy": 85, - "reliability": 90, - "duck_start_time": null, - "gun_level": 1, - "luck": 0, - "gun_type": "pistol" - }, - "colby": { - "coins": 119, - "caught": 12, - "ammo": 5, - "max_ammo": 10, - "chargers": 2, - "max_chargers": 2, - "xp": 100, - "accuracy": 65, - "reliability": 90, - "duck_start_time": null, - "gun_level": 1, - "luck": 0, - "gun_type": "pistol", - "gun_confiscated": true, - "jammed": false, - "jammed_count": 1, - "total_ammo_used": 18, - "shot_at": 9, - "reflex_shots": 6, - "total_reflex_time": 47.87793278694153, - "best_time": 2.6269078254699707, - "karma": 1, - "wild_shots": 10, - "befriended": 6, - "missed": 1, - "inventory": { - "15": 1 - }, - "sand": 1, - "shots": 9, - "max_shots": 10, - "reload_time": 5.0, - "ducks_shot": 12, - "ducks_befriended": 6, - "accuracy_bonus": 0, - "xp_bonus": 0, - "charm_bonus": 0, - "exp": 100, - "money": 119, - "last_hunt": 0, - "last_reload": 0, - "level": 1, - "ignored_users": [], - "confiscated_count": 2 - }, - "colby_": { - "xp": 0, - "caught": 0, - "befriended": 0, - "missed": 0, - "ammo": 6, - "max_ammo": 6, - "chargers": 2, - "max_chargers": 2, - "accuracy": 65, - "reliability": 70, - "gun_confiscated": false, - "explosive_ammo": false, - "settings": { - "notices": true, - "private_messages": false - }, - "golden_ducks": 0, - "karma": 0, - "deflection": 0, - "defense": 0, - "jammed": false, - "jammed_count": 0, - "deaths": 0, - "neutralized": 0, - "deflected": 0, - "best_time": 999.9, - "total_reflex_time": 0.0, - "reflex_shots": 0, - "wild_shots": 0, - "accidents": 0, - "total_ammo_used": 0, - "shot_at": 0, - "lucky_shots": 0, - "luck": 0, - "detector": 0, - "silencer": 0, - "sunglasses": 0, - "clothes": 0, - "grease": 0, - "brush": 0, - "mirror": 0, - "sand": 0, - "water": 0, - "sabotage": 0, - "life_insurance": 0, - "liability": 0, - "decoy": 0, - "bread": 0, - "duck_detector": 0, - "mechanical": 0 - }, "computertech": { - "xp": 4, - "caught": 1, - "befriended": 0, - "missed": 2, - "ammo": 3, - "max_ammo": 6, - "chargers": 2, - "max_chargers": 2, - "accuracy": 65, - "reliability": 70, - "weapon": "pistol", - "gun_confiscated": false, - "explosive_ammo": false, - "settings": { - "notices": true, - "private_messages": false - }, - "golden_ducks": 0, - "karma": 0, - "deflection": 0, - "defense": 0, - "jammed": true, - "jammed_count": 4, - "deaths": 0, - "neutralized": 0, - "deflected": 0, - "best_time": 1.458634614944458, - "total_reflex_time": 1.458634614944458, - "reflex_shots": 1, - "wild_shots": 0, - "accidents": 0, - "total_ammo_used": 3, - "shot_at": 3, - "lucky_shots": 0, - "luck": 0, - "detector": 0, - "silencer": 0, - "sunglasses": 0, - "clothes": 0, - "grease": 0, - "brush": 0, - "mirror": 0, - "sand": 0, - "water": 0, - "sabotage": 0, - "life_insurance": 0, - "liability": 0, - "decoy": 0, - "bread": 0, - "duck_detector": 0, - "mechanical": 0, - "inventory": {} - }, - "loulan": { - "xp": -9, - "caught": 0, - "befriended": 0, - "missed": 2, + "nick": "ComputerTech", + "xp": 10, + "ducks_shot": 1, "ammo": 5, "max_ammo": 6, "chargers": 2, "max_chargers": 2, - "accuracy": 65, - "reliability": 70, - "weapon": "pistol", - "gun_confiscated": false, - "explosive_ammo": false, - "settings": { - "notices": true, - "private_messages": false - }, - "golden_ducks": 0, - "karma": -5, - "deflection": 0, - "defense": 0, - "jammed": true, - "jammed_count": 1, - "deaths": 0, - "neutralized": 0, - "deflected": 0, - "best_time": 999.9, - "total_reflex_time": 0.0, - "reflex_shots": 0, - "wild_shots": 1, - "accidents": 0, - "total_ammo_used": 3, - "shot_at": 2, - "lucky_shots": 0, - "luck": 0, - "detector": 0, - "silencer": 0, - "sunglasses": 0, - "clothes": 0, - "grease": 0, - "brush": 0, - "mirror": 0, - "sand": 0, - "water": 0, - "sabotage": 0, - "life_insurance": 0, - "liability": 0, - "decoy": 0, - "bread": 0, - "duck_detector": 0, - "mechanical": 0 - }, - "madafaka": { - "xp": 42, - "caught": 3, - "befriended": 0, - "missed": 0, - "ammo": 3, - "max_ammo": 6, - "chargers": 2, - "max_chargers": 2, - "accuracy": 65, - "reliability": 70, - "weapon": "pistol", - "gun_confiscated": false, - "explosive_ammo": false, - "settings": { - "notices": true, - "private_messages": false - }, - "golden_ducks": 0, - "karma": 6, - "deflection": 0, - "defense": 0, - "jammed": false, - "jammed_count": 0, - "deaths": 0, - "neutralized": 0, - "deflected": 0, - "best_time": 2.8583714962005615, - "total_reflex_time": 12.643521547317505, - "reflex_shots": 3, - "wild_shots": 0, - "accidents": 0, - "total_ammo_used": 3, - "shot_at": 4, - "lucky_shots": 0, - "luck": 0, - "detector": 0, - "silencer": 0, - "sunglasses": 0, - "clothes": 0, - "grease": 0, - "brush": 0, - "mirror": 0, - "sand": 0, - "water": 0, - "sabotage": 0, - "life_insurance": 0, - "liability": 0, - "decoy": 0, - "bread": 0, - "duck_detector": 0, - "mechanical": 0 - }, - "peorth": { - "xp": 4, - "caught": 1, - "befriended": 0, - "missed": 0, - "ammo": 6, - "max_ammo": 6, - "chargers": 2, - "max_chargers": 2, - "accuracy": 65, - "reliability": 70, - "weapon": "pistol", - "gun_confiscated": false, - "explosive_ammo": false, - "settings": { - "notices": true, - "private_messages": false - }, - "golden_ducks": 0, - "karma": 2, - "deflection": 0, - "defense": 0, - "jammed": false, - "jammed_count": 1, - "deaths": 0, - "neutralized": 0, - "deflected": 0, - "best_time": 18.33902668952942, - "total_reflex_time": 18.33902668952942, - "reflex_shots": 1, - "wild_shots": 0, - "accidents": 0, - "total_ammo_used": 1, - "shot_at": 2, - "lucky_shots": 0, - "luck": 0, - "detector": 0, - "silencer": 0, - "sunglasses": 0, - "clothes": 0, - "grease": 0, - "brush": 0, - "mirror": 0, - "sand": 0, - "water": 0, - "sabotage": 0, - "life_insurance": 0, - "liability": 0, - "decoy": 0, - "bread": 0, - "duck_detector": 0, - "mechanical": 0 - }, - "dgw": { - "xp": 30, - "caught": 2, - "befriended": 0, - "missed": 0, - "ammo": 4, - "max_ammo": 6, - "chargers": 2, - "max_chargers": 2, - "accuracy": 65, - "reliability": 70, - "weapon": "pistol", - "gun_confiscated": false, - "explosive_ammo": false, - "settings": { - "notices": true, - "private_messages": false - }, - "golden_ducks": 0, - "karma": 4, - "deflection": 0, - "defense": 0, - "jammed": false, - "jammed_count": 0, - "deaths": 0, - "neutralized": 0, - "deflected": 0, - "best_time": 9.253741025924683, - "total_reflex_time": 20.60851550102234, - "reflex_shots": 2, - "wild_shots": 0, - "accidents": 0, - "total_ammo_used": 2, - "shot_at": 2, - "lucky_shots": 0, - "luck": 0, - "detector": 0, - "silencer": 0, - "sunglasses": 0, - "clothes": 0, - "grease": 0, - "brush": 0, - "mirror": 0, - "sand": 0, - "water": 0, - "sabotage": 0, - "life_insurance": 1, - "liability": 0, - "decoy": 0, - "bread": 0, - "duck_detector": 0, - "mechanical": 0 - }, - "admiral_hubris": { - "xp": 0, - "caught": 1, - "befriended": 0, - "missed": 0, - "ammo": 4, - "max_ammo": 6, - "chargers": 2, - "max_chargers": 2, - "accuracy": 45, - "reliability": 70, - "weapon": "pistol", - "gun_confiscated": true, - "explosive_ammo": false, - "settings": { - "notices": true, - "private_messages": false - }, - "golden_ducks": 0, - "karma": -17, - "deflection": 0, - "defense": 0, - "jammed": false, - "jammed_count": 0, - "deaths": 0, - "neutralized": 0, - "deflected": 0, - "best_time": 6.574016809463501, - "total_reflex_time": 6.574016809463501, - "reflex_shots": 1, - "wild_shots": 3, - "accidents": 1, - "total_ammo_used": 4, - "shot_at": 1, - "lucky_shots": 0, - "luck": 0, - "detector": 0, - "silencer": 0, - "sunglasses": 0, - "clothes": 0, - "grease": 0, - "brush": 0, - "mirror": 0, - "sand": 0, - "water": 0, - "sabotage": 0, - "life_insurance": 0, - "liability": 0, - "decoy": 0, - "bread": 0, - "duck_detector": 0, - "mechanical": 0, - "inventory": {} - }, - "xysha": { - "xp": 0, - "caught": 0, - "befriended": 0, - "missed": 0, - "ammo": 5, - "max_ammo": 6, - "chargers": 2, - "max_chargers": 2, - "accuracy": 65, - "reliability": 70, - "weapon": "pistol", - "gun_confiscated": true, - "explosive_ammo": false, - "settings": { - "notices": true, - "private_messages": false - }, - "golden_ducks": 0, - "karma": -14, - "deflection": 0, - "defense": 0, - "jammed": false, - "jammed_count": 1, - "deaths": 0, - "neutralized": 0, - "deflected": 0, - "best_time": 999.9, - "total_reflex_time": 0.0, - "reflex_shots": 0, - "wild_shots": 1, - "accidents": 1, - "total_ammo_used": 1, - "shot_at": 1, - "lucky_shots": 0, - "luck": 0, - "detector": 0, - "silencer": 0, - "sunglasses": 0, - "clothes": 0, - "grease": 0, - "brush": 0, - "mirror": 0, - "sand": 0, - "water": 0, - "sabotage": 0, - "life_insurance": 0, - "liability": 0, - "decoy": 0, - "bread": 0, - "duck_detector": 0, - "mechanical": 0, - "inventory": {} - }, - "boliver": { - "xp": 125, - "caught": 5, - "befriended": 4, - "missed": 2, - "ammo": 5, - "max_ammo": 6, - "chargers": 1, - "max_chargers": 2, - "accuracy": 45, - "reliability": 70, - "weapon": "pistol", - "gun_confiscated": true, - "explosive_ammo": false, - "settings": { - "notices": true, - "private_messages": false - }, - "golden_ducks": 1, - "karma": -3, - "deflection": 0, - "defense": 0, - "jammed": false, - "jammed_count": 7, - "deaths": 0, - "neutralized": 0, - "deflected": 0, - "best_time": 2.484381914138794, - "total_reflex_time": 19.068661212921143, - "reflex_shots": 5, - "wild_shots": 2, - "accidents": 1, - "total_ammo_used": 9, - "shot_at": 7, - "lucky_shots": 1, - "luck": 1, - "detector": 0, - "silencer": 0, - "sunglasses": 0, - "clothes": 0, - "grease": 0, - "brush": 0, - "mirror": 0, - "sand": 0, - "water": 1, - "sabotage": 0, - "life_insurance": 0, - "liability": 0, - "decoy": 0, - "bread": 0, - "duck_detector": 0, - "mechanical": 0, - "inventory": {} - }, - "kaitphone": { - "xp": 13, - "caught": 1, - "befriended": 0, - "missed": 1, - "ammo": 4, - "max_ammo": 6, - "chargers": 2, - "max_chargers": 2, - "accuracy": 65, - "reliability": 70, - "weapon": "pistol", - "gun_confiscated": false, - "explosive_ammo": false, - "settings": { - "notices": true, - "private_messages": false - }, - "golden_ducks": 0, - "karma": 1, - "deflection": 0, - "defense": 0, - "jammed": false, - "jammed_count": 3, - "deaths": 0, - "neutralized": 0, - "deflected": 0, - "best_time": 28.610472440719604, - "total_reflex_time": 28.610472440719604, - "reflex_shots": 1, - "wild_shots": 0, - "accidents": 0, - "total_ammo_used": 2, - "shot_at": 2, - "lucky_shots": 0, - "luck": 0, - "detector": 0, - "silencer": 0, - "sunglasses": 0, - "clothes": 0, - "grease": 0, - "brush": 0, - "mirror": 0, - "sand": 0, - "water": 0, - "sabotage": 0, - "life_insurance": 0, - "liability": 0, - "decoy": 0, - "bread": 0, - "duck_detector": 0, - "mechanical": 0 - }, - "milambar": { - "xp": 12, - "caught": 2, - "befriended": 3, - "missed": 0, - "ammo": 3, - "max_ammo": 6, - "chargers": 2, - "max_chargers": 2, - "accuracy": 65, - "reliability": 70, - "weapon": "pistol", - "gun_confiscated": true, - "explosive_ammo": false, - "settings": { - "notices": true, - "private_messages": false - }, - "golden_ducks": 0, - "karma": 7, - "deflection": 0, - "defense": 0, - "jammed": false, - "jammed_count": 1, - "deaths": 0, - "neutralized": 0, - "deflected": 0, - "best_time": 2.784888982772827, - "total_reflex_time": 8.606451749801636, - "reflex_shots": 2, - "wild_shots": 1, - "accidents": 0, - "total_ammo_used": 3, - "shot_at": 2, - "lucky_shots": 0, - "luck": 1, - "detector": 0, - "silencer": 0, - "sunglasses": 0, - "clothes": 0, - "grease": 0, - "brush": 0, - "mirror": 0, - "sand": 0, - "water": 0, - "sabotage": 0, - "life_insurance": 0, - "liability": 0, - "decoy": 0, - "bread": 0, - "duck_detector": 0, - "mechanical": 0, - "inventory": {} - }, - "wobotkoala": { - "xp": 0, - "caught": 0, - "befriended": 0, - "missed": 0, - "ammo": 6, - "max_ammo": 6, - "chargers": 2, - "max_chargers": 2, - "accuracy": 65, - "reliability": 70, - "weapon": "pistol", - "gun_confiscated": false, - "explosive_ammo": false, - "settings": { - "notices": true, - "private_messages": false - }, - "golden_ducks": 0, - "karma": 0, - "deflection": 0, - "defense": 0, - "jammed": false, - "jammed_count": 0, - "deaths": 0, - "neutralized": 0, - "deflected": 0, - "best_time": 999.9, - "total_reflex_time": 0.0, - "reflex_shots": 0, - "wild_shots": 0, - "accidents": 0, - "total_ammo_used": 0, - "shot_at": 0, - "lucky_shots": 0, - "luck": 0, - "detector": 0, - "silencer": 0, - "sunglasses": 0, - "clothes": 0, - "grease": 0, - "brush": 0, - "mirror": 0, - "sand": 0, - "water": 0, - "sabotage": 0, - "life_insurance": 0, - "liability": 0, - "decoy": 0, - "bread": 0, - "duck_detector": 0, - "mechanical": 0 - }, - "general_kornwallace": { - "xp": 0, - "caught": 0, - "befriended": 0, - "missed": 0, - "ammo": 6, - "max_ammo": 6, - "chargers": 2, - "max_chargers": 2, - "accuracy": 65, - "reliability": 70, - "weapon": "pistol", - "gun_confiscated": false, - "explosive_ammo": false, - "settings": { - "notices": true, - "private_messages": false - }, - "golden_ducks": 0, - "karma": 0, - "deflection": 0, - "defense": 0, - "jammed": false, - "jammed_count": 1, - "deaths": 0, - "neutralized": 0, - "deflected": 0, - "best_time": 999.9, - "total_reflex_time": 0.0, - "reflex_shots": 0, - "wild_shots": 0, - "accidents": 0, - "total_ammo_used": 0, - "shot_at": 0, - "lucky_shots": 0, - "luck": 0, - "detector": 0, - "silencer": 0, - "sunglasses": 0, - "clothes": 0, - "grease": 0, - "brush": 0, - "mirror": 0, - "sand": 0, - "water": 0, - "sabotage": 0, - "life_insurance": 0, - "liability": 0, - "decoy": 0, - "bread": 0, - "duck_detector": 0, - "mechanical": 0 - }, - "helderheid": { - "xp": 0, - "caught": 0, - "befriended": 0, - "missed": 0, - "ammo": 6, - "max_ammo": 6, - "chargers": 2, - "max_chargers": 2, - "accuracy": 65, - "reliability": 70, - "weapon": "pistol", - "gun_confiscated": false, - "explosive_ammo": false, - "settings": { - "notices": true, - "private_messages": false - }, - "golden_ducks": 0, - "karma": 0, - "deflection": 0, - "defense": 0, - "jammed": false, - "jammed_count": 0, - "deaths": 0, - "neutralized": 0, - "deflected": 0, - "best_time": 999.9, - "total_reflex_time": 0.0, - "reflex_shots": 0, - "wild_shots": 0, - "accidents": 0, - "total_ammo_used": 0, - "shot_at": 0, - "lucky_shots": 0, - "luck": 0, - "detector": 0, - "silencer": 0, - "sunglasses": 0, - "clothes": 0, - "grease": 0, - "brush": 0, - "mirror": 0, - "sand": 0, - "water": 0, - "sabotage": 0, - "life_insurance": 0, - "liability": 0, - "decoy": 0, - "bread": 0, - "duck_detector": 0, - "mechanical": 0 - }, - "coolkevin": { - "xp": 12, - "caught": 1, - "befriended": 0, - "missed": 0, - "ammo": 5, - "max_ammo": 6, - "chargers": 2, - "max_chargers": 2, - "accuracy": 65, - "reliability": 70, - "weapon": "pistol", - "gun_confiscated": false, - "explosive_ammo": false, - "settings": { - "notices": true, - "private_messages": false - }, - "golden_ducks": 0, - "karma": 2, - "deflection": 0, - "defense": 0, - "jammed": false, - "jammed_count": 0, - "deaths": 0, - "neutralized": 0, - "deflected": 0, - "best_time": 4.75800085067749, - "total_reflex_time": 4.75800085067749, - "reflex_shots": 1, - "wild_shots": 0, - "accidents": 0, - "total_ammo_used": 1, - "shot_at": 2, - "lucky_shots": 0, - "luck": 0, - "detector": 0, - "silencer": 0, - "sunglasses": 0, - "clothes": 0, - "grease": 0, - "brush": 0, - "mirror": 0, - "sand": 0, - "water": 0, - "sabotage": 0, - "life_insurance": 0, - "liability": 0, - "decoy": 0, - "bread": 0, - "duck_detector": 0, - "mechanical": 0 - }, - "magicalpig": { - "xp": 23, - "caught": 2, - "befriended": 1, - "missed": 1, - "ammo": 3, - "max_ammo": 6, - "chargers": 2, - "max_chargers": 2, - "accuracy": 85, - "reliability": 90, - "weapon": "pistol", - "gun_confiscated": false, - "explosive_ammo": false, - "settings": { - "notices": true, - "private_messages": false - }, - "golden_ducks": 0, - "karma": -11, - "deflection": 0, - "defense": 0, - "jammed": false, - "jammed_count": 0, - "deaths": 0, - "neutralized": 0, - "deflected": 0, - "best_time": 9.407448291778564, - "total_reflex_time": 21.80314612388611, - "reflex_shots": 2, - "wild_shots": 2, - "accidents": 1, - "total_ammo_used": 5, - "shot_at": 3, - "lucky_shots": 0, - "luck": 0, - "detector": 0, - "silencer": 0, - "sunglasses": 0, - "clothes": 0, - "grease": 0, - "brush": 0, - "mirror": 0, - "sand": 0, - "water": 0, - "sabotage": 0, - "life_insurance": 0, - "liability": 0, - "decoy": 0, - "bread": 0, - "duck_detector": 0, - "mechanical": 0, - "inventory": {} - }, - "tabb": { - "xp": -5, - "caught": 0, - "befriended": 0, - "missed": 0, - "ammo": 5, - "max_ammo": 6, - "chargers": 2, - "max_chargers": 2, - "accuracy": 85, - "reliability": 90, - "weapon": "pistol", - "gun_confiscated": true, - "explosive_ammo": false, - "settings": { - "notices": true, - "private_messages": false - }, - "inventory": {}, - "golden_ducks": 0, - "karma": -3, - "deflection": 0, - "defense": 0, - "jammed": false, - "jammed_count": 0, - "deaths": 0, - "neutralized": 0, - "deflected": 0, - "best_time": 999.9, - "total_reflex_time": 0.0, - "reflex_shots": 0, - "wild_shots": 1, - "accidents": 0, - "total_ammo_used": 1, - "shot_at": 0, - "lucky_shots": 0, - "luck": 0, - "detector": 0, - "silencer": 0, - "sunglasses": 0, - "clothes": 0, - "grease": 0, - "brush": 0, - "mirror": 0, - "sand": 0, - "water": 0, - "sabotage": 0, - "life_insurance": 0, - "liability": 0, - "decoy": 0, - "bread": 0, - "duck_detector": 0, - "mechanical": 0 - }, - "testuser!test@test.com": { - "xp": 100, - "caught": 0, - "befriended": 0 - }, - "testuser": { - "xp": 0, - "caught": 0, - "befriended": 0, - "missed": 0, - "ammo": 6, - "max_ammo": 6, - "chargers": 2, - "max_chargers": 2, - "accuracy": 65, - "reliability": 70, - "weapon": "pistol", - "gun_confiscated": false, - "explosive_ammo": false, - "settings": { - "output_mode": "PUBLIC", - "notices": true, - "private_messages": false - }, - "inventory": {}, - "golden_ducks": 0, - "karma": 0, - "deflection": 0, - "defense": 0, - "jammed": false, - "jammed_count": 0, - "deaths": 0, - "neutralized": 0, - "deflected": 0, - "best_time": 999.9, - "total_reflex_time": 0.0, - "reflex_shots": 0, - "wild_shots": 0, - "accidents": 0, - "total_ammo_used": 0, - "shot_at": 0, - "lucky_shots": 0, - "luck": 0, - "detector": 0, - "silencer": 0, - "sunglasses": 0, - "clothes": 0, - "grease": 0, - "brush": 0, - "mirror": 0, - "sand": 0, - "water": 0, - "sabotage": 0, - "life_insurance": 0, - "liability": 0, - "decoy": 0, - "bread": 0, - "duck_detector": 0, - "mechanical": 0 - }, - "py-ctcp": { - "xp": 0, - "caught": 0, - "befriended": 0, - "missed": 0, - "ammo": 6, - "max_ammo": 6, - "chargers": 2, - "max_chargers": 2, - "accuracy": 65, - "reliability": 70, - "weapon": "pistol", - "gun_confiscated": false, - "explosive_ammo": false, - "settings": { - "output_mode": "PUBLIC", - "notices": false, - "private_messages": false - }, - "inventory": {}, - "golden_ducks": 0, - "karma": 0, - "deflection": 0, - "defense": 0, - "jammed": false, - "jammed_count": 0, - "deaths": 0, - "neutralized": 0, - "deflected": 0, - "best_time": 999.9, - "total_reflex_time": 0.0, - "reflex_shots": 0, - "wild_shots": 0, - "accidents": 0, - "total_ammo_used": 0, - "shot_at": 0, - "lucky_shots": 0, - "luck": 0, - "detector": 0, - "silencer": 0, - "sunglasses": 0, - "clothes": 0, - "grease": 0, - "brush": 0, - "mirror": 0, - "sand": 0, - "water": 0, - "sabotage": 0, - "life_insurance": 0, - "liability": 0, - "decoy": 0, - "bread": 0, - "duck_detector": 0, - "mechanical": 0, - "shots": 6, - "max_shots": 6, - "reload_time": 5.0, - "ducks_shot": 0, - "ducks_befriended": 0, - "accuracy_bonus": 0, - "xp_bonus": 0, - "charm_bonus": 0, - "exp": 0, - "money": 100, - "last_hunt": 0, - "last_reload": 0, - "level": 1, - "ignored_users": [], - "confiscated_count": 0 + "accuracy": 64, + "gun_confiscated": false } }, - "last_save": "1758314578.1957033" + "last_save": "1758592642.337953" } \ No newline at end of file diff --git a/duckhunt.log b/duckhunt.log index 8037e9f..06b41fe 100644 --- a/duckhunt.log +++ b/duckhunt.log @@ -196,3 +196,149 @@ 2025-09-19 21:42:58,198 [INFO ] DuckHuntBot - run:1154: Database saved 2025-09-19 21:42:58,261 [ERROR ] DuckHuntBot - run:1166: Error closing connection: [SSL: APPLICATION_DATA_AFTER_CLOSE_NOTIFY] application data after close notify (_ssl.c:2685) 2025-09-19 21:42:58,262 [INFO ] DuckHuntBot - run:1168: Bot shutdown complete +2025-09-22 20:48:31,249 [INFO ] DuckHuntBot - setup_logger:61: Enhanced logging system initialized with file rotation +2025-09-22 20:48:31,249 [INFO ] SASL - setup_logger:61: Enhanced logging system initialized with file rotation +2025-09-22 20:48:31,250 [INFO ] DuckHuntBot - main:22: 🦆 Starting DuckHunt Bot... +2025-09-22 20:48:31,252 [INFO ] DuckHuntBot.DB - load_database:38: Loaded 22 players from duckhunt.json +2025-09-22 20:48:31,588 [INFO ] DuckHuntBot - connect:86: Connected to irc.rizon.net:6697 +2025-09-22 20:48:32,079 [INFO ] DuckHuntBot - handle_message:190: Successfully registered with IRC server +2025-09-22 20:48:33,627 [INFO ] DuckHuntBot - signal_handler:174: Received signal 2, shutting down... +2025-09-22 20:48:33,699 [INFO ] DuckHuntBot - run:1063: Main loop cancelled +2025-09-22 20:48:33,699 [INFO ] DuckHuntBot - run:1072: Shutting down bot... +2025-09-22 20:48:33,699 [INFO ] DuckHuntBot - message_loop:1118: Message loop cancelled +2025-09-22 20:48:33,700 [INFO ] DuckHuntBot - message_loop:1124: Message loop ended +2025-09-22 20:48:33,700 [INFO ] DuckHuntBot.Game - duck_timeout_checker:293: Duck timeout checker cancelled +2025-09-22 20:48:33,700 [INFO ] DuckHuntBot.Game - spawn_ducks:248: Duck spawning loop cancelled +2025-09-22 20:48:33,703 [INFO ] DuckHuntBot - run:1086: Database saved +2025-09-22 20:48:33,772 [ERROR ] DuckHuntBot - run:1097: Error closing connection: [SSL: APPLICATION_DATA_AFTER_CLOSE_NOTIFY] application data after close notify (_ssl.c:2685) +2025-09-22 20:48:33,772 [INFO ] DuckHuntBot - run:1099: Bot shutdown complete +2025-09-22 20:48:37,968 [INFO ] DuckHuntBot - setup_logger:61: Enhanced logging system initialized with file rotation +2025-09-22 20:48:37,969 [INFO ] SASL - setup_logger:61: Enhanced logging system initialized with file rotation +2025-09-22 20:48:37,969 [INFO ] DuckHuntBot - main:22: 🦆 Starting DuckHunt Bot... +2025-09-22 20:48:37,971 [INFO ] DuckHuntBot.DB - load_database:38: Loaded 22 players from duckhunt.json +2025-09-22 20:48:38,200 [INFO ] DuckHuntBot - connect:86: Connected to irc.rizon.net:6697 +2025-09-22 20:48:38,340 [INFO ] DuckHuntBot - handle_message:190: Successfully registered with IRC server +2025-09-22 20:50:02,412 [INFO ] DuckHuntBot - signal_handler:174: Received signal 2, shutting down... +2025-09-22 20:50:02,471 [INFO ] DuckHuntBot.Game - duck_timeout_checker:293: Duck timeout checker cancelled +2025-09-22 20:50:02,471 [INFO ] DuckHuntBot - message_loop:1118: Message loop cancelled +2025-09-22 20:50:02,471 [INFO ] DuckHuntBot - message_loop:1124: Message loop ended +2025-09-22 20:50:02,471 [INFO ] DuckHuntBot.Game - spawn_ducks:248: Duck spawning loop cancelled +2025-09-22 20:50:02,471 [INFO ] DuckHuntBot - run:1063: Main loop cancelled +2025-09-22 20:50:02,472 [INFO ] DuckHuntBot - run:1072: Shutting down bot... +2025-09-22 20:50:02,474 [INFO ] DuckHuntBot - run:1086: Database saved +2025-09-22 20:50:02,522 [ERROR ] DuckHuntBot - run:1097: Error closing connection: [SSL: APPLICATION_DATA_AFTER_CLOSE_NOTIFY] application data after close notify (_ssl.c:2685) +2025-09-22 20:50:02,522 [INFO ] DuckHuntBot - run:1099: Bot shutdown complete +2025-09-23 00:16:59,762 [INFO ] DuckHuntBot - setup_logger:61: Enhanced logging system initialized with file rotation +2025-09-23 00:16:59,763 [INFO ] SASL - setup_logger:61: Enhanced logging system initialized with file rotation +2025-09-23 00:16:59,763 [INFO ] DuckHuntBot - load_shop_config:81: Loaded shop configuration with 15 items +2025-09-23 00:22:10,719 [INFO ] DuckHuntBot - setup_logger:61: Enhanced logging system initialized with file rotation +2025-09-23 00:22:10,720 [INFO ] SASL - setup_logger:61: Enhanced logging system initialized with file rotation +2025-09-23 00:22:10,722 [INFO ] DuckHuntBot - load_shop_config:81: Loaded shop configuration with 15 items +2025-09-23 00:23:53,961 [INFO ] DuckHuntBot - setup_logger:61: Enhanced logging system initialized with file rotation +2025-09-23 00:23:53,963 [INFO ] SASL - setup_logger:61: Enhanced logging system initialized with file rotation +2025-09-23 00:23:53,964 [INFO ] DuckHuntBot - load_shop_config:81: Loaded shop configuration with 15 items +2025-09-23 01:45:21,315 [INFO ] DuckHuntBot - setup_logger:61: Enhanced logging system initialized with file rotation +2025-09-23 01:45:21,316 [INFO ] SASL - setup_logger:61: Enhanced logging system initialized with file rotation +2025-09-23 01:45:21,317 [INFO ] DuckHuntBot - load_shop_config:90: Loaded shop configuration with 15 items +2025-09-23 01:45:21,318 [INFO ] DuckHuntBot - load_messages_config:107: Loaded messages configuration +2025-09-23 01:45:21,319 [INFO ] DuckHuntBot - main:22: 🦆 Starting DuckHunt Bot... +2025-09-23 01:45:21,322 [INFO ] DuckHuntBot.DB - load_database:38: Loaded 22 players from duckhunt.json +2025-09-23 01:45:21,670 [INFO ] DuckHuntBot - connect:272: Connected to irc.rizon.net:6697 +2025-09-23 01:45:22,224 [INFO ] DuckHuntBot - handle_message:381: Successfully registered with IRC server +2025-09-23 01:48:04,868 [INFO ] DuckHuntBot - signal_handler:365: Received signal 1, shutting down... +2025-09-23 01:48:04,922 [INFO ] DuckHuntBot - run:1473: Main loop cancelled +2025-09-23 01:48:04,924 [INFO ] DuckHuntBot - run:1482: Shutting down bot... +2025-09-23 01:48:04,926 [INFO ] DuckHuntBot - message_loop:1530: Message loop cancelled +2025-09-23 01:48:04,926 [INFO ] DuckHuntBot - message_loop:1536: Message loop ended +2025-09-23 01:48:04,927 [INFO ] DuckHuntBot.Game - duck_timeout_checker:311: Duck timeout checker cancelled +2025-09-23 01:48:04,928 [INFO ] DuckHuntBot.Game - spawn_ducks:260: Duck spawning loop cancelled +2025-09-23 01:48:04,938 [INFO ] DuckHuntBot - run:1496: Database saved +2025-09-23 01:48:04,987 [ERROR ] DuckHuntBot - run:1509: Error closing connection: [SSL: APPLICATION_DATA_AFTER_CLOSE_NOTIFY] application data after close notify (_ssl.c:2685) +2025-09-23 01:48:04,988 [INFO ] DuckHuntBot - run:1511: Bot shutdown complete +2025-09-23 02:23:43,434 [INFO ] DuckHuntBot - setup_logger:61: Enhanced logging system initialized with file rotation +2025-09-23 02:23:43,436 [INFO ] DuckHuntBot.DB - load_database:28: Loaded 0 players from duckhunt_simple.json +2025-09-23 02:23:43,437 [INFO ] SASL - setup_logger:61: Enhanced logging system initialized with file rotation +2025-09-23 02:23:43,438 [INFO ] DuckHuntBot - main:29: 🦆 Starting Simplified DuckHunt Bot... +2025-09-23 02:23:43,773 [INFO ] DuckHuntBot - connect:86: Connected to irc.rizon.net:6697 +2025-09-23 02:23:43,950 [INFO ] DuckHuntBot - handle_message:112: Successfully registered with IRC server +2025-09-23 02:25:35,240 [INFO ] DuckHuntBot - signal_handler:71: Received signal 2, shutting down... +2025-09-23 02:26:37,051 [INFO ] DuckHuntBot - message_loop:283: Message loop ended +2025-09-23 02:26:37,051 [INFO ] DuckHuntBot.Game - duck_spawn_loop:51: Duck spawning loop cancelled +2025-09-23 02:26:37,051 [INFO ] DuckHuntBot.Game - duck_timeout_loop:81: Duck timeout loop cancelled +2025-09-23 02:26:37,052 [INFO ] DuckHuntBot.Game - start_game_loops:31: Game loops cancelled +2025-09-23 02:26:37,052 [INFO ] DuckHuntBot - run:316: Shutting down bot... +2025-09-23 02:26:37,053 [INFO ] DuckHuntBot - run:321: Database saved +2025-09-23 02:26:37,113 [ERROR ] DuckHuntBot - run:331: Error closing connection: [SSL: APPLICATION_DATA_AFTER_CLOSE_NOTIFY] application data after close notify (_ssl.c:2685) +2025-09-23 02:26:37,113 [INFO ] DuckHuntBot - run:335: Bot shutdown complete +2025-09-23 02:38:10,738 [INFO ] DuckHuntBot - setup_logger:61: Enhanced logging system initialized with file rotation +2025-09-23 02:38:10,739 [INFO ] DuckHuntBot.DB - load_database:28: Loaded 1 players from duckhunt.json +2025-09-23 02:38:10,739 [INFO ] SASL - setup_logger:61: Enhanced logging system initialized with file rotation +2025-09-23 02:38:10,739 [INFO ] DuckHuntBot - main:28: 🦆 Starting DuckHunt Bot... +2025-09-23 02:38:10,980 [INFO ] DuckHuntBot - connect:86: Connected to irc.rizon.net:6697 +2025-09-23 02:38:11,465 [INFO ] DuckHuntBot - handle_message:112: Successfully registered with IRC server +2025-09-23 02:39:58,322 [INFO ] DuckHuntBot - signal_handler:71: Received signal 2, shutting down... +2025-09-23 02:40:03,825 [INFO ] DuckHuntBot - signal_handler:71: Received signal 2, shutting down... +2025-09-23 02:40:04,024 [INFO ] DuckHuntBot - signal_handler:71: Received signal 2, shutting down... +2025-09-23 02:40:04,205 [INFO ] DuckHuntBot - signal_handler:71: Received signal 2, shutting down... +2025-09-23 02:40:04,381 [INFO ] DuckHuntBot - signal_handler:71: Received signal 2, shutting down... +2025-09-23 02:40:04,554 [INFO ] DuckHuntBot - signal_handler:71: Received signal 2, shutting down... +2025-09-23 02:40:04,743 [INFO ] DuckHuntBot - signal_handler:71: Received signal 2, shutting down... +2025-09-23 02:40:05,003 [INFO ] DuckHuntBot - signal_handler:71: Received signal 2, shutting down... +2025-09-23 02:40:05,500 [INFO ] DuckHuntBot - signal_handler:71: Received signal 2, shutting down... +2025-09-23 02:40:05,572 [INFO ] DuckHuntBot - signal_handler:71: Received signal 2, shutting down... +2025-09-23 02:40:05,596 [INFO ] DuckHuntBot - signal_handler:71: Received signal 2, shutting down... +2025-09-23 02:40:05,648 [INFO ] DuckHuntBot - signal_handler:71: Received signal 2, shutting down... +2025-09-23 02:40:05,653 [INFO ] DuckHuntBot - signal_handler:71: Received signal 2, shutting down... +2025-09-23 02:40:05,683 [INFO ] DuckHuntBot - signal_handler:71: Received signal 2, shutting down... +2025-09-23 02:40:05,739 [INFO ] DuckHuntBot - signal_handler:71: Received signal 2, shutting down... +2025-09-23 02:40:05,744 [INFO ] DuckHuntBot - signal_handler:71: Received signal 2, shutting down... +2025-09-23 02:40:05,807 [INFO ] DuckHuntBot - signal_handler:71: Received signal 2, shutting down... +2025-09-23 02:40:05,809 [INFO ] DuckHuntBot - signal_handler:71: Received signal 2, shutting down... +2025-09-23 02:40:05,883 [INFO ] DuckHuntBot - signal_handler:71: Received signal 2, shutting down... +2025-09-23 02:40:05,898 [INFO ] DuckHuntBot - signal_handler:71: Received signal 2, shutting down... +2025-09-23 02:40:05,948 [INFO ] DuckHuntBot - signal_handler:71: Received signal 2, shutting down... +2025-09-23 02:40:05,960 [INFO ] DuckHuntBot - signal_handler:71: Received signal 2, shutting down... +2025-09-23 02:40:06,029 [INFO ] DuckHuntBot - signal_handler:71: Received signal 2, shutting down... +2025-09-23 02:40:06,031 [INFO ] DuckHuntBot - signal_handler:71: Received signal 2, shutting down... +2025-09-23 02:40:06,046 [INFO ] DuckHuntBot - signal_handler:71: Received signal 2, shutting down... +2025-09-23 02:40:06,102 [INFO ] DuckHuntBot - signal_handler:71: Received signal 2, shutting down... +2025-09-23 02:40:21,162 [INFO ] DuckHuntBot - message_loop:350: Message loop ended +2025-09-23 02:40:21,162 [INFO ] DuckHuntBot.Game - duck_spawn_loop:51: Duck spawning loop cancelled +2025-09-23 02:40:21,163 [INFO ] DuckHuntBot.Game - duck_timeout_loop:81: Duck timeout loop cancelled +2025-09-23 02:40:21,163 [INFO ] DuckHuntBot.Game - start_game_loops:31: Game loops cancelled +2025-09-23 02:40:21,163 [INFO ] DuckHuntBot - run:383: Shutting down bot... +2025-09-23 02:40:21,165 [INFO ] DuckHuntBot - run:388: Database saved +2025-09-23 02:40:21,214 [ERROR ] DuckHuntBot - run:398: Error closing connection: [SSL: APPLICATION_DATA_AFTER_CLOSE_NOTIFY] application data after close notify (_ssl.c:2685) +2025-09-23 02:40:21,218 [INFO ] DuckHuntBot - run:402: Bot shutdown complete +2025-09-23 02:42:31,255 [INFO ] DuckHuntBot - setup_logger:61: Enhanced logging system initialized with file rotation +2025-09-23 02:42:31,255 [INFO ] DuckHuntBot.DB - load_database:28: Loaded 1 players from duckhunt.json +2025-09-23 02:42:31,256 [INFO ] SASL - setup_logger:61: Enhanced logging system initialized with file rotation +2025-09-23 02:42:31,256 [INFO ] DuckHuntBot - main:28: 🦆 Starting DuckHunt Bot... +2025-09-23 02:42:31,501 [INFO ] DuckHuntBot - connect:95: Connected to irc.rizon.net:6697 +2025-09-23 02:42:31,502 [INFO ] DuckHuntBot - run:377: 🦆 Bot is now running! Press Ctrl+C to stop. +2025-09-23 02:42:31,642 [INFO ] DuckHuntBot - handle_message:121: Successfully registered with IRC server +2025-09-23 02:42:47,509 [INFO ] DuckHuntBot.Game - spawn_duck:103: Duck spawned in #ct +2025-09-23 02:43:02,511 [INFO ] DuckHuntBot.Game - spawn_duck:103: Duck spawned in #ct +2025-09-23 02:43:14,515 [INFO ] DuckHuntBot.Game - spawn_duck:103: Duck spawned in #ct +2025-09-23 02:44:28,542 [INFO ] DuckHuntBot.Game - spawn_duck:103: Duck spawned in #ct +2025-09-23 02:45:34,560 [INFO ] DuckHuntBot.Game - spawn_duck:103: Duck spawned in #ct +2025-09-23 02:46:46,591 [INFO ] DuckHuntBot.Game - spawn_duck:103: Duck spawned in #ct +2025-09-23 02:47:57,623 [INFO ] DuckHuntBot.Game - spawn_duck:103: Duck spawned in #ct +2025-09-23 02:49:22,643 [INFO ] DuckHuntBot.Game - spawn_duck:103: Duck spawned in #ct +2025-09-23 02:51:01,687 [INFO ] DuckHuntBot.Game - spawn_duck:103: Duck spawned in #ct +2025-09-23 02:52:03,709 [INFO ] DuckHuntBot.Game - spawn_duck:103: Duck spawned in #ct +2025-09-23 02:53:32,732 [INFO ] DuckHuntBot.Game - spawn_duck:103: Duck spawned in #ct +2025-09-23 02:54:45,747 [INFO ] DuckHuntBot.Game - spawn_duck:103: Duck spawned in #ct +2025-09-23 02:56:12,772 [INFO ] DuckHuntBot.Game - spawn_duck:103: Duck spawned in #ct +2025-09-23 02:57:17,679 [INFO ] DuckHuntBot - signal_handler:73: 🛑 Received SIGINT (Ctrl+C), initiating graceful shutdown... +2025-09-23 02:57:21,807 [INFO ] DuckHuntBot - run:386: 🛑 Shutdown signal received, cleaning up... +2025-09-23 02:57:21,811 [INFO ] DuckHuntBot - _graceful_shutdown:433: 📤 Sending QUIT message to IRC... +2025-09-23 02:57:21,880 [INFO ] DuckHuntBot - message_loop:359: Message loop ended +2025-09-23 02:57:22,314 [INFO ] DuckHuntBot - _graceful_shutdown:441: 💾 Saving database... +2025-09-23 02:57:22,333 [INFO ] DuckHuntBot.Game - duck_spawn_loop:51: Duck spawning loop cancelled +2025-09-23 02:57:22,334 [INFO ] DuckHuntBot.Game - duck_timeout_loop:81: Duck timeout loop cancelled +2025-09-23 02:57:22,336 [INFO ] DuckHuntBot.Game - start_game_loops:31: Game loops cancelled +2025-09-23 02:57:22,337 [INFO ] DuckHuntBot - run:405: 🔄 Final cleanup... +2025-09-23 02:57:22,339 [INFO ] DuckHuntBot - run:419: 💾 Database saved +2025-09-23 02:57:22,340 [INFO ] DuckHuntBot - _close_connection:454: 🔌 IRC connection closed +2025-09-23 02:57:22,341 [INFO ] DuckHuntBot - run:426: ✅ Bot shutdown complete diff --git a/duckhunt.py b/duckhunt.py index 5340e9b..28eaf81 100644 --- a/duckhunt.py +++ b/duckhunt.py @@ -1,5 +1,6 @@ """ -DuckHunt IRC Bot - Main Entry Point +DuckHunt IRC Bot - Simplified Entry Point +Commands: !bang, !reload, !shop, !rearm, !disarm """ import asyncio @@ -15,23 +16,31 @@ from src.duckhuntbot import DuckHuntBot def main(): """Main entry point for DuckHunt Bot""" try: - with open('config.json') as f: + config_file = 'config.json' + if not os.path.exists(config_file): + print("❌ config.json not found!") + sys.exit(1) + + with open(config_file) as f: config = json.load(f) bot = DuckHuntBot(config) bot.logger.info("🦆 Starting DuckHunt Bot...") + # Run the bot asyncio.run(bot.run()) except KeyboardInterrupt: - print("\n🛑 Bot stopped by user") + print("\n🛑 Shutdown interrupted by user") except FileNotFoundError: print("❌ config.json not found!") sys.exit(1) except Exception as e: print(f"❌ Error: {e}") sys.exit(1) + else: + print("👋 DuckHunt Bot stopped gracefully") if __name__ == '__main__': - main() + main() \ No newline at end of file diff --git a/messages.json b/messages.json new file mode 100644 index 0000000..fdf5ae6 --- /dev/null +++ b/messages.json @@ -0,0 +1,54 @@ +{{ + "duck_spawn": "・゜゜・。。・゜゜\\_o< \u000303QUACK!\u000F A \u000308duck\u000F has appeared! Type \u000302!bang\u000F to shoot it!", + "duck_flies_away": "The \u000308duck\u000F flies away. ·°'`'°-.,¸¸.·°'`", + "bang_hit": "{nick} > \u000304*BANG*\u000F You shot the \u000308duck\u000F! [\u000303+{xp_gained} xp\u000F] [Total ducks: \u000302{ducks_shot}\u000F]", + "bang_miss": "{nick} > \u000304*BANG*\u000F You missed the \u000308duck\u000F!", + "bang_no_duck": "{nick} > \u000304*BANG*\u000F What did you shoot at? There is \u000304no duck\u000F in the area... [\u000304GUN CONFISCATED\u000F]", + "bang_no_ammo": "{nick} > \u000307*click*\u000F You're out of ammo! Use \u000302!reload\u000F", + "bang_not_armed": "{nick} > You are \u000304not armed\u000F.", + "reload_success": "{nick} > \u000307*click*\u000F Reloaded! [Ammo: \u000303{ammo}\u000F/\u000303{max_ammo}\u000F] [Chargers: \u000302{chargers}\u000F]", + "reload_already_loaded": "{nick} > Your gun is \u000303already loaded\u000F!", + "reload_no_chargers": "{nick} > You're out of \u000304chargers\u000F!", + "reload_not_armed": "{nick} > You are \u000304not armed\u000F.", + "shop_display": "DuckHunt Shop: {items} | You have \u000303{xp} XP\u000F", + "shop_item_format": "(\u000302{id}\u000F) \u000310{name}\u000F - \u000303{price} XP\u000F", + "help_header": "\u000302DuckHunt Commands:\u000F", + "help_user_commands": "\u000302!bang\u000F - Shoot at ducks | \u000302!reload\u000F - Reload your gun | \u000302!shop\u000F - View the shop", + "help_help_command": "\u000302!duckhelp\u000F - Show this help", + "help_admin_commands": "\u000304Admin:\u000F \u000302!rearm \u000F | \u000302!disarm \u000F | \u000302!ignore \u000F | \u000302!unignore \u000F | \u000302!ducklaunch\u000F", + "admin_rearm_player": "[\u000304ADMIN\u000F] \u000310{target}\u000F has been rearmed by \u000302{admin}\u000F", + "admin_rearm_all": "[\u000304ADMIN\u000F] All players have been rearmed by \u000302{admin}\u000F", + "admin_disarm": "[\u000304ADMIN\u000F] \u000310{target}\u000F has been disarmed by \u000302{admin}\u000F", + "admin_ignore": "[\u000304ADMIN\u000F] \u000310{target}\u000F is now ignored by \u000302{admin}\u000F", + "admin_unignore": "[\u000304ADMIN\u000F] \u000310{target}\u000F is no longer ignored by \u000302{admin}\u000F", + "admin_ducklaunch": "[\u000304ADMIN\u000F] A \u000308duck\u000F has been launched by \u000302{admin}\u000F", + "admin_ducklaunch_not_enabled": "[\u000304ADMIN\u000F] This channel is \u000304not enabled\u000F for duckhunt", + "usage_rearm": "Usage: \u000302!rearm \u000F", + "usage_disarm": "Usage: \u000302!disarm \u000F", + "usage_ignore": "Usage: \u000302!ignore \u000F", + "usage_unignore": "Usage: \u000302!unignore \u000F", + + "colours": { + "white": "\u00030", + "black": "\u00031", + "blue": "\u00032", + "green": "\u00033", + "red": "\u00034", + "brown": "\u00035", + "purple": "\u00036", + "orange": "\u00037", + "yellow": "\u00038", + "light_green": "\u00039", + "cyan": "\u000310", + "light_cyan": "\u000311", + "light_blue": "\u000312", + "pink": "\u000313", + "grey": "\u000314", + "light_grey": "\u000315", + "bold": "\u0002", + "underline": "\u001f", + "italic": "\u001d", + "strikethrough": "\u001e", + "reset": "\u000f" + } +} \ No newline at end of file diff --git a/src/__pycache__/db.cpython-312.pyc b/src/__pycache__/db.cpython-312.pyc index 68ae343..c76a996 100644 Binary files a/src/__pycache__/db.cpython-312.pyc and b/src/__pycache__/db.cpython-312.pyc differ diff --git a/src/__pycache__/duckhuntbot.cpython-312.pyc b/src/__pycache__/duckhuntbot.cpython-312.pyc index 0ed963a..b591786 100644 Binary files a/src/__pycache__/duckhuntbot.cpython-312.pyc and b/src/__pycache__/duckhuntbot.cpython-312.pyc differ diff --git a/src/__pycache__/game.cpython-312.pyc b/src/__pycache__/game.cpython-312.pyc index 0ab22ef..863a05f 100644 Binary files a/src/__pycache__/game.cpython-312.pyc and b/src/__pycache__/game.cpython-312.pyc differ diff --git a/src/__pycache__/logging_utils.cpython-312.pyc b/src/__pycache__/logging_utils.cpython-312.pyc index 1ea0c6c..e1d49cb 100644 Binary files a/src/__pycache__/logging_utils.cpython-312.pyc and b/src/__pycache__/logging_utils.cpython-312.pyc differ diff --git a/src/__pycache__/sasl.cpython-312.pyc b/src/__pycache__/sasl.cpython-312.pyc index 9736ca2..8961ac1 100644 Binary files a/src/__pycache__/sasl.cpython-312.pyc and b/src/__pycache__/sasl.cpython-312.pyc differ diff --git a/src/__pycache__/utils.cpython-312.pyc b/src/__pycache__/utils.cpython-312.pyc index 372fe94..2d841cf 100644 Binary files a/src/__pycache__/utils.cpython-312.pyc and b/src/__pycache__/utils.cpython-312.pyc differ diff --git a/src/db.py b/src/db.py index a8d6379..dbf2b72 100644 --- a/src/db.py +++ b/src/db.py @@ -1,181 +1,81 @@ """ -Database functionality for DuckHunt Bot +Simplified Database management for DuckHunt Bot +Only essential player fields """ import json -import os -import time -import asyncio import logging +import time +import os class DuckDB: - """Database management for DuckHunt Bot""" + """Simplified database management""" def __init__(self, db_file="duckhunt.json"): self.db_file = db_file self.players = {} - self._save_pending = False self.logger = logging.getLogger('DuckHuntBot.DB') - - def get_config(self, path, default=None): - """Helper method to get config values (needs to be set by bot)""" - if hasattr(self, '_config_getter'): - return self._config_getter(path, default) - return default - - def set_config_getter(self, config_getter): - """Set the config getter function from the main bot""" - self._config_getter = config_getter - + self.load_database() + def load_database(self): """Load player data from JSON file""" - if os.path.exists(self.db_file): - try: + try: + if os.path.exists(self.db_file): with open(self.db_file, 'r') as f: data = json.load(f) self.players = data.get('players', {}) - self.logger.info(f"Loaded {len(self.players)} players from {self.db_file}") - except (json.JSONDecodeError, IOError) as e: - self.logger.error(f"Error loading database: {e}") + self.logger.info(f"Loaded {len(self.players)} players from {self.db_file}") + else: self.players = {} - else: + self.logger.info(f"No existing database found, starting fresh") + except Exception as e: + self.logger.error(f"Error loading database: {e}") self.players = {} - self.logger.info(f"Created new database: {self.db_file}") - + def save_database(self): - """Save all player data to JSON file with error handling""" + """Save all player data to JSON file""" try: - temp_file = f"{self.db_file}.tmp" data = { 'players': self.players, 'last_save': str(time.time()) } - with open(temp_file, 'w') as f: + + # Create backup + if os.path.exists(self.db_file): + backup_file = f"{self.db_file}.backup" + try: + with open(self.db_file, 'r') as src, open(backup_file, 'w') as dst: + dst.write(src.read()) + except Exception as e: + self.logger.warning(f"Failed to create backup: {e}") + + # Save main file + with open(self.db_file, 'w') as f: json.dump(data, f, indent=2) - - os.replace(temp_file, self.db_file) - - except IOError as e: - self.logger.error(f"Error saving database: {e}") + except Exception as e: - self.logger.error(f"Unexpected database save error: {e}") - - def _get_starting_accuracy(self): - """Get starting accuracy with optional randomization""" - base_accuracy = self.get_config('new_players.starting_accuracy', 65) or 65 - if self.get_config('new_players.random_stats.enabled', False): - import random - variance = self.get_config('new_players.random_stats.accuracy_variance', 10) or 10 - return max(10, min(95, base_accuracy + random.randint(-variance, variance))) - return base_accuracy - - def _get_starting_reliability(self): - """Get starting reliability with optional randomization""" - base_reliability = self.get_config('new_players.starting_reliability', 70) or 70 - if self.get_config('new_players.random_stats.enabled', False): - import random - variance = self.get_config('new_players.random_stats.reliability_variance', 10) or 10 - return max(10, min(95, base_reliability + random.randint(-variance, variance))) - return base_reliability - - def get_player(self, user): + self.logger.error(f"Error saving database: {e}") + + def get_player(self, nick): """Get player data, creating if doesn't exist""" - if '!' not in user: - nick = user.lower() - else: - nick = user.split('!')[0].lower() - - if nick in self.players: - player = self.players[nick] - self._ensure_player_fields(player) - return player - else: - return self.create_player(nick) + nick_lower = nick.lower() + + if nick_lower not in self.players: + self.players[nick_lower] = self.create_player(nick) + + return self.players[nick_lower] def create_player(self, nick): - """Create a new player with default stats""" - player = { - 'shots': 6, - 'max_shots': 6, + """Create a new player with basic stats""" + return { + 'nick': nick, + 'xp': 0, + 'ducks_shot': 0, + 'ammo': 6, + 'max_ammo': 6, 'chargers': 2, 'max_chargers': 2, - 'reload_time': 5.0, - 'ducks_shot': 0, - 'ducks_befriended': 0, - 'accuracy_bonus': 0, - 'xp_bonus': 0, - 'charm_bonus': 0, - 'exp': 0, - 'money': 100, - 'last_hunt': 0, - 'last_reload': 0, - 'level': 1, - 'inventory': {}, - 'ignored_users': [], - 'jammed': False, - 'jammed_count': 0, - 'total_ammo_used': 0, - 'shot_at': 0, - 'wild_shots': 0, - 'accuracy': self._get_starting_accuracy(), - 'reliability': self._get_starting_reliability(), - 'gun_confiscated': False, - 'confiscated_count': 0 - } - - self.players[nick] = player - self.logger.info(f"Created new player: {nick}") - return player - - def _ensure_player_fields(self, player): - """Ensure player has all required fields for backward compatibility""" - required_fields = { - 'shots': player.get('ammo', 6), - 'max_shots': player.get('max_ammo', 6), - 'chargers': player.get('chargers', 2), - 'max_chargers': player.get('max_chargers', 2), - 'reload_time': 5.0, - 'ducks_shot': player.get('caught', 0), - 'ducks_befriended': player.get('befriended', 0), - 'accuracy_bonus': 0, - 'xp_bonus': 0, - 'charm_bonus': 0, - 'exp': player.get('xp', 0), - 'money': player.get('coins', 100), - 'last_hunt': 0, - 'last_reload': 0, - 'level': 1, - 'inventory': {}, - 'ignored_users': [], - 'jammed': False, - 'jammed_count': player.get('jammed_count', 0), - 'total_ammo_used': player.get('total_ammo_used', 0), - 'shot_at': player.get('shot_at', 0), - 'wild_shots': player.get('wild_shots', 0), - 'accuracy': player.get('accuracy', 65), - 'reliability': player.get('reliability', 70), - 'gun_confiscated': player.get('gun_confiscated', False), - 'confiscated_count': player.get('confiscated_count', 0) - } - - for field, default_value in required_fields.items(): - if field not in player: - player[field] = default_value - - def save_player(self, user): - """Save player data - batch saves for performance""" - if not self._save_pending: - self._save_pending = True - asyncio.create_task(self._delayed_save()) - - async def _delayed_save(self): - """Batch save to reduce disk I/O""" - await asyncio.sleep(0.5) - try: - self.save_database() - self.logger.debug("Database batch save completed") - except Exception as e: - self.logger.error(f"Database batch save failed: {e}") - finally: - self._save_pending = False \ No newline at end of file + 'accuracy': 65, + 'gun_confiscated': False + } \ No newline at end of file diff --git a/src/duckhuntbot.py b/src/duckhuntbot.py index e2c139c..edfb569 100644 --- a/src/duckhuntbot.py +++ b/src/duckhuntbot.py @@ -1,5 +1,6 @@ """ -Main DuckHunt IRC Bot +Simplified DuckHunt IRC Bot - Core Features Only +Commands: !bang, !reload, !shop, !duckhelp, !rearm, !disarm, !ignore, !unignore, !ducklaunch """ import asyncio @@ -14,14 +15,14 @@ import signal from typing import Optional from .logging_utils import setup_logger -from .utils import parse_message, InputValidator +from .utils import parse_irc_message, InputValidator, MessageManager from .db import DuckDB from .game import DuckGame from .sasl import SASLHandler class DuckHuntBot: - """Main IRC Bot for DuckHunt game""" + """Simplified IRC Bot for DuckHunt game""" def __init__(self, config): self.config = config @@ -33,30 +34,18 @@ class DuckHuntBot: self.shutdown_requested = False self.db = DuckDB() - self.db.set_config_getter(self.get_config) self.game = DuckGame(self, self.db) + self.messages = MessageManager() self.sasl_handler = SASLHandler(self, config) self.admins = [admin.lower() for admin in self.config.get('admins', ['colby'])] - self.ignored_nicks = set() - self.duck_spawn_times = {} - self.channel_records = {} - - self.dropped_items = {} - - self.colors = { - 'red': '\x0304', - 'green': '\x0303', - 'yellow': '\x0308', - 'blue': '\x0302', - 'cyan': '\x0311', - 'magenta': '\x0306', - 'white': '\x0300', - 'bold': '\x02', - 'reset': '\x03', - 'underline': '\x1f' + # Simple shop items - hardcoded + self.shop_items = { + 1: {"name": "Extra Shots", "price": 10, "description": "5 extra shots"}, + 2: {"name": "Accuracy Boost", "price": 20, "description": "+10% accuracy"}, + 3: {"name": "Lucky Charm", "price": 30, "description": "+5% duck spawn chance"} } def get_config(self, path, default=None): @@ -69,994 +58,426 @@ class DuckHuntBot: else: return default return value - - async def connect(self): - """Connect to IRC server""" - try: - ssl_context = None - if self.config.get('ssl', False): - ssl_context = ssl.create_default_context() - - self.reader, self.writer = await asyncio.open_connection( - self.config['server'], - self.config['port'], - ssl=ssl_context - ) - - self.logger.info(f"Connected to {self.config['server']}:{self.config['port']}") - - if self.config.get('password'): - self.send_raw(f"PASS {self.config['password']}") - - await self.register_user() - - except Exception as e: - self.logger.error(f"Connection failed: {e}") - raise - - async def register_user(self): - """Register user with IRC server""" - nick = self.config['nick'] - self.send_raw(f'NICK {nick}') - self.send_raw(f'USER {nick} 0 * :DuckHunt Bot') - - def send_raw(self, msg): - """Send raw IRC message""" - if self.writer and not self.writer.is_closing(): - try: - self.writer.write(f'{msg}\r\n'.encode()) - except Exception as e: - self.logger.error(f"Error sending message: {e}") - - def send_message(self, target, msg): - """Send message to target (channel or user)""" - self.send_raw(f'PRIVMSG {target} :{msg}') - + def is_admin(self, user): """Check if user is admin by nick only""" if '!' not in user: return False nick = user.split('!')[0].lower() return nick in self.admins - - def get_random_player_for_friendly_fire(self, shooter_nick): - """Get random player for friendly fire accident""" - other_players = [nick for nick in self.db.players.keys() - if nick.lower() != shooter_nick.lower()] - if other_players: - return random.choice(other_players) - return None - - async def send_user_message(self, nick, channel, message, message_type='default'): - """Send message to user respecting their output mode preferences""" - player = self.db.get_player(f"{nick}!user@host") - if not player: - self.send_message(channel, f"{nick} > {message}") - return - - force_public_types = self.get_config('message_output.force_public', {}) or {} - if force_public_types.get(message_type, False): - self.send_message(channel, f"{nick} > {message}") - return - - output_mode = player.get('settings', {}).get('output_mode', 'PUBLIC') - - if output_mode == 'NOTICE': - self.send_raw(f'NOTICE {nick} :{message}') - elif output_mode == 'PRIVMSG': - self.send_message(nick, message) - else: - self.send_message(channel, f"{nick} > {message}") - - async def auto_rearm_confiscated_guns(self, channel, shooter_nick): - """Auto-rearm confiscated guns when someone shoots a duck""" - if not self.get_config('weapons.auto_rearm_on_duck_shot', True): - return - - rearmed_players = [] - for nick, player in self.db.players.items(): - if player.get('gun_confiscated', False): - player['gun_confiscated'] = False - player['ammo'] = player.get('max_ammo', 6) - player['chargers'] = player.get('max_chargers', 2) - rearmed_players.append(nick) - - if rearmed_players: - self.logger.info(f"Auto-rearmed guns for: {', '.join(rearmed_players)}") - self.send_message(channel, - f"{self.colors['green']}Guns have been returned to all hunters! " - f"({len(rearmed_players)} players rearmed){self.colors['reset']}") - self.db.save_database() - + def setup_signal_handlers(self): """Setup signal handlers for graceful shutdown""" def signal_handler(signum, frame): - self.logger.info(f"Received signal {signum}, shutting down...") + signal_name = "SIGINT" if signum == signal.SIGINT else "SIGTERM" + self.logger.info(f"🛑 Received {signal_name} (Ctrl+C), initiating graceful shutdown...") self.shutdown_requested = True - for task in asyncio.all_tasks(): - if not task.done(): - task.cancel() - + signal.signal(signal.SIGINT, signal_handler) signal.signal(signal.SIGTERM, signal_handler) - if hasattr(signal, 'SIGHUP'): - signal.signal(signal.SIGHUP, signal_handler) - + + async def connect(self): + """Connect to IRC server""" + try: + ssl_context = ssl.create_default_context() if self.config.get('ssl', False) else None + self.reader, self.writer = await asyncio.open_connection( + self.config['server'], + self.config['port'], + ssl=ssl_context + ) + self.logger.info(f"Connected to {self.config['server']}:{self.config['port']}") + except Exception as e: + self.logger.error(f"Failed to connect: {e}") + raise + + def send_raw(self, msg): + """Send raw IRC message""" + if self.writer: + self.writer.write(f"{msg}\r\n".encode('utf-8')) + + def send_message(self, target, msg): + """Send message to target (channel or user)""" + self.send_raw(f"PRIVMSG {target} :{msg}") + + async def register_user(self): + """Register user with IRC server""" + if self.config.get('password'): + self.send_raw(f"PASS {self.config['password']}") + + self.send_raw(f"NICK {self.config['nick']}") + self.send_raw(f"USER {self.config['nick']} 0 * :{self.config['nick']}") + async def handle_message(self, prefix, command, params, trailing): """Handle incoming IRC messages""" - try: - if command == '001': - self.registered = True - self.logger.info("Successfully registered with IRC server") - - for channel in self.config['channels']: - self.send_raw(f'JOIN {channel}') - - elif command == 'JOIN': - if params and prefix.split('!')[0] == self.config['nick']: - channel = params[0] - self.channels_joined.add(channel) - self.logger.info(f"Joined channel: {channel}") - - elif command == 'PRIVMSG': - if len(params) >= 1: - target = params[0] - message = trailing - - if message.startswith('!') or target == self.config['nick']: - await self.handle_command(prefix, target, message) - - elif command == 'PING': - self.send_raw(f'PONG :{trailing}') - - elif command == 'CAP': - await self.sasl_handler.handle_cap_response(params, trailing) - elif command == 'AUTHENTICATE': - await self.sasl_handler.handle_authenticate_response(params) - elif command in ['903', '904', '905', '906', '907']: - await self.sasl_handler.handle_sasl_result(command, params, trailing) - - except Exception as e: - self.logger.error(f"Error handling message: {e}") + if command == "001": # Welcome message + self.registered = True + self.logger.info("Successfully registered with IRC server") + # Join channels + for channel in self.config.get('channels', []): + self.send_raw(f"JOIN {channel}") + self.channels_joined.add(channel) + + elif command == "PRIVMSG": + if len(params) >= 1: + target = params[0] + message = trailing or "" + await self.handle_command(prefix, target, message) + + elif command == "PING": + self.send_raw(f"PONG :{trailing}") + async def handle_command(self, user, channel, message): """Handle bot commands""" - if not user: - return - - try: - nick = user.split('!')[0] - nick_lower = nick.lower() - - if not InputValidator.validate_nickname(nick): - return - - if nick_lower in self.ignored_nicks: - return - - message = InputValidator.sanitize_message(message) - if not message: - return - - is_private = channel == self.config['nick'] - response_target = nick if is_private else channel - - if message.startswith('!'): - cmd_parts = message[1:].split() - else: - cmd_parts = message.split() - - if not cmd_parts: - return - - cmd = cmd_parts[0].lower() - args = cmd_parts[1:] if len(cmd_parts) > 1 else [] - - player = self.db.get_player(user) - if not player: - return - - await self.process_command(nick, response_target, cmd, args, player, user) - - except Exception as e: - self.logger.error(f"Error in command handler: {e}") - - async def process_command(self, nick, target, cmd, args, player, user): - """Process individual commands""" - if cmd == 'bang': - await self.handle_bang(nick, target, player) - elif cmd == 'reload': - await self.handle_reload(nick, target, player) - elif cmd == 'bef' or cmd == 'befriend': - await self.handle_befriend(nick, target, player) - elif cmd == 'duckstats': - await self.handle_duckstats(nick, target, player) - elif cmd == 'shop': - await self.handle_shop(nick, target, player) - elif cmd == 'sell': - if args: - await self.handle_sell(nick, target, args[0], player) - else: - await self.send_user_message(nick, target, "Usage: !sell ") - elif cmd == 'use': - if args: - target_nick = args[1] if len(args) > 1 else None - await self.handle_use(nick, target, args[0], player, target_nick) - else: - await self.send_user_message(nick, target, "Usage: !use [target_player]") - elif cmd == 'duckhelp': - await self.handle_duckhelp(nick, target) - elif cmd == 'ignore': - if args: - await self.handle_ignore(nick, target, args[0]) - else: - await self.send_user_message(nick, target, "Usage: !ignore ") - elif cmd == 'delignore': - if args: - await self.handle_delignore(nick, target, args[0]) - else: - await self.send_user_message(nick, target, "Usage: !delignore ") - elif cmd == 'topduck': - await self.handle_topduck(nick, target) - elif cmd == 'snatch': - await self.handle_snatch(nick, target, player) - elif cmd == 'rearm' and self.is_admin(user): - target_nick = args[0] if args else None - await self.handle_rearm(nick, target, player, target_nick) - elif cmd == 'disarm' and self.is_admin(user): - target_nick = args[0] if args else None - await self.handle_disarm(nick, target, target_nick) - elif cmd == 'ducklaunch' and self.is_admin(user): - await self.handle_ducklaunch(nick, target) - elif cmd == 'reset' and self.is_admin(user): - if len(args) >= 2 and args[1] == 'confirm': - await self.handle_reset_confirm(nick, target, args[0]) - elif args: - await self.handle_reset(nick, target, args[0]) - else: - await self.send_user_message(nick, target, "Usage: !reset [confirm]") - else: - pass - - async def handle_bang(self, nick, channel, player): - """Handle !bang command - shoot at duck (eggdrop style)""" - if player.get('gun_confiscated', False): - await self.send_user_message(nick, channel, f"{nick} > Your gun has been confiscated! You cannot shoot.") - return - - if player.get('jammed', False): - message = f"{nick} > Gun jammed! Use !reload" - await self.send_user_message(nick, channel, message) - return - - if player['shots'] <= 0: - message = f"{nick} > *click* You're out of ammo! | Ammo: {player['shots']}/{player['max_shots']} | Chargers: {player.get('chargers', 0)}/{player.get('max_chargers', 2)}" - await self.send_user_message(nick, channel, message) + if not message.startswith('!'): return + parts = message[1:].split() + if not parts: + return + + cmd = parts[0].lower() + args = parts[1:] if len(parts) > 1 else [] + nick = user.split('!')[0] if '!' in user else user + + player = self.db.get_player(nick) + + # Check if player is ignored (unless it's an admin) + if player.get('ignored', False) and not self.is_admin(user): + return + + if cmd == "bang": + await self.handle_bang(nick, channel, player) + elif cmd == "reload": + await self.handle_reload(nick, channel, player) + elif cmd == "shop": + await self.handle_shop(nick, channel, player) + elif cmd == "duckhelp": + await self.handle_duckhelp(nick, channel, player) + elif cmd == "rearm" and self.is_admin(user): + await self.handle_rearm(nick, channel, args) + elif cmd == "disarm" and self.is_admin(user): + await self.handle_disarm(nick, channel, args) + elif cmd == "ignore" and self.is_admin(user): + await self.handle_ignore(nick, channel, args) + elif cmd == "unignore" and self.is_admin(user): + await self.handle_unignore(nick, channel, args) + elif cmd == "ducklaunch" and self.is_admin(user): + await self.handle_ducklaunch(nick, channel, args) + + async def handle_bang(self, nick, channel, player): + """Handle !bang command""" + # Check if gun is confiscated + if player.get('gun_confiscated', False): + message = self.messages.get('bang_not_armed', nick=nick) + self.send_message(channel, message) + return + + # Check ammo + if player['ammo'] <= 0: + message = self.messages.get('bang_no_ammo', nick=nick) + self.send_message(channel, message) + return + + # Check for duck if channel not in self.game.ducks or not self.game.ducks[channel]: - player['shots'] -= 1 - player['total_ammo_used'] = player.get('total_ammo_used', 0) + 1 - player['wild_shots'] = player.get('wild_shots', 0) + 1 - - if self.game.gun_jams(player): - player['jammed'] = True - player['jammed_count'] = player.get('jammed_count', 0) + 1 - message = f"{nick} > *BANG* You shot at nothing! What were you aiming at? *click* Gun jammed! | 0 xp | {self.colors['red']}GUN CONFISCATED{self.colors['reset']}" - else: - message = f"{nick} > *BANG* You shot at nothing! What were you aiming at? | 0 xp | {self.colors['red']}GUN CONFISCATED{self.colors['reset']}" - + # Wild shot - gun confiscated + player['ammo'] -= 1 player['gun_confiscated'] = True - player['confiscated_count'] = player.get('confiscated_count', 0) + 1 - + message = self.messages.get('bang_no_duck', nick=nick) self.send_message(channel, message) self.db.save_database() return - duck = self.game.ducks[channel][0] - player['shots'] -= 1 - player['total_ammo_used'] = player.get('total_ammo_used', 0) + 1 - player['shot_at'] = player.get('shot_at', 0) + 1 + # Shoot at duck + player['ammo'] -= 1 - if self.game.gun_jams(player): - player['jammed'] = True - player['jammed_count'] = player.get('jammed_count', 0) + 1 - message = f"{nick} > *BANG* *click* Gun jammed while shooting! | Ammo: {player['shots']}/{player['max_shots']}" - self.send_message(channel, f"{self.colors['red']}{message}{self.colors['reset']}") + # Calculate hit chance + hit_chance = player.get('accuracy', 65) / 100.0 + if random.random() < hit_chance: + # Hit! Remove the duck + duck = self.game.ducks[channel].pop(0) + xp_gained = 10 + player['xp'] = player.get('xp', 0) + xp_gained + player['ducks_shot'] = player.get('ducks_shot', 0) + 1 + player['accuracy'] = min(player.get('accuracy', 65) + 1, 100) + + message = self.messages.get('bang_hit', + nick=nick, + xp_gained=xp_gained, + ducks_shot=player['ducks_shot']) + self.send_message(channel, message) else: - hit_chance = min(0.7 + (player.get('accuracy', 0) * 0.001), 0.95) - if random.random() < hit_chance: - await self.handle_duck_hit(nick, channel, player, duck) - else: - await self.handle_duck_miss(nick, channel, player) + # Miss! Duck stays in the channel + player['accuracy'] = max(player.get('accuracy', 65) - 2, 10) + message = self.messages.get('bang_miss', nick=nick) + self.send_message(channel, message) self.db.save_database() - async def handle_duck_hit(self, nick, channel, player, duck): - """Handle successful duck hit (eggdrop style)""" - self.game.ducks[channel].remove(duck) - - shot_time = time.time() - reaction_time = shot_time - duck.get('spawn_time', shot_time) - - points_earned = duck['points'] - xp_earned = duck['xp'] - - if reaction_time < 2.0: - quick_bonus = int(points_earned * 0.5) - points_earned += quick_bonus - quick_shot_msg = f" [Quick shot bonus: +{quick_bonus}]" - else: - quick_shot_msg = "" - - xp_earned = int(xp_earned * (1 + player.get('xp_bonus', 0) * 0.001)) - - player['ducks_shot'] += 1 - player['exp'] += xp_earned - player['money'] += points_earned - player['last_hunt'] = time.time() - - current_accuracy = player.get('accuracy', 65) - player['accuracy'] = min(current_accuracy + 1, 95) - - if 'best_time' not in player or reaction_time < player['best_time']: - player['best_time'] = reaction_time - - player['total_reflex_time'] = player.get('total_reflex_time', 0) + reaction_time - player['reflex_shots'] = player.get('reflex_shots', 0) + 1 - - await self.check_level_up(nick, channel, player) - - message = f"{nick} > *BANG* you shot down the duck in {reaction_time:.2f} seconds. \\_X< *KWAK* [+{xp_earned} xp] [TOTAL DUCKS: {player['ducks_shot']}]" - self.send_message(channel, f"{self.colors['green']}{message}{self.colors['reset']}") - - if random.random() < 0.1: - await self.drop_random_item(nick, channel) - - async def handle_duck_miss(self, nick, channel, player): - """Handle duck miss (eggdrop style)""" - current_accuracy = player.get('accuracy', 65) - player['accuracy'] = max(current_accuracy - 2, 10) - - player['missed'] = player.get('missed', 0) + 1 - - message = f"{nick} > *BANG* You missed the duck! | Ammo: {player['shots']}/{player['max_shots']} | Chargers: {player.get('chargers', 0)}/{player.get('max_chargers', 2)}" - self.send_message(channel, f"{self.colors['red']}{message}{self.colors['reset']}") - - if channel in self.game.ducks and len(self.game.ducks[channel]) > 1: - for other_duck in self.game.ducks[channel][:]: - if random.random() < 0.2: - self.game.ducks[channel].remove(other_duck) - self.send_message(channel, f"-.,¸¸.-·°'`'°·-.,¸¸.-·°'`'°· \\_o> The other ducks fly away, scared by the noise!") - async def handle_reload(self, nick, channel, player): - """Handle reload command (eggdrop style) - reload ammo and clear jams""" - current_time = time.time() - + """Handle !reload command""" if player.get('gun_confiscated', False): - await self.send_user_message(nick, channel, f"{nick} > Your gun has been confiscated! You cannot reload.") + message = self.messages.get('reload_not_armed', nick=nick) + self.send_message(channel, message) return - if player.get('jammed', False): - player['jammed'] = False - player['last_reload'] = current_time - - message = f"{nick} > *click click* You unjammed your gun! | Ammo: {player['shots']}/{player['max_shots']} | Chargers: {player.get('chargers', 0)}/{player.get('max_chargers', 2)}" - self.send_message(channel, f"{self.colors['cyan']}{message}{self.colors['reset']}") - self.db.save_database() - return - - if player['shots'] >= player['max_shots']: - message = f"{nick} > Gun is already fully loaded! | Ammo: {player['shots']}/{player['max_shots']} | Chargers: {player.get('chargers', 0)}/{player.get('max_chargers', 2)}" - await self.send_user_message(nick, channel, message) + if player['ammo'] >= player.get('max_ammo', 6): + message = self.messages.get('reload_already_loaded', nick=nick) + self.send_message(channel, message) return - if player.get('chargers', 0) <= 0: - message = f"{nick} > No chargers left to reload with! | Ammo: {player['shots']}/{player['max_shots']} | Chargers: 0/{player.get('max_chargers', 2)}" - await self.send_user_message(nick, channel, message) + if player.get('chargers', 2) <= 0: + message = self.messages.get('reload_no_chargers', nick=nick) + self.send_message(channel, message) return - if current_time - player.get('last_reload', 0) < player['reload_time']: - remaining = int(player['reload_time'] - (current_time - player.get('last_reload', 0))) - message = f"{nick} > Reload cooldown: {remaining} seconds remaining | Ammo: {player['shots']}/{player['max_shots']} | Chargers: {player.get('chargers', 0)}/{player.get('max_chargers', 2)}" - await self.send_user_message(nick, channel, message) - return - - old_shots = player['shots'] - player['shots'] = player['max_shots'] - player['chargers'] = max(0, player.get('chargers', 2) - 1) - player['last_reload'] = current_time - shots_added = player['shots'] - old_shots - - message = f"{nick} > *click clack* Reloaded! +{shots_added} shots | Ammo: {player['shots']}/{player['max_shots']} | Chargers: {player['chargers']}/{player.get('max_chargers', 2)}" - - self.send_message(channel, f"{self.colors['cyan']}{message}{self.colors['reset']}") - - self.db.save_database() - - async def handle_befriend(self, nick, channel, player): - """Handle !bef command - befriend a duck""" - if channel not in self.game.ducks or not self.game.ducks[channel]: - await self.send_user_message(nick, channel, "There are no ducks to befriend!") - return - - duck = self.game.ducks[channel][0] - - befriend_chance = 0.5 + (player.get('charm_bonus', 0) * 0.001) - - if random.random() < befriend_chance: - self.game.ducks[channel].remove(duck) - - xp_earned = duck['xp'] - friendship_bonus = duck['points'] // 2 - - player['exp'] += xp_earned - player['money'] += friendship_bonus - player['ducks_befriended'] += 1 - - await self.check_level_up(nick, channel, player) - - effects = [ - ("luck", 10, "You feel lucky!"), - ("charm_bonus", 5, "The duck teaches you about friendship!"), - ("accuracy_bonus", 3, "The duck gives you aiming tips!") - ] - - if random.random() < 0.3: - effect, amount, message = random.choice(effects) - player[effect] = player.get(effect, 0) + amount - bonus_msg = f" {message}" - else: - bonus_msg = "" - - message = (f"{nick} befriended a {duck['type']} duck! " - f"+{friendship_bonus} coins, +{xp_earned} XP.{bonus_msg}") - self.send_message(channel, f"{self.colors['magenta']}{message}{self.colors['reset']}") - - if random.random() < 0.15: - await self.award_random_item(nick, channel, player) - else: - miss_messages = [ - f"The {duck['type']} duck doesn't trust you yet!", - f"The {duck['type']} duck flies away from you!", - f"You need to be more patient with the {duck['type']} duck!", - f"The {duck['type']} duck looks at you suspiciously!" - ] - - message = f"{nick} {random.choice(miss_messages)}" - self.send_message(channel, f"{self.colors['yellow']}{message}{self.colors['reset']}") - - player['charm_bonus'] = max(player.get('charm_bonus', 0) - 1, -50) + player['ammo'] = player.get('max_ammo', 6) + player['chargers'] = player.get('chargers', 2) - 1 + message = self.messages.get('reload_success', + nick=nick, + ammo=player['ammo'], + max_ammo=player.get('max_ammo', 6), + chargers=player['chargers']) + self.send_message(channel, message) self.db.save_database() async def handle_shop(self, nick, channel, player): - """Handle shop command""" - shop_items = [ - "=== DUCK HUNT SHOP ===", - "1. Extra Shots (3) - $50", - "2. Faster Reload - $100", - "3. Accuracy Charm - $75", - "4. Lucky Charm - $125", - "5. Friendship Bracelet - $80", - "6. Duck Caller - $200", - "7. Camouflage - $150", - "8. Energy Drink - $60", - "==================", - f"Your money: ${player['money']}", - "Use !use to purchase/use items" - ] - for line in shop_items: - await self.send_user_message(nick, channel, line) + """Handle !shop command""" + items = [] + for item_id, item in self.shop_items.items(): + item_text = self.messages.get('shop_item_format', + id=item_id, + name=item['name'], + price=item['price']) + items.append(item_text) + + shop_text = self.messages.get('shop_display', + items=" | ".join(items), + xp=player.get('xp', 0)) + + self.send_message(channel, shop_text) - async def handle_sell(self, nick, channel, item_id, player): - """Handle sell command""" - try: - item_id = int(item_id) - except ValueError: - await self.send_user_message(nick, channel, "Invalid item ID!") - return - - if 'inventory' not in player: - player['inventory'] = {} - - item_key = str(item_id) - if item_key not in player['inventory'] or player['inventory'][item_key] <= 0: - await self.send_user_message(nick, channel, "You don't have that item!") - return - - shop_items = { - 1: {'name': 'Extra Shots', 'price': 50}, - 2: {'name': 'Faster Reload', 'price': 100}, - 3: {'name': 'Accuracy Charm', 'price': 75}, - 4: {'name': 'Lucky Charm', 'price': 125}, - 5: {'name': 'Friendship Bracelet', 'price': 80}, - 6: {'name': 'Duck Caller', 'price': 200}, - 7: {'name': 'Camouflage', 'price': 150}, - 8: {'name': 'Energy Drink', 'price': 60} - } - item_info = shop_items.get(item_id) - if not item_info: - await self.send_user_message(nick, channel, "Invalid item!") - return - - player['inventory'][item_key] -= 1 - if player['inventory'][item_key] <= 0: - del player['inventory'][item_key] - - sell_price = item_info['price'] // 2 - player['money'] += sell_price - - message = f"Sold {item_info['name']} for ${sell_price}!" - await self.send_user_message(nick, channel, message) - - self.db.save_database() - - async def handle_use(self, nick, channel, item_id, player, target_nick=None): - """Handle use command""" - try: - item_id = int(item_id) - except ValueError: - await self.send_user_message(nick, channel, "Invalid item ID!") - return - - if 'inventory' not in player: - player['inventory'] = {} - - shop_items = { - 1: {'name': 'Extra Shots', 'price': 50, 'consumable': True}, - 2: {'name': 'Faster Reload', 'price': 100, 'consumable': True}, - 3: {'name': 'Accuracy Charm', 'price': 75, 'consumable': False}, - 4: {'name': 'Lucky Charm', 'price': 125, 'consumable': False}, - 5: {'name': 'Friendship Bracelet', 'price': 80, 'consumable': False}, - 6: {'name': 'Duck Caller', 'price': 200, 'consumable': True}, - 7: {'name': 'Camouflage', 'price': 150, 'consumable': True}, - 8: {'name': 'Energy Drink', 'price': 60, 'consumable': True} - } - - item_key = str(item_id) - item_info = shop_items.get(item_id) - - if not item_info: - await self.send_user_message(nick, channel, "Invalid item ID!") - return - - if item_key in player['inventory'] and player['inventory'][item_key] > 0: - await self.use_item_effect(player, item_id, nick, channel, target_nick) - player['inventory'][item_key] -= 1 - if player['inventory'][item_key] <= 0: - del player['inventory'][item_key] - else: - if player['money'] >= item_info['price']: - if item_info.get('consumable', True): - player['money'] -= item_info['price'] - await self.use_item_effect(player, item_id, nick, channel, target_nick) - else: - player['money'] -= item_info['price'] - player['inventory'][item_key] = player['inventory'].get(item_key, 0) + 1 - await self.send_user_message(nick, channel, f"Purchased {item_info['name']}!") - else: - await self.send_user_message(nick, channel, - f"Not enough money! Need ${item_info['price']}, you have ${player['money']}") - return - - self.db.save_database() - - async def handle_topduck(self, nick, channel): - """Handle topduck command - show leaderboard""" - sorted_players = sorted( - [(name, data) for name, data in self.db.players.items()], - key=lambda x: x[1]['ducks_shot'], - reverse=True - ) - - if not sorted_players: - await self.send_user_message(nick, channel, "No players found!") - return - - await self.send_user_message(nick, channel, "=== TOP DUCK HUNTERS ===") - for i, (name, data) in enumerate(sorted_players[:5], 1): - stats = f"{i}. {name}: {data['ducks_shot']} ducks (Level {data['level']})" - await self.send_user_message(nick, channel, stats) - - async def handle_snatch(self, nick, channel, player): - """Handle snatch command - grab dropped items competitively""" - import time - - if channel not in self.dropped_items or not self.dropped_items[channel]: - await self.send_user_message(nick, channel, f"{nick} > There are no items to snatch!") - return - - item = self.dropped_items[channel].pop(0) - - current_time = time.time() - if current_time - item['timestamp'] > 60: - await self.send_user_message(nick, channel, f"{nick} > The item has disappeared!") - self.dropped_items[channel] = [ - i for i in self.dropped_items[channel] - if current_time - i['timestamp'] <= 60 - ] - return - - if 'inventory' not in player: - player['inventory'] = {} - - item_key = item['item_id'] - player['inventory'][item_key] = player['inventory'].get(item_key, 0) + 1 - - message = f"{nick} snatched a {item['item_name']}! ⚡" - self.send_message(channel, f"{self.colors['cyan']}{message}{self.colors['reset']}") - - async def handle_rearm(self, nick, channel, player, target_nick=None): - """Handle rearm command - restore confiscated guns""" - if target_nick: - target_player = self.db.get_player(target_nick.lower()) - if target_player: - target_player['gun_confiscated'] = False - target_player['shots'] = target_player['max_shots'] - target_player['chargers'] = target_player.get('max_chargers', 2) - target_player['jammed'] = False - target_player['last_reload'] = 0 - message = f"{nick} returned {target_nick}'s confiscated gun! | Ammo: {target_player['shots']}/{target_player['max_shots']} | Chargers: {target_player['chargers']}/{target_player.get('max_chargers', 2)}" - self.send_message(channel, f"{self.colors['cyan']}{message}{self.colors['reset']}") - else: - await self.send_user_message(nick, channel, "Player not found!") - else: - if not player.get('gun_confiscated', False): - await self.send_user_message(nick, channel, f"{nick} > Your gun is not confiscated!") - return - - if self.is_admin(nick): - player['gun_confiscated'] = False - player['shots'] = player['max_shots'] - player['chargers'] = player.get('max_chargers', 2) - player['jammed'] = False - player['last_reload'] = 0 - message = f"{nick} > Gun returned by admin! | Ammo: {player['shots']}/{player['max_shots']} | Chargers: {player['chargers']}/{player.get('max_chargers', 2)}" - self.send_message(channel, f"{self.colors['cyan']}{message}{self.colors['reset']}") - else: - await self.send_user_message(nick, channel, f"{nick} > Your gun has been confiscated! Wait for an admin or automatic return.") - - self.db.save_database() - - async def handle_disarm(self, nick, channel, target_nick): - """Handle disarm command (admin only)""" - target_player = self.db.get_player(target_nick.lower()) - if target_player: - target_player['shots'] = 0 - message = f"Admin {nick} disarmed {target_nick}!" - self.send_message(channel, f"{self.colors['red']}{message}{self.colors['reset']}") - - self.db.save_database() - else: - await self.send_user_message(nick, channel, "Player not found!") - - async def handle_ducklaunch(self, nick, channel): - """Handle !ducklaunch admin command""" - duck = await self.game.spawn_duck_now(channel) - if duck: - self.send_message(channel, - f"{self.colors['green']}Admin {nick} launched a duck!{self.colors['reset']}") - else: - await self.send_user_message(nick, channel, "Failed to spawn duck (channel may be full)!") - - async def handle_duckstats(self, nick, channel, player): - """Handle duckstats command""" - stats_msg = ( - f"{nick}'s duck hunting stats: " - f"Level {player['level']} | " - f"Ducks shot: {player['ducks_shot']} | " - f"Befriended: {player['ducks_befriended']} | " - f"Money: ${player['money']} | " - f"XP: {player['exp']}/{self.get_xp_for_level(player['level'] + 1)}" - ) - await self.send_user_message(nick, channel, stats_msg) - - if 'inventory' in player and player['inventory']: - shop_items = { - 1: 'Extra Shots', 2: 'Faster Reload', 3: 'Accuracy Charm', 4: 'Lucky Charm', - 5: 'Friendship Bracelet', 6: 'Duck Caller', 7: 'Camouflage', 8: 'Energy Drink', - 9: 'Armor Vest', 10: 'Gunpowder', 11: 'Sight', 12: 'Silencer', - 13: 'Explosive Ammo', 14: 'Mirror', 15: 'Sunglasses', 16: 'Clothes', - 17: 'Grease', 18: 'Brush', 19: 'Sand', 20: 'Water', - 21: 'Sabotage Kit', 22: 'Life Insurance', 23: 'Decoy' - } - - inventory_items = [] - for item_id, quantity in player['inventory'].items(): - item_name = shop_items.get(int(item_id), f"Item {item_id}") - inventory_items.append(f"{item_name} x{quantity}") - - if inventory_items: - inventory_msg = f"Inventory: {', '.join(inventory_items)}" - await self.send_user_message(nick, channel, inventory_msg) - - async def handle_duckhelp(self, nick, channel): - """Handle duckhelp command""" + async def handle_duckhelp(self, nick, channel, player): + """Handle !duckhelp command""" help_lines = [ - "=== DUCK HUNT COMMANDS ===", - "!bang - Shoot at ducks", - "!reload - Reload your gun", - "!bef - Befriend a duck", - "!duckstats - View your statistics", - "!shop - View the shop", - "!inventory - View your items", - "!use - Use/buy shop items", - "!sell - Sell inventory items", - "!topduck - View leaderboard", - "!rearm - Quick reload (costs money)", - "!ducklaunch - Spawn duck (admin)", - "!disarm - Disarm player (admin)", - "!reset - Reset player (admin)", - "========================" + self.messages.get('help_header'), + self.messages.get('help_user_commands'), + self.messages.get('help_help_command') ] - for line in help_lines: - await self.send_user_message(nick, channel, line) - - async def handle_ignore(self, nick, channel, target_nick): - """Handle ignore command""" - if 'ignored_users' not in self.db.players[nick.lower()]: - self.db.players[nick.lower()]['ignored_users'] = [] - ignored_list = self.db.players[nick.lower()]['ignored_users'] - if target_nick.lower() not in ignored_list: - ignored_list.append(target_nick.lower()) - await self.send_user_message(nick, channel, f"Now ignoring {target_nick}") - self.db.save_database() - else: - await self.send_user_message(nick, channel, f"{target_nick} is already ignored") + # Add admin commands if user is admin + if self.is_admin(f"{nick}!user@host"): + help_lines.append(self.messages.get('help_admin_commands')) + + for line in help_lines: + self.send_message(channel, line) - async def handle_delignore(self, nick, channel, target_nick): - """Handle delignore command""" - if 'ignored_users' not in self.db.players[nick.lower()]: - await self.send_user_message(nick, channel, f"{target_nick} is not ignored") + async def handle_rearm(self, nick, channel, args): + """Handle !rearm command (admin only)""" + if args: + target = args[0].lower() + player = self.db.get_player(target) + player['gun_confiscated'] = False + player['ammo'] = player.get('max_ammo', 6) + player['chargers'] = 2 + message = self.messages.get('admin_rearm_player', target=target, admin=nick) + self.send_message(channel, message) + else: + # Rearm everyone + for player_data in self.db.players.values(): + player_data['gun_confiscated'] = False + player_data['ammo'] = player_data.get('max_ammo', 6) + player_data['chargers'] = 2 + message = self.messages.get('admin_rearm_all', admin=nick) + self.send_message(channel, message) + + self.db.save_database() + + async def handle_disarm(self, nick, channel, args): + """Handle !disarm command (admin only)""" + if not args: + message = self.messages.get('usage_disarm') + self.send_message(channel, message) return - ignored_list = self.db.players[nick.lower()]['ignored_users'] - if target_nick.lower() in ignored_list: - ignored_list.remove(target_nick.lower()) - await self.send_user_message(nick, channel, f"No longer ignoring {target_nick}") - self.db.save_database() - else: - await self.send_user_message(nick, channel, f"{target_nick} is not ignored") - - async def handle_reset(self, nick, channel, target_nick): - """Handle !reset admin command (requires confirmation)""" - await self.send_user_message(nick, channel, - f"⚠️ WARNING: This will completely reset {target_nick}'s progress! " - f"Use `!reset {target_nick} confirm` to proceed.") - - async def handle_reset_confirm(self, nick, channel, target_nick): - """Handle !reset confirm admin command""" - if target_nick.lower() in self.db.players: - del self.db.players[target_nick.lower()] - self.send_message(channel, - f"{self.colors['red']}Admin {nick} has reset {target_nick}'s progress!{self.colors['reset']}") - self.db.save_database() - else: - await self.send_user_message(nick, channel, "Player not found!") + target = args[0].lower() + player = self.db.get_player(target) + player['gun_confiscated'] = True + + message = self.messages.get('admin_disarm', target=target, admin=nick) + self.send_message(channel, message) + self.db.save_database() - async def check_level_up(self, nick, channel, player): - """Check if player leveled up""" - current_level = player['level'] - new_level = self.calculate_level(player['exp']) + async def handle_ignore(self, nick, channel, args): + """Handle !ignore command (admin only)""" + if not args: + message = self.messages.get('usage_ignore') + self.send_message(channel, message) + return - if new_level > current_level: - player['level'] = new_level - - player['max_shots'] = min(player['max_shots'] + 1, 10) - player['reload_time'] = max(player['reload_time'] - 0.5, 2.0) - - message = (f"🎉 {nick} leveled up to level {new_level}! " - f"Max shots: {player['max_shots']}, " - f"Reload time: {player['reload_time']}s") - self.send_message(channel, f"{self.colors['yellow']}{message}{self.colors['reset']}") + target = args[0].lower() + player = self.db.get_player(target) + player['ignored'] = True + + message = self.messages.get('admin_ignore', target=target, admin=nick) + self.send_message(channel, message) + self.db.save_database() - def calculate_level(self, exp): - """Calculate level from experience points""" - import math - return int(math.sqrt(exp / 100)) + 1 + async def handle_unignore(self, nick, channel, args): + """Handle !unignore command (admin only)""" + if not args: + message = self.messages.get('usage_unignore') + self.send_message(channel, message) + return + + target = args[0].lower() + player = self.db.get_player(target) + player['ignored'] = False + + message = self.messages.get('admin_unignore', target=target, admin=nick) + self.send_message(channel, message) + self.db.save_database() - def get_xp_for_level(self, level): - """Get XP required for a specific level""" - return (level - 1) ** 2 * 100 + async def handle_ducklaunch(self, nick, channel, args): + """Handle !ducklaunch command (admin only)""" + if channel not in self.channels_joined: + message = self.messages.get('admin_ducklaunch_not_enabled') + self.send_message(channel, message) + return + + # Force spawn a duck + if channel not in self.game.ducks: + self.game.ducks[channel] = [] + self.game.ducks[channel].append({"spawn_time": time.time()}) + admin_message = self.messages.get('admin_ducklaunch', admin=nick) + duck_message = self.messages.get('duck_spawn') + + self.send_message(channel, admin_message) + self.send_message(channel, duck_message) - async def drop_random_item(self, nick, channel): - """Drop a random item to the ground for competitive snatching""" - import time - - item_ids = [1, 2, 3, 4, 5, 6, 7, 8] - item_id = random.choice(item_ids) - item_key = str(item_id) - - item_names = { - '1': 'Extra Shots', '2': 'Faster Reload', '3': 'Accuracy Charm', - '4': 'Lucky Charm', '5': 'Friendship Bracelet', '6': 'Duck Caller', - '7': 'Camouflage', '8': 'Energy Drink' - } - - item_name = item_names.get(item_key, f'Item {item_id}') - - if channel not in self.dropped_items: - self.dropped_items[channel] = [] - - dropped_item = { - 'item_id': item_key, - 'item_name': item_name, - 'timestamp': time.time(), - 'dropper': nick - } - self.dropped_items[channel].append(dropped_item) - - message = f"🎁 A {item_name} has been dropped! Type !snatch to grab it!" - self.send_message(channel, f"{self.colors['magenta']}{message}{self.colors['reset']}") - - async def award_random_item(self, nick, channel, player): - """Award a random item to player""" - if 'inventory' not in player: - player['inventory'] = {} - - item_ids = [1, 2, 3, 4, 5, 6, 7, 8] - item_id = random.choice(item_ids) - item_key = str(item_id) - - player['inventory'][item_key] = player['inventory'].get(item_key, 0) + 1 - - item_names = { - '1': 'Extra Shots', '2': 'Faster Reload', '3': 'Accuracy Charm', - '4': 'Lucky Charm', '5': 'Friendship Bracelet', '6': 'Duck Caller', - '7': 'Camouflage', '8': 'Energy Drink' - } - - item_name = item_names.get(item_key, f'Item {item_id}') - message = f"🎁 {nick} found a {item_name}!" - self.send_message(channel, f"{self.colors['magenta']}{message}{self.colors['reset']}") - async def use_item_effect(self, player, item_id, nick, channel, target_nick=None): - """Apply item effects""" - effects = { - 1: "Extra Shots! +3 shots", - 2: "Faster Reload! -1s reload time", - 3: "Accuracy Charm! +5 accuracy", - 4: "Lucky Charm! +10 luck", - 5: "Friendship Bracelet! +5 charm", - 6: "Duck Caller! Next duck spawns faster", - 7: "Camouflage! Ducks can't see you for 60s", - 8: "Energy Drink! +50 energy" - } - - if item_id == 1: - player['shots'] = min(player['shots'] + 3, player['max_shots']) - elif item_id == 2: - player['reload_time'] = max(player['reload_time'] - 1, 1) - elif item_id == 3: - player['accuracy_bonus'] = player.get('accuracy_bonus', 0) + 5 - elif item_id == 4: - player['luck'] = player.get('luck', 0) + 10 - elif item_id == 5: - player['charm_bonus'] = player.get('charm_bonus', 0) + 5 - elif item_id == 6: - pass - elif item_id == 7: - player['camouflaged_until'] = time.time() + 60 - elif item_id == 8: - player['energy'] = player.get('energy', 100) + 50 - - effect_msg = effects.get(item_id, "Unknown effect") - await self.send_user_message(nick, channel, f"Used item: {effect_msg}") - - async def cleanup_expired_items(self): - """Background task to clean up expired dropped items""" - import time - - while not self.shutdown_requested: - try: - current_time = time.time() - - for channel in list(self.dropped_items.keys()): - if channel in self.dropped_items: - original_count = len(self.dropped_items[channel]) - - self.dropped_items[channel] = [ - item for item in self.dropped_items[channel] - if current_time - item['timestamp'] <= 60 - ] - - removed_count = original_count - len(self.dropped_items[channel]) - if removed_count > 0: - self.logger.debug(f"Cleaned up {removed_count} expired items from {channel}") - - await asyncio.sleep(30) - - except Exception as e: - self.logger.error(f"Error in cleanup_expired_items: {e}") - await asyncio.sleep(30) - - async def run(self): - """Main bot run loop""" - tasks = [] + async def message_loop(self): + """Main message processing loop""" try: - self.setup_signal_handlers() - self.db.load_database() - await self.connect() - - tasks = [ - asyncio.create_task(self.message_loop()), - asyncio.create_task(self.game.spawn_ducks()), - asyncio.create_task(self.game.duck_timeout_checker()), - asyncio.create_task(self.cleanup_expired_items()), - ] - - try: - while not self.shutdown_requested: - for task in tasks: - if task.done() and task.exception(): - self.logger.error(f"Task failed: {task.exception()}") - self.shutdown_requested = True - break - - await asyncio.sleep(0.1) - - except asyncio.CancelledError: - self.logger.info("Main loop cancelled") - except KeyboardInterrupt: - self.logger.info("Keyboard interrupt received") - self.shutdown_requested = True + while not self.shutdown_requested and self.reader: + line = await self.reader.readline() + if not line: + break + line = line.decode('utf-8').strip() + if not line: + continue + + try: + prefix, command, params, trailing = parse_irc_message(line) + await self.handle_message(prefix, command, params, trailing) + except Exception as e: + self.logger.error(f"Error processing message '{line}': {e}") + + except asyncio.CancelledError: + self.logger.info("Message loop cancelled") except Exception as e: - self.logger.error(f"Bot error: {e}") - raise + self.logger.error(f"Message loop error: {e}") finally: - self.logger.info("Shutting down bot...") + self.logger.info("Message loop ended") + + async def run(self): + """Main bot loop with improved shutdown handling""" + self.setup_signal_handlers() + + game_task = None + message_task = None + + try: + await self.connect() + await self.register_user() - for task in tasks: + # Start game loops + game_task = asyncio.create_task(self.game.start_game_loops()) + message_task = asyncio.create_task(self.message_loop()) + shutdown_task = asyncio.create_task(self.shutdown_event.wait()) + + self.logger.info("🦆 Bot is now running! Press Ctrl+C to stop.") + + # Wait for shutdown signal or task completion + done, pending = await asyncio.wait( + [game_task, message_task, shutdown_task], + return_when=asyncio.FIRST_COMPLETED + ) + + if shutdown_task in done: + self.logger.info("🛑 Shutdown signal received, cleaning up...") + await self._graceful_shutdown() + + # Cancel remaining tasks + for task in pending: if not task.done(): task.cancel() try: await task except asyncio.CancelledError: - pass - except Exception as e: - self.logger.error(f"Error cancelling task: {e}") + self.logger.debug(f"Task cancelled: {task}") + except asyncio.TimeoutError: + self.logger.debug(f"Task timed out: {task}") + + except asyncio.CancelledError: + self.logger.info("🛑 Main loop cancelled") + except Exception as e: + self.logger.error(f"❌ Bot error: {e}") + finally: + self.logger.info("🔄 Final cleanup...") + # Ensure tasks are cancelled + for task in [game_task, message_task]: + if task and not task.done(): + task.cancel() + try: + await task + except asyncio.CancelledError: + pass + + # Final database save try: self.db.save_database() - self.logger.info("Database saved") + self.logger.info("💾 Database saved") except Exception as e: - self.logger.error(f"Error saving database: {e}") + self.logger.error(f"❌ Error saving database: {e}") + # Close IRC connection + await self._close_connection() + + self.logger.info("✅ Bot shutdown complete") + + async def _graceful_shutdown(self): + """Perform graceful shutdown steps""" + try: + # Send quit message to IRC if self.writer and not self.writer.is_closing(): - try: - self.send_raw("QUIT :Bot shutting down") - self.writer.close() - await self.writer.wait_closed() - self.logger.info("IRC connection closed") - except Exception as e: - self.logger.error(f"Error closing connection: {e}") - - self.logger.info("Bot shutdown complete") - - async def message_loop(self): - """Handle incoming IRC messages""" - while not self.shutdown_requested and self.reader: - try: - line = await asyncio.wait_for(self.reader.readline(), timeout=1.0) - if not line: - self.logger.warning("Empty line received, connection may be closed") - break - - line = line.decode().strip() - if line: - prefix, command, params, trailing = parse_message(line) - await self.handle_message(prefix, command, params, trailing) - - except asyncio.TimeoutError: - continue - except asyncio.CancelledError: - self.logger.info("Message loop cancelled") - break - except Exception as e: - self.logger.error(f"Message loop error: {e}") - break + self.logger.info("📤 Sending QUIT message to IRC...") + quit_message = self.config.get('quit_message', 'DuckHunt Bot shutting down') + self.send_raw(f"QUIT :{quit_message}") - self.logger.info("Message loop ended") \ No newline at end of file + # Give IRC server time to process quit + await asyncio.sleep(0.5) + + # Save database + self.logger.info("💾 Saving database...") + self.db.save_database() + + except Exception as e: + self.logger.error(f"❌ Error during graceful shutdown: {e}") + + async def _close_connection(self): + """Close IRC connection safely""" + if self.writer: + try: + if not self.writer.is_closing(): + self.writer.close() + await asyncio.wait_for(self.writer.wait_closed(), timeout=3.0) + self.logger.info("🔌 IRC connection closed") + except asyncio.TimeoutError: + self.logger.warning("⚠️ Connection close timed out") + except Exception as e: + self.logger.error(f"❌ Error closing connection: {e}") \ No newline at end of file diff --git a/src/game.py b/src/game.py index 6adea68..ae4942b 100644 --- a/src/game.py +++ b/src/game.py @@ -1,310 +1,105 @@ """ -Game mechanics for DuckHunt Bot +Simplified Game mechanics for DuckHunt Bot +Basic duck spawning and timeout only """ import asyncio import random import time -import uuid import logging -from typing import Dict, Any, Optional, List class DuckGame: - """Game mechanics and duck management""" + """Simplified game mechanics - just duck spawning""" def __init__(self, bot, db): self.bot = bot self.db = db - self.ducks = {} + self.ducks = {} # {channel: [duck1, duck2, ...]} self.logger = logging.getLogger('DuckHuntBot.Game') - - self.colors = { - 'red': '\x0304', - 'green': '\x0303', - 'yellow': '\x0308', - 'blue': '\x0302', - 'cyan': '\x0311', - 'magenta': '\x0306', - 'white': '\x0300', - 'bold': '\x02', - 'reset': '\x03', - 'underline': '\x1f' - } - - def get_config(self, path, default=None): - """Helper method to get config values""" - return self.bot.get_config(path, default) - - def get_player_level(self, xp): - """Calculate player level from XP""" - if xp < 0: - return 0 - return int((xp ** 0.5) / 2) + 1 + self.spawn_task = None + self.timeout_task = None - def get_xp_for_next_level(self, xp): - """Calculate XP needed for next level""" - level = self.get_player_level(xp) - return ((level * 2) ** 2) - xp - - def calculate_penalty_by_level(self, base_penalty, xp): - """Reduce penalties for higher level players""" - level = self.get_player_level(xp) - return max(1, base_penalty - (level - 1)) - - def update_karma(self, player, event): - """Update player karma based on events""" - if not self.get_config('karma.enabled', True): - return - - karma_changes = { - 'hit': self.get_config('karma.hit_bonus', 2), - 'golden_hit': self.get_config('karma.golden_hit_bonus', 5), - 'teamkill': -self.get_config('karma.teamkill_penalty', 10), - 'wild_shot': -self.get_config('karma.wild_shot_penalty', 3), - 'miss': -self.get_config('karma.miss_penalty', 1), - 'befriend_success': self.get_config('karma.befriend_success_bonus', 2), - 'befriend_fail': -self.get_config('karma.befriend_fail_penalty', 1) - } + async def start_game_loops(self): + """Start the game loops""" + self.spawn_task = asyncio.create_task(self.duck_spawn_loop()) + self.timeout_task = asyncio.create_task(self.duck_timeout_loop()) - if event in karma_changes: - player['karma'] = player.get('karma', 0) + karma_changes[event] + try: + await asyncio.gather(self.spawn_task, self.timeout_task) + except asyncio.CancelledError: + self.logger.info("Game loops cancelled") - def is_sleep_time(self): - """Check if ducks should not spawn due to sleep hours""" - sleep_hours = self.get_config('sleep_hours', []) - if not sleep_hours or len(sleep_hours) != 2: - return False - - import datetime - current_hour = datetime.datetime.now().hour - start_hour, end_hour = sleep_hours - - if start_hour <= end_hour: - return start_hour <= current_hour <= end_hour - else: - return current_hour >= start_hour or current_hour <= end_hour - - def calculate_gun_reliability(self, player): - """Calculate gun reliability with modifiers""" - base_reliability = player.get('reliability', 70) - return min(100, max(0, base_reliability)) - - def gun_jams(self, player): - """Check if gun jams (eggdrop style)""" - reliability = player.get('reliability', 70) - jam_chance = max(1, 101 - reliability) - - if player.get('total_ammo_used', 0) > 100: - jam_chance += 2 - - if player.get('jammed_count', 0) > 5: - jam_chance += 1 - - return random.randint(1, 100) <= jam_chance + async def duck_spawn_loop(self): + """Simple duck spawning loop""" + try: + while True: + # Wait random time between spawns + min_wait = self.bot.get_config('duck_spawn_min', 300) # 5 minutes + max_wait = self.bot.get_config('duck_spawn_max', 900) # 15 minutes + wait_time = random.randint(min_wait, max_wait) - async def scare_other_ducks(self, channel, shot_duck_id): - """Scare other ducks when one is shot""" - if channel not in self.ducks: - return - - for duck in self.ducks[channel][:]: - if duck['id'] != shot_duck_id and duck['alive']: - if random.random() < 0.3: - duck['alive'] = False - self.ducks[channel].remove(duck) + await asyncio.sleep(wait_time) + + # Spawn duck in random channel + channels = list(self.bot.channels_joined) + if channels: + channel = random.choice(channels) + await self.spawn_duck(channel) + + except asyncio.CancelledError: + self.logger.info("Duck spawning loop cancelled") + + async def duck_timeout_loop(self): + """Simple duck timeout loop""" + try: + while True: + await asyncio.sleep(10) # Check every 10 seconds + + current_time = time.time() + channels_to_clear = [] + + for channel, ducks in self.ducks.items(): + ducks_to_remove = [] + for duck in ducks: + if current_time - duck['spawn_time'] > self.bot.get_config('duck_timeout', 60): + ducks_to_remove.append(duck) - async def scare_duck_on_miss(self, channel, target_duck): - """Scare duck when someone misses""" - if target_duck and random.random() < 0.15: - target_duck['alive'] = False - if channel in self.ducks and target_duck in self.ducks[channel]: - self.ducks[channel].remove(target_duck) + for duck in ducks_to_remove: + ducks.remove(duck) + message = self.bot.messages.get('duck_flies_away') + self.bot.send_message(channel, message) + + if not ducks: + channels_to_clear.append(channel) - async def find_bushes_items(self, nick, channel, player): - """Find random items in bushes""" - if not self.get_config('items.enabled', True): - return - - if random.random() < 0.1: - items = [ - ("a mirror", "mirror", "You can now deflect shots!"), - ("some sand", "sand", "Throw this to blind opponents!"), - ("a rusty bullet", None, "It's too rusty to use..."), - ("some bread crumbs", "bread", "Feed ducks to make them friendly!"), - ] - - found_item, item_key, message = random.choice(items) - - if item_key and item_key in player: - player[item_key] = player.get(item_key, 0) + 1 - elif item_key in player: - player[item_key] = player.get(item_key, 0) + 1 - - await self.bot.send_user_message(nick, channel, - f"You found {found_item} in the bushes! {message}") + # Clean up empty channels + for channel in channels_to_clear: + if channel in self.ducks and not self.ducks[channel]: + del self.ducks[channel] + + except asyncio.CancelledError: + self.logger.info("Duck timeout loop cancelled") - def get_duck_spawn_message(self): - """Get random duck spawn message (eggdrop style)""" - messages = [ - "-.,¸¸.-·°'`'°·-.,¸¸.-·°'`'°· \\_O< QUACK", - "-.,¸¸.-·°'`'°·-.,¸¸.-·°'`'°· \\_o< QUACK!", - "-.,¸¸.-·°'`'°·-.,¸¸.-·°'`'°· \\_O< QUAAACK!", - "-.,¸¸.-·°'`'°·-.,¸¸.-·°'`'°· \\_ö< Quack?", - "-.,¸¸.-·°'`'°·-.,¸¸.-·°'`'°· \\_O< *QUACK*" - ] - return random.choice(messages) - - async def spawn_duck_now(self, channel, force_golden=False): - """Spawn a duck immediately in the specified channel""" + async def spawn_duck(self, channel): + """Spawn a duck in the channel""" if channel not in self.ducks: self.ducks[channel] = [] - - max_ducks = self.get_config('max_ducks_per_channel', 3) - if len([d for d in self.ducks[channel] if d['alive']]) >= max_ducks: - self.logger.debug(f"Max ducks already in {channel}") + + # Don't spawn if there's already a duck + if self.ducks[channel]: return - - if force_golden: - duck_type = "golden" - else: - rand = random.random() - if rand < 0.02: - duck_type = "armored" - elif rand < 0.10: - duck_type = "golden" - elif rand < 0.30: - duck_type = "rare" - elif rand < 0.40: - duck_type = "fast" - else: - duck_type = "normal" - - duck_config = self.get_config(f'duck_types.{duck_type}', {}) - if not duck_config.get('enabled', True): - duck_type = "normal" - duck_config = self.get_config('duck_types.normal', {}) - + duck = { - 'id': str(uuid.uuid4())[:8], - 'type': duck_type, - 'alive': True, + 'id': f"duck_{int(time.time())}_{random.randint(1000, 9999)}", 'spawn_time': time.time(), - 'health': duck_config.get('health', 1), - 'max_health': duck_config.get('health', 1) + 'channel': channel } self.ducks[channel].append(duck) - messages = duck_config.get('messages', [self.get_duck_spawn_message()]) - spawn_message = random.choice(messages) + # Send spawn message + message = self.bot.messages.get('duck_spawn') + self.bot.send_message(channel, message) - self.bot.send_message(channel, spawn_message) - self.logger.info(f"Spawned {duck_type} duck in {channel}") - - await self.send_duck_alerts(channel, duck_type) - - return duck - - async def send_duck_alerts(self, channel, duck_type): - """Send alerts to users who have them enabled""" - if not self.get_config('social.duck_alerts_enabled', True): - return - - self.logger.debug(f"Duck alerts for {duck_type} duck in {channel}") - - async def spawn_ducks(self): - """Main duck spawning loop""" - while not self.bot.shutdown_requested: - try: - if self.is_sleep_time(): - await asyncio.sleep(300) - continue - - for channel in self.bot.channels_joined: - if self.bot.shutdown_requested: - break - - if channel not in self.ducks: - self.ducks[channel] = [] - - self.ducks[channel] = [d for d in self.ducks[channel] if d['alive']] - - max_ducks = self.get_config('max_ducks_per_channel', 3) - alive_ducks = len([d for d in self.ducks[channel] if d['alive']]) - - if alive_ducks < max_ducks: - min_spawn_time = self.get_config('duck_spawn_min', 1800) - max_spawn_time = self.get_config('duck_spawn_max', 5400) - - if random.random() < 0.1: - await self.spawn_duck_now(channel) - - await asyncio.sleep(random.randint(60, 300)) - - except asyncio.CancelledError: - self.logger.info("Duck spawning loop cancelled") - break - except Exception as e: - self.logger.error(f"Error in duck spawning: {e}") - await asyncio.sleep(60) - - async def duck_timeout_checker(self): - """Check for ducks that should timeout""" - while not self.bot.shutdown_requested: - try: - current_time = time.time() - - for channel in list(self.ducks.keys()): - if self.bot.shutdown_requested: - break - - if channel not in self.ducks: - continue - - for duck in self.ducks[channel][:]: - if not duck['alive']: - continue - - age = current_time - duck['spawn_time'] - min_timeout = self.get_config('duck_timeout_min', 45) - max_timeout = self.get_config('duck_timeout_max', 75) - - timeout = random.randint(min_timeout, max_timeout) - - if age > timeout: - duck['alive'] = False - self.ducks[channel].remove(duck) - - timeout_messages = [ - "-.,¸¸.-·°'`'°·-.,¸¸.-·°'`'°· \\_o> The duck flew away!", - "-.,¸¸.-·°'`'°·-.,¸¸.-·°'`'°· \\_O> *FLAP FLAP FLAP*", - "-.,¸¸.-·°'`'°·-.,¸¸.-·°'`'°· \\_o> The duck got tired of waiting and left!", - "-.,¸¸.-·°'`'°·-.,¸¸.-·°'`'°· \\_O> *KWAK* The duck escaped!" - ] - self.bot.send_message(channel, random.choice(timeout_messages)) - self.logger.debug(f"Duck timed out in {channel}") - - await asyncio.sleep(10) - - except asyncio.CancelledError: - self.logger.info("Duck timeout checker cancelled") - break - except Exception as e: - self.logger.error(f"Error in duck timeout checker: {e}") - await asyncio.sleep(30) - - def get_alive_ducks(self, channel): - """Get list of alive ducks in channel""" - if channel not in self.ducks: - return [] - return [d for d in self.ducks[channel] if d['alive']] - - def get_duck_by_id(self, channel, duck_id): - """Get duck by ID""" - if channel not in self.ducks: - return None - for duck in self.ducks[channel]: - if duck['id'] == duck_id and duck['alive']: - return duck - return None \ No newline at end of file + self.logger.info(f"Duck spawned in {channel}") \ No newline at end of file diff --git a/src/logging_utils.py b/src/logging_utils.py index 77f22fc..9d2962b 100644 --- a/src/logging_utils.py +++ b/src/logging_utils.py @@ -6,9 +6,9 @@ import logging import logging.handlers -class DetailedColorFormatter(logging.Formatter): - """Console formatter with color support""" - COLORS = { +class DetailedColourFormatter(logging.Formatter): + """Console formatter with colour support""" + COLOURS = { 'DEBUG': '\033[94m', 'INFO': '\033[92m', 'WARNING': '\033[93m', @@ -18,14 +18,14 @@ class DetailedColorFormatter(logging.Formatter): } def format(self, record): - color = self.COLORS.get(record.levelname, '') - endc = self.COLORS['ENDC'] + colour = self.COLOURS.get(record.levelname, '') + endc = self.COLOURS['ENDC'] msg = super().format(record) - return f"{color}{msg}{endc}" + return f"{colour}{msg}{endc}" class DetailedFileFormatter(logging.Formatter): - """File formatter with extra context but no colors""" + """File formatter with extra context but no colours""" def format(self, record): return super().format(record) @@ -39,7 +39,7 @@ def setup_logger(name="DuckHuntBot"): console_handler = logging.StreamHandler() console_handler.setLevel(logging.INFO) - console_formatter = DetailedColorFormatter( + console_formatter = DetailedColourFormatter( '%(asctime)s [%(levelname)s] %(name)s: %(message)s' ) console_handler.setFormatter(console_formatter) diff --git a/src/utils.py b/src/utils.py index 6d7b592..7050445 100644 --- a/src/utils.py +++ b/src/utils.py @@ -3,7 +3,83 @@ Utility functions for DuckHunt Bot """ import re -from typing import Optional +import json +import os +from typing import Optional, Tuple, List, Dict, Any + + +class MessageManager: + """Manages customizable IRC messages with color support""" + + def __init__(self, messages_file: str = "messages.json"): + self.messages_file = messages_file + self.messages = {} + self.load_messages() + + def load_messages(self): + """Load messages from JSON file""" + try: + if os.path.exists(self.messages_file): + with open(self.messages_file, 'r', encoding='utf-8') as f: + self.messages = json.load(f) + else: + # Fallback messages if file doesn't exist + self.messages = self._get_default_messages() + except Exception as e: + print(f"Error loading messages: {e}, using defaults") + self.messages = self._get_default_messages() + + def _get_default_messages(self) -> Dict[str, str]: + """Default fallback messages without colors""" + return { + "duck_spawn": "・゜゜・。。・゜゜\\_o< QUACK! A duck has appeared! Type !bang to shoot it!", + "duck_flies_away": "The duck flies away. ·°'`'°-.,¸¸.·°'`", + "bang_hit": "{nick} > *BANG* You shot the duck! [+{xp_gained} xp] [Total ducks: {ducks_shot}]", + "bang_miss": "{nick} > *BANG* You missed the duck!", + "bang_no_duck": "{nick} > *BANG* What did you shoot at? There is no duck in the area... [GUN CONFISCATED]", + "bang_no_ammo": "{nick} > *click* You're out of ammo! Use !reload", + "bang_not_armed": "{nick} > You are not armed.", + "reload_success": "{nick} > *click* Reloaded! [Ammo: {ammo}/{max_ammo}] [Chargers: {chargers}]", + "reload_already_loaded": "{nick} > Your gun is already loaded!", + "reload_no_chargers": "{nick} > You're out of chargers!", + "reload_not_armed": "{nick} > You are not armed.", + "shop_display": "DuckHunt Shop: {items} | You have {xp} XP", + "shop_item_format": "({id}) {name} - {price} XP", + "help_header": "DuckHunt Commands:", + "help_user_commands": "!bang - Shoot at ducks | !reload - Reload your gun | !shop - View the shop", + "help_help_command": "!duckhelp - Show this help", + "help_admin_commands": "Admin: !rearm | !disarm | !ignore | !unignore | !ducklaunch", + "admin_rearm_player": "[ADMIN] {target} has been rearmed by {admin}", + "admin_rearm_all": "[ADMIN] All players have been rearmed by {admin}", + "admin_disarm": "[ADMIN] {target} has been disarmed by {admin}", + "admin_ignore": "[ADMIN] {target} is now ignored by {admin}", + "admin_unignore": "[ADMIN] {target} is no longer ignored by {admin}", + "admin_ducklaunch": "[ADMIN] A duck has been launched by {admin}", + "admin_ducklaunch_not_enabled": "[ADMIN] This channel is not enabled for duckhunt", + "usage_rearm": "Usage: !rearm ", + "usage_disarm": "Usage: !disarm ", + "usage_ignore": "Usage: !ignore ", + "usage_unignore": "Usage: !unignore " + } + + def get(self, key: str, **kwargs) -> str: + """Get a formatted message by key""" + if key not in self.messages: + return f"[Missing message: {key}]" + + message = self.messages[key] + + # Format with provided variables + try: + return message.format(**kwargs) + except KeyError as e: + return f"[Message format error: {e}]" + except Exception as e: + return f"[Message error: {e}]" + + def reload(self): + """Reload messages from file""" + self.load_messages() class InputValidator: @@ -46,12 +122,17 @@ class InputValidator: return sanitized[:500] -def parse_message(line): +def parse_irc_message(line: str) -> Tuple[str, str, List[str], str]: """Parse IRC message format""" prefix = '' trailing = '' if line.startswith(':'): - prefix, line = line[1:].split(' ', 1) + if ' ' in line[1:]: + prefix, line = line[1:].split(' ', 1) + else: + # Handle malformed IRC line with no space after prefix + prefix = line[1:] + line = '' if ' :' in line: line, trailing = line.split(' :', 1) parts = line.split()