Table handling

This commit is contained in:
Claude Brisson
2023-12-22 17:25:14 +01:00
parent 6ea9bdb7ea
commit 5d6e6cefcb
5 changed files with 22 additions and 4 deletions

View File

@@ -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,

View File

@@ -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,

View File

@@ -1,6 +1,7 @@
package org.jeudego.pairgoth.pairing
import org.jeudego.pairgoth.model.*
import java.util.*
abstract class BasePairingHelper(
history: List<List<Game>>, // 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
}
}
}

View File

@@ -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<Game> {
// 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))
}
}

View File

@@ -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)
}
}