Some code cleaning; define ID type as an alias to Int
This commit is contained in:
@@ -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")
|
||||
|
@@ -0,0 +1,6 @@
|
||||
package org.jeudego.pairgoth.model
|
||||
|
||||
typealias ID = Int
|
||||
|
||||
fun String.toID() = toInt()
|
||||
fun Number.toID() = toInt()
|
@@ -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
|
||||
) {
|
||||
|
@@ -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,
|
||||
|
@@ -1,2 +0,0 @@
|
||||
package org.jeudego.pairgoth.model
|
||||
|
@@ -8,7 +8,7 @@ import org.jeudego.pairgoth.store.Store
|
||||
import kotlin.math.roundToInt
|
||||
|
||||
sealed class Tournament <P: Pairable>(
|
||||
val id: Int,
|
||||
val id: ID,
|
||||
val type: Type,
|
||||
val name: String,
|
||||
val shortName: String,
|
||||
@@ -41,11 +41,11 @@ sealed class Tournament <P: Pairable>(
|
||||
}
|
||||
|
||||
// players per id
|
||||
abstract val players: MutableMap<Int, Player>
|
||||
abstract val players: MutableMap<ID, Player>
|
||||
|
||||
// pairables per id
|
||||
protected val _pairables = mutableMapOf<Int, P>()
|
||||
val pairables: Map<Int, Pairable> get() = _pairables
|
||||
protected val _pairables = mutableMapOf<ID, P>()
|
||||
val pairables: Map<ID, Pairable> get() = _pairables
|
||||
|
||||
// pairing
|
||||
fun pair(round: Int, pairables: List<Pairable>): List<Game> {
|
||||
@@ -63,7 +63,7 @@ sealed class Tournament <P: Pairable>(
|
||||
}
|
||||
|
||||
// games per id for each round
|
||||
private val games = mutableListOf<MutableMap<Int, Game>>()
|
||||
private val games = mutableListOf<MutableMap<ID, Game>>()
|
||||
|
||||
fun games(round: Int) = games.getOrNull(round - 1) ?: mutableMapOf()
|
||||
fun lastRound() = games.size
|
||||
@@ -78,7 +78,7 @@ sealed class Tournament <P: Pairable>(
|
||||
|
||||
// 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<TeamTournament.Team>(id, type, name, shortName, startDate, endDate, country, location, online, timeSystem, rounds, pairing, rules, gobanSize, komi) {
|
||||
companion object {}
|
||||
override val players = mutableMapOf<Int, Player>()
|
||||
val teams: MutableMap<Int, Team> = _pairables
|
||||
override val players = mutableMapOf<ID, Player>()
|
||||
val teams: MutableMap<ID, Team> = _pairables
|
||||
|
||||
inner class Team(id: Int, name: String): Pairable(id, name, 0, 0) {
|
||||
val playerIds = mutableSetOf<Int>()
|
||||
inner class Team(id: ID, name: String): Pairable(id, name, 0, 0) {
|
||||
val playerIds = mutableSetOf<ID>()
|
||||
val teamPlayers: Set<Player> 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()
|
||||
|
@@ -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<Int, Tournament<*>>()
|
||||
private val tournaments = mutableMapOf<ID, Tournament<*>>()
|
||||
|
||||
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<Int> = tournaments.keys
|
||||
fun getTournamentsIDs(): Set<ID> = tournaments.keys
|
||||
|
||||
fun replaceTournament(tournament: Tournament<*>) {
|
||||
if (!tournaments.containsKey(tournament.id)) throw Error("tournament id #${tournament.id} not known")
|
||||
|
@@ -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)+"...")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user