From b41e3fe173b49517cbc4eadae172106d947f3e3a Mon Sep 17 00:00:00 2001 From: CalMWolfs <94038482+CalMWolfs@users.noreply.github.com> Date: Sat, 2 Sep 2023 20:06:42 +1000 Subject: Fetch crop weights from the elite api (#421) Loading farming weight values directly from elitebot.dev #421 --- .../garden/farming/FarmingWeightDisplay.kt | 33 +++++++++++++++++++--- 1 file changed, 29 insertions(+), 4 deletions(-) (limited to 'src') 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) { 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() + 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 -- cgit