aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/at/hannibal2/skyhanni/features/bazaar/BazaarDataHolder.kt
blob: a149b09c13fd076ee9e210293884fb6716cae71c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package at.hannibal2.skyhanni.features.bazaar

import at.hannibal2.skyhanni.SkyHanniMod
import at.hannibal2.skyhanni.utils.APIUtil
import at.hannibal2.skyhanni.utils.ItemUtils.name
import at.hannibal2.skyhanni.utils.LorenzUtils
import at.hannibal2.skyhanni.utils.NEUItems
import at.hannibal2.skyhanni.utils.StringUtils.removeColor
import kotlinx.coroutines.launch
import kotlin.concurrent.fixedRateTimer

class BazaarDataHolder {

    companion object {
        private val bazaarData = mutableMapOf<String, BazaarData>()
        private var npcPrices = mapOf<String, Double>()
    }

    private fun loadNpcPrices(): MutableMap<String, Double> {
        val list = mutableMapOf<String, Double>()
        try {
            val itemsData = APIUtil.getJSONResponse("https://api.hypixel.net/resources/skyblock/items")
            for (element in itemsData["items"].asJsonArray) {
                val jsonObject = element.asJsonObject
                if (jsonObject.has("npc_sell_price")) {
                    val hypixelId = jsonObject["id"].asString
                    val npcPrice = jsonObject["npc_sell_price"].asDouble
                    val neuItemId = NEUItems.transHypixelNameToInternalName(hypixelId)
                    list[neuItemId] = npcPrice
                }
            }
        } catch (e: Throwable) {
            e.printStackTrace()
            LorenzUtils.error("Error while trying to read bazaar item list from api: " + e.message)
        }
        return list
    }

    fun start() {
        SkyHanniMod.coroutineScope.launch {
            npcPrices = loadNpcPrices()
        }

        fixedRateTimer(name = "skyhanni-bazaar-update", period = 10_000L) {
            bazaarData.clear()
        }
    }

    fun getData(internalName: String) = bazaarData[internalName] ?: createNewData(internalName)

    private fun createNewData(internalName: String): BazaarData {
        val displayName = NEUItems.getItemStack(internalName).name!!.removeColor()
        val sellPrice = NEUItems.getPrice(internalName, true)
        val buyPrice = NEUItems.getPrice(internalName, false)
        val npcPrice = npcPrices[internalName].let {
            if (it == null) {
                if (!ignoreNoNpcPrice(internalName)) {
                    LorenzUtils.debug("NPC price not found for item '$internalName'")
                }
                0.0
            } else it
        }

        val data = BazaarData(internalName, displayName, sellPrice, buyPrice, npcPrice)
        bazaarData[internalName] = data
        return data
    }

    private fun ignoreNoNpcPrice(internalName: String): Boolean {
        if (internalName.startsWith("TURBO_")) return true
        if (internalName == "PURPLE_CANDY") return true
        if (internalName == "JACOBS_TICKET") return true
        if (internalName == "RAW_SOULFLOW") return true

        return false
    }
}