diff options
author | CalMWolfs <94038482+CalMWolfs@users.noreply.github.com> | 2023-09-02 20:06:42 +1000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-09-02 12:06:42 +0200 |
commit | b41e3fe173b49517cbc4eadae172106d947f3e3a (patch) | |
tree | a2ba441c533d6dc7f1af5791a2574d68af32410b /src | |
parent | 65b5057955dcf67c91ac8e95746f48c5fffae0b7 (diff) | |
download | skyhanni-b41e3fe173b49517cbc4eadae172106d947f3e3a.tar.gz skyhanni-b41e3fe173b49517cbc4eadae172106d947f3e3a.tar.bz2 skyhanni-b41e3fe173b49517cbc4eadae172106d947f3e3a.zip |
Fetch crop weights from the elite api (#421)
Loading farming weight values directly from elitebot.dev #421
Diffstat (limited to 'src')
-rw-r--r-- | src/main/java/at/hannibal2/skyhanni/features/garden/farming/FarmingWeightDisplay.kt | 33 |
1 files changed, 29 insertions, 4 deletions
diff --git a/src/main/java/at/hannibal2/skyhanni/features/garden/farming/FarmingWeightDisplay.kt b/src/main/java/at/hannibal2/skyhanni/features/garden/farming/FarmingWeightDisplay.kt index 71b69107b..30473f39f 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/garden/farming/FarmingWeightDisplay.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/garden/farming/FarmingWeightDisplay.kt @@ -66,6 +66,11 @@ class FarmingWeightDisplay { if (!isEnabled()) return if (!event.isMod(5)) return update() + + SkyHanniMod.coroutineScope.launch { + getCropWeights() + hasFetchedCropWeights = true + } } companion object { @@ -403,7 +408,9 @@ class FarmingWeightDisplay { private fun CropType.getLocalCounter() = localCounter[this] ?: 0L - private fun CropType.getFactor() = factorPerCrop[this]!! + private fun CropType.getFactor(): Double { + return factorPerCrop[this] ?: backupFactors[this]!! + } fun lookUpCommand(it: Array<String>) { val name = if (it.size == 1) it[0] else LorenzUtils.getPlayerName() @@ -411,7 +418,25 @@ class FarmingWeightDisplay { LorenzUtils.chat("§e[SkyHanni] Opening Farming Profile from §b$name") } - private val factorPerCrop by lazy { + private val factorPerCrop = mutableMapOf<CropType, Double>() + private var attemptingCropWeightFetch = false + private var hasFetchedCropWeights = false + + private suspend fun getCropWeights() { + if (attemptingCropWeightFetch || hasFetchedCropWeights) return + attemptingCropWeightFetch = true + + val url = "https://api.elitebot.dev/weights" + val result = withContext(Dispatchers.IO) { APIUtil.getJSONResponse(url) }.asJsonObject + + for (crop in result.entrySet()) { + val cropType = CropType.entries.firstOrNull { it.cropName == crop.key } ?: continue + factorPerCrop[cropType] = crop.value.asDouble + } + } + + // still needed when first joining garden and if they cant make https requests + private val backupFactors by lazy { mapOf( CropType.WHEAT to 100_000.0, CropType.CARROT to 302_061.86, @@ -425,7 +450,7 @@ class FarmingWeightDisplay { CropType.CACTUS to 177_254.45, ) } - } - class UpcomingPlayer(val name: String, val weight: Double) + class UpcomingPlayer(val name: String, val weight: Double) + } }
\ No newline at end of file |