forked from SteamWar/SteamWar
171 lines
5.9 KiB
Kotlin
171 lines
5.9 KiB
Kotlin
/*
|
|
* This file is a part of the SteamWar software.
|
|
*
|
|
* Copyright (C) 2024 SteamWar.de-Serverteam
|
|
*
|
|
* This program is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU Affero General Public License as published by
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU Affero General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
package de.steamwar.routes
|
|
|
|
import de.steamwar.ResponseError
|
|
import de.steamwar.data.Groups
|
|
import de.steamwar.plugins.SWPermissionCheck
|
|
import de.steamwar.sql.EventFight
|
|
import de.steamwar.sql.SteamwarUser
|
|
import de.steamwar.sql.Team
|
|
import de.steamwar.sql.UserPerm
|
|
import io.ktor.http.*
|
|
import io.ktor.server.application.*
|
|
import io.ktor.server.request.*
|
|
import io.ktor.server.response.*
|
|
import io.ktor.server.routing.*
|
|
import kotlinx.serialization.Serializable
|
|
import java.sql.Timestamp
|
|
import java.time.Instant
|
|
|
|
@Serializable
|
|
data class ResponseEventFight(
|
|
val id: Int,
|
|
val spielmodus: String,
|
|
val map: String,
|
|
val blueTeam: ResponseTeam,
|
|
val redTeam: ResponseTeam,
|
|
val start: Long,
|
|
val ergebnis: Int,
|
|
val spectatePort: Int?,
|
|
val group: String?
|
|
) {
|
|
constructor(eventFight: EventFight) : this(
|
|
eventFight.fightID,
|
|
eventFight.spielmodus,
|
|
eventFight.map,
|
|
ResponseTeam(Team.get(eventFight.teamBlue)),
|
|
ResponseTeam(Team.get(eventFight.teamRed)),
|
|
eventFight.startTime.time,
|
|
eventFight.ergebnis,
|
|
eventFight.spectatePort,
|
|
Groups.getGroup(eventFight.fightID)?.name
|
|
)
|
|
}
|
|
|
|
@Serializable
|
|
data class ResponseTeam(val id: Int, val name: String, val kuerzel: String, val color: String) {
|
|
constructor(team: Team) : this(team.teamId, team.teamName, team.teamKuerzel, team.teamColor)
|
|
}
|
|
|
|
@Serializable
|
|
data class UpdateEventFight(
|
|
val blueTeam: Int? = null,
|
|
val redTeam: Int? = null,
|
|
val start: Long? = null,
|
|
val spielmodus: String? = null,
|
|
val map: String? = null,
|
|
val group: String? = null,
|
|
val spectatePort: Int? = null
|
|
)
|
|
|
|
@Serializable
|
|
data class CreateEventFight(
|
|
val event: Int,
|
|
val spielmodus: String,
|
|
val map: String,
|
|
val blueTeam: Int,
|
|
val redTeam: Int,
|
|
val start: Long,
|
|
val spectatePort: Int? = null,
|
|
val group: String? = null
|
|
)
|
|
|
|
fun Route.configureEventFightRoutes() {
|
|
route("/fights") {
|
|
install(SWPermissionCheck) {
|
|
allowMethod(HttpMethod.Get)
|
|
permission = UserPerm.MODERATION
|
|
}
|
|
post {
|
|
val fight = call.receiveNullable<CreateEventFight>()
|
|
if (fight == null) {
|
|
call.respond(HttpStatusCode.BadRequest, ResponseError("Invalid body"))
|
|
return@post
|
|
}
|
|
val eventFight = EventFight.create(
|
|
fight.event,
|
|
Timestamp.from(Instant.ofEpochMilli(fight.start)),
|
|
fight.spielmodus,
|
|
fight.map,
|
|
fight.blueTeam,
|
|
fight.redTeam,
|
|
fight.spectatePort
|
|
)
|
|
if (fight.group != null) {
|
|
if (fight.group != "null") {
|
|
Groups.setGroup(eventFight.fightID, fight.group)
|
|
}
|
|
}
|
|
call.respond(HttpStatusCode.Created, ResponseEventFight(eventFight))
|
|
}
|
|
route("/{fight}") {
|
|
put {
|
|
val fightId = call.parameters["fight"]?.toIntOrNull()
|
|
if (fightId == null) {
|
|
call.respond(HttpStatusCode.BadRequest, ResponseError("Invalid fight ID"))
|
|
return@put
|
|
}
|
|
val fight = EventFight.get(fightId)
|
|
if (fight == null) {
|
|
call.respond(HttpStatusCode.NotFound, ResponseError("Fight not found"))
|
|
return@put
|
|
}
|
|
val updateFight = call.receiveNullable<UpdateEventFight>()
|
|
if (updateFight == null) {
|
|
call.respond(HttpStatusCode.BadRequest, ResponseError("Invalid body"))
|
|
return@put
|
|
}
|
|
|
|
val teamBlue = updateFight.blueTeam ?: fight.teamBlue
|
|
val teamRed = updateFight.redTeam ?: fight.teamRed
|
|
val start = updateFight.start?.let { Timestamp.from(Instant.ofEpochMilli(it)) } ?: fight.startTime
|
|
val spielmodus = updateFight.spielmodus ?: fight.spielmodus
|
|
val map = updateFight.map ?: fight.map
|
|
val spectatePort = updateFight.spectatePort ?: fight.spectatePort
|
|
|
|
if (updateFight.group != null) {
|
|
if (updateFight.group == "null") {
|
|
Groups.resetGroup(fightId, true)
|
|
} else {
|
|
Groups.setGroup(fightId, updateFight.group)
|
|
}
|
|
}
|
|
fight.update(start, spielmodus, map, teamBlue, teamRed, spectatePort)
|
|
call.respond(HttpStatusCode.OK, ResponseEventFight(fight))
|
|
}
|
|
delete {
|
|
val fightId = call.parameters["fight"]?.toIntOrNull()
|
|
if (fightId == null) {
|
|
call.respond(HttpStatusCode.BadRequest, ResponseError("Invalid fight ID"))
|
|
return@delete
|
|
}
|
|
val fight = EventFight.get(fightId)
|
|
if (fight == null) {
|
|
call.respond(HttpStatusCode.NotFound, ResponseError("Fight not found"))
|
|
return@delete
|
|
}
|
|
fight.delete()
|
|
call.respond(HttpStatusCode.OK)
|
|
}
|
|
|
|
}
|
|
}
|
|
} |