Code cleaning
This commit is contained in:
@@ -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,27 +109,7 @@ 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
|
||||||
val sortedPairables = tournament.getSortedPairables(round)
|
renumberTables(request, tournament, round, game)
|
||||||
val sortedMap = sortedPairables.associateBy {
|
|
||||||
it.getID()!!
|
|
||||||
}
|
|
||||||
val changed = tournament.renumberTables(round, game) { 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)
|
return Json.Object("success" to true)
|
||||||
@@ -142,28 +123,37 @@ object PairingHandler: PairgothApiHandler {
|
|||||||
tournament.dispatchEvent(TournamentUpdated, request, tournament.toJson())
|
tournament.dispatchEvent(TournamentUpdated, request, tournament.toJson())
|
||||||
}
|
}
|
||||||
|
|
||||||
val sortedPairables = tournament.getSortedPairables(round)
|
renumberTables(request, tournament, 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)
|
return Json.Object("success" to true)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun renumberTables(request: HttpServletRequest, tournament: Tournament<*>, round: Int, pivot: Game? = null) {
|
||||||
|
val sortedPairables = tournament.getSortedPairables(round)
|
||||||
|
val sortedMap = sortedPairables.associateBy {
|
||||||
|
it.getID()!!
|
||||||
|
}
|
||||||
|
val changed = tournament.renumberTables(round, pivot) { gm ->
|
||||||
|
val whitePosition = sortedMap[gm.white]?.getInt("num") ?: Int.MIN_VALUE
|
||||||
|
val blackPosition = sortedMap[gm.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())
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
override fun delete(request: HttpServletRequest, response: HttpServletResponse): Json {
|
override fun delete(request: HttpServletRequest, response: HttpServletResponse): Json {
|
||||||
val tournament = getTournament(request)
|
val tournament = getTournament(request)
|
||||||
val round = getSubSelector(request)?.toIntOrNull() ?: badRequest("invalid round number")
|
val round = getSubSelector(request)?.toIntOrNull() ?: badRequest("invalid round number")
|
||||||
|
@@ -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")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user