diff --git a/build.gradle.kts b/build.gradle.kts index b60f160..63c4867 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -9,14 +9,17 @@ repositories { mavenCentral() } +val ktorVersion = "2.3.6" + dependencies { testImplementation(kotlin("test")) - implementation("io.ktor:ktor-server-core-jvm:2.3.6") - implementation("io.ktor:ktor-server-netty-jvm:2.3.6") - implementation("io.ktor:ktor-server-status-pages-jvm:2.3.6") - implementation("io.ktor:ktor-server-default-headers-jvm:2.3.6") - implementation("io.ktor:ktor-server-html-builder:2.3.6") + implementation("io.ktor:ktor-server-core-jvm:$ktorVersion") + implementation("io.ktor:ktor-server-netty-jvm:$ktorVersion") + implementation("io.ktor:ktor-server-status-pages-jvm:$ktorVersion") + implementation("io.ktor:ktor-server-default-headers-jvm:$ktorVersion") + implementation("io.ktor:ktor-server-html-builder:$ktorVersion") + implementation("io.ktor:ktor-server-resources:$ktorVersion") implementation("org.jetbrains.kotlin-wrappers:kotlin-css:1.0.0-pre.650") implementation("com.jcraft:jsch:0.1.55") diff --git a/src/main/kotlin/yenon/squadcompanion/Main.kt b/src/main/kotlin/yenon/squadcompanion/Main.kt index e469b40..73e7e3c 100644 --- a/src/main/kotlin/yenon/squadcompanion/Main.kt +++ b/src/main/kotlin/yenon/squadcompanion/Main.kt @@ -7,6 +7,7 @@ import io.ktor.server.html.* import io.ktor.server.netty.* import io.ktor.server.plugins.statuspages.* import io.ktor.server.request.* +import io.ktor.server.resources.* import io.ktor.server.response.* import io.ktor.server.routing.* import kotlinx.coroutines.runBlocking @@ -53,6 +54,8 @@ object Main { call.respondText(text = "500: $writer", status = HttpStatusCode.InternalServerError) } } + install(Resources) + routing { get("/layers") { call.respondText( @@ -61,6 +64,7 @@ object Main { LayerInfoModule.layerList.joinToString("\n") ) } + get("/rotation") { call.respondText( contentType = ContentType.Text.Plain, @@ -68,18 +72,21 @@ object Main { LayerInfoModule.rotationList.joinToString("\n") ) } + get("/currentPlayers") { call.respondText( contentType = ContentType.Text.Plain, text = abstraction.getCurrentPlayers().toString() ) } + get("/overview") { val players = abstraction.getCurrentPlayers() val squadInfo = abstraction.getSquadList() call.respondHtmlTemplate(OverviewTemplate(players, squadInfo)) {} } + get("/overviewDemo") { val players = RconAbstraction.ListPlayersOutput( arrayListOf( @@ -117,28 +124,49 @@ object Main { ) ) ) - call.respondHtmlTemplate(OverviewTemplate(players, squadInfo)) {} } - get("/css") { - call.respondText(contentType = ContentType.Text.CSS, text = Css.cssString) - } + post("/kick") { val playerId = call.request.queryParameters["id"]!!.toLong() val reason = call.receiveText() abstraction.kickPlayer(playerId, reason) } + post("/ban") { val playerId = call.request.queryParameters["id"]!!.toLong() val time = call.request.queryParameters["time"]!! val reason = call.receiveText() abstraction.banPlayer(playerId, time, reason) } + post("/message") { val playerId = call.request.queryParameters["id"]!!.toLong() val message = call.receiveText() abstraction.messagePlayer(playerId, message) } + + get("resources/{path...}") { + val path = call.parameters["path"] + val streamedResource = javaClass.getResourceAsStream("/" + path!!) + + println(path) + + val extension = path.substring(path.lastIndexOf('.')) + + val type = ContentType.defaultForFileExtension(extension) + + println(type) + + if (streamedResource != null) { + call.respondOutputStream(type, HttpStatusCode.OK) { + streamedResource.copyTo(this) + } + streamedResource.close() + } else { + call.respondText(type, HttpStatusCode.NotFound) { "No resource found" } + } + } } }.start(true) } diff --git a/src/main/kotlin/yenon/squadcompanion/OverviewTemplate.kt b/src/main/kotlin/yenon/squadcompanion/OverviewTemplate.kt index d7eb55e..3d9487f 100644 --- a/src/main/kotlin/yenon/squadcompanion/OverviewTemplate.kt +++ b/src/main/kotlin/yenon/squadcompanion/OverviewTemplate.kt @@ -83,7 +83,7 @@ class OverviewTemplate( +squadListFaction.name } if (team.keys.isEmpty()) { - +"empty" + +"No people on this side." return } team.forEach { @@ -101,7 +101,7 @@ class OverviewTemplate( val teamB = players.players.filter { it.teamId == 2 }.groupBy { it.squadId } head { - styleLink("/css") + styleLink("/resources/main.css") } body { @@ -115,5 +115,4 @@ class OverviewTemplate( } } } - } \ No newline at end of file diff --git a/src/main/kotlin/yenon/squadcompanion/Css.kt b/src/main/resources/main.css similarity index 86% rename from src/main/kotlin/yenon/squadcompanion/Css.kt rename to src/main/resources/main.css index 7a72b49..86e80c8 100644 --- a/src/main/kotlin/yenon/squadcompanion/Css.kt +++ b/src/main/resources/main.css @@ -1,7 +1,3 @@ -package yenon.squadcompanion - -object Css { - val cssString = """ .teamView{ height: 100%; width: 100%; @@ -11,7 +7,8 @@ object Css { border-radius: 20px; width: 50%; margin: 6px; - padding: 6px; + padding: 8px; + padding-inline: 16px; } .colorBluFor{ background-color: #bbffff; @@ -50,6 +47,4 @@ object Css { } tr:nth-child(even) { background-color: rgba(0, 0, 0, 0.1); -} - """.trimIndent() } \ No newline at end of file