Skip to content

Commit b7160c3

Browse files
authored
랭킹 상위 0%가 나오는 이슈를 수정한다. (#129)
1 parent c70c6c1 commit b7160c3

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

domain/src/main/kotlin/com/gotchai/domain/user/adapter/in/UserQueryService.kt

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,12 @@ class UserQueryService(
1616
) : UserQueryUseCase {
1717
override fun getUserRanking(userId: Long): GetUserRankingResult {
1818
val profile = profileQueryPort.getProfileByUserId(userId) ?: throw ProfileNotFoundException()
19-
val allHistories = examHistoryQueryPort.getAllExamHistoriesWithQuizIds()
19+
val allSolvedHistories =
20+
examHistoryQueryPort
21+
.getAllExamHistoriesWithQuizIds()
22+
.filter { it.isSolved }
2023

21-
val rating = calculateUserRating(userId, allHistories)
24+
val rating = calculateUserRating(userId, allSolvedHistories)
2225
return GetUserRankingResult.of(profile, rating)
2326
}
2427

@@ -38,10 +41,17 @@ class UserQueryService(
3841
}
3942

4043
val userScore = userScores[userId] ?: return 100
41-
if (userScores.size == 1) return 0
44+
if (userScores.size == 1) return 1
4245

4346
val usersWithHigherScore = userScores.values.count { it > userScore }
44-
val rating = (usersWithHigherScore.toDouble() / userScores.size.toDouble()) * 100
45-
return ceil(rating).toInt()
47+
48+
return when {
49+
usersWithHigherScore == 0 && userScores.values.all { it == userScore } -> 50
50+
usersWithHigherScore == 0 -> 1
51+
else -> {
52+
val rating = (usersWithHigherScore.toDouble() / userScores.size.toDouble()) * 100
53+
ceil(rating).toInt().coerceAtLeast(1) // 최소 1% 보장
54+
}
55+
}
4656
}
4757
}

0 commit comments

Comments
 (0)