Add a tournament overview dialog

This commit is contained in:
Claude Brisson
2024-04-13 23:15:42 +02:00
parent 9e09a0edef
commit 7837daf973
6 changed files with 85 additions and 3 deletions

View File

@@ -30,7 +30,9 @@ object TournamentHandler: PairgothApiHandler {
if (accept == "application/pairgoth") {
it.toFullJson()
} else {
it.toJson()
it.toJson().also { json ->
(json as Json.MutableObject)["stats"] = it.stats()
}
}
} ?: badRequest("no tournament with id #${id}")
}

View File

@@ -120,6 +120,15 @@ sealed class Tournament <P: Pairable>(
fun pairedPlayers() = games.flatMap { it.values }.flatMap { listOf(it.black, it.white) }.toSet()
fun hasPlayer(dbId: DatabaseId, pId: String) = pId.isNotBlank() && players.values.filter { player -> pId == player.externalIds[dbId] }.isNotEmpty()
fun stats() = (0..rounds - 1).map { index ->
Json.Object(
"participants" to pairables.values.count { !it.skip.contains(index + 1) },
"paired" to (games.getOrNull(index)?.values?.flatMap { listOf(it.black, it.white) }?.count { it != 0 } ?: 0),
"games" to (games.getOrNull(index)?.values?.count() ?: 0),
"ready" to (games.getOrNull(index)?.values?.count { it.result != Game.Result.UNKNOWN } ?: 0)
)
}.toJsonArray()
}
// standard tournament of individuals
@@ -257,7 +266,7 @@ fun Tournament.Companion.fromJson(json: Json.Object, default: Tournament<*>? = n
return tournament
}
fun Tournament<*>.toJson() = Json.Object(
fun Tournament<*>.toJson() = Json.MutableObject(
"id" to id,
"type" to type.name,
"name" to name,