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 a9ba349..808ae7c 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 @@ -23,7 +23,9 @@ object PairingHandler: PairgothApiHandler { }.toSet() val unpairables = tournament.pairables.values.filter { !it.final || it.skip.contains(round) }.sortedByDescending { it.rating }.map { it.id }.toJsonArray() val pairables = tournament.pairables.values.filter { it.final && !it.skip.contains(round) && !playing.contains(it.id) }.sortedByDescending { it.rating }.map { it.id }.toJsonArray() - val games = tournament.games(round).values + val games = tournament.games(round).values.sortedBy { + if (it.table == 0) Int.MAX_VALUE else it.table + } return Json.Object( "games" to games.map { it.toJson() }.toCollection(Json.MutableArray()), "pairables" to pairables, diff --git a/api-webapp/src/main/kotlin/org/jeudego/pairgoth/model/Pairing.kt b/api-webapp/src/main/kotlin/org/jeudego/pairgoth/model/Pairing.kt index 9f52d14..ce3e152 100644 --- a/api-webapp/src/main/kotlin/org/jeudego/pairgoth/model/Pairing.kt +++ b/api-webapp/src/main/kotlin/org/jeudego/pairgoth/model/Pairing.kt @@ -6,6 +6,7 @@ import org.jeudego.pairgoth.model.MainCritParams.SeedMethod.SPLIT_AND_SLIP import org.jeudego.pairgoth.model.PairingType.* import org.jeudego.pairgoth.pairing.solver.MacMahonSolver import org.jeudego.pairgoth.pairing.solver.SwissSolver +import java.util.* // base pairing parameters data class BaseCritParams( @@ -172,7 +173,7 @@ class Swiss( ): Pairing(SWISS, pairingParams, placementParams) { companion object {} override fun pair(tournament: Tournament<*>, round: Int, pairables: List): List { - return SwissSolver(round, tournament.historyBefore(round), pairables, pairingParams, placementParams).pair() + return SwissSolver(round, tournament.historyBefore(round), pairables, pairingParams, placementParams, tournament.usedTables(round)).pair() } } @@ -201,7 +202,7 @@ class MacMahon( ): Pairing(MAC_MAHON, pairingParams, placementParams) { companion object {} override fun pair(tournament: Tournament<*>, round: Int, pairables: List): List { - return MacMahonSolver(round, tournament.historyBefore(round), pairables, pairingParams, placementParams, mmFloor, mmBar).pair() + return MacMahonSolver(round, tournament.historyBefore(round), pairables, pairingParams, placementParams, tournament.usedTables(round), mmFloor, mmBar).pair() } } 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 fc2e116..f3adff8 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 @@ -7,6 +7,7 @@ import org.jeudego.pairgoth.api.ApiHandler.Companion.badRequest import org.jeudego.pairgoth.pairing.solver.MacMahonSolver import org.jeudego.pairgoth.pairing.solver.SwissSolver import org.jeudego.pairgoth.store.Store +import java.util.* import kotlin.math.roundToInt sealed class Tournament ( @@ -68,13 +69,19 @@ sealed class Tournament ( else mutableMapOf().also { games.add(it) } fun lastRound() = games.size + fun usedTables(round: Int): BitSet = + games(round).values.map { it.table }.fold(BitSet()) { acc, table -> + acc.set(table) + acc + } + fun recomputeDUDD(round: Int, gameID: ID) { // Instantiate solver with game history // TODO cleaner solver instantiation val history = games.map { games -> games.values.toList() } val solver = when (pairing.type) { - PairingType.SWISS -> SwissSolver(round, history, pairables.values.toList(), pairing.pairingParams, pairing.placementParams) - PairingType.MAC_MAHON -> MacMahonSolver(round, history, pairables.values.toList(), pairing.pairingParams, pairing.placementParams, mmBar = 3, mmFloor = -20) + PairingType.SWISS -> SwissSolver(round, history, pairables.values.toList(), pairing.pairingParams, pairing.placementParams, usedTables(round)) + PairingType.MAC_MAHON -> MacMahonSolver(round, history, pairables.values.toList(), pairing.pairingParams, pairing.placementParams, usedTables(round), mmBar = 3, mmFloor = -20) else -> throw Exception("Invalid tournament type") } // Recomputes DUDD 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 cba11c2..eef2a3f 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 @@ -134,11 +134,4 @@ 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 f49d819..c6c262b 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 @@ -26,6 +26,7 @@ sealed class BaseSolver( pairables: List, // All pairables for this round, it may include the bye player pairing: PairingParams, placement: PlacementParams, + val usedTables: BitSet ) : BasePairingHelper(history, pairables, pairing, placement) { companion object { @@ -532,7 +533,6 @@ sealed class BaseSolver( } open fun games(black: Pairable, white: Pairable): List { // CB TODO team of individuals pairing - 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 = dudd(black, white))) diff --git a/api-webapp/src/main/kotlin/org/jeudego/pairgoth/pairing/solver/MacMahonSolver.kt b/api-webapp/src/main/kotlin/org/jeudego/pairgoth/pairing/solver/MacMahonSolver.kt index 2c35af3..8cb37a2 100644 --- a/api-webapp/src/main/kotlin/org/jeudego/pairgoth/pairing/solver/MacMahonSolver.kt +++ b/api-webapp/src/main/kotlin/org/jeudego/pairgoth/pairing/solver/MacMahonSolver.kt @@ -1,6 +1,7 @@ package org.jeudego.pairgoth.pairing.solver import org.jeudego.pairgoth.model.* +import java.util.* import kotlin.math.max import kotlin.math.min @@ -9,8 +10,9 @@ class MacMahonSolver(round: Int, pairables: List, pairingParams: PairingParams, placementParams: PlacementParams, + usedTables: BitSet, private val mmFloor: Int, private val mmBar: Int): - BaseSolver(round, history, pairables, pairingParams, placementParams) { + BaseSolver(round, history, pairables, pairingParams, placementParams, usedTables) { override val scores: Map by lazy { pairablesMap.mapValues { diff --git a/api-webapp/src/main/kotlin/org/jeudego/pairgoth/pairing/solver/SwissSolver.kt b/api-webapp/src/main/kotlin/org/jeudego/pairgoth/pairing/solver/SwissSolver.kt index 9a0065d..21b127b 100644 --- a/api-webapp/src/main/kotlin/org/jeudego/pairgoth/pairing/solver/SwissSolver.kt +++ b/api-webapp/src/main/kotlin/org/jeudego/pairgoth/pairing/solver/SwissSolver.kt @@ -1,13 +1,16 @@ package org.jeudego.pairgoth.pairing.solver import org.jeudego.pairgoth.model.* +import java.util.* class SwissSolver(round: Int, history: List>, pairables: List, pairingParams: PairingParams, - placementParams: PlacementParams): - BaseSolver(round, history, pairables, pairingParams, placementParams) { + placementParams: PlacementParams, + usedTables: BitSet +): + BaseSolver(round, history, pairables, pairingParams, placementParams, usedTables) { // In a Swiss tournament the main criterion is the number of wins and already computed