From da61b1a3e73fa554d05692600ebd3e00ca5e355d Mon Sep 17 00:00:00 2001 From: Azalea <22280294+hykilpikonna@users.noreply.github.com> Date: Sat, 28 Dec 2024 00:18:15 -0500 Subject: [PATCH] [+] Paged post process --- src/main/java/ext/Ext.kt | 2 ++ .../samnyan/aqua/sega/chusan/ChusanApis.kt | 8 +++++--- .../samnyan/aqua/sega/general/BaseHandler.kt | 19 ++++++++++++------- 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/src/main/java/ext/Ext.kt b/src/main/java/ext/Ext.kt index 4a955c5c..3378516a 100644 --- a/src/main/java/ext/Ext.kt +++ b/src/main/java/ext/Ext.kt @@ -41,6 +41,7 @@ typealias Bool = Boolean typealias JavaSerializable = java.io.Serializable typealias JDict = Map +typealias MutJDict = MutableMap @Target(AnnotationTarget.CLASS, AnnotationTarget.FUNCTION, AnnotationTarget.PROPERTY, AnnotationTarget.PROPERTY_GETTER) @Retention(AnnotationRetention.RUNTIME) @@ -136,6 +137,7 @@ fun Map.toUrl() = entries.joinToString("&") { (k, v) -> "$k=$v" } fun String.firstCharLower() = replaceFirstChar { it.lowercase() } fun Any.long() = when (this) { + is Long -> this is Boolean -> if (this) 1L else 0 is Number -> toLong() is String -> toLong() diff --git a/src/main/java/icu/samnyan/aqua/sega/chusan/ChusanApis.kt b/src/main/java/icu/samnyan/aqua/sega/chusan/ChusanApis.kt index ed6f9bb8..e8d847de 100644 --- a/src/main/java/icu/samnyan/aqua/sega/chusan/ChusanApis.kt +++ b/src/main/java/icu/samnyan/aqua/sega/chusan/ChusanApis.kt @@ -88,7 +88,7 @@ val chusanInit: ChusanController.() -> Unit = { // Paged user list endpoints that has a kind in their request "GetUserActivity".pagedWithKind("userActivityList") { val kind = parsing { data["kind"]!!.int } - mapOf("kind" to kind) to { + mapOf("kind" to kind) grabs { db.userActivity.findAllByUser_Card_ExtIdAndKind(uid, kind).sortedBy { -it.sortNumber } } } @@ -97,15 +97,17 @@ val chusanInit: ChusanController.() -> Unit = { val rawIndex = data["nextIndex"]!!.long val kind = parsing { (rawIndex / 10000000000L).int } data["nextIndex"] = rawIndex % 10000000000L - mapOf("itemKind" to kind) to { + mapOf("itemKind" to kind) grabs { // TODO: All unlock db.userItem.findAllByUser_Card_ExtIdAndItemKind(uid, kind) + } postProcess { + it["nextIndex"] = rawIndex + (kind * 10000000000L) } } "GetUserFavoriteItem".pagedWithKind("userFavoriteItemList") { val kind = parsing { data["kind"]!!.int } - mapOf("kind" to kind) to { + mapOf("kind" to kind) grabs { // TODO: Actually store this info at UpsertUserAll val fav = when (kind) { 1 -> "favorite_music" diff --git a/src/main/java/icu/samnyan/aqua/sega/general/BaseHandler.kt b/src/main/java/icu/samnyan/aqua/sega/general/BaseHandler.kt index 93e47673..50726065 100644 --- a/src/main/java/icu/samnyan/aqua/sega/general/BaseHandler.kt +++ b/src/main/java/icu/samnyan/aqua/sega/general/BaseHandler.kt @@ -25,7 +25,9 @@ typealias SpecialHandler = RequestContext.() -> Any? fun BaseHandler.toSpecial() = { ctx: RequestContext -> handle(ctx.data) } typealias PagedHandler = RequestContext.() -> List -typealias AddFn = RequestContext.() -> Pair?, PagedHandler> +typealias AddFn = RequestContext.() -> PagedProcessor +typealias PagePost = (MutJDict) -> Unit +data class PagedProcessor(val add: JDict?, val fn: PagedHandler, var post: PagePost? = null) // A very :3 way of declaring APIs abstract class MeowApi(val serialize: (String, Any?) -> String) { @@ -37,14 +39,14 @@ abstract class MeowApi(val serialize: (String, Any?) -> String) { val pageCache = mutableMapOf>>() fun String.pagedWithKind(key: String, addFn: AddFn) = this api@ { - val (maybeAdd, fn) = addFn() - val add = maybeAdd ?: emptyMap() + val p = addFn() + val add = p.add ?: emptyMap() - if (nextIndex == -1) return@api fn().let { mapOf("userId" to uid, "length" to it.size, key to it) + add } + if (nextIndex == -1) return@api p.fn(this).let { mapOf("userId" to uid, "length" to it.size, key to it) + add } // Try to get cache val cacheKey = "$key:$uid:$add" - val list = pageCache.getOrPut(cacheKey) { fn().let { + val list = pageCache.getOrPut(cacheKey) { p.fn(this).let { if (it.size > maxCount) millis() to it else return@api mapOf("userId" to uid, "length" to it.size, "nextIndex" to -1, key to it) + add } }.r @@ -60,9 +62,12 @@ abstract class MeowApi(val serialize: (String, Any?) -> String) { } else pageCache[cacheKey] = millis() to list - mapOf("userId" to uid, "length" to lst.size, "nextIndex" to iAfter, key to lst) + add + (mapOf("userId" to uid, "length" to lst.size, "nextIndex" to iAfter, key to lst) + add).toMutableMap() + .also { p.post?.invoke(it) } } - fun String.paged(key: String, fn: PagedHandler) = pagedWithKind(key) { null to fn } + fun String.paged(key: String, fn: PagedHandler) = pagedWithKind(key) { PagedProcessor(null, fn) } + infix fun JDict.grabs(fn: PagedHandler) = PagedProcessor(this, fn, null) + infix fun PagedProcessor.postProcess(post: PagePost) = also { it.post = post } // Page cache cleanup every minute @Scheduled(fixedRate = (1000 * 60))