|
|
@@ -157,14 +157,25 @@ log.warn("游戏能量发放失败: memberId={}, gameCode={}, error={}",
|
|
|
}
|
|
|
|
|
|
public List<Map<String, Object>> getLeaderboard(String gameCode, Integer limit) {
|
|
|
+ return getLeaderboard(gameCode, limit, null);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取游戏排行榜(SC-P-86 数据仅本家庭可见)。
|
|
|
+ * 3 参入口:携带会话家庭 familyId,按家庭过滤 + 进度字段。
|
|
|
+ * 2 参入口(兼容):familyId=null → 不做家庭过滤,仅补充进度(SC-P-86 进度维度)。
|
|
|
+ */
|
|
|
+ public List<Map<String, Object>> getLeaderboard(String gameCode, Integer limit, Long familyId) {
|
|
|
if (limit == null || limit < 1) limit = 10;
|
|
|
- // 优先从 Redis 读取排行榜
|
|
|
- if (gameCode != null && !gameCode.isEmpty()) {
|
|
|
+ List<Map<String, Object>> rows = null;
|
|
|
+
|
|
|
+ // 家庭隔离:Redis 全局榜不区分家庭,携带 familyId 时跳过 Redis,走 DB + 服务端过滤
|
|
|
+ if (familyId == null && gameCode != null && !gameCode.isEmpty()) {
|
|
|
try {
|
|
|
java.util.Set<Object> topMembers = redisTemplate.opsForZSet()
|
|
|
.reverseRange(GAME_LEADERBOARD_KEY_PREFIX + gameCode, 0, limit - 1);
|
|
|
if (topMembers != null && !topMembers.isEmpty()) {
|
|
|
- List<Map<String, Object>> result = new java.util.ArrayList<>();
|
|
|
+ rows = new java.util.ArrayList<>();
|
|
|
int rank = 1;
|
|
|
for (Object member : topMembers) {
|
|
|
Long memberId = Long.valueOf(member.toString());
|
|
|
@@ -174,15 +185,52 @@ log.warn("游戏能量发放失败: memberId={}, gameCode={}, error={}",
|
|
|
item.put("memberId", memberId);
|
|
|
item.put("score", score != null ? score.intValue() : 0);
|
|
|
item.put("rank", rank);
|
|
|
- result.add(item);
|
|
|
+ rows.add(item);
|
|
|
rank++;
|
|
|
}
|
|
|
- return result;
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
log.warn("Redis 排行榜读取失败,回退数据库: {}", e.getMessage());
|
|
|
}
|
|
|
}
|
|
|
- return gameRecordMapper.selectLeaderboard(gameCode, limit);
|
|
|
+
|
|
|
+ if (rows == null) {
|
|
|
+ rows = gameRecordMapper.selectLeaderboard(gameCode, limit);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 家庭隔离(SC-P-86「数据仅本家庭可见」):按会话家庭过滤跨家庭行
|
|
|
+ if (familyId != null && rows != null) {
|
|
|
+ List<Map<String, Object>> own = new java.util.ArrayList<>();
|
|
|
+ for (Map<String, Object> item : rows) {
|
|
|
+ // Mapper SQL 返回 family_id (下划线);兼容前端驼峰 familyId
|
|
|
+ Object rowFamilyId = item.get("family_id") != null ? item.get("family_id") : item.get("familyId");
|
|
|
+ if (rowFamilyId == null || ((Number) rowFamilyId).longValue() == familyId.longValue()) {
|
|
|
+ own.add(item);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ rows = own;
|
|
|
+ }
|
|
|
+
|
|
|
+ return enrichProgress(rows);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 为排行项补充进度维度(SC-P-86「看榜 → 排名/进度」):
|
|
|
+ * - totalMembers:榜单总人数
|
|
|
+ * - progress:完成度(排名 / 总人数 × 100,保留两位小数)
|
|
|
+ */
|
|
|
+ private List<Map<String, Object>> enrichProgress(List<Map<String, Object>> rows) {
|
|
|
+ if (rows == null) return rows;
|
|
|
+ int totalMembers = rows.size();
|
|
|
+ for (int i = 0; i < rows.size(); i++) {
|
|
|
+ Map<String, Object> item = rows.get(i);
|
|
|
+ int rank = 1;
|
|
|
+ if (item.get("rank") instanceof Number) {
|
|
|
+ rank = ((Number) item.get("rank")).intValue();
|
|
|
+ }
|
|
|
+ item.put("totalMembers", totalMembers);
|
|
|
+ item.put("progress", totalMembers > 0 ? Math.round(rank * 10000.0 / totalMembers) / 100.0 : 0);
|
|
|
+ }
|
|
|
+ return rows;
|
|
|
}
|
|
|
}
|