forked from Cookies_Github_mirror/AquaDX
[O] Refactor
This commit is contained in:
@@ -1,10 +1,9 @@
|
||||
package icu.samnyan.aqua.net.games
|
||||
|
||||
import ext.API
|
||||
import ext.JSON
|
||||
import ext.RP
|
||||
import icu.samnyan.aqua.net.utils.IGenericGamePlaylog
|
||||
import ext.*
|
||||
import icu.samnyan.aqua.net.utils.*
|
||||
import kotlinx.serialization.Serializable
|
||||
import kotlin.jvm.optionals.getOrNull
|
||||
|
||||
data class TrendOut(val date: String, val rating: Int, val plays: Int)
|
||||
|
||||
@@ -70,14 +69,45 @@ abstract class GameApiController(name: String) {
|
||||
?.mapKeys { it.key.toInt() }
|
||||
?: emptyMap()
|
||||
|
||||
abstract val userDataRepo: GenericUserDataRepo<*, *>
|
||||
abstract val playlogRepo: GenericPlaylogRepo
|
||||
abstract val shownRanks: List<Pair<Int, String>>
|
||||
|
||||
@API("trend")
|
||||
abstract suspend fun trend(@RP username: String): List<TrendOut>
|
||||
@API("user-summary")
|
||||
abstract suspend fun userSummary(@RP username: String): GenericGameSummary
|
||||
@API("ranking")
|
||||
abstract suspend fun ranking(): List<GenericRankingPlayer>
|
||||
@API("playlog")
|
||||
abstract suspend fun playlog(@RP id: Long): IGenericGamePlaylog
|
||||
@API("recent")
|
||||
abstract suspend fun recent(@RP username: String): List<IGenericGamePlaylog>
|
||||
|
||||
|
||||
private val rankingCache = mutableMapOf<String, Pair<Long, List<GenericRankingPlayer>>>()
|
||||
@API("ranking")
|
||||
fun ranking(): List<GenericRankingPlayer> {
|
||||
// Read from cache if we just computed it less than 2 minutes ago
|
||||
val cacheKey = playlogRepo::class.java.name
|
||||
rankingCache[cacheKey]?.let { (t, r) ->
|
||||
if (millis() - t < 120_000) return r
|
||||
}
|
||||
|
||||
// TODO: pagination
|
||||
val players = userDataRepo.findAll().sortedByDescending { it.playerRating }
|
||||
return players.filter { it.card != null }.mapIndexed { i, user ->
|
||||
val plays = playlogRepo.findByUserCardExtId(user.card!!.extId)
|
||||
|
||||
GenericRankingPlayer(
|
||||
rank = i + 1,
|
||||
name = user.userName,
|
||||
accuracy = plays.acc(),
|
||||
rating = user.playerRating,
|
||||
allPerfect = plays.count { it.isAllPerfect },
|
||||
fullCombo = plays.count { it.isFullCombo },
|
||||
lastSeen = user.lastPlayDate.toString(),
|
||||
username = user.card!!.aquaUser?.username ?: "user${user.card!!.id}"
|
||||
)
|
||||
}.also { rankingCache[cacheKey] = millis() to it } // Update the cache
|
||||
}
|
||||
|
||||
@API("playlog")
|
||||
fun playlog(@RP id: Long): IGenericGamePlaylog = playlogRepo.findById(id).getOrNull() ?: (404 - "Playlog not found")
|
||||
}
|
||||
Reference in New Issue
Block a user