From 76aa60f3c698ae8a36995308307afd61e8ee9288 Mon Sep 17 00:00:00 2001 From: Claude Brisson Date: Mon, 5 Jun 2023 07:43:07 +0200 Subject: [PATCH] Add manual pairing API call --- .../org/jeudego/pairgoth/api/PairingHandler.kt | 15 +++++++++++++++ .../kotlin/org/jeudego/pairgoth/model/Defs.kt | 6 +++++- .../kotlin/org/jeudego/pairgoth/model/Game.kt | 6 +++--- .../main/kotlin/org/jeudego/pairgoth/web/Event.kt | 1 + 4 files changed, 24 insertions(+), 4 deletions(-) diff --git a/webapp/src/main/kotlin/org/jeudego/pairgoth/api/PairingHandler.kt b/webapp/src/main/kotlin/org/jeudego/pairgoth/api/PairingHandler.kt index 3c18877..a4fb956 100644 --- a/webapp/src/main/kotlin/org/jeudego/pairgoth/api/PairingHandler.kt +++ b/webapp/src/main/kotlin/org/jeudego/pairgoth/api/PairingHandler.kt @@ -4,6 +4,7 @@ import com.republicate.kson.Json import com.republicate.kson.toJsonArray import org.jeudego.pairgoth.api.ApiHandler.Companion.badRequest import org.jeudego.pairgoth.model.Pairing +import org.jeudego.pairgoth.model.getID import org.jeudego.pairgoth.model.toID import org.jeudego.pairgoth.model.toJson import org.jeudego.pairgoth.web.Event @@ -49,6 +50,20 @@ object PairingHandler: PairgothApiHandler { return ret } + override fun put(request: HttpServletRequest): Json { + val tournament = getTournament(request) + val round = getSubSelector(request)?.toIntOrNull() ?: badRequest("invalid round number") + // only allow last round (if players have not been paired in the last round, it *may* be possible to be more laxist...) + if (round != tournament.lastRound()) badRequest("cannot edit pairings in other rounds but the last") + val payload = getObjectPayload(request) + val game = tournament.games(round)[payload.getInt("id")] ?: badRequest("invalid game id") + game.black = payload.getID("b") ?: badRequest("missing black player id") + game.white = payload.getID("w") ?: badRequest("missing white player id") + if (payload.containsKey("h")) game.handicap = payload.getString("h")?.toIntOrNull() ?: badRequest("invalid handicap") + Event.dispatch(gameUpdated, Json.Object("tournament" to tournament.id, "round" to round, "data" to game.toJson())) + return Json.Object("success" to true) + } + override fun delete(request: HttpServletRequest): Json { val tournament = getTournament(request) val round = getSubSelector(request)?.toIntOrNull() ?: badRequest("invalid round number") diff --git a/webapp/src/main/kotlin/org/jeudego/pairgoth/model/Defs.kt b/webapp/src/main/kotlin/org/jeudego/pairgoth/model/Defs.kt index 32ecadf..2ac170a 100644 --- a/webapp/src/main/kotlin/org/jeudego/pairgoth/model/Defs.kt +++ b/webapp/src/main/kotlin/org/jeudego/pairgoth/model/Defs.kt @@ -1,6 +1,10 @@ package org.jeudego.pairgoth.model +import com.republicate.kson.Json + typealias ID = Int fun String.toID() = toInt() -fun Number.toID() = toInt() \ No newline at end of file +fun Number.toID() = toInt() +fun Json.Object.getID(key: String) = getInt(key) +fun Json.Array.getID(index: Int) = getInt(index) \ No newline at end of file diff --git a/webapp/src/main/kotlin/org/jeudego/pairgoth/model/Game.kt b/webapp/src/main/kotlin/org/jeudego/pairgoth/model/Game.kt index 288652a..13920d5 100644 --- a/webapp/src/main/kotlin/org/jeudego/pairgoth/model/Game.kt +++ b/webapp/src/main/kotlin/org/jeudego/pairgoth/model/Game.kt @@ -6,9 +6,9 @@ import java.util.* data class Game( val id: ID, - val white: ID, - val black: ID, - val handicap: Int = 0, + var white: ID, + var black: ID, + var handicap: Int = 0, var result: Result = UNKNOWN ) { enum class Result(val symbol: Char) { diff --git a/webapp/src/main/kotlin/org/jeudego/pairgoth/web/Event.kt b/webapp/src/main/kotlin/org/jeudego/pairgoth/web/Event.kt index 2949aaf..a250c67 100644 --- a/webapp/src/main/kotlin/org/jeudego/pairgoth/web/Event.kt +++ b/webapp/src/main/kotlin/org/jeudego/pairgoth/web/Event.kt @@ -15,6 +15,7 @@ enum class Event { teamDeleted, gamesAdded, gamesDeleted, + gameUpdated, resultUpdated, ;