From f037ed7f43c06a22bb66a66725c07fda28da3f3e Mon Sep 17 00:00:00 2001 From: TheSaminator Date: Fri, 1 Jul 2022 20:42:26 -0400 Subject: [PATCH] Add labs --- .../campaign/endpoints_campaign.kt | 94 +++++++++++++++- .../starshipfights/campaign/views_campaign.kt | 101 ------------------ .../net/starshipfights/info/view_bar.kt | 63 +++++++++++ .../net/starshipfights/info/views_user.kt | 70 ++---------- .../net/starshipfights/labs/endpoints_labs.kt | 63 +++++++++++ .../kotlin/net/starshipfights/server.kt | 4 +- 6 files changed, 226 insertions(+), 169 deletions(-) delete mode 100644 src/jvmMain/kotlin/net/starshipfights/campaign/views_campaign.kt create mode 100644 src/jvmMain/kotlin/net/starshipfights/labs/endpoints_labs.kt diff --git a/src/jvmMain/kotlin/net/starshipfights/campaign/endpoints_campaign.kt b/src/jvmMain/kotlin/net/starshipfights/campaign/endpoints_campaign.kt index cebfa5f..3f09afe 100644 --- a/src/jvmMain/kotlin/net/starshipfights/campaign/endpoints_campaign.kt +++ b/src/jvmMain/kotlin/net/starshipfights/campaign/endpoints_campaign.kt @@ -6,16 +6,104 @@ import io.ktor.http.* import io.ktor.request.* import io.ktor.routing.* import io.ktor.util.* +import kotlinx.html.* import net.starshipfights.data.Id import net.starshipfights.game.ClientMode +import net.starshipfights.game.toUrlSlug import net.starshipfights.game.view +import net.starshipfights.labs.lab +import net.starshipfights.labs.labPost fun Routing.installCampaign() { - get("/test-cluster") { - call.respondHtml(block = call.campaignTestPage()) + lab("cluster", "Star Clusters") { + section { + h1 { +"Star Clusters" } + p { +"This is only a test and may not be indicative of the finished star-cluster feature for Starship Fights" } + form(action = "/labs/cluster", method = FormMethod.post) { + h2 { +"Generation Parameters" } + h3 { +"Background Color" } + for (color in StarClusterBackground.values()) { + val colorId = "color-${color.toUrlSlug()}" + label { + htmlFor = colorId + radioInput(name = "color") { + id = colorId + value = color.name + required = true + } + +Entities.nbsp + +color.displayName + br + } + } + h3 { +"Size" } + for (size in ClusterSize.values()) { + val sizeId = "size-${size.toUrlSlug()}" + label { + htmlFor = sizeId + radioInput(name = "size") { + id = sizeId + value = size.name + required = true + } + +Entities.nbsp + +size.displayName + br + } + } + h3 { +"Warp Lane Density" } + for (density in ClusterLaneDensity.values()) { + val densityId = "density-${density.toUrlSlug()}" + label { + htmlFor = densityId + radioInput(name = "density") { + id = densityId + value = density.name + required = true + } + +Entities.nbsp + +density.displayName + br + } + } + h3 { +"Planet Density" } + for (planets in ClusterPlanetDensity.values()) { + val planetsId = "planets-${planets.toUrlSlug()}" + label { + htmlFor = planetsId + radioInput(name = "planets") { + id = planetsId + value = planets.name + required = true + } + +Entities.nbsp + +planets.displayName + br + } + } + h3 { +"Eldritch Corruption" } + for (corruption in ClusterCorruption.values()) { + val corruptionId = "corruption-${corruption.toUrlSlug()}" + label { + htmlFor = corruptionId + radioInput(name = "corruption") { + id = corruptionId + value = corruption.name + required = true + } + +Entities.nbsp + +corruption.displayName + br + } + } + submitInput { + value = "Generate Star Cluster" + } + } + } } - post("/test-cluster") { + labPost("cluster") { val parameters = call.receiveParameters() val color = StarClusterBackground.valueOf(parameters.getOrFail("color")) diff --git a/src/jvmMain/kotlin/net/starshipfights/campaign/views_campaign.kt b/src/jvmMain/kotlin/net/starshipfights/campaign/views_campaign.kt deleted file mode 100644 index e3a008c..0000000 --- a/src/jvmMain/kotlin/net/starshipfights/campaign/views_campaign.kt +++ /dev/null @@ -1,101 +0,0 @@ -package net.starshipfights.campaign - -import io.ktor.application.* -import kotlinx.html.* -import net.starshipfights.game.toUrlSlug -import net.starshipfights.info.page -import net.starshipfights.info.standardNavBar - -suspend fun ApplicationCall.campaignTestPage(): HTML.() -> Unit { - return page( - "Star Cluster Test", - standardNavBar(), - null - ) { - section { - h1 { +"Star Cluster Test" } - p { +"This is only a test and may not be indicative of the finished star-cluster feature for Starship Fights" } - form(action = "/test-cluster", method = FormMethod.post) { - h2 { +"Generation Parameters" } - h3 { +"Background Color" } - for (color in StarClusterBackground.values()) { - val colorId = "color-${color.toUrlSlug()}" - label { - htmlFor = colorId - radioInput(name = "color") { - id = colorId - value = color.name - required = true - } - +Entities.nbsp - +color.displayName - br - } - } - h3 { +"Size" } - for (size in ClusterSize.values()) { - val sizeId = "size-${size.toUrlSlug()}" - label { - htmlFor = sizeId - radioInput(name = "size") { - id = sizeId - value = size.name - required = true - } - +Entities.nbsp - +size.displayName - br - } - } - h3 { +"Warp Lane Density" } - for (density in ClusterLaneDensity.values()) { - val densityId = "density-${density.toUrlSlug()}" - label { - htmlFor = densityId - radioInput(name = "density") { - id = densityId - value = density.name - required = true - } - +Entities.nbsp - +density.displayName - br - } - } - h3 { +"Planet Density" } - for (planets in ClusterPlanetDensity.values()) { - val planetsId = "planets-${planets.toUrlSlug()}" - label { - htmlFor = planetsId - radioInput(name = "planets") { - id = planetsId - value = planets.name - required = true - } - +Entities.nbsp - +planets.displayName - br - } - } - h3 { +"Eldritch Corruption" } - for (corruption in ClusterCorruption.values()) { - val corruptionId = "corruption-${corruption.toUrlSlug()}" - label { - htmlFor = corruptionId - radioInput(name = "corruption") { - id = corruptionId - value = corruption.name - required = true - } - +Entities.nbsp - +corruption.displayName - br - } - } - submitInput { - value = "Generate Star Cluster" - } - } - } - } -} diff --git a/src/jvmMain/kotlin/net/starshipfights/info/view_bar.kt b/src/jvmMain/kotlin/net/starshipfights/info/view_bar.kt index 7963688..d660058 100644 --- a/src/jvmMain/kotlin/net/starshipfights/info/view_bar.kt +++ b/src/jvmMain/kotlin/net/starshipfights/info/view_bar.kt @@ -1,6 +1,10 @@ package net.starshipfights.info import kotlinx.html.* +import net.starshipfights.data.auth.User +import net.starshipfights.data.auth.UserStatus +import net.starshipfights.data.auth.getTrophies +import net.starshipfights.data.auth.renderTrophy import net.starshipfights.game.ShipType import net.starshipfights.game.getDefiniteShortName @@ -38,3 +42,62 @@ data class PageNavSidebar(val contents: List) : Sidebar() { } } } + +data class UserProfileSidebar(val user: User, val isCurrentUser: Boolean, val hasOpenSessions: Boolean) : Sidebar() { + override fun TagConsumer<*>.display() { + if (user.showDiscordName) { + img(src = user.discordAvatarUrl) { + style = "border-radius:50%" + } + p { + style = "text-align:center" + +user.discordName + +"#" + +user.discordDiscriminator + } + } else { + img(src = user.anonymousAvatarUrl) { + style = "border-radius:50%" + } + } + for (trophy in user.getTrophies()) + renderTrophy(trophy) + + if (user.showUserStatus) { + p { + style = "text-align:center" + +when (user.status) { + UserStatus.IN_BATTLE -> "In Battle" + UserStatus.READY_FOR_BATTLE -> "In Battle" + UserStatus.IN_MATCHMAKING -> "In Matchmaking" + UserStatus.AVAILABLE -> if (hasOpenSessions) "Online" else "Offline" + } + } + p { + style = "text-align:center" + +"Registered at " + span(classes = "moment") { + style = "display:none" + +user.registeredAt.toEpochMilli().toString() + } + br + +"Last active at " + span(classes = "moment") { + style = "display:none" + +user.lastActivity.toEpochMilli().toString() + } + } + } + if (isCurrentUser) { + hr { style = "border-color:#036" } + div(classes = "list") { + div(classes = "item") { + a(href = "/admiral/new") { +"Create New Admiral" } + } + div(classes = "item") { + a(href = "/me/manage") { +"Edit Profile" } + } + } + } + } +} diff --git a/src/jvmMain/kotlin/net/starshipfights/info/views_user.kt b/src/jvmMain/kotlin/net/starshipfights/info/views_user.kt index 370bc22..ed52670 100644 --- a/src/jvmMain/kotlin/net/starshipfights/info/views_user.kt +++ b/src/jvmMain/kotlin/net/starshipfights/info/views_user.kt @@ -9,7 +9,9 @@ import kotlinx.html.* import net.starshipfights.auth.* import net.starshipfights.data.Id import net.starshipfights.data.admiralty.* -import net.starshipfights.data.auth.* +import net.starshipfights.data.auth.PreferredTheme +import net.starshipfights.data.auth.User +import net.starshipfights.data.auth.UserSession import net.starshipfights.forbid import net.starshipfights.game.* import net.starshipfights.redirect @@ -32,69 +34,9 @@ suspend fun ApplicationCall.userPage(): HTML.() -> Unit { val admirals = Admiral.filter(Admiral::owningUser eq user.id).toList() return page( - user.profileName, standardNavBar(), CustomSidebar { - if (user.showDiscordName) { - img(src = user.discordAvatarUrl) { - style = "border-radius:50%" - } - p { - style = "text-align:center" - +user.discordName - +"#" - +user.discordDiscriminator - } - } else { - img(src = user.anonymousAvatarUrl) { - style = "border-radius:50%" - } - } - for (trophy in user.getTrophies()) - renderTrophy(trophy) - - if (user.showUserStatus) { - p { - style = "text-align:center" - +when (user.status) { - UserStatus.IN_BATTLE -> "In Battle" - UserStatus.READY_FOR_BATTLE -> "In Battle" - UserStatus.IN_MATCHMAKING -> "In Matchmaking" - UserStatus.AVAILABLE -> if (hasOpenSessions) "Online" else "Offline" - } - } - p { - style = "text-align:center" - +"Registered at " - span(classes = "moment") { - style = "display:none" - +user.registeredAt.toEpochMilli().toString() - } - br - +"Last active at " - span(classes = "moment") { - style = "display:none" - +user.lastActivity.toEpochMilli().toString() - } - } - } - if (isCurrentUser) { - hr { style = "border-color:#036" } - div(classes = "list") { - div(classes = "item") { - a(href = "/admiral/new") { +"Create New Admiral" } - } - div(classes = "item") { - a(href = "/me/manage") { +"Edit Profile" } - } - } - } /*else if (currentUser != null) { - hr { style = "border-color:#036" } - div(classes = "list") { - div(classes = "item") { - a(href = "/user/${userId}/send") { +"Send Message" } - } - } - }*/ - } + user.profileName, + standardNavBar(), + UserProfileSidebar(user, isCurrentUser, hasOpenSessions) ) { section { h1 { +user.profileName } diff --git a/src/jvmMain/kotlin/net/starshipfights/labs/endpoints_labs.kt b/src/jvmMain/kotlin/net/starshipfights/labs/endpoints_labs.kt new file mode 100644 index 0000000..13af78e --- /dev/null +++ b/src/jvmMain/kotlin/net/starshipfights/labs/endpoints_labs.kt @@ -0,0 +1,63 @@ +package net.starshipfights.labs + +import io.ktor.application.* +import io.ktor.html.* +import io.ktor.routing.* +import io.ktor.util.pipeline.* +import kotlinx.html.* +import net.starshipfights.info.* + +private val labs = mutableMapOf() + +private val labsSidebar: Sidebar + get() = PageNavSidebar( + listOf(NavHead("Other Labs")) + labs.map { (slug, title) -> + NavLink("/labs/$slug", title) + } + ) + +fun Routing.lab(slug: String, title: String, pageBody: SECTIONS.() -> Unit) { + labs[slug] = title + + get("/labs/$slug") { + call.respondHtml( + block = call.page( + title, + call.standardNavBar(), + labsSidebar, + pageBody + ) + ) + } +} + +fun Routing.labPost(slug: String, action: PipelineInterceptor) { + post("/labs/$slug", action) +} + +fun Routing.installLabs() { + get("/labs") { + call.respondHtml( + block = call.page( + "Experimental Features", + call.standardNavBar(), + null + ) { + section { + h1 { +"Experimental Features" } + p { +"Sometimes it is desirable that an in-progress feature be demonstrated to the playerbase before it is fully ready to be integrated into the game. In that case, the progress on the feature made so far will be accessible here." } + if (labs.isEmpty()) + p { +"No labs are currently visible." } + else + ul { + for ((slug, title) in labs) { + li { + a(href = "/labs/$slug") { +title } + } + } + } + } + } + ) + } +} diff --git a/src/jvmMain/kotlin/net/starshipfights/server.kt b/src/jvmMain/kotlin/net/starshipfights/server.kt index e9acb8e..9f4441a 100644 --- a/src/jvmMain/kotlin/net/starshipfights/server.kt +++ b/src/jvmMain/kotlin/net/starshipfights/server.kt @@ -23,6 +23,7 @@ import net.starshipfights.data.ConnectionHolder import net.starshipfights.data.DataRoutines import net.starshipfights.game.installGame import net.starshipfights.info.* +import net.starshipfights.labs.installLabs import org.slf4j.event.Level import java.io.InputStream import java.util.concurrent.atomic.AtomicLong @@ -110,8 +111,9 @@ fun main() { routing { installPages() installGame() - installAdmin() installCampaign() + installAdmin() + installLabs() static("/static") { // I HAVE TO DO THIS MANUALLY -- 2.25.1