From ecd973dd227bd1be5df47848ff942cdf4ed8d556 Mon Sep 17 00:00:00 2001 From: Claude Brisson Date: Sun, 28 Jul 2024 00:07:01 +0200 Subject: [PATCH] Manual tables handling --- .../org/jeudego/pairgoth/api/PairingHandler.kt | 1 + .../main/kotlin/org/jeudego/pairgoth/model/Game.kt | 10 +++++++--- .../kotlin/org/jeudego/pairgoth/model/Tournament.kt | 13 ++++++++++--- 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/api-webapp/src/main/kotlin/org/jeudego/pairgoth/api/PairingHandler.kt b/api-webapp/src/main/kotlin/org/jeudego/pairgoth/api/PairingHandler.kt index 1e9eef1..6e926c3 100644 --- a/api-webapp/src/main/kotlin/org/jeudego/pairgoth/api/PairingHandler.kt +++ b/api-webapp/src/main/kotlin/org/jeudego/pairgoth/api/PairingHandler.kt @@ -103,6 +103,7 @@ object PairingHandler: PairgothApiHandler { if (payload.containsKey("h")) game.handicap = payload.getString("h")?.toIntOrNull() ?: badRequest("invalid handicap") if (payload.containsKey("t")) { game.table = payload.getString("t")?.toIntOrNull() ?: badRequest("invalid table number") + game.forcedTable = true } tournament.dispatchEvent(GameUpdated, request, Json.Object("round" to round, "game" to game.toJson())) if (game.table != previousTable) { diff --git a/api-webapp/src/main/kotlin/org/jeudego/pairgoth/model/Game.kt b/api-webapp/src/main/kotlin/org/jeudego/pairgoth/model/Game.kt index 252f7cb..7d6f618 100644 --- a/api-webapp/src/main/kotlin/org/jeudego/pairgoth/model/Game.kt +++ b/api-webapp/src/main/kotlin/org/jeudego/pairgoth/model/Game.kt @@ -11,8 +11,10 @@ data class Game( var black: ID, var handicap: Int = 0, var result: Result = UNKNOWN, - var drawnUpDown: Int = 0 // counted for white (black gets the opposite) + var drawnUpDown: Int = 0, // counted for white (black gets the opposite) + var forcedTable: Boolean = false ) { + companion object {} enum class Result(val symbol: Char) { UNKNOWN('?'), @@ -43,7 +45,8 @@ fun Game.toJson() = Json.Object( "b" to black, "h" to handicap, "r" to "${result.symbol}", - "dd" to drawnUpDown + "dd" to drawnUpDown, + "ft" to forcedTable ) fun Game.Companion.fromJson(json: Json.Object) = Game( @@ -53,5 +56,6 @@ fun Game.Companion.fromJson(json: Json.Object) = Game( black = json.getID("b") ?: throw Error("missing black player"), handicap = json.getInt("h") ?: 0, result = json.getChar("r")?.let { Game.Result.fromSymbol(it) } ?: UNKNOWN, - drawnUpDown = json.getInt("dd") ?: 0 + drawnUpDown = json.getInt("dd") ?: 0, + forcedTable = json.getBoolean("ft") ?: false ) diff --git a/api-webapp/src/main/kotlin/org/jeudego/pairgoth/model/Tournament.kt b/api-webapp/src/main/kotlin/org/jeudego/pairgoth/model/Tournament.kt index a95dc15..5ea4daf 100644 --- a/api-webapp/src/main/kotlin/org/jeudego/pairgoth/model/Tournament.kt +++ b/api-webapp/src/main/kotlin/org/jeudego/pairgoth/model/Tournament.kt @@ -130,11 +130,18 @@ sealed class Tournament ( var changed = false var nextTable = 1 val excluded = excludedTables(round) - games(round).values.filter{ game -> pivot?.let { pivot.id != game.id } ?: true }.sortedBy(orderBY).forEach { game -> - while (excluded.contains(nextTable)) ++nextTable + val forcedTablesGames = games(round).values.filter { game -> game.forcedTable && (pivot == null || game != pivot && game.table != pivot.table) } + val forcedTables = forcedTablesGames.map { game -> game.table }.toSet() + val excludedAndForced = excluded union forcedTables + games(round).values + .filter { game -> pivot?.let { pivot.id != game.id } ?: true } + .filter { game -> !forcedTablesGames.contains(game) } + .sortedBy(orderBY) + .forEach { game -> + while (excludedAndForced.contains(nextTable)) ++nextTable if (pivot != null && nextTable == pivot.table) { ++nextTable - while (excluded.contains(nextTable)) ++nextTable + while (excludedAndForced.contains(nextTable)) ++nextTable } if (game.table != 0) { changed = changed || game.table != nextTable