Merge branch 'master' into privateBet
continuous-integration/drone/pr Build was killed Details
continuous-integration/drone/push Build is passing Details

pull/18/head
Lucas EVARD 11 months ago
commit d000e900dc

@ -200,12 +200,27 @@ class PostgresBetDataSource(private val database: Database) : BetDataSource {
} }
) )
if (bet.type == BetType.CUSTOM) { val responses = if (bet.type == BetType.BINARY) {
bet.response.forEach { selected -> listOf(YES_VALUE, NO_VALUE)
} else {
bet.response
}
responses.forEach { response ->
database.betAnswerInfos.add(
BetAnswerInfoEntity {
this.betId = bet.id
this.response = response
this.totalStakes = 0
this.odds = 1f
}
)
if (bet.type == BetType.CUSTOM) {
database.responses.add( database.responses.add(
ResponseEntity { ResponseEntity {
this.betId = bet.id this.betId = bet.id
this.response = selected this.response = response
} }
) )
} }

@ -34,8 +34,9 @@ class PostgresParticipationDataSource(private val database: Database) : Particip
if (it.response == participation.answer) { if (it.response == participation.answer) {
it.totalStakes += participation.stake it.totalStakes += participation.stake
} }
val probability = it.totalStakes / betInfo.totalStakes.toFloat()
it.odds = 1 / probability val probability = (it.totalStakes / betInfo.totalStakes.toFloat())
it.odds = if (probability == 0f) 1f else 1 / probability
it.flushChanges() it.flushChanges()
} }
} }

@ -28,7 +28,7 @@ interface ParticipationEntity : Entity<ParticipationEntity> {
} }
object ParticipationsEntity : Table<ParticipationEntity>("participation") { object ParticipationsEntity : Table<ParticipationEntity>("participation") {
val id = varchar("id").primaryKey() val id = varchar("id").primaryKey().bindTo { it.id }
val betId = varchar("bet").references(BetsEntity) { it.bet } val betId = varchar("bet").references(BetsEntity) { it.bet }
val username = varchar("username").bindTo { it.username } val username = varchar("username").bindTo { it.username }
val answer = varchar("answer").bindTo { it.answer } val answer = varchar("answer").bindTo { it.answer }

@ -1,6 +1,7 @@
package allin.routing package allin.routing
import allin.dataSource import allin.dataSource
import allin.dto.UserDTO
import allin.ext.hasToken import allin.ext.hasToken
import allin.ext.verifyUserFromToken import allin.ext.verifyUserFromToken
import allin.model.* import allin.model.*
@ -21,6 +22,7 @@ val tokenManagerBet = AppConfig.tokenManager
fun Application.betRouter() { fun Application.betRouter() {
val userDataSource = this.dataSource.userDataSource val userDataSource = this.dataSource.userDataSource
val betDataSource = this.dataSource.betDataSource val betDataSource = this.dataSource.betDataSource
val participationDataSource = this.dataSource.participationDataSource
val logManager = AppConfig.logManager val logManager = AppConfig.logManager
routing { routing {
@ -352,11 +354,38 @@ fun Application.betRouter() {
logManager.log("Routing", "UNAUTHORIZED /bets/confirm/{id}") logManager.log("Routing", "UNAUTHORIZED /bets/confirm/{id}")
call.respond(HttpStatusCode.Unauthorized) call.respond(HttpStatusCode.Unauthorized)
} }
} }
} }
} }
post("/bets/users", {
description = "gets all userDTO of a bet"
request {
headerParameter<JWTPrincipal>("JWT token of the logged user")
pathParameter<String>("Id of the desired bet")
body<List<UserDTO>> {
description = "UserDTO of the bet"
}
}
response {
HttpStatusCode.OK to {
description = "The final answer has been set"
}
HttpStatusCode.Unauthorized to {
description = "The user is not the creator of the bet"
}
}
}) {
logManager.log("Routing","POST /bets/users")
hasToken { principal ->
verifyUserFromToken(userDataSource, principal) { user, _ ->
val id = call.receive<Map<String, String>>()["id"] ?: ""
val participations = participationDataSource.getParticipationFromBetId(id)
val users = participations.map { userDataSource.getUserByUsername(it.username).first }.toSet().take(4).toList()
call.respond(users)
}
}
} }
} }
}
} }
Loading…
Cancel
Save