diff --git a/LichessClientTG_bot/bot.py b/LichessClientTG_bot/bot.py index b27611f..c5570f8 100644 --- a/LichessClientTG_bot/bot.py +++ b/LichessClientTG_bot/bot.py @@ -649,35 +649,66 @@ class LichessBot: await query.edit_message_text(t('gamer_not_found', lang)) async def get_stats(self, update: Update, context: ContextTypes.DEFAULT_TYPE, period: str): - """Get statistics for a period""" + """Get statistics for a period - shows stats for all players with activity""" user_id = update.effective_user.id - # Get active gamer for this user - active_gamer = self.db.get_user_active_gamer(user_id) + # Get all gamers for this user + gamers = self.db.get_user_gamers(user_id) lang = self.get_user_language_from_update(update) - if not active_gamer: + if not gamers: await update.message.reply_text( - t('no_active_gamer', lang) + t('no_gamers', lang) ) return - username = active_gamer['username'] + # Process each gamer + has_any_activity = False + for i, gamer in enumerate(gamers): + username = gamer['username'] + + # Get stats based on period + if period == "today": + data = await self.lichess_api.get_today_stats(username) + elif period == "yesterday": + data = await self.lichess_api.get_yesterday_stats(username) + elif period == "week": + data = await self.lichess_api.get_week_stats(username) + else: + await update.message.reply_text(t('unknown_period', lang)) + return + + # Check if there's activity + has_activity = False + if data and data.get('data'): + api_data = data.get('data', {}) + tasks = api_data.get('tasks', {}) + games = api_data.get('games', {}) + + # Check for puzzles activity + if tasks and tasks.get('total', 0) > 0: + has_activity = True + + # Check for games activity + if games: + for game_type, game_data in games.items(): + if game_data and game_data.get('games_played', 0) > 0: + has_activity = True + break + + # Only send response if there's activity + if has_activity: + formatted_response = StatsFormatter.format_stats_response(data, username, period, lang) + await update.message.reply_text(formatted_response) + has_any_activity = True + + # Add delay between requests to avoid rate limiting + if i < len(gamers) - 1: + await asyncio.sleep(1.0) - # Get stats based on period - if period == "today": - data = await self.lichess_api.get_today_stats(username) - elif period == "yesterday": - data = await self.lichess_api.get_yesterday_stats(username) - elif period == "week": - data = await self.lichess_api.get_week_stats(username) - else: - await update.message.reply_text(t('unknown_period', lang)) - return - - # Format and send response - formatted_response = StatsFormatter.format_stats_response(data, username, period, lang) - await update.message.reply_text(formatted_response) + # If no activity for any player + if not has_any_activity: + await update.message.reply_text(t('no_activity', lang)) # Increment counter for the period command if period == "today": @@ -707,35 +738,67 @@ class LichessBot: self.counters.increment('support') async def last_year_or_1000games(self, update: Update, context: ContextTypes.DEFAULT_TYPE): - """Get last year stats or last 1000 rated games, whichever limits first""" + """Get last year stats or last 1000 rated games for all players with activity""" user_id = update.effective_user.id - active_gamer = self.db.get_user_active_gamer(user_id) + + # Get all gamers for this user + gamers = self.db.get_user_gamers(user_id) + lang = self.get_user_language_from_update(update) - if not active_gamer: + if not gamers: await update.message.reply_text( - t('no_active_gamer', lang) + t('no_gamers', lang) ) return - username = active_gamer['username'] + + # Send initial message about processing + try: + await update.message.reply_text(t('last_year_1000_processing', lang), parse_mode='HTML') + except Exception: + pass + now_ms = int(time.time() * 1000) year_ms = 365 * 24 * 3600 * 1000 since_ms = now_ms - year_ms - try: - # Inform user that the request is being processed + + has_any_activity = False + + # Process each gamer sequentially + for i, gamer in enumerate(gamers): + username = gamer['username'] + try: - await update.message.reply_text("⏳ Please wait a moment, processing the request…") - except Exception: - pass - data = await self.lichess_api.get_games_period(username, since_ms, now_ms, rated_only=True) - if not data: - await update.message.reply_text(t('no_data', lang)) - return - text = StatsFormatter.format_last_year_or_1000(data, username, lang) - await update.message.reply_text(text) - self.counters.increment('last_year_1000') - except Exception as e: - logger.error(f"/lastYear_or_1000games error: {e}") - await update.message.reply_text(f"Error: {e}") + # Send message about processing this player + try: + await update.message.reply_text(t('last_year_1000_player_processing', lang, username=username), parse_mode='HTML') + except Exception: + pass + + # Get data for this player + data = await self.lichess_api.get_games_period(username, since_ms, now_ms, rated_only=True) + + if data: + # Check if there's activity (games_count > 0) + games_count = data.get('games_count', 0) + if games_count > 0: + # Format and send immediately + text = StatsFormatter.format_last_year_or_1000(data, username, lang) + await update.message.reply_text(text) + has_any_activity = True + + # Wait 1 second before next request (except after the last one) + if i < len(gamers) - 1: + await asyncio.sleep(1.0) + + except Exception as e: + logger.error(f"/lastYear_or_1000games error for {username}: {e}") + await update.message.reply_text(f"Error for {username}: {e}") + + # If no activity for any player + if not has_any_activity: + await update.message.reply_text(t('no_activity', lang)) + + self.counters.increment('last_year_1000') async def setperiod(self, update: Update, context: ContextTypes.DEFAULT_TYPE): """Set period command""" diff --git a/LichessClientTG_bot/i18n.py b/LichessClientTG_bot/i18n.py index 9db8f08..8915d0b 100644 --- a/LichessClientTG_bot/i18n.py +++ b/LichessClientTG_bot/i18n.py @@ -95,6 +95,10 @@ TRANSLATIONS = { 'period_games_section': "{emoji} {game_type} — {games_count} games • {rating_change}\nRating: {rating}\n✅ Wins: {wins}\n❌ Losses: {losses}\n🤝 Draws: {draws}\n\n", 'no_activity': "📭 No activity for this period", + # Last year or 1000 games + 'last_year_1000_processing': "⏳ Processing request... This may take a while as requests are very slow.", + 'last_year_1000_player_processing': "🔄 Requesting data for player {username}...", + # Support 'support_message': ( "💬 Support & Feedback\n\n"