Code cleaning

This commit is contained in:
Claude Brisson
2024-08-13 13:23:38 +02:00
parent 9c5eb1c333
commit 250b3ec61d
2 changed files with 30 additions and 40 deletions

View File

@@ -5,6 +5,7 @@ import com.republicate.kson.toJsonArray
import org.jeudego.pairgoth.api.ApiHandler.Companion.badRequest import org.jeudego.pairgoth.api.ApiHandler.Companion.badRequest
import org.jeudego.pairgoth.api.TournamentHandler.dispatchEvent import org.jeudego.pairgoth.api.TournamentHandler.dispatchEvent
import org.jeudego.pairgoth.model.Game import org.jeudego.pairgoth.model.Game
import org.jeudego.pairgoth.model.Tournament
import org.jeudego.pairgoth.model.getID import org.jeudego.pairgoth.model.getID
import org.jeudego.pairgoth.model.toID import org.jeudego.pairgoth.model.toID
import org.jeudego.pairgoth.model.toJson import org.jeudego.pairgoth.model.toJson
@@ -108,13 +109,34 @@ object PairingHandler: PairgothApiHandler {
val tableWasOccupied = ( tournament.games(round).values.find { g -> g != game && g.table == game.table } != null ) val tableWasOccupied = ( tournament.games(round).values.find { g -> g != game && g.table == game.table } != null )
if (tableWasOccupied) { if (tableWasOccupied) {
// some renumbering is necessary // some renumbering is necessary
renumberTables(request, tournament, round, game)
}
}
return Json.Object("success" to true)
} else {
// without id, it's a table renumbering
if (payload.containsKey("excludeTables")) {
val tablesExclusion = payload.getString("excludeTables") ?: badRequest("missing 'excludeTables'")
TournamentHandler.validateTablesExclusion(tablesExclusion)
while (tournament.tablesExclusion.size < round) tournament.tablesExclusion.add("")
tournament.tablesExclusion[round - 1] = tablesExclusion
tournament.dispatchEvent(TournamentUpdated, request, tournament.toJson())
}
renumberTables(request, tournament, round)
return Json.Object("success" to true)
}
}
private fun renumberTables(request: HttpServletRequest, tournament: Tournament<*>, round: Int, pivot: Game? = null) {
val sortedPairables = tournament.getSortedPairables(round) val sortedPairables = tournament.getSortedPairables(round)
val sortedMap = sortedPairables.associateBy { val sortedMap = sortedPairables.associateBy {
it.getID()!! it.getID()!!
} }
val changed = tournament.renumberTables(round, game) { game -> val changed = tournament.renumberTables(round, pivot) { gm ->
val whitePosition = sortedMap[game.white]?.getInt("num") ?: Int.MIN_VALUE val whitePosition = sortedMap[gm.white]?.getInt("num") ?: Int.MIN_VALUE
val blackPosition = sortedMap[game.black]?.getInt("num") ?: Int.MIN_VALUE val blackPosition = sortedMap[gm.black]?.getInt("num") ?: Int.MIN_VALUE
(whitePosition + blackPosition) (whitePosition + blackPosition)
} }
if (changed) { if (changed) {
@@ -129,39 +151,7 @@ object PairingHandler: PairgothApiHandler {
) )
) )
} }
}
}
return Json.Object("success" to true)
} else {
// without id, it's a table renumbering
if (payload.containsKey("excludeTables")) {
val tablesExclusion = payload.getString("excludeTables") ?: badRequest("missing 'excludeTables'")
TournamentHandler.validateTablesExclusion(tablesExclusion)
while (tournament.tablesExclusion.size < round) tournament.tablesExclusion.add("")
tournament.tablesExclusion[round - 1] = tablesExclusion
tournament.dispatchEvent(TournamentUpdated, request, tournament.toJson())
}
val sortedPairables = tournament.getSortedPairables(round)
val sortedMap = sortedPairables.associateBy {
it.getID()!!
}
val changed = tournament.renumberTables(round, null) { game ->
val whitePosition = sortedMap[game.white]?.getInt("num") ?: Int.MIN_VALUE
val blackPosition = sortedMap[game.black]?.getInt("num") ?: Int.MIN_VALUE
(whitePosition + blackPosition)
}
if (changed) {
val games = tournament.games(round).values.sortedBy {
if (it.table == 0) Int.MAX_VALUE else it.table
}
tournament.dispatchEvent(
TablesRenumbered, request,
Json.Object("round" to round, "games" to games.map { it.toJson() }.toCollection(Json.MutableArray()))
)
}
return Json.Object("success" to true)
}
} }
override fun delete(request: HttpServletRequest, response: HttpServletResponse): Json { override fun delete(request: HttpServletRequest, response: HttpServletResponse): Json {

View File

@@ -46,7 +46,7 @@ object PlayerHandler: PairgothApiHandler {
if (round <= tournament.lastRound()) { if (round <= tournament.lastRound()) {
val playing = tournament.games(round).values.flatMap { listOf(it.black, it.white) } val playing = tournament.games(round).values.flatMap { listOf(it.black, it.white) }
if (playing.contains(id)) { if (playing.contains(id)) {
throw badRequest("player is playing in round #$round") badRequest("player is playing in round #$round")
} }
} }
} }