From 5d6e6cefcb8f3780a66ad7b89dae6c0926076062 Mon Sep 17 00:00:00 2001 From: Claude Brisson Date: Fri, 22 Dec 2023 17:25:14 +0100 Subject: [PATCH] Table handling --- .../src/main/kotlin/org/jeudego/pairgoth/ext/OpenGotha.kt | 1 + .../src/main/kotlin/org/jeudego/pairgoth/model/Game.kt | 3 +++ .../org/jeudego/pairgoth/pairing/BasePairingHelper.kt | 8 ++++++++ .../org/jeudego/pairgoth/pairing/solver/BaseSolver.kt | 8 +++++--- .../main/kotlin/org/jeudego/pairgoth/store/FileStore.kt | 6 +++++- 5 files changed, 22 insertions(+), 4 deletions(-) diff --git a/api-webapp/src/main/kotlin/org/jeudego/pairgoth/ext/OpenGotha.kt b/api-webapp/src/main/kotlin/org/jeudego/pairgoth/ext/OpenGotha.kt index 2c9db0e..c49f5a5 100644 --- a/api-webapp/src/main/kotlin/org/jeudego/pairgoth/ext/OpenGotha.kt +++ b/api-webapp/src/main/kotlin/org/jeudego/pairgoth/ext/OpenGotha.kt @@ -151,6 +151,7 @@ object OpenGotha { it.value.map { game -> Game( id = Store.nextGameId, + table = game.tableNumber, black = canonicMap[game.blackPlayer] ?: throw Error("player not found: ${game.blackPlayer}"), white = canonicMap[game.whitePlayer] ?: throw Error("player not found: ${game.whitePlayer}"), handicap = game.handicap, 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 50d0ae4..8bae5b3 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 @@ -6,6 +6,7 @@ import java.util.* data class Game( val id: ID, + var table: Int, var white: ID, var black: ID, var handicap: Int = 0, @@ -33,6 +34,7 @@ data class Game( fun Game.toJson() = Json.Object( "id" to id, + "t" to table, "w" to white, "b" to black, "h" to handicap, @@ -42,6 +44,7 @@ fun Game.toJson() = Json.Object( fun Game.Companion.fromJson(json: Json.Object) = Game( id = json.getID("id") ?: throw Error("missing game id"), + table = json.getInt("t") ?: throw Error("missing game table"), white = json.getID("w") ?: throw Error("missing white player"), black = json.getID("b") ?: throw Error("missing black player"), handicap = json.getInt("h") ?: 0, diff --git a/api-webapp/src/main/kotlin/org/jeudego/pairgoth/pairing/BasePairingHelper.kt b/api-webapp/src/main/kotlin/org/jeudego/pairgoth/pairing/BasePairingHelper.kt index 44d493f..9e04484 100644 --- a/api-webapp/src/main/kotlin/org/jeudego/pairgoth/pairing/BasePairingHelper.kt +++ b/api-webapp/src/main/kotlin/org/jeudego/pairgoth/pairing/BasePairingHelper.kt @@ -1,6 +1,7 @@ package org.jeudego.pairgoth.pairing import org.jeudego.pairgoth.model.* +import java.util.* abstract class BasePairingHelper( history: List>, // History of all games played for each round @@ -131,4 +132,11 @@ abstract class BasePairingHelper( open fun nameSort(p: Pairable, q: Pairable): Int { return if (p.name > q.name) 1 else -1 } + + val tables = history.mapTo(mutableListOf()) { games -> + games.map { it.table }.fold(BitSet()) { acc, table -> + acc.set(table) + acc + } + } } \ No newline at end of file diff --git a/api-webapp/src/main/kotlin/org/jeudego/pairgoth/pairing/solver/BaseSolver.kt b/api-webapp/src/main/kotlin/org/jeudego/pairgoth/pairing/solver/BaseSolver.kt index 8cabe2c..f70396e 100644 --- a/api-webapp/src/main/kotlin/org/jeudego/pairgoth/pairing/solver/BaseSolver.kt +++ b/api-webapp/src/main/kotlin/org/jeudego/pairgoth/pairing/solver/BaseSolver.kt @@ -121,7 +121,7 @@ sealed class BaseSolver( var result = sorted.flatMap { games(white = it[0], black = it[1]) } // add game for ByePlayer - if (chosenByePlayer != ByePlayer) result += Game(id = Store.nextGameId, white = ByePlayer.id, black = chosenByePlayer.id, result = Game.Result.fromSymbol('b')) + if (chosenByePlayer != ByePlayer) result += Game(id = Store.nextGameId, table = 0, white = ByePlayer.id, black = chosenByePlayer.id, result = Game.Result.fromSymbol('b')) if (DEBUG_EXPORT_WEIGHT) { println("DUDD debug") @@ -510,7 +510,9 @@ sealed class BaseSolver( open fun games(black: Pairable, white: Pairable): List { // CB TODO team of individuals pairing - - return listOf(Game(id = Store.nextGameId, black = black.id, white = white.id, handicap = pairing.handicap.handicap(black, white), drawnUpDown = white.group-black.group)) + val usedTables = tables.getOrNull(round - 1) ?: BitSet().also { tables.add(it) } + val table = if (black.id == 0 || white.id == 0) 0 else usedTables.nextClearBit(1) + usedTables.set(table) + return listOf(Game(id = Store.nextGameId, table = table, black = black.id, white = white.id, handicap = pairing.handicap.handicap(white, black), drawnUpDown = white.group-black.group)) } } diff --git a/api-webapp/src/main/kotlin/org/jeudego/pairgoth/store/FileStore.kt b/api-webapp/src/main/kotlin/org/jeudego/pairgoth/store/FileStore.kt index b10c224..72404ea 100644 --- a/api-webapp/src/main/kotlin/org/jeudego/pairgoth/store/FileStore.kt +++ b/api-webapp/src/main/kotlin/org/jeudego/pairgoth/store/FileStore.kt @@ -90,11 +90,15 @@ class FileStore(pathStr: String): StoreImplementation { } val games = json["games"] as Json.Array? ?: Json.Array() (1..games.size).forEach { round -> + var nextDefaultTable = 1; val roundGames = games[round - 1] as Json.Array tournament.games(round).putAll( roundGames.associate { (it as Json.Object).let { game -> - Pair(game.getID("id") ?: throw Error("invalid tournament file"), Game.fromJson(game)).also { + val fixedGame = + if (game.containsKey("t")) game + else Json.MutableObject(game).set("t", nextDefaultTable++) + Pair(game.getID("id") ?: throw Error("invalid tournament file"), Game.fromJson(fixedGame)).also { maxGameId = max(maxGameId, it.first) } }