Add a 'final' filter to registration page; coherence check on server for final and skipping

This commit is contained in:
Claude Brisson
2024-02-28 19:39:03 +01:00
parent e692dbeabb
commit 024a8e8a5e
7 changed files with 58 additions and 13 deletions

View File

@@ -34,7 +34,18 @@ object PlayerHandler: PairgothApiHandler {
val player = tournament.players[id] ?: badRequest("invalid player id")
val payload = getObjectPayload(request)
val updated = Player.fromJson(payload, player)
tournament.players[updated.id] = updated
// check coherence
if (player.final && !updated.final && tournament.pairedPlayers().contains(updated.id)) {
badRequest("player is playing")
}
val leavingRounds = updated.skip.toSet().minus(player.skip.toSet())
leavingRounds.forEach { round ->
val playing = tournament.games(round).values.flatMap { listOf(it.black, it.white) }
if (playing.contains(id)) {
throw badRequest("player is playing in round #$round")
}
}
tournament.players[id] = updated
tournament.dispatchEvent(PlayerUpdated, player.toJson())
return Json.Object("success" to true)
}
@@ -42,6 +53,11 @@ object PlayerHandler: PairgothApiHandler {
override fun delete(request: HttpServletRequest, response: HttpServletResponse): Json {
val tournament = getTournament(request)
val id = getSubSelector(request)?.toIntOrNull() ?: badRequest("missing or invalid player selector")
// check coherence
val player = tournament.players[id] ?: badRequest("invalid player id")
if (player.final && tournament.pairedPlayers().contains(id)) {
badRequest("player is playing")
}
tournament.players.remove(id) ?: badRequest("invalid player id")
tournament.dispatchEvent(PlayerDeleted, Json.Object("id" to id))
return Json.Object("success" to true)

View File

@@ -114,6 +114,8 @@ sealed class Tournament <P: Pairable>(
}
return changed
}
fun pairedPlayers() = games.flatMap { it.values }.flatMap { listOf(it.black, it.white) }.toSet()
}
// standard tournament of individuals