Newer
Older
Twilight / src / main / kotlin / net / taehui / twilight / system / AbilityClassSystem.kt
package net.taehui.twilight.system

import com.fasterxml.jackson.annotation.JsonValue
import com.fasterxml.jackson.core.type.TypeReference
import com.fasterxml.jackson.databind.ObjectMapper
import net.taehui.twilight.Logger
import net.taehui.twilight.TwilightComponent
import java.io.IOException
import java.nio.file.Files

object AbilityClassSystem : Logger {
    enum class AbilityClassVariety(@JvmField val value: Int) {
        ETC(-1), INPUT_MODE_5K(0), INPUT_MODE_7K(1), INPUT_MODE_9K(2);

        @JsonValue
        fun getValue(): Int {
            return value
        }
    }

    private var drawings = arrayOf(emptyArray<ByteArray>(), emptyArray<ByteArray>(), emptyArray<ByteArray>())
    private var abilityClasses = emptyArray<Array<Double>>()
    var isLoading = false

    fun getDrawing(abilityClassVariety: AbilityClassVariety, abilityClass: Double): ByteArray {
        val drawings = drawings[abilityClassVariety.value]
        val abilityClasses = abilityClasses[abilityClassVariety.value]
        for (i in abilityClasses.indices) {
            if (abilityClass >= abilityClasses[i]) {
                return drawings[i]
            }
        }
        return drawings[drawings.size - 1]
    }

    fun getAbilityClasses(abilityClassVariety: AbilityClassVariety, abilityClass: Double): Array<Double> {
        val abilityClasses = abilityClasses[abilityClassVariety.value]
        for (i in abilityClasses.indices) {
            if (abilityClass >= abilityClasses[i]) {
                return if (i == 0) {
                    arrayOf(-1.0, -1.0)
                } else {
                    arrayOf(abilityClasses[i - 1] - abilityClass, abilityClasses[i - 1] - abilityClasses[i])
                }
            }
        }
        return arrayOf(
            abilityClasses[abilityClasses.size - 1],
            abilityClasses[abilityClasses.size - 1]
        )
    }

    fun loadAbilityClass() {
        isLoading = true
        try {
            val jm = ObjectMapper()
            abilityClasses = jm.readValue(
                TwilightComponent.ABILITY_CLASS_ENTRY_PATH.resolve("Default.json").toFile().absoluteFile,
                object : TypeReference<Array<Array<Double>>>() {})

            fun loadAbilityClass(abilityClassVariety: AbilityClassVariety) {
                fun loadDrawing(fileName: String): ByteArray {
                    return Files.readAllBytes(
                        TwilightComponent.ABILITY_CLASS_ENTRY_PATH.resolve(
                            when (abilityClassVariety) {
                                AbilityClassVariety.ETC -> ""
                                AbilityClassVariety.INPUT_MODE_5K -> "5K"
                                AbilityClassVariety.INPUT_MODE_7K -> "7K"
                                AbilityClassVariety.INPUT_MODE_9K -> "9K"
                            }
                        ).resolve(fileName)
                    )
                }

                drawings[abilityClassVariety.value] = arrayOf(
                    loadDrawing("S+1.png"),
                    loadDrawing("S+2.png"),
                    loadDrawing("S+3.png"),
                    loadDrawing("S+4.png"),
                    loadDrawing("S+5.png"),
                    loadDrawing("S1.png"),
                    loadDrawing("S2.png"),
                    loadDrawing("S3.png"),
                    loadDrawing("S4.png"),
                    loadDrawing("S5.png"),
                    loadDrawing("A+1.png"),
                    loadDrawing("A+2.png"),
                    loadDrawing("A+3.png"),
                    loadDrawing("A+4.png"),
                    loadDrawing("A+5.png"),
                    loadDrawing("A1.png"),
                    loadDrawing("A2.png"),
                    loadDrawing("A3.png"),
                    loadDrawing("A4.png"),
                    loadDrawing("A5.png"),
                    loadDrawing("B1.png"),
                    loadDrawing("B2.png"),
                    loadDrawing("B3.png"),
                    loadDrawing("B4.png"),
                    loadDrawing("B5.png"),
                    loadDrawing("C1.png"),
                    loadDrawing("C2.png"),
                    loadDrawing("C3.png"),
                    loadDrawing("C4.png"),
                    loadDrawing("C5.png"),
                    loadDrawing("D1.png"),
                    loadDrawing("D2.png"),
                    loadDrawing("D3.png"),
                    loadDrawing("D4.png"),
                    loadDrawing("D5.png"),
                    loadDrawing("F.png")
                )
            }

            loadAbilityClass(AbilityClassVariety.INPUT_MODE_5K)
            loadAbilityClass(AbilityClassVariety.INPUT_MODE_7K)
            loadAbilityClass(AbilityClassVariety.INPUT_MODE_9K)

            logInfo("Loaded Ability Class")

            jm.writerWithDefaultPrettyPrinter()
                .writeValue(
                    TwilightComponent.ABILITY_CLASS_ENTRY_PATH.resolve("Default.json").toFile().absoluteFile,
                    abilityClasses
                )
            logInfo("Saved Ability Class")
        } catch (e: IOException) {
            logFault(e)
        } finally {
            isLoading = false
        }
    }
}