/today /yesterday ... view all stats of gamers

This commit is contained in:
vrubelroman 2025-11-16 22:49:56 +03:00
parent 14db5765a5
commit 5a8c705d54
2 changed files with 107 additions and 40 deletions

View file

@ -649,20 +649,23 @@ 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":
@ -675,9 +678,37 @@ class LichessBot:
await update.message.reply_text(t('unknown_period', lang))
return
# Format and send response
# 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)
# 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
has_any_activity = False
# Process each gamer sequentially
for i, gamer in enumerate(gamers):
username = gamer['username']
try:
# Inform user that the request is being processed
# Send message about processing this player
try:
await update.message.reply_text("⏳ Please wait a moment, processing the request…")
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 not data:
await update.message.reply_text(t('no_data', lang))
return
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)
self.counters.increment('last_year_1000')
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: {e}")
await update.message.reply_text(f"Error: {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"""

View file

@ -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 <b>{username}</b>...",
# Support
'support_message': (
"💬 <b>Support & Feedback</b>\n\n"