diff --git a/build.gradle.kts b/build.gradle.kts index 9c8c47d..88f6a7c 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -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") diff --git a/src/main/kotlin/de/steamwar/plugins/Plugins.kt b/src/main/kotlin/de/steamwar/plugins/Plugins.kt index 12dd88f..4c5e421 100644 --- a/src/main/kotlin/de/steamwar/plugins/Plugins.kt +++ b/src/main/kotlin/de/steamwar/plugins/Plugins.kt @@ -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 { call, cause -> - call.respondText(text = "500: $cause\n${cause.stackTraceToString()}", status = HttpStatusCode.InternalServerError) - } - } install(CORS) { allowMethod(HttpMethod.Options) allowMethod(HttpMethod.Get) diff --git a/src/main/kotlin/de/steamwar/routes/Data.kt b/src/main/kotlin/de/steamwar/routes/Data.kt index a489c18..208c0d2 100644 --- a/src/main/kotlin/de/steamwar/routes/Data.kt +++ b/src/main/kotlin/de/steamwar/routes/Data.kt @@ -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() + 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 }) diff --git a/src/main/kotlin/de/steamwar/routes/EventFights.kt b/src/main/kotlin/de/steamwar/routes/EventFights.kt index f60af91..e88eb11 100644 --- a/src/main/kotlin/de/steamwar/routes/EventFights.kt +++ b/src/main/kotlin/de/steamwar/routes/EventFights.kt @@ -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() + 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() - 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)) + } } } \ No newline at end of file diff --git a/src/main/kotlin/de/steamwar/sql/SQLWrapperImpl.kt b/src/main/kotlin/de/steamwar/sql/SQLWrapperImpl.kt index 882d939..9853b7f 100644 --- a/src/main/kotlin/de/steamwar/sql/SQLWrapperImpl.kt +++ b/src/main/kotlin/de/steamwar/sql/SQLWrapperImpl.kt @@ -5,37 +5,38 @@ import java.io.File import java.util.* import java.util.stream.Collectors -class SQLWrapperImpl: SQLWrapper { - override fun loadSchemTypes(tmpTypes: MutableList?, tmpFromDB: MutableMap?) { - 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?, tmpFromDB: MutableMap?) { + 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?, tmpFromDB: MutableMap?) = loadSchematicTypes(tmpTypes, tmpFromDB) override fun additionalExceptionMetadata(builder: StringBuilder) { builder.append("EventAPI")