import json from web.database import query_db class DiscoveryService: TONE_LABELS = { 'positive': '高光', 'negative': '低谷', 'fun': '趣味', } MEDAL_LABELS = { 'gold': '金牌', 'silver': '银牌', 'bronze': '铜牌', } @staticmethod def _identity_map(steam_ids): steam_ids = sorted({str(value) for value in steam_ids if value}) if not steam_ids: return {} placeholders = ','.join('?' for _ in steam_ids) rows = query_db( 'l2', f""" SELECT steam_id_64, username, avatar_url FROM dim_players WHERE steam_id_64 IN ({placeholders}) """, steam_ids, ) return {str(row['steam_id_64']): dict(row) for row in rows} @staticmethod def get_insights(tone=None): args = [] where = '' if tone in DiscoveryService.TONE_LABELS: where = 'WHERE tone = ?' args.append(tone) rows = query_db( 'l3', f""" SELECT * FROM dm_discovery_insights {where} ORDER BY display_order, insight_key """, args, ) identities = DiscoveryService._identity_map( [row['steam_id_64'] for row in rows] ) result = [] for row in rows: item = dict(row) item['identity'] = identities.get(str(item['steam_id_64']), {}) item['tone_label'] = DiscoveryService.TONE_LABELS.get( item['tone'], item['tone'], ) try: item['evidence'] = json.loads(item['evidence_json'] or '{}') except json.JSONDecodeError: item['evidence'] = {} result.append(item) return result @staticmethod def get_medals(dimension_type=None): args = [] where = '' if dimension_type in {'map', 'elo'}: where = 'WHERE dimension_type = ?' args.append(dimension_type) rows = query_db( 'l3', f""" SELECT * FROM dm_performance_medals {where} ORDER BY CASE dimension_type WHEN 'map' THEN 1 ELSE 2 END, dimension_key, medal_rank """, args, ) identities = DiscoveryService._identity_map( [row['steam_id_64'] for row in rows] ) grouped = [] current_key = None current = None for row in rows: item = dict(row) item['identity'] = identities.get(str(item['steam_id_64']), {}) item['medal_label'] = DiscoveryService.MEDAL_LABELS.get( item['medal_tier'], item['medal_tier'], ) key = (item['dimension_type'], item['dimension_key']) if key != current_key: current = { 'dimension_type': item['dimension_type'], 'dimension_key': item['dimension_key'], 'dimension_label': item['dimension_label'], 'medals': [], } grouped.append(current) current_key = key current['medals'].append(item) return grouped @staticmethod def get_medal_leaderboard(): rows = query_db( 'l3', """ SELECT steam_id_64, COUNT(*) AS medals, SUM(CASE WHEN medal_tier = 'gold' THEN 1 ELSE 0 END) AS gold, SUM(CASE WHEN medal_tier = 'silver' THEN 1 ELSE 0 END) AS silver, SUM(CASE WHEN medal_tier = 'bronze' THEN 1 ELSE 0 END) AS bronze FROM dm_performance_medals GROUP BY steam_id_64 ORDER BY gold DESC, silver DESC, bronze DESC """, ) identities = DiscoveryService._identity_map( [row['steam_id_64'] for row in rows] ) result = [] for row in rows: item = dict(row) item['identity'] = identities.get(str(item['steam_id_64']), {}) result.append(item) return result @staticmethod def get_player_medals(steam_id): rows = query_db( 'l3', """ SELECT * FROM dm_performance_medals WHERE steam_id_64 = ? ORDER BY medal_rank, dimension_type, dimension_key """, [steam_id], ) result = [] for row in rows: item = dict(row) item['medal_label'] = DiscoveryService.MEDAL_LABELS.get( item['medal_tier'], item['medal_tier'], ) result.append(item) return result