aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/at/hannibal2/skyhanni/features/garden/GardenBestCropTime.kt
blob: db8cb6e0727c4f394c59648e75aef3bed875cfb6 (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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package at.hannibal2.skyhanni.features.garden

import at.hannibal2.skyhanni.SkyHanniMod
import at.hannibal2.skyhanni.data.GardenCropMilestones
import at.hannibal2.skyhanni.data.GardenCropMilestones.Companion.getCounter
import at.hannibal2.skyhanni.features.garden.GardenAPI.Companion.addCropIcon
import at.hannibal2.skyhanni.features.garden.GardenAPI.Companion.getSpeed
import at.hannibal2.skyhanni.utils.LorenzUtils.addAsSingletonList
import at.hannibal2.skyhanni.utils.LorenzUtils.sorted
import at.hannibal2.skyhanni.utils.TimeUtils

class GardenBestCropTime {
    var display = listOf<List<Any>>()
    val timeTillNextCrop = mutableMapOf<CropType, Long>()
    private val config get() = SkyHanniMod.feature.garden

    fun drawBestDisplay(currentCrop: CropType?): List<List<Any>> {
        val newList = mutableListOf<List<Any>>()
        if (timeTillNextCrop.size < CropType.values().size) {
            updateTimeTillNextCrop()
        }

        val gardenExp = config.cropMilestoneBestType == 0
        val sorted = if (gardenExp) {
            val helpMap = mutableMapOf<CropType, Long>()
            for ((cropName, time) in timeTillNextCrop) {
                val currentTier = GardenCropMilestones.getTierForCrops(cropName.getCounter())
                val gardenExpForTier = getGardenExpForTier(currentTier + 1)
                val fakeTime = time / gardenExpForTier
                helpMap[cropName] = fakeTime
            }
            helpMap.sorted()
        } else {
            timeTillNextCrop.sorted()
        }

        val title = if (gardenExp) "§2Garden Experience" else "§bSkyBlock Level"
        if (config.cropMilestoneBestCompact) {
            newList.addAsSingletonList("§eBest Crop Time")
        } else {
            newList.addAsSingletonList("§eBest Crop Time §7($title§7)")
        }

        if (!config.cropMilestoneProgress) {
            newList.addAsSingletonList("§cCrop Milestone Progress Display is disabled!")
            return newList
        }

        if (sorted.isEmpty()) {
            newList.addAsSingletonList("§cFarm crops to add them to this list!")
            return newList
        }

        var number = 0
        for (crop in sorted.keys) {
            val millis = timeTillNextCrop[crop]!!
            val maxUnits = if (config.cropMilestoneBestCompact) 2 else -1
            val duration = TimeUtils.formatDuration(millis, maxUnits = maxUnits)
            val isCurrent = crop == currentCrop
            number++
            if (number > config.cropMilestoneShowOnlyBest && !isCurrent) continue

            val list = mutableListOf<Any>()
            list.add("§7$number# ")
            list.addCropIcon(crop)

            val color = if (isCurrent) "§e" else "§7"
            val contestFormat = if (GardenNextJacobContest.isNextCrop(crop)) "§n" else ""
            val nextTier = GardenCropMilestones.getTierForCrops(crop.getCounter()) + 1

            val cropName = if (!config.cropMilestoneBestCompact) crop.cropName + " " else ""
            val cropNameDisplay = "$color$contestFormat$cropName$nextTier§r"
            list.add("$cropNameDisplay §b$duration")

            if (gardenExp && !config.cropMilestoneBestCompact) {
                val gardenExpForTier = getGardenExpForTier(nextTier)
                list.add(" §7(§2$gardenExpForTier §7Exp)")
            }
            newList.add(list)
        }
        return newList
    }

    private fun getGardenExpForTier(gardenLevel: Int) = if (gardenLevel > 30) 300 else gardenLevel * 10

    fun updateTimeTillNextCrop() {
        for (crop in CropType.values()) {
            val speed = crop.getSpeed()
            if (speed == -1) continue

            val counter = crop.getCounter()
            val currentTier = GardenCropMilestones.getTierForCrops(counter)

            val cropsForCurrentTier = GardenCropMilestones.getCropsForTier(currentTier)
            val nextTier = currentTier + 1
            val cropsForNextTier = GardenCropMilestones.getCropsForTier(nextTier)

            val have = counter - cropsForCurrentTier
            val need = cropsForNextTier - cropsForCurrentTier

            val missing = need - have
            val missingTimeSeconds = missing / speed
            val millis = missingTimeSeconds * 1000
            timeTillNextCrop[crop] = millis
        }
    }
}