Refactor event group management and routing system

This commit is contained in:
2025-05-08 17:32:12 +02:00
parent c633694222
commit e3179c69aa
11 changed files with 385 additions and 170 deletions
+66 -48
View File
@@ -20,9 +20,10 @@
package de.steamwar.routes
import de.steamwar.ResponseError
import de.steamwar.data.Groups
import de.steamwar.plugins.SWPermissionCheck
import de.steamwar.sql.*
import de.steamwar.sql.EventGroup.EventGroupType
import de.steamwar.sql.EventRelation.FromType
import io.ktor.http.*
import io.ktor.server.application.*
import io.ktor.server.request.*
@@ -39,6 +40,45 @@ data class ShortEvent(val id: Int, val name: String, val start: Long, val end: L
constructor(event: Event) : this(event.eventID, event.eventName, event.start.time, event.end.time)
}
@Serializable
data class ResponseGroups(
val id: Int,
val name: String,
val pointsPerWin: Int,
val pointsPerLoss: Int,
val pointsPerDraw: Int,
val type: EventGroupType,
val points: Map<Int, Int>
) {
constructor(group: EventGroup, short: Boolean = false) : this(
group.id,
group.name,
group.pointsPerWin,
group.pointsPerLoss,
group.pointsPerDraw,
group.type,
if (short) mapOf() else group.calculatePoints().mapKeys { it.key.teamId })
}
@Serializable
data class ResponseRelation(
val id: Int,
val fight: ResponseEventFight,
val type: FromType,
val fromFight: ResponseEventFight? = null,
val fromGroup: ResponseGroups? = null,
val fromPlace: Int
) {
constructor(relation: EventRelation) : this(
relation.id,
ResponseEventFight(relation.fight),
relation.fromType,
relation.fromFight.map { ResponseEventFight(it) }.orElse(null),
relation.fromGroup.map { ResponseGroups(it) }.orElse(null),
relation.fromPlace
)
}
@Serializable
data class ResponseEvent(
val id: Int,
@@ -49,7 +89,6 @@ data class ResponseEvent(
val maxTeamMembers: Int,
val schemType: String?,
val publicSchemsOnly: Boolean,
val referees: List<ResponseUser>,
) {
constructor(event: Event) : this(
event.eventID,
@@ -60,7 +99,6 @@ data class ResponseEvent(
event.maximumTeamMembers,
event.schematicType?.toDB(),
event.publicSchemsOnly(),
Referee.get(event.eventID).map { ResponseUser(SteamwarUser.get(it)) }
)
}
@@ -68,8 +106,20 @@ data class ResponseEvent(
data class ExtendedResponseEvent(
val event: ResponseEvent,
val teams: List<ResponseTeam>,
val fights: List<ResponseEventFight>
)
val groups: List<ResponseGroups>,
val fights: List<ResponseEventFight>,
val referees: List<ResponseUser>,
val relations: List<ResponseRelation>
) {
constructor(event: Event) : this(
ResponseEvent(event),
TeamTeilnahme.getTeams(event.eventID).map { ResponseTeam(it) },
EventGroup.get(event).map { ResponseGroups(it) },
EventFight.getEvent(event.eventID).map { ResponseEventFight(it) },
Referee.get(event.eventID).map { ResponseUser(SteamwarUser.get(it)) },
EventRelation.get(event).map { ResponseRelation(it) }
)
}
@Serializable
data class CreateEvent(val name: String, val start: Long, val end: Long)
@@ -111,49 +161,11 @@ fun Route.configureEventsRoute() {
}
route("/{id}") {
get {
val id = call.parameters["id"]?.toIntOrNull()
if (id == null) {
call.respond(HttpStatusCode.BadRequest, ResponseError("Invalid ID"))
return@get
}
val event = Event.get(id)
if (event == null) {
call.respond(HttpStatusCode.NotFound, ResponseError("Event not found"))
return@get
}
val event = call.receiveEvent() ?: return@get
call.respond(
ExtendedResponseEvent(
ResponseEvent(event),
TeamTeilnahme.getTeams(event.eventID).map { ResponseTeam(it) },
EventFight.getEvent(event.eventID).map { ResponseEventFight(it) })
ExtendedResponseEvent(event)
)
}
get("/teams") {
val id = call.parameters["id"]?.toIntOrNull()
if (id == null) {
call.respond(HttpStatusCode.BadRequest, ResponseError("Invalid ID"))
return@get
}
val event = Event.get(id)
if (event == null) {
call.respond(HttpStatusCode.NotFound, ResponseError("Event not found"))
return@get
}
call.respond(TeamTeilnahme.getTeams(event.eventID).map { ResponseTeam(it) })
}
get("/fights") {
val id = call.parameters["id"]?.toIntOrNull()
if (id == null) {
call.respond(HttpStatusCode.BadRequest, ResponseError("Invalid ID"))
return@get
}
val event = Event.get(id)
if (event == null) {
call.respond(HttpStatusCode.NotFound, ResponseError("Event not found"))
return@get
}
call.respond(EventFight.getEvent(event.eventID).map { ResponseEventFight(it) })
}
get("/csv") {
val event = call.receiveEvent() ?: return@get
@@ -164,7 +176,7 @@ fun Route.configureEventsRoute() {
csv.appendLine()
val blue = Team.get(it.teamBlue)
val red = Team.get(it.teamRed)
val winner = when(it.ergebnis) {
val winner = when (it.ergebnis) {
1 -> blue.teamName
2 -> red.teamName
3 -> "Tie"
@@ -176,7 +188,7 @@ fun Route.configureEventsRoute() {
Team.get(it.teamBlue).teamName,
Team.get(it.teamRed).teamName,
winner,
Groups.getGroup(it.fightID)?.name ?: "Ungrouped"
it.group.map { it.name }.orElse("Ungrouped")
).joinToString(",")
)
}
@@ -200,7 +212,9 @@ fun Route.configureEventsRoute() {
val end = updateEvent.end?.let { Timestamp.from(Instant.ofEpochMilli(it)) } ?: event.end
val maxTeamMembers = updateEvent.maxTeamMembers ?: event.maximumTeamMembers
val schemType = if (updateEvent.schemType == "null") null else updateEvent.schemType?.let { SchematicType.fromDB(it) } ?: event.schematicType
val schemType =
if (updateEvent.schemType == "null") null else updateEvent.schemType?.let { SchematicType.fromDB(it) }
?: event.schematicType
val publicSchemsOnly = updateEvent.publicSchemsOnly ?: event.publicSchemsOnly()
if (updateEvent.addReferee != null) {
@@ -231,6 +245,10 @@ fun Route.configureEventsRoute() {
event.delete()
call.respond(HttpStatusCode.NoContent)
}
configureEventFightRoutes()
configureEventTeams()
configureEventGroups()
configureEventRelations()
}
}
}