Add Fight Delete

Dieser Commit ist enthalten in:
Chaoscaot 2022-12-24 14:38:17 +01:00
Ursprung 84a1ce87c6
Commit 0b670cd016
5 geänderte Dateien mit 85 neuen und 98 gelöschten Zeilen

Datei anzeigen

@ -23,17 +23,13 @@ repositories {
}
dependencies {
implementation("io.ktor:ktor-server-auto-head-response-jvm:$ktor_version")
implementation("io.ktor:ktor-server-core-jvm:$ktor_version")
implementation("io.ktor:ktor-server-caching-headers-jvm:$ktor_version")
implementation("io.ktor:ktor-server-compression-jvm:$ktor_version")
implementation("io.ktor:ktor-server-content-negotiation-jvm:$ktor_version")
implementation("io.ktor:ktor-server-cors-jvm:$ktor_version")
implementation("io.ktor:ktor-serialization-kotlinx-json-jvm:$ktor_version")
implementation("io.ktor:ktor-server-netty-jvm:$ktor_version")
implementation("ch.qos.logback:logback-classic:$logback_version")
implementation("io.ktor:ktor-server-host-common-jvm:2.2.1")
implementation("io.ktor:ktor-server-status-pages-jvm:2.2.1")
testImplementation("io.ktor:ktor-server-tests-jvm:$ktor_version")
testImplementation("org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version")
implementation("io.ktor:ktor-server-request-validation:$ktor_version")

Datei anzeigen

@ -1,46 +1,15 @@
package de.steamwar.plugins
import io.ktor.http.*
import io.ktor.http.content.*
import io.ktor.serialization.kotlinx.json.*
import io.ktor.server.application.*
import io.ktor.server.plugins.autohead.*
import io.ktor.server.plugins.cachingheaders.*
import io.ktor.server.plugins.compression.*
import io.ktor.server.plugins.contentnegotiation.*
import io.ktor.server.plugins.cors.routing.*
import io.ktor.server.plugins.statuspages.*
import io.ktor.server.response.*
import kotlinx.serialization.json.Json
data class Session(val name: String)
fun Application.configurePlugins() {
install(AutoHeadResponse)
install(CachingHeaders) {
options { call, outgoingContent ->
when (outgoingContent.contentType?.withoutParameters()) {
ContentType.Text.CSS -> CachingOptions(CacheControl.MaxAge(maxAgeSeconds = 24 * 60 * 60))
else -> null
}
}
}
install(Compression) {
gzip {
priority = 1.0
}
deflate {
priority = 10.0
minimumSize(1024) // condition
}
}
install(StatusPages) {
exception<Throwable> { call, cause ->
call.respondText(text = "500: $cause\n${cause.stackTraceToString()}", status = HttpStatusCode.InternalServerError)
}
}
install(CORS) {
allowMethod(HttpMethod.Options)
allowMethod(HttpMethod.Get)

Datei anzeigen

@ -1,8 +1,10 @@
package de.steamwar.routes
import de.steamwar.ResponseError
import de.steamwar.sql.SQLWrapperImpl
import de.steamwar.sql.SchematicType
import de.steamwar.sql.SteamwarUser
import de.steamwar.sql.loadSchematicTypes
import io.ktor.http.*
import io.ktor.server.application.*
import io.ktor.server.response.*
@ -20,7 +22,9 @@ data class ResponseUser(val id: Int, val name: String, val uuid: String)
fun Routing.configureDataRoutes() {
route("/data") {
get("/schematicTypes") {
call.respond(SchematicType.values().filter { !it.check() }.map { ResponseSchematicType(it.name(), it.toDB()) })
val types = mutableListOf<SchematicType>()
loadSchematicTypes(types, mutableMapOf())
call.respond(types.filter { !it.check() }.map { ResponseSchematicType(it.name(), it.toDB()) })
}
get("/gamemodes") {
call.respond(File("/configs/GameModes/").listFiles().filter { it.name.endsWith(".yml") && !it.name.endsWith(".kits.yml") }.map { it.nameWithoutExtension })

Datei anzeigen

@ -42,42 +42,59 @@ fun Routing.configureEventFightRoutes() {
val eventFight = EventFight.create(fight.event, Timestamp.from(Instant.ofEpochMilli(fight.start)), fight.spielmodus, fight.map, fight.blueTeam, fight.redTeam)
call.respond(HttpStatusCode.Created, ResponseEventFight(eventFight))
}
put("/{fight}") {
val fightId = call.parameters["fight"]?.toIntOrNull()
if (fightId == null) {
call.respond(HttpStatusCode.BadRequest, ResponseError("Invalid fight ID"))
return@put
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
}
if(updateFight.blueTeam != null && Team.get(updateFight.blueTeam) != null) {
fight.teamBlue = updateFight.blueTeam
}
if(updateFight.redTeam != null && Team.get(updateFight.redTeam) != null) {
fight.teamRed = updateFight.redTeam
}
if(updateFight.kampfleiter != null && SteamwarUser.get(updateFight.kampfleiter) != null) {
fight.kampfleiter = updateFight.kampfleiter
}
if(updateFight.start != null) {
fight.startTime = Timestamp.from(Instant.ofEpochMilli(updateFight.start))
}
if(updateFight.map != null) {
fight.map = updateFight.map
}
if(updateFight.spielmodus != null) {
fight.spielModus = updateFight.spielmodus
}
fight.update()
call.respond(HttpStatusCode.OK, ResponseEventFight(fight))
}
val fight = EventFight.get(fightId)
if (fight == null) {
call.respond(HttpStatusCode.NotFound, ResponseError("Fight not found"))
return@put
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)
}
val updateFight = call.receiveNullable<UpdateEventFight>()
if (updateFight == null) {
call.respond(HttpStatusCode.BadRequest, ResponseError("Invalid body"))
return@put
}
if(updateFight.blueTeam != null && Team.get(updateFight.blueTeam) != null) {
fight.teamBlue = updateFight.blueTeam
}
if(updateFight.redTeam != null && Team.get(updateFight.redTeam) != null) {
fight.teamRed = updateFight.redTeam
}
if(updateFight.kampfleiter != null && SteamwarUser.get(updateFight.kampfleiter) != null) {
fight.kampfleiter = updateFight.kampfleiter
}
if(updateFight.start != null) {
fight.startTime = Timestamp.from(Instant.ofEpochMilli(updateFight.start))
}
if(updateFight.map != null) {
fight.map = updateFight.map
}
if(updateFight.spielmodus != null) {
fight.spielModus = updateFight.spielmodus
}
fight.update()
call.respond(HttpStatusCode.OK, ResponseEventFight(fight))
}
}
}

Datei anzeigen

@ -5,37 +5,38 @@ import java.io.File
import java.util.*
import java.util.stream.Collectors
class SQLWrapperImpl: SQLWrapper {
override fun loadSchemTypes(tmpTypes: MutableList<SchematicType>?, tmpFromDB: MutableMap<String, SchematicType>?) {
val folder = File("/configs/GameModes")
if (folder.exists()) {
for (configFile in Arrays.stream(folder.listFiles { _, name -> name.endsWith(".yml") && !name.endsWith(".kits.yml") })
.sorted().collect(Collectors.toList())) {
val config: YamlConfiguration = YamlConfiguration.loadConfiguration(configFile)
if (!config.isConfigurationSection("Schematic")) continue
val type: String = config.getString("Schematic.Type")!!
val shortcut: String = config.getString("Schematic.Shortcut")!!
if (tmpFromDB!!.containsKey(type.lowercase(Locale.getDefault()))) continue
var checktype: SchematicType? = null
val material: String = config.getString("Schematic.Material", "STONE_BUTTON")!!
if (!config.getStringList("CheckQuestions").isEmpty()) {
checktype = SchematicType("C$type", "C$shortcut", SchematicType.Type.CHECK_TYPE, null, material)
tmpTypes!!.add(checktype)
tmpFromDB[checktype.toDB()] = checktype
}
val current = SchematicType(
type,
shortcut,
if (config.isConfigurationSection("Server")) SchematicType.Type.FIGHT_TYPE else SchematicType.Type.NORMAL,
checktype,
material
)
tmpTypes!!.add(current)
tmpFromDB[type.lowercase(Locale.getDefault())] = current
fun loadSchematicTypes(tmpTypes: MutableList<SchematicType>?, tmpFromDB: MutableMap<String, SchematicType>?) {
val folder = File("/configs/GameModes")
if (folder.exists()) {
for (configFile in Arrays.stream(folder.listFiles { _, name -> name.endsWith(".yml") && !name.endsWith(".kits.yml") })
.sorted().collect(Collectors.toList())) {
val config: YamlConfiguration = YamlConfiguration.loadConfiguration(configFile)
if (!config.isConfigurationSection("Schematic")) continue
val type: String = config.getString("Schematic.Type")!!
val shortcut: String = config.getString("Schematic.Shortcut")!!
if (tmpFromDB!!.containsKey(type.lowercase(Locale.getDefault()))) continue
var checktype: SchematicType? = null
val material: String = config.getString("Schematic.Material", "STONE_BUTTON")!!
if (!config.getStringList("CheckQuestions").isEmpty()) {
checktype = SchematicType("C$type", "C$shortcut", SchematicType.Type.CHECK_TYPE, null, material)
tmpTypes!!.add(checktype)
tmpFromDB[checktype.toDB()] = checktype
}
val current = SchematicType(
type,
shortcut,
if (config.isConfigurationSection("Server")) SchematicType.Type.FIGHT_TYPE else SchematicType.Type.NORMAL,
checktype,
material
)
tmpTypes!!.add(current)
tmpFromDB[type.lowercase(Locale.getDefault())] = current
}
}
}
class SQLWrapperImpl: SQLWrapper {
override fun loadSchemTypes(tmpTypes: MutableList<SchematicType>?, tmpFromDB: MutableMap<String, SchematicType>?) = loadSchematicTypes(tmpTypes, tmpFromDB)
override fun additionalExceptionMetadata(builder: StringBuilder) {
builder.append("EventAPI")