/today /yesterday ... view all stats of gamers
This commit is contained in:
parent
14db5765a5
commit
5a8c705d54
2 changed files with 107 additions and 40 deletions
|
|
@ -649,35 +649,66 @@ class LichessBot:
|
||||||
await query.edit_message_text(t('gamer_not_found', lang))
|
await query.edit_message_text(t('gamer_not_found', lang))
|
||||||
|
|
||||||
async def get_stats(self, update: Update, context: ContextTypes.DEFAULT_TYPE, period: str):
|
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
|
user_id = update.effective_user.id
|
||||||
|
|
||||||
# Get active gamer for this user
|
# Get all gamers for this user
|
||||||
active_gamer = self.db.get_user_active_gamer(user_id)
|
gamers = self.db.get_user_gamers(user_id)
|
||||||
|
|
||||||
lang = self.get_user_language_from_update(update)
|
lang = self.get_user_language_from_update(update)
|
||||||
if not active_gamer:
|
if not gamers:
|
||||||
await update.message.reply_text(
|
await update.message.reply_text(
|
||||||
t('no_active_gamer', lang)
|
t('no_gamers', lang)
|
||||||
)
|
)
|
||||||
return
|
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 no activity for any player
|
||||||
if period == "today":
|
if not has_any_activity:
|
||||||
data = await self.lichess_api.get_today_stats(username)
|
await update.message.reply_text(t('no_activity', lang))
|
||||||
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)
|
|
||||||
|
|
||||||
# Increment counter for the period command
|
# Increment counter for the period command
|
||||||
if period == "today":
|
if period == "today":
|
||||||
|
|
@ -707,35 +738,67 @@ class LichessBot:
|
||||||
self.counters.increment('support')
|
self.counters.increment('support')
|
||||||
|
|
||||||
async def last_year_or_1000games(self, update: Update, context: ContextTypes.DEFAULT_TYPE):
|
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
|
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)
|
lang = self.get_user_language_from_update(update)
|
||||||
if not active_gamer:
|
if not gamers:
|
||||||
await update.message.reply_text(
|
await update.message.reply_text(
|
||||||
t('no_active_gamer', lang)
|
t('no_gamers', lang)
|
||||||
)
|
)
|
||||||
return
|
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)
|
now_ms = int(time.time() * 1000)
|
||||||
year_ms = 365 * 24 * 3600 * 1000
|
year_ms = 365 * 24 * 3600 * 1000
|
||||||
since_ms = now_ms - year_ms
|
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:
|
try:
|
||||||
await update.message.reply_text("⏳ Please wait a moment, processing the request…")
|
# Send message about processing this player
|
||||||
except Exception:
|
try:
|
||||||
pass
|
await update.message.reply_text(t('last_year_1000_player_processing', lang, username=username), parse_mode='HTML')
|
||||||
data = await self.lichess_api.get_games_period(username, since_ms, now_ms, rated_only=True)
|
except Exception:
|
||||||
if not data:
|
pass
|
||||||
await update.message.reply_text(t('no_data', lang))
|
|
||||||
return
|
# Get data for this player
|
||||||
text = StatsFormatter.format_last_year_or_1000(data, username, lang)
|
data = await self.lichess_api.get_games_period(username, since_ms, now_ms, rated_only=True)
|
||||||
await update.message.reply_text(text)
|
|
||||||
self.counters.increment('last_year_1000')
|
if data:
|
||||||
except Exception as e:
|
# Check if there's activity (games_count > 0)
|
||||||
logger.error(f"/lastYear_or_1000games error: {e}")
|
games_count = data.get('games_count', 0)
|
||||||
await update.message.reply_text(f"Error: {e}")
|
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):
|
async def setperiod(self, update: Update, context: ContextTypes.DEFAULT_TYPE):
|
||||||
"""Set period command"""
|
"""Set period command"""
|
||||||
|
|
|
||||||
|
|
@ -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",
|
'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",
|
'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
|
||||||
'support_message': (
|
'support_message': (
|
||||||
"💬 <b>Support & Feedback</b>\n\n"
|
"💬 <b>Support & Feedback</b>\n\n"
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue