xyz.cssxsh.arknights.penguin.PenguinDataHolder.kt Maven / Gradle / Ivy
package xyz.cssxsh.arknights.penguin
import io.ktor.client.request.*
import kotlinx.coroutines.sync.*
import kotlinx.serialization.*
import xyz.cssxsh.arknights.*
import java.io.*
import java.util.*
public class PenguinDataHolder(override val folder: File, override val ignore: suspend (Throwable) -> Boolean) :
CacheDataHolder() {
public override val cache: MutableMap = EnumMap(PenguinDataType::class.java)
private suspend inline fun PenguinDataType.get(): T = mutex.withLock {
val raw = cache[this]
return if (raw == null) {
val read = CustomJson.decodeFromString(file.readText())
cache[this] = read
read
} else {
raw as T
}
}
override suspend fun load(key: PenguinDataType): Unit = mutex.withLock {
http.prepareGet(key.url).copyTo(target = key.file)
cache.remove(key)
}
@Deprecated(message = "raw is empty", level = DeprecationLevel.HIDDEN)
override suspend fun raw(key: PenguinDataType): List = emptyList()
override suspend fun clear(): Unit = Unit
public suspend fun items(): List- = PenguinDataType.ITEMS.get()
public suspend fun stages(): List
= PenguinDataType.STAGES.get()
public suspend fun zones(): List = PenguinDataType.ZONES.get()
public suspend fun period(): List = PenguinDataType.PERIOD.get()
public suspend fun stats(): ServerStats = PenguinDataType.STATS.get()
public suspend fun matrices(): List = PenguinDataType.RESULT_MATRIX.get().matrices
public suspend fun patterns(): List = PenguinDataType.RESULT_PATTERN.get().patterns
}