Teams of individual players: Teams composition page ok

This commit is contained in:
Claude Brisson
2025-01-26 01:28:44 +01:00
parent 8f8e23d5b1
commit 169546ae66
15 changed files with 193 additions and 44 deletions

View File

@@ -6,12 +6,12 @@ import org.jeudego.pairgoth.model.Game
import org.jeudego.pairgoth.model.MacMahon
import org.jeudego.pairgoth.model.Pairable
import org.jeudego.pairgoth.model.PairingType
import org.jeudego.pairgoth.model.Player
import org.jeudego.pairgoth.model.Tournament
import org.jeudego.pairgoth.model.getID
import org.jeudego.pairgoth.model.historyBefore
import org.jeudego.pairgoth.pairing.HistoryHelper
import org.jeudego.pairgoth.pairing.solver.MacMahonSolver
import kotlin.math.ceil
import kotlin.math.floor
import kotlin.math.max
import kotlin.math.min
@@ -129,20 +129,23 @@ fun Tournament<*>.getSortedPairables(round: Int, includePreliminary: Boolean = f
return sortedPairables
}
fun Tournament<*>.populateFrozenStandings(sortedPairables: List<Json.Object>, round: Int = rounds) {
fun Tournament<*>.populateStandings(sortedPairables: List<Json.Object>, round: Int = rounds) {
val sortedMap = sortedPairables.associateBy {
it.getID()!!
}
// refresh name, firstname, club and level
sortedMap.forEach { (id, player) ->
val mutable = player as Json.MutableObject
val live = players[id]!!
mutable["name"] = live.name
mutable["firstname"] = live.firstname
mutable["club"] = live.club
mutable["rating"] = live.rating
mutable["rank"] = live.rank
sortedMap.forEach { (id, pairable) ->
val mutable = pairable as Json.MutableObject
pairables[id]?.let {
mutable["name"] = it.name
if (it is Player) {
mutable["firstname"] = it.firstname
}
mutable["club"] = it.club
mutable["rating"] = it.rating
mutable["rank"] = it.rank
}
}
// fill result

View File

@@ -2,8 +2,13 @@ package org.jeudego.pairgoth.api
import com.republicate.kson.Json
import com.republicate.kson.toJsonArray
import com.republicate.kson.toMutableJsonArray
import org.jeudego.pairgoth.api.ApiHandler.Companion.badRequest
import org.jeudego.pairgoth.model.Game
import org.jeudego.pairgoth.model.ID
import org.jeudego.pairgoth.model.Player
import org.jeudego.pairgoth.model.TeamTournament
import org.jeudego.pairgoth.model.TeamTournament.Team
import org.jeudego.pairgoth.model.Tournament
import org.jeudego.pairgoth.model.getID
import org.jeudego.pairgoth.model.toID
@@ -21,8 +26,8 @@ object PairingHandler: PairgothApiHandler {
val playing = tournament.games(round).values.flatMap {
listOf(it.black, it.white)
}.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 unpairables = tournament.pairables.values.filter { it.final && !it.canPlay(round) }.sortedByDescending { it.rating }.map { it.id }.toJsonArray()
val pairables = tournament.pairables.values.filter { it.final && it.canPlay(round) && !playing.contains(it.id) }.sortedByDescending { it.rating }.map { it.id }.toJsonArray()
val games = tournament.games(round).values.sortedBy {
if (it.table == 0) Int.MAX_VALUE else it.table
}

View File

@@ -22,7 +22,6 @@ import java.nio.charset.StandardCharsets
import java.text.DecimalFormat
import java.text.Normalizer
import java.util.*
import kotlin.collections.ArrayList
object StandingsHandler: PairgothApiHandler {
override fun get(request: HttpServletRequest, response: HttpServletResponse): Json? {
@@ -31,7 +30,7 @@ object StandingsHandler: PairgothApiHandler {
val includePreliminary = request.getParameter("include_preliminary")?.let { it.toBoolean() } ?: false
val sortedPairables = tournament.getSortedPairables(round, includePreliminary)
tournament.populateFrozenStandings(sortedPairables, round)
tournament.populateStandings(sortedPairables, round)
val acceptHeader = request.getHeader("Accept") as String?
val accept = acceptHeader?.substringBefore(";")

View File

@@ -26,6 +26,8 @@ sealed class Pairable(val id: ID, val name: String, val rating: Int, val rank: I
fun equals(other: Pairable): Boolean {
return id == other.id
}
open fun canPlay(round: Int) = !skip.contains(round)
}
object ByePlayer: Pairable(0, "bye", 0, Int.MIN_VALUE, true) {

View File

@@ -255,10 +255,14 @@ class TeamTournament(
json["rank"] = rank
country?.also { json["country"] = it }
club?.also { json["club"] = it }
json["names"] = playerIds.mapNotNull { players[it]?.fullName() }.toJsonArray()
json["ranks"] = playerIds.mapNotNull { players[it]?.rank }.toJsonArray()
}
val teamOfIndividuals: Boolean get() = type.individual
override val skip get() = playerIds.map { players[it]!!.skip }.reduce { left, right -> (left union right) as MutableSet<Int> }
// override val skip get() = playerIds.map { players[it]!!.skip }.reduce { left, right -> (left union right) as MutableSet<Int> }
override fun canPlay(round: Int) = teamPlayers.filter { it.canPlay(round) }.size == type.playersNumber
}
fun teamFromJson(json: Json.Object, default: TeamTournament.Team? = null): Team {
@@ -288,7 +292,8 @@ class TeamTournament(
fun Tournament.Companion.fromJson(json: Json.Object, default: Tournament<*>? = null): Tournament<*> {
val type = json.getString("type")?.uppercase()?.let { Tournament.Type.valueOf(it) } ?: default?.type ?: badRequest("missing type")
// No clean way to avoid this redundancy
val tournament = if (type.playersNumber == 1)
val tournament =
if (type.playersNumber == 1)
StandardTournament(
id = json.getInt("id") ?: default?.id ?: nextTournamentId,
type = type,
@@ -301,7 +306,7 @@ fun Tournament.Companion.fromJson(json: Json.Object, default: Tournament<*>? = n
location = json.getString("location") ?: default?.location ?: badRequest("missing location"),
online = json.getBoolean("online") ?: default?.online ?: false,
komi = json.getDouble("komi") ?: default?.komi ?: 7.5,
rules = json.getString("rules")?.let { Rules.valueOf(it) } ?: default?.rules ?: Rules.FRENCH,
rules = json.getString("rules")?.let { Rules.valueOf(it) } ?: default?.rules ?: if (json.getString("country")?.lowercase(Locale.ROOT) == "fr") Rules.FRENCH else Rules.AGA,
gobanSize = json.getInt("gobanSize") ?: default?.gobanSize ?: 19,
timeSystem = json.getObject("timeSystem")?.let { TimeSystem.fromJson(it) } ?: default?.timeSystem ?: badRequest("missing timeSystem"),
rounds = json.getInt("rounds") ?: default?.rounds ?: badRequest("missing rounds"),
@@ -317,7 +322,7 @@ fun Tournament.Companion.fromJson(json: Json.Object, default: Tournament<*>? = n
startDate = json.getString("startDate")?.let { LocalDate.parse(it) } ?: default?.startDate ?: badRequest("missing startDate"),
endDate = json.getString("endDate")?.let { LocalDate.parse(it) } ?: default?.endDate ?: badRequest("missing endDate"),
director = json.getString("director") ?: default?.director ?: "",
country = json.getString("country") ?: default?.country ?: badRequest("missing country"),
country = (json.getString("country") ?: default?.country ?: "fr").let { if (it.isEmpty()) "fr" else it },
location = json.getString("location") ?: default?.location ?: badRequest("missing location"),
online = json.getBoolean("online") ?: default?.online ?: false,
komi = json.getDouble("komi") ?: default?.komi ?: 7.5,