From: TheSaminator Date: Mon, 28 Feb 2022 22:34:56 +0000 (-0500) Subject: Various QoL improvements and refactorings X-Git-Url: https://gitweb.starshipfights.net/?a=commitdiff_plain;h=36c1c681dd517c07a0c35da7b941a478823e066c;p=starship-fights Various QoL improvements and refactorings --- diff --git a/src/jvmMain/kotlin/starshipfights/auth/providers.kt b/src/jvmMain/kotlin/starshipfights/auth/providers.kt index caf1697..a075e70 100644 --- a/src/jvmMain/kotlin/starshipfights/auth/providers.kt +++ b/src/jvmMain/kotlin/starshipfights/auth/providers.kt @@ -19,17 +19,21 @@ import kotlinx.coroutines.coroutineScope import kotlinx.coroutines.flow.toList import kotlinx.coroutines.launch import kotlinx.html.* -import kotlinx.serialization.json.JsonObject -import kotlinx.serialization.json.JsonPrimitive +import kotlinx.serialization.Serializable import org.litote.kmongo.* -import starshipfights.* +import starshipfights.CurrentConfiguration +import starshipfights.DiscordLogin import starshipfights.data.Id import starshipfights.data.admiralty.* import starshipfights.data.auth.User import starshipfights.data.auth.UserSession import starshipfights.data.createNonce -import starshipfights.game.* +import starshipfights.forbid +import starshipfights.game.Faction +import starshipfights.game.ShipType +import starshipfights.game.toUrlSlug import starshipfights.info.* +import starshipfights.redirect import java.time.Instant import java.time.temporal.ChronoUnit @@ -501,7 +505,9 @@ class ProductionAuthProvider(private val discordLogin: DiscordLogin) : AuthProvi agent = discordLogin.userAgent } - install(RateLimit) + install(RateLimit) { + jsonCodec = JsonClientCodec + } } override fun installAuth(conf: Authentication.Configuration) { @@ -563,11 +569,8 @@ class ProductionAuthProvider(private val discordLogin: DiscordLogin) : AuthProvi } } - val userInfo = JsonConfigCodec.parseToJsonElement(userInfoJson) as? JsonObject ?: redirect("/login") - val discordId = (userInfo["id"] as? JsonPrimitive)?.content ?: redirect("/login") - val discordUsername = (userInfo["username"] as? JsonPrimitive)?.content ?: redirect("/login") - val discordDiscriminator = (userInfo["discriminator"] as? JsonPrimitive)?.content ?: redirect("/login") - val discordAvatar = (userInfo["avatar"] as? JsonPrimitive)?.content + val userInfo = JsonClientCodec.decodeFromString(DiscordUserInfo.serializer(), userInfoJson) + val (discordId, discordUsername, discordDiscriminator, discordAvatar) = userInfo var redirectTo = "/me" @@ -591,7 +594,7 @@ class ProductionAuthProvider(private val discordLogin: DiscordLogin) : AuthProvi val userSession = UserSession( user = user.id, - clientAddresses = listOf(call.request.origin.remoteHost), + clientAddresses = if (user.logIpAddresses) listOf(call.request.origin.remoteHost) else emptyList(), userAgent = userAgent, expiration = Instant.now().plus(1, ChronoUnit.HOURS) ) @@ -620,3 +623,11 @@ object StateParameterManager : NonceManager { return nonces.remove(nonce) } } + +@Serializable +data class DiscordUserInfo( + val id: String, + val username: String, + val discriminator: String, + val avatar: String +) diff --git a/src/jvmMain/kotlin/starshipfights/auth/ratelimit.kt b/src/jvmMain/kotlin/starshipfights/auth/ratelimit.kt index 6440690..e339598 100644 --- a/src/jvmMain/kotlin/starshipfights/auth/ratelimit.kt +++ b/src/jvmMain/kotlin/starshipfights/auth/ratelimit.kt @@ -67,6 +67,5 @@ class RateLimit( data class RateLimitedResponse( val message: String, @SerialName("retry_after") - val retryAfter: Double, - val global: Boolean + val retryAfter: Double ) diff --git a/src/jvmMain/kotlin/starshipfights/auth/utils.kt b/src/jvmMain/kotlin/starshipfights/auth/utils.kt index 9c9c1ef..9808248 100644 --- a/src/jvmMain/kotlin/starshipfights/auth/utils.kt +++ b/src/jvmMain/kotlin/starshipfights/auth/utils.kt @@ -6,6 +6,7 @@ import io.ktor.http.* import io.ktor.request.* import io.ktor.sessions.* import io.ktor.util.* +import kotlinx.serialization.json.Json import starshipfights.forbid import starshipfights.data.Id import starshipfights.data.auth.User @@ -20,7 +21,7 @@ suspend fun Id.resolve(userAgent: String) = UserSession.get(this)?. } suspend fun UserSession.renewed(clientAddress: String) = copy( - expiration = Instant.now().plus(1, ChronoUnit.HOURS), + expiration = Instant.now().plus(2, ChronoUnit.HOURS), clientAddresses = if (User.get(user)?.logIpAddresses != true) emptyList() else if (clientAddresses.lastOrNull() != clientAddress) @@ -75,3 +76,7 @@ suspend fun ApplicationCall.receiveValidatedParameters(): Parameters { else forbid() } + +val JsonClientCodec = Json { + ignoreUnknownKeys = true +} diff --git a/src/jvmMain/kotlin/starshipfights/info/views_user.kt b/src/jvmMain/kotlin/starshipfights/info/views_user.kt index 610b1d5..a81ccb6 100644 --- a/src/jvmMain/kotlin/starshipfights/info/views_user.kt +++ b/src/jvmMain/kotlin/starshipfights/info/views_user.kt @@ -10,11 +10,11 @@ import org.litote.kmongo.and import org.litote.kmongo.eq import org.litote.kmongo.gt import org.litote.kmongo.or -import starshipfights.forbid import starshipfights.auth.* import starshipfights.data.Id import starshipfights.data.admiralty.* import starshipfights.data.auth.* +import starshipfights.forbid import starshipfights.game.* import starshipfights.redirect import java.time.Instant @@ -637,6 +637,32 @@ suspend fun ApplicationCall.manageAdmiralPage(): HTML.() -> Unit { } } } + section { + val currRank = admiral.rank + if (currRank.ordinal < AdmiralRank.values().size - 1) { + val nextRank = AdmiralRank.values()[currRank.ordinal + 1] + val reqAcumen = nextRank.minAcumen - currRank.minAcumen + val hasAcumen = admiral.acumen - currRank.minAcumen + + label { + h2 { +"Progress to Promotion" } + progress { + style = "width:100%;box-sizing:border-box" + max = "$reqAcumen" + value = "$hasAcumen" + +"$hasAcumen/$reqAcumen" + } + } + p { + +"${admiral.fullName} is $hasAcumen/$reqAcumen Acumen away from being promoted to ${nextRank.getDisplayName(admiral.faction)}" + } + } else { + h2 { +"Progress to Promotion" } + p { + +"${admiral.fullName} is at the maximum rank possible for the ${admiral.faction.navyName}." + } + } + } section { h2 { +"Manage Fleet" } p {