Standings page ok

This commit is contained in:
Claude Brisson
2023-12-25 06:21:05 +01:00
parent 31411eb859
commit 91e0bc839a
5 changed files with 105 additions and 20 deletions

View File

@@ -15,6 +15,7 @@ import kotlin.math.max
import kotlin.math.min
import org.jeudego.pairgoth.model.Criterion.*
import org.jeudego.pairgoth.model.Game.Result.*
import org.jeudego.pairgoth.model.ID
import org.jeudego.pairgoth.model.getID
@@ -36,7 +37,9 @@ object StandingsHandler: PairgothApiHandler {
}
}
}
val criteria = tournament.pairing.placementParams.criteria.map { crit ->
val neededCriteria = ArrayList(tournament.pairing.placementParams.criteria)
if (!neededCriteria.contains(NBW)) neededCriteria.add(NBW)
val criteria = neededCriteria.map { crit ->
crit.name to when (crit) {
NONE -> nullMap
CATEGORY -> nullMap
@@ -70,13 +73,13 @@ object StandingsHandler: PairgothApiHandler {
}
}
val pairables = tournament.pairables.values.map { it.toMutableJson() }
pairables.forEach { it ->
val player = it as Json.MutableObject
pairables.forEach { player ->
for (crit in criteria) {
player[crit.first] = crit.second[player.getID()]
player[crit.first] = crit.second[player.getID()] ?: 0.0
}
player["results"] = Json.MutableArray(List(round) { "=0" })
}
return pairables.sortedWith { left, right ->
val sortedPairables = pairables.sortedWith { left, right ->
for (crit in criteria) {
val lval = left.getDouble(crit.first) ?: 0.0
val rval = right.getDouble(crit.first) ?: 0.0
@@ -84,8 +87,58 @@ object StandingsHandler: PairgothApiHandler {
if (cmp != 0) return@sortedWith -cmp
}
return@sortedWith 0
}.toJsonArray()
}.mapIndexed() { i, obj ->
obj.set("num", i+1)
}
val sortedMap = sortedPairables.associateBy {
it.getID()!!
}
var place = 1
sortedPairables.groupBy { p ->
Triple(p.getDouble(criteria[0].first) ?: 0.0, p.getDouble(criteria[1].first) ?: 0.0, p.getDouble(criteria[2].first) ?: 0.0)
}.forEach {
it.value.forEach { p -> p["place"] = place }
place += it.value.size
}
for (r in 1..round) {
tournament.games(r).values.forEach { game ->
val white = if (game.white != 0) sortedMap[game.white] else null
val black = if (game.black != 0) sortedMap[game.black] else null
val whiteNum = white?.getInt("num") ?: 0
val blackNum = black?.getInt("num") ?: 0
val whiteColor = if (black == null) "" else "w"
val blackColor = if (white == null) "" else "b"
val handicap = if (game.handicap == 0) "" else "/h${game.handicap}"
assert(white != null || black != null)
if (white != null) {
val mark = when (game.result) {
UNKNOWN -> "?"
BLACK -> "-"
WHITE -> "+"
JIGO -> "="
CANCELLED -> "X"
BOTHWIN -> "++"
BOTHLOOSE -> "--"
}
val results = white.getArray("results") as Json.MutableArray
results[r - 1] = "$whiteColor$mark$blackNum$handicap"
}
if (black != null) {
val mark = when (game.result) {
UNKNOWN -> "?"
BLACK -> "+"
WHITE -> "-"
JIGO -> "="
CANCELLED -> "X"
BOTHWIN -> "++"
BOTHLOOSE -> "--"
}
val results = black.getArray("results") as Json.MutableArray
results[r - 1] = "$blackColor$mark$whiteNum$handicap"
}
}
}
return sortedPairables.toJsonArray()
}
val nullMap = mapOf<ID, Double>()