Newer
Older
Twilight / src / main / kotlin / net / taehui / twilight / system / PlatformIDSystem.kt
@Taehui Taehui on 5 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.io.IOException
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 savePlatformID() {
        ObjectMapper().writerWithDefaultPrettyPrinter()
            .writeValue(Paths.get("Platform ID.json").toFile().absoluteFile, platformIDAvatarIDMap)
        logInfo("Saved Platform ID")
    }

    fun loadPlatformID() {
        try {
            platformIDAvatarIDMap.putAll(
                ObjectMapper().readValue(Paths.get("Platform ID.json").toFile().absoluteFile,
                    object : TypeReference<ConcurrentHashMap<String, String>>() {})
            )
            platformIDAvatarIDMap.forEach { (platformID, avatarID) ->
                avatarIDPlatformIDMap[avatarID] = platformID
            }
            logInfo("Loaded Platform ID")
        } catch (e: IOException) {
            logFault(e)
        }
    }

    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)
    }
}