Newer
Older
Twilight / src / main / kotlin / net / taehui / twilight / system / PlatformIDSystem.kt
@Taehui Taehui on 8 Jul 1 KB v1.0-SNAPSHOT
package net.taehui.twilight.system

import com.fasterxml.jackson.core.type.TypeReference
import com.fasterxml.jackson.databind.ObjectMapper
import net.taehui.twilight.Logger
import java.nio.file.Paths
import java.util.concurrent.ConcurrentHashMap

object PlatformIDSystem : Logger {
    private var platformIDAvatarIDMap = ConcurrentHashMap<String, String>()
    private var avatarIDPlatformIDMap = ConcurrentHashMap<String, String>()

    fun loadPlatformID() {
        platformIDAvatarIDMap = ObjectMapper().readValue(Paths.get("Platform ID.json").toFile().absoluteFile,
            object : TypeReference<ConcurrentHashMap<String, String>>() {})
        avatarIDPlatformIDMap = ConcurrentHashMap(platformIDAvatarIDMap.map {
            Pair(it.value, it.key)
        }.toMap())

        logInfo("Loaded Platform ID")

        savePlatformID()
    }

    fun savePlatformID() {
        ObjectMapper().writerWithDefaultPrettyPrinter()
            .writeValue(Paths.get("Platform ID.json").toFile().absoluteFile, platformIDAvatarIDMap)
        logInfo("Saved Platform ID")
    }

    fun putPlatformID(platformID: String, avatarID: String) {
        platformIDAvatarIDMap[platformID] = avatarID
        avatarIDPlatformIDMap[avatarID] = platformID
    }

    fun wipePlatformID(platformID: String) {
        val avatarID = platformIDAvatarIDMap.remove(platformID)
        if (avatarID != null) {
            avatarIDPlatformIDMap.remove(avatarID)
        }
    }

    fun getAvatarID(platformID: String): String? {
        return platformIDAvatarIDMap[platformID]
    }

    fun getPlatformID(avatarID: String): String? {
        return avatarIDPlatformIDMap[avatarID]
    }

    fun hasPlatformID(platformID: String): Boolean {
        return platformIDAvatarIDMap.containsKey(platformID)
    }
}