From 728ed482f92f85988e6108a8b95643357314d156 Mon Sep 17 00:00:00 2001 From: Claude Brisson Date: Tue, 23 May 2023 18:08:04 +0200 Subject: [PATCH] Some code cleaning; define ID type as an alias to Int --- .../jeudego/pairgoth/api/PairingHandler.kt | 3 ++- .../kotlin/org/jeudego/pairgoth/model/Defs.kt | 6 +++++ .../kotlin/org/jeudego/pairgoth/model/Game.kt | 6 ++--- .../org/jeudego/pairgoth/model/Pairable.kt | 4 ++-- .../kotlin/org/jeudego/pairgoth/model/Team.kt | 2 -- .../org/jeudego/pairgoth/model/Tournament.kt | 22 +++++++++---------- .../org/jeudego/pairgoth/store/Store.kt | 7 +++--- webapp/src/test/kotlin/ImportExportTests.kt | 6 ++--- 8 files changed, 31 insertions(+), 25 deletions(-) create mode 100644 webapp/src/main/kotlin/org/jeudego/pairgoth/model/Defs.kt delete mode 100644 webapp/src/main/kotlin/org/jeudego/pairgoth/model/Team.kt diff --git a/webapp/src/main/kotlin/org/jeudego/pairgoth/api/PairingHandler.kt b/webapp/src/main/kotlin/org/jeudego/pairgoth/api/PairingHandler.kt index b07235b..3c18877 100644 --- a/webapp/src/main/kotlin/org/jeudego/pairgoth/api/PairingHandler.kt +++ b/webapp/src/main/kotlin/org/jeudego/pairgoth/api/PairingHandler.kt @@ -4,6 +4,7 @@ import com.republicate.kson.Json import com.republicate.kson.toJsonArray import org.jeudego.pairgoth.api.ApiHandler.Companion.badRequest import org.jeudego.pairgoth.model.Pairing +import org.jeudego.pairgoth.model.toID import org.jeudego.pairgoth.model.toJson import org.jeudego.pairgoth.web.Event import org.jeudego.pairgoth.web.Event.* @@ -35,7 +36,7 @@ object PairingHandler: PairgothApiHandler { tournament.pairables.values.filter { !it.skip.contains(round) && !playing.contains(it.id) } else payload.map { // CB - because of the '["all"]' map, conversion to int lands here... Better API syntax for 'all players'? - if (it is Number) it.toInt() else badRequest("invalid pairable id: #$it") + if (it is Number) it.toID() else badRequest("invalid pairable id: #$it") }.map { id -> tournament.pairables[id]?.also { if (it.skip.contains(round)) badRequest("pairable #$id does not play round $round") diff --git a/webapp/src/main/kotlin/org/jeudego/pairgoth/model/Defs.kt b/webapp/src/main/kotlin/org/jeudego/pairgoth/model/Defs.kt new file mode 100644 index 0000000..32ecadf --- /dev/null +++ b/webapp/src/main/kotlin/org/jeudego/pairgoth/model/Defs.kt @@ -0,0 +1,6 @@ +package org.jeudego.pairgoth.model + +typealias ID = Int + +fun String.toID() = toInt() +fun Number.toID() = toInt() \ No newline at end of file diff --git a/webapp/src/main/kotlin/org/jeudego/pairgoth/model/Game.kt b/webapp/src/main/kotlin/org/jeudego/pairgoth/model/Game.kt index 02eb289..288652a 100644 --- a/webapp/src/main/kotlin/org/jeudego/pairgoth/model/Game.kt +++ b/webapp/src/main/kotlin/org/jeudego/pairgoth/model/Game.kt @@ -5,9 +5,9 @@ import org.jeudego.pairgoth.model.Game.Result.* import java.util.* data class Game( - val id: Int, - val white: Int, - val black: Int, + val id: ID, + val white: ID, + val black: ID, val handicap: Int = 0, var result: Result = UNKNOWN ) { diff --git a/webapp/src/main/kotlin/org/jeudego/pairgoth/model/Pairable.kt b/webapp/src/main/kotlin/org/jeudego/pairgoth/model/Pairable.kt index a90cb7e..711d8c0 100644 --- a/webapp/src/main/kotlin/org/jeudego/pairgoth/model/Pairable.kt +++ b/webapp/src/main/kotlin/org/jeudego/pairgoth/model/Pairable.kt @@ -9,7 +9,7 @@ import kotlin.math.roundToInt // Pairable -sealed class Pairable(val id: Int, val name: String, open val rating: Int, open val rank: Int) { +sealed class Pairable(val id: ID, val name: String, open val rating: Int, open val rank: Int) { companion object {} abstract fun toJson(): Json.Object abstract val club: String? @@ -49,7 +49,7 @@ fun Pairable.Companion.parseRank(rankStr: String): Int { // Player class Player( - id: Int, + id: ID, name: String, var firstname: String, rating: Int, diff --git a/webapp/src/main/kotlin/org/jeudego/pairgoth/model/Team.kt b/webapp/src/main/kotlin/org/jeudego/pairgoth/model/Team.kt deleted file mode 100644 index 0c4f25a..0000000 --- a/webapp/src/main/kotlin/org/jeudego/pairgoth/model/Team.kt +++ /dev/null @@ -1,2 +0,0 @@ -package org.jeudego.pairgoth.model - diff --git a/webapp/src/main/kotlin/org/jeudego/pairgoth/model/Tournament.kt b/webapp/src/main/kotlin/org/jeudego/pairgoth/model/Tournament.kt index 78bdeb2..586a09d 100644 --- a/webapp/src/main/kotlin/org/jeudego/pairgoth/model/Tournament.kt +++ b/webapp/src/main/kotlin/org/jeudego/pairgoth/model/Tournament.kt @@ -8,7 +8,7 @@ import org.jeudego.pairgoth.store.Store import kotlin.math.roundToInt sealed class Tournament ( - val id: Int, + val id: ID, val type: Type, val name: String, val shortName: String, @@ -41,11 +41,11 @@ sealed class Tournament ( } // players per id - abstract val players: MutableMap + abstract val players: MutableMap // pairables per id - protected val _pairables = mutableMapOf() - val pairables: Map get() = _pairables + protected val _pairables = mutableMapOf() + val pairables: Map get() = _pairables // pairing fun pair(round: Int, pairables: List): List { @@ -63,7 +63,7 @@ sealed class Tournament ( } // games per id for each round - private val games = mutableListOf>() + private val games = mutableListOf>() fun games(round: Int) = games.getOrNull(round - 1) ?: mutableMapOf() fun lastRound() = games.size @@ -78,7 +78,7 @@ sealed class Tournament ( // standard tournament of individuals class StandardTournament( - id: Int, + id: ID, type: Tournament.Type, name: String, shortName: String, @@ -99,7 +99,7 @@ class StandardTournament( // team tournament class TeamTournament( - id: Int, + id: ID, type: Tournament.Type, name: String, shortName: String, @@ -116,11 +116,11 @@ class TeamTournament( komi: Double = 7.5 ): Tournament(id, type, name, shortName, startDate, endDate, country, location, online, timeSystem, rounds, pairing, rules, gobanSize, komi) { companion object {} - override val players = mutableMapOf() - val teams: MutableMap = _pairables + override val players = mutableMapOf() + val teams: MutableMap = _pairables - inner class Team(id: Int, name: String): Pairable(id, name, 0, 0) { - val playerIds = mutableSetOf() + inner class Team(id: ID, name: String): Pairable(id, name, 0, 0) { + val playerIds = mutableSetOf() val teamPlayers: Set get() = playerIds.mapNotNull { players[id] }.toSet() override val rating: Int get() = if (teamPlayers.isEmpty()) super.rating else (teamPlayers.sumOf { player -> player.rating.toDouble() } / players.size).roundToInt() override val rank: Int get() = if (teamPlayers.isEmpty()) super.rank else (teamPlayers.sumOf { player -> player.rank.toDouble() } / players.size).roundToInt() diff --git a/webapp/src/main/kotlin/org/jeudego/pairgoth/store/Store.kt b/webapp/src/main/kotlin/org/jeudego/pairgoth/store/Store.kt index d62cd8f..f34a3d2 100644 --- a/webapp/src/main/kotlin/org/jeudego/pairgoth/store/Store.kt +++ b/webapp/src/main/kotlin/org/jeudego/pairgoth/store/Store.kt @@ -1,5 +1,6 @@ package org.jeudego.pairgoth.store +import org.jeudego.pairgoth.model.ID import org.jeudego.pairgoth.model.Player import org.jeudego.pairgoth.model.Tournament import java.util.concurrent.atomic.AtomicInteger @@ -13,16 +14,16 @@ object Store { val nextPlayerId get() = _nextPlayerId.incrementAndGet() val nextGameId get() = _nextGameId.incrementAndGet() - private val tournaments = mutableMapOf>() + private val tournaments = mutableMapOf>() fun addTournament(tournament: Tournament<*>) { if (tournaments.containsKey(tournament.id)) throw Error("tournament id #${tournament.id} already exists") tournaments[tournament.id] = tournament } - fun getTournament(id: Int) = tournaments[id] + fun getTournament(id: ID) = tournaments[id] - fun getTournamentsIDs(): Set = tournaments.keys + fun getTournamentsIDs(): Set = tournaments.keys fun replaceTournament(tournament: Tournament<*>) { if (!tournaments.containsKey(tournament.id)) throw Error("tournament id #${tournament.id} not known") diff --git a/webapp/src/test/kotlin/ImportExportTests.kt b/webapp/src/test/kotlin/ImportExportTests.kt index 1874442..a0a3467 100644 --- a/webapp/src/test/kotlin/ImportExportTests.kt +++ b/webapp/src/test/kotlin/ImportExportTests.kt @@ -13,15 +13,15 @@ class ImportExportTests: TestBase() { val resp = TestAPI.post("/api/tour", resource) val id = resp.asObject().getInt("id") val tournament = TestAPI.get("/api/tour/$id").asObject() - logger.info(tournament.toString()) + logger.info(tournament.toString().slice(0..50) + "...") val players = TestAPI.get("/api/tour/$id/part").asArray() - logger.info(players.toString()) + logger.info(players.toString().slice(0..50) + "...") for (round in 1..tournament.getInt("rounds")!!) { val games = TestAPI.get("/api/tour/$id/res/1").asArray() logger.info("games for round $round: {}", games.toString()) } val xml = TestAPI.getXml("/api/tour/$id") - logger.info(xml) + logger.info(xml.slice(0..50)+"...") } } }