From a81ed5377cbfd632ff27c135e7de0101f9e5f1e0 Mon Sep 17 00:00:00 2001 From: Claude Brisson Date: Thu, 27 Feb 2025 18:28:42 +0100 Subject: [PATCH] [teams] bugfix and code clanup --- .../jeudego/pairgoth/api/PairingHandler.kt | 6 ++- .../org/jeudego/pairgoth/model/Tournament.kt | 41 +++++++++++++------ 2 files changed, 34 insertions(+), 13 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 3ca1fd4..50552ec 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 @@ -87,7 +87,8 @@ object PairingHandler: PairgothApiHandler { val playing = (tournament.games(round).values).filter { it.id != gameId }.flatMap { listOf(it.black, it.white) }.toSet() - if (game.result != Game.Result.UNKNOWN && ( + if ((game.result != Game.Result.UNKNOWN || + tournament is TeamTournament && tournament.hasIndividualResults(game.id)) && ( game.black != payload.getInt("b") || game.white != payload.getInt("w") || game.handicap != payload.getInt("h") @@ -112,6 +113,9 @@ object PairingHandler: PairgothApiHandler { game.table = payload.getString("t")?.toIntOrNull() ?: badRequest("invalid table number") game.forcedTable = true } + if (tournament is TeamTournament) { + tournament.propagateTeamGameEdition(round, game.id) + } tournament.dispatchEvent(GameUpdated, request, Json.Object("round" to round, "game" to game.toJson())) if (game.table != previousTable) { val tableWasOccupied = ( tournament.games(round).values.find { g -> g != game && g.table == game.table } != null ) 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 9852270..4037102 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 @@ -276,22 +276,26 @@ class TeamTournament( super.pair(round, pairables).also { games -> if (type.individual) { games.forEach { game -> - individualGames.computeIfAbsent(game.id) { id -> - // Here white and black just denote the first board color - val whitePlayers = teams[game.white]!!.activePlayers(round) - val blackPlayers = teams[game.black]!!.activePlayers(round) - whitePlayers.zip(blackPlayers).mapIndexed { i, players -> - // alternate colors in the following boards - if ((i % 2) == 0) - Game(nextGameId, game.table, players.first.id, players.second.id) - else - Game(nextGameId, game.table, players.second.id, players.first.id) - }.toMutableSet() - } + pairIndividualGames(round, game) } } } + private fun pairIndividualGames(round: Int, game: Game) { + individualGames.computeIfAbsent(game.id) { id -> + // Here white and black just denote the first board color + val whitePlayers = teams[game.white]!!.activePlayers(round) + val blackPlayers = teams[game.black]!!.activePlayers(round) + whitePlayers.zip(blackPlayers).mapIndexed { i, players -> + // alternate colors in the following boards + if ((i % 2) == 0) + Game(nextGameId, game.table, players.first.id, players.second.id) + else + Game(nextGameId, game.table, players.second.id, players.first.id) + }.toMutableSet() + } + } + override fun unpair(round: Int) { games(round).values.forEach { game -> individualGames.remove(game.id) @@ -304,6 +308,19 @@ class TeamTournament( super.unpair(round, id) } + fun hasIndividualResults(teamGameID: ID): Boolean { + return individualGames[teamGameID]?.any { game -> + game.result != Game.Result.UNKNOWN + } == true + } + + fun propagateTeamGameEdition(round: Int, id: ID) { + // recreate individual games + val teamGame = games(round)[id] ?: error("Game with id $id not found") + individualGames.remove(id) + pairIndividualGames(round, teamGame) + } + fun pairedTeams() = super.pairedPlayers() fun pairedTeams(round: Int) = super.pairedPlayers(round)