All Downloads are FREE. Search and download functionalities are using the official Maven repository.

xyz.cssxsh.arknights.penguin.PenguinDataHolder.kt Maven / Gradle / Ivy

There is a newer version: 2.3.1
Show newest version
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
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy