From 340772040d05045329ab5686053b3697e5f7bfc3 Mon Sep 17 00:00:00 2001 From: "arthur.valin" Date: Tue, 23 Jan 2024 21:02:04 +0100 Subject: [PATCH] Fix getBetAnswerDetail --- .../src/main/kotlin/allin/model/BetDetail.kt | 22 +++++++++--------- .../kotlin/allin/routing/BetDetailRouter.kt | 23 +++++++++---------- 2 files changed, 22 insertions(+), 23 deletions(-) diff --git a/Sources/src/main/kotlin/allin/model/BetDetail.kt b/Sources/src/main/kotlin/allin/model/BetDetail.kt index 387d43f..757b297 100644 --- a/Sources/src/main/kotlin/allin/model/BetDetail.kt +++ b/Sources/src/main/kotlin/allin/model/BetDetail.kt @@ -19,16 +19,16 @@ data class BetDetail( val userParticipation: Participation? // La participation du User current ) -fun getBetAnswerDetail(participations: List): List { - val groupedParticipations = participations.groupBy { it.answer } - val betAnswerDetails = mutableListOf() - for ((answer, participationList) in groupedParticipations) { - val totalStakes = participationList.sumBy { it.stake } - val totalParticipants = participationList.size - val highestStake = participationList.maxByOrNull { it.stake }?.stake ?: 0 - val odds = 1.0f - val betAnswerDetail = BetAnswerDetail(answer, totalStakes, totalParticipants, highestStake, odds) - betAnswerDetails.add(betAnswerDetail) +fun getBetAnswerDetail(bet: Bet, participations: List): List { + return bet.response.map { response -> + val responseParticipations = participations.filter { it.answer == response } + BetAnswerDetail( + response = response, + totalStakes = responseParticipations.sumOf { it.stake }, + totalParticipants = responseParticipations.size, + highestStake = responseParticipations.maxOfOrNull { it.stake } ?: 0, + odds = if (participations.isEmpty()) 1f else responseParticipations.size / participations.size.toFloat() + ) } - return betAnswerDetails + } diff --git a/Sources/src/main/kotlin/allin/routing/BetDetailRouter.kt b/Sources/src/main/kotlin/allin/routing/BetDetailRouter.kt index b093e67..8596f34 100644 --- a/Sources/src/main/kotlin/allin/routing/BetDetailRouter.kt +++ b/Sources/src/main/kotlin/allin/routing/BetDetailRouter.kt @@ -6,7 +6,6 @@ import allin.entities.ParticipationsEntity.getParticipationEntityFromUserId import allin.ext.hasToken import allin.ext.verifyUserFromToken import allin.model.BetDetail -import allin.model.Participation import allin.model.getBetAnswerDetail import io.ktor.http.* import io.ktor.server.application.* @@ -24,22 +23,22 @@ fun Application.BetDetailRouter() { val participations = getParticipationEntityFromBetId(id) val selectedBet = getBets().find { it.id == id } if (selectedBet != null) { - call.respond( - HttpStatusCode.Accepted, - BetDetail( - selectedBet, - getBetAnswerDetail(participations), - participations.toList(), - getParticipationEntityFromUserId(user.username,id).lastOrNull() - ) + call.respond( + HttpStatusCode.Accepted, + BetDetail( + selectedBet, + getBetAnswerDetail(selectedBet, participations), + participations.toList(), + getParticipationEntityFromUserId(user.username, id).lastOrNull() ) - } else { - call.respond(HttpStatusCode.NotFound, "Bet not found") - } + ) + } else { + call.respond(HttpStatusCode.NotFound, "Bet not found") } } } } } } +}