feat: Add recent performance stability stats (matches/days) to player profile

This commit is contained in:
2026-01-28 15:11:31 +08:00
parent a5a9016b7f
commit f110ae52f0
3 changed files with 131 additions and 1 deletions

View File

@@ -632,6 +632,72 @@ class StatsService:
"""
return query_db('l2', sql, [steam_id, limit])
@staticmethod
def get_recent_performance_stats(steam_id):
"""
Calculates Avg Rating and Rating Variance for:
- Last 5, 10, 15 matches
- Last 5, 10, 15 days
"""
import numpy as np
from datetime import datetime, timedelta
# Fetch all match ratings with timestamps
sql = """
SELECT m.start_time, mp.rating
FROM fact_match_players mp
JOIN fact_matches m ON mp.match_id = m.match_id
WHERE mp.steam_id_64 = ?
ORDER BY m.start_time DESC
"""
rows = query_db('l2', sql, [steam_id])
if not rows:
return {}
# Convert to list of dicts
matches = [{'time': r['start_time'], 'rating': r['rating'] or 0} for r in rows]
stats = {}
# 1. Recent N Matches
for n in [5, 10, 15]:
subset = matches[:n]
if not subset:
stats[f'last_{n}_matches'] = {'avg': 0, 'var': 0, 'count': 0}
continue
ratings = [m['rating'] for m in subset]
stats[f'last_{n}_matches'] = {
'avg': np.mean(ratings),
'var': np.var(ratings),
'count': len(ratings)
}
# 2. Recent N Days
# Use server time or max match time? usually server time 'now' is fine if data is fresh.
# But if data is old, 'last 5 days' might be empty.
# User asked for "recent 5/10/15 days", implying calendar days from now.
import time
now = time.time()
for d in [5, 10, 15]:
cutoff = now - (d * 24 * 3600)
subset = [m for m in matches if m['time'] >= cutoff]
if not subset:
stats[f'last_{d}_days'] = {'avg': 0, 'var': 0, 'count': 0}
continue
ratings = [m['rating'] for m in subset]
stats[f'last_{d}_days'] = {
'avg': np.mean(ratings),
'var': np.var(ratings),
'count': len(ratings)
}
return stats
@staticmethod
def get_roster_stats_distribution(target_steam_id):
"""