forked from Cookies_Github_mirror/AquaDX
[O] Refactor maimai2 repositories
This commit is contained in:
@@ -113,7 +113,7 @@ class CardController(
|
||||
*
|
||||
* Assumption: The card is already linked to the user.
|
||||
*/
|
||||
suspend fun <T : IGenericUserData> migrateCard(repo: GenericUserDataRepo<T, *>, card: Card): Bool
|
||||
suspend fun <T : IGenericUserData> migrateCard(repo: GenericUserDataRepo<T>, card: Card): Bool
|
||||
{
|
||||
// Check if data already exists in the user's ghost card
|
||||
async { repo.findByCard(card.aquaUser!!.ghostCard) }?.let {
|
||||
@@ -130,7 +130,7 @@ suspend fun <T : IGenericUserData> migrateCard(repo: GenericUserDataRepo<T, *>,
|
||||
return true
|
||||
}
|
||||
|
||||
suspend fun getSummaryFor(repo: GenericUserDataRepo<*, *>, card: Card): Map<Str, Any>?
|
||||
suspend fun getSummaryFor(repo: GenericUserDataRepo<*>, card: Card): Map<Str, Any>?
|
||||
{
|
||||
val data = async { repo.findByCard(card) } ?: return null
|
||||
return mapOf(
|
||||
|
||||
@@ -13,14 +13,14 @@ import org.springframework.web.bind.annotation.RestController
|
||||
@RestController
|
||||
@API("api/v2/game/chu3")
|
||||
class Chusan(
|
||||
val us: AquaUserServices,
|
||||
override val us: AquaUserServices,
|
||||
override val playlogRepo: UserPlaylogRepository,
|
||||
override val userDataRepo: UserDataRepository,
|
||||
val userGeneralDataRepository: UserGeneralDataRepository
|
||||
): GameApiController("chu3")
|
||||
{
|
||||
override suspend fun trend(@RP username: Str): List<TrendOut> = us.cardByName(username) { card ->
|
||||
findTrend(playlogRepo.findByUser_Card_ExtId(card.extId)
|
||||
findTrend(playlogRepo.findByUserCardExtId(card.extId)
|
||||
.map { TrendLog(it.playDate.toString(), it.playerRating) })
|
||||
}
|
||||
|
||||
@@ -39,8 +39,4 @@ class Chusan(
|
||||
|
||||
genericUserSummary(card, ratingComposition)
|
||||
}
|
||||
|
||||
override suspend fun recent(@RP username: Str) = us.cardByName(username) { card ->
|
||||
playlogRepo.findByUser_Card_ExtId(card.extId)
|
||||
}
|
||||
}
|
||||
@@ -40,11 +40,6 @@ fun findTrend(log: List<TrendLog>): List<TrendOut> {
|
||||
return trend
|
||||
}
|
||||
|
||||
interface GenericPlaylogRepo {
|
||||
fun findByUserCardExtId(extId: Long): List<IGenericGamePlaylog>
|
||||
fun findById(id: Long): Optional<IGenericGamePlaylog>
|
||||
}
|
||||
|
||||
fun List<IGenericGamePlaylog>.acc() = if (isEmpty()) 0.0 else sumOf { it.achievement }.toDouble() / size / 10000.0
|
||||
|
||||
fun GameApiController.genericUserSummary(
|
||||
|
||||
@@ -5,22 +5,20 @@ import ext.RP
|
||||
import ext.Str
|
||||
import icu.samnyan.aqua.net.db.AquaUserServices
|
||||
import icu.samnyan.aqua.net.utils.*
|
||||
import icu.samnyan.aqua.sega.maimai2.dao.userdata.UserDataRepository
|
||||
import icu.samnyan.aqua.sega.maimai2.dao.userdata.UserGeneralDataRepository
|
||||
import icu.samnyan.aqua.sega.maimai2.dao.userdata.UserPlaylogRepository
|
||||
import icu.samnyan.aqua.sega.maimai2.dao.userdata.*
|
||||
import org.springframework.web.bind.annotation.RestController
|
||||
|
||||
@RestController
|
||||
@API("api/v2/game/mai2")
|
||||
class Maimai2(
|
||||
val us: AquaUserServices,
|
||||
override val us: AquaUserServices,
|
||||
override val playlogRepo: UserPlaylogRepository,
|
||||
override val userDataRepo: UserDataRepository,
|
||||
val userGeneralDataRepository: UserGeneralDataRepository
|
||||
): GameApiController("mai2")
|
||||
{
|
||||
override suspend fun trend(@RP username: Str): List<TrendOut> = us.cardByName(username) { card ->
|
||||
findTrend(playlogRepo.findByUser_Card_ExtId(card.extId)
|
||||
findTrend(playlogRepo.findByUserCardExtId(card.extId)
|
||||
.map { TrendLog(it.playDate, it.afterRating) })
|
||||
}
|
||||
|
||||
@@ -38,8 +36,4 @@ class Maimai2(
|
||||
|
||||
genericUserSummary(card, ratingComposition)
|
||||
}
|
||||
|
||||
override suspend fun recent(@RP username: Str) = us.cardByName(username) { card ->
|
||||
playlogRepo.findByUser_Card_ExtId(card.extId)
|
||||
}
|
||||
}
|
||||
@@ -1,12 +1,14 @@
|
||||
package icu.samnyan.aqua.net.games
|
||||
|
||||
import ext.*
|
||||
import icu.samnyan.aqua.net.db.AquaUserServices
|
||||
import icu.samnyan.aqua.net.utils.*
|
||||
import icu.samnyan.aqua.sega.general.model.Card
|
||||
import kotlinx.serialization.Serializable
|
||||
import org.springframework.data.jpa.repository.JpaRepository
|
||||
import org.springframework.data.jpa.repository.Query
|
||||
import org.springframework.data.repository.NoRepositoryBean
|
||||
import java.util.*
|
||||
import kotlin.jvm.optionals.getOrNull
|
||||
|
||||
data class TrendOut(val date: String, val rating: Int, val plays: Int)
|
||||
@@ -78,14 +80,6 @@ interface IGenericUserData {
|
||||
var card: Card?
|
||||
}
|
||||
|
||||
@NoRepositoryBean
|
||||
interface GenericUserDataRepo<T : IGenericUserData, ID> : JpaRepository<T, ID> {
|
||||
fun findByCard(card: Card): T?
|
||||
|
||||
@Query("select count(*) from #{#entityName} where playerRating > :rating")
|
||||
fun getRanking(rating: Int): Long
|
||||
}
|
||||
|
||||
interface IGenericGamePlaylog {
|
||||
val musicId: Int
|
||||
val level: Int
|
||||
@@ -98,6 +92,19 @@ interface IGenericGamePlaylog {
|
||||
val isAllPerfect: Boolean
|
||||
}
|
||||
|
||||
@NoRepositoryBean
|
||||
interface GenericUserDataRepo<T : IGenericUserData> : JpaRepository<T, Long> {
|
||||
fun findByCard(card: Card): T?
|
||||
|
||||
@Query("select count(*) from #{#entityName} where playerRating > :rating")
|
||||
fun getRanking(rating: Int): Long
|
||||
}
|
||||
|
||||
@NoRepositoryBean
|
||||
interface GenericPlaylogRepo<T: IGenericGamePlaylog> : JpaRepository<T, Long> {
|
||||
fun findByUserCardExtId(extId: Long): List<T>
|
||||
}
|
||||
|
||||
abstract class GameApiController(name: String) {
|
||||
val musicMapping: Map<Int, GenericMusicMeta> = GameApiController::class.java
|
||||
.getResourceAsStream("/meta/$name/music.json")
|
||||
@@ -106,24 +113,26 @@ abstract class GameApiController(name: String) {
|
||||
?.mapKeys { it.key.toInt() }
|
||||
?: emptyMap()
|
||||
|
||||
abstract val userDataRepo: GenericUserDataRepo<*, *>
|
||||
abstract val playlogRepo: GenericPlaylogRepo
|
||||
abstract val us: AquaUserServices
|
||||
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("recent")
|
||||
abstract suspend fun recent(@RP username: String): List<IGenericGamePlaylog>
|
||||
suspend fun recent(@RP username: String): List<IGenericGamePlaylog> = us.cardByName(username) { card ->
|
||||
playlogRepo.findByUserCardExtId(card.extId)
|
||||
}
|
||||
|
||||
|
||||
private val rankingCache = mutableMapOf<String, Pair<Long, List<GenericRankingPlayer>>>()
|
||||
private var rankingCache: Pair<Long, List<GenericRankingPlayer>>? = null
|
||||
@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) ->
|
||||
rankingCache?.let { (t, r) ->
|
||||
if (millis() - t < 120_000) return r
|
||||
}
|
||||
|
||||
@@ -142,7 +151,7 @@ abstract class GameApiController(name: String) {
|
||||
lastSeen = user.lastPlayDate.toString(),
|
||||
username = user.card!!.aquaUser?.username ?: "user${user.card!!.id}"
|
||||
)
|
||||
}.also { rankingCache[cacheKey] = millis() to it } // Update the cache
|
||||
}.also { rankingCache = millis() to it } // Update the cache
|
||||
}
|
||||
|
||||
@API("playlog")
|
||||
|
||||
@@ -11,7 +11,7 @@ import org.springframework.web.bind.annotation.RestController
|
||||
@RestController
|
||||
@API("api/v2/game/ongeki")
|
||||
class Ongeki(
|
||||
val us: AquaUserServices,
|
||||
override val us: AquaUserServices,
|
||||
override val playlogRepo: UserPlaylogRepository,
|
||||
override val userDataRepo: UserDataRepository,
|
||||
val userGeneralDataRepository: UserGeneralDataRepository
|
||||
@@ -31,8 +31,4 @@ class Ongeki(
|
||||
|
||||
genericUserSummary(card, mapOf())
|
||||
}
|
||||
|
||||
override suspend fun recent(username: String) = us.cardByName(username) { card ->
|
||||
playlogRepo.findByUser_Card_ExtId(card.extId)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user