summaryrefslogtreecommitdiff
path: root/src/main/java/at/hannibal2/skyhanni/features
diff options
context:
space:
mode:
authorCalMWolfs <94038482+CalMWolfs@users.noreply.github.com>2024-04-12 23:17:26 +1000
committerGitHub <noreply@github.com>2024-04-12 15:17:26 +0200
commit7960d2ad27ac68ea9dcfaf848bd611b13db0af87 (patch)
treea875bcc3b55c501d27cdcbfa6d7af1260556efab /src/main/java/at/hannibal2/skyhanni/features
parent30b0321f8c7de975375252c7a49b03baa605db6f (diff)
downloadskyhanni-7960d2ad27ac68ea9dcfaf848bd611b13db0af87.tar.gz
skyhanni-7960d2ad27ac68ea9dcfaf848bd611b13db0af87.tar.bz2
skyhanni-7960d2ad27ac68ea9dcfaf848bd611b13db0af87.zip
Backend: Update farming weight api links and format (#1308)
Co-authored-by: hannibal2 <24389977+hannibal00212@users.noreply.github.com>
Diffstat (limited to 'src/main/java/at/hannibal2/skyhanni/features')
-rw-r--r--src/main/java/at/hannibal2/skyhanni/features/garden/farming/FarmingWeightDisplay.kt72
-rw-r--r--src/main/java/at/hannibal2/skyhanni/features/garden/pests/PestType.kt9
2 files changed, 56 insertions, 25 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 71490fe2a..3025ae7d1 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
@@ -7,7 +7,8 @@ import at.hannibal2.skyhanni.config.enums.OutsideSbFeature
import at.hannibal2.skyhanni.data.HypixelData
import at.hannibal2.skyhanni.data.ProfileStorageData
import at.hannibal2.skyhanni.data.jsonobjects.other.EliteLeaderboardJson
-import at.hannibal2.skyhanni.data.jsonobjects.other.EliteWeightJson
+import at.hannibal2.skyhanni.data.jsonobjects.other.ElitePlayerWeightJson
+import at.hannibal2.skyhanni.data.jsonobjects.other.EliteWeightsJson
import at.hannibal2.skyhanni.data.jsonobjects.other.UpcomingLeaderboardPlayer
import at.hannibal2.skyhanni.events.GardenToolChangeEvent
import at.hannibal2.skyhanni.events.GuiRenderEvent
@@ -17,6 +18,7 @@ import at.hannibal2.skyhanni.events.ProfileJoinEvent
import at.hannibal2.skyhanni.features.garden.CropType
import at.hannibal2.skyhanni.features.garden.GardenAPI
import at.hannibal2.skyhanni.features.garden.farming.GardenCropSpeed.getSpeed
+import at.hannibal2.skyhanni.features.garden.pests.PestType
import at.hannibal2.skyhanni.test.command.ErrorManager
import at.hannibal2.skyhanni.utils.APIUtil
import at.hannibal2.skyhanni.utils.ChatUtils
@@ -31,9 +33,12 @@ import at.hannibal2.skyhanni.utils.TimeUtils
import at.hannibal2.skyhanni.utils.fromJson
import at.hannibal2.skyhanni.utils.renderables.Renderable
import com.google.gson.JsonObject
-import com.google.gson.reflect.TypeToken
+import com.google.gson.TypeAdapter
+import com.google.gson.stream.JsonReader
+import com.google.gson.stream.JsonWriter
import kotlinx.coroutines.launch
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
+import kotlin.time.Duration.Companion.minutes
import kotlin.time.Duration.Companion.seconds
class FarmingWeightDisplay {
@@ -106,13 +111,13 @@ class FarmingWeightDisplay {
private var display = emptyList<Renderable>()
private var profileId = ""
- private var lastLeaderboardUpdate = 0L
+ private var lastLeaderboardUpdate = SimpleTimeMark.farPast()
private var apiError = false
private var leaderboardPosition = -1
private var weight = -1.0
private var localWeight = 0.0
private var weightPerSecond = -1.0
- private var dirtyCropWeight = false
+ private var weightNeedsRecalculating = false
private var isLoadingWeight = false
private var isLoadingLeaderboard = false
private var rankGoal = -1
@@ -126,6 +131,25 @@ class FarmingWeightDisplay {
})
}
+ private val eliteWeightApiGson by lazy {
+ ConfigManager.createBaseGsonBuilder()
+ .registerTypeAdapter(CropType::class.java, object : TypeAdapter<CropType>() {
+ override fun write(out: JsonWriter, value: CropType) {}
+
+ override fun read(reader: JsonReader): CropType {
+ return CropType.getByName(reader.nextString())
+ }
+ }.nullSafe())
+ .registerTypeAdapter(PestType::class.java, object : TypeAdapter<PestType>() {
+ override fun write(out: JsonWriter, value: PestType) {}
+
+ override fun read(reader: JsonReader): PestType {
+ return PestType.getByName(reader.nextString())
+ }
+ }.nullSafe())
+ .create()
+ }
+
private val errorMessage by lazy {
listOf(
Renderable.clickAndHover(
@@ -199,7 +223,7 @@ class FarmingWeightDisplay {
if (!config.leaderboard) return ""
// Fetching new leaderboard position every 10.5 minutes
- if (System.currentTimeMillis() > lastLeaderboardUpdate + 630_000) {
+ if (lastLeaderboardUpdate.passedSince() > 10.5.minutes) {
loadLeaderboardIfAble()
}
@@ -212,11 +236,11 @@ class FarmingWeightDisplay {
}
private fun getWeight(): String {
- if (dirtyCropWeight) {
+ if (weightNeedsRecalculating) {
val values = calculateCollectionWeight().values
if (values.isNotEmpty()) {
localWeight = values.sum()
- dirtyCropWeight = false
+ weightNeedsRecalculating = false
}
}
@@ -321,8 +345,8 @@ class FarmingWeightDisplay {
weightPerSecond = -1.0
leaderboardPosition = -1
- dirtyCropWeight = true
- lastLeaderboardUpdate = 0
+ weightNeedsRecalculating = true
+ lastLeaderboardUpdate = SimpleTimeMark.farPast()
nextPlayers.clear()
rankGoal = -1
@@ -353,7 +377,7 @@ class FarmingWeightDisplay {
updateWeightPerSecond(crop, before, after, addedCounter)
- dirtyCropWeight = true
+ weightNeedsRecalculating = true
}
private fun updateWeightPerSecond(crop: CropType, before: Double, after: Double, diff: Int) {
@@ -381,7 +405,7 @@ class FarmingWeightDisplay {
}
GardenAPI.storage?.farmingWeight?.lastFarmingWeightLeaderboard =
leaderboardPosition
- lastLeaderboardUpdate = System.currentTimeMillis()
+ lastLeaderboardUpdate = SimpleTimeMark.now()
isLoadingLeaderboard = false
}
}
@@ -443,7 +467,7 @@ class FarmingWeightDisplay {
private fun toEliteLeaderboardJson(obj: JsonObject): EliteLeaderboardJson {
val jsonObject = JsonObject()
jsonObject.add("data", obj)
- return ConfigManager.gson.fromJson<EliteLeaderboardJson>(jsonObject)
+ return eliteWeightApiGson.fromJson<EliteLeaderboardJson>(jsonObject)
}
private fun loadWeight(localProfile: String) {
@@ -454,7 +478,8 @@ class FarmingWeightDisplay {
var error: Throwable? = null
try {
- val apiData = ConfigManager.gson.fromJson<EliteWeightJson>(apiResponse)
+
+ val apiData = eliteWeightApiGson.fromJson<ElitePlayerWeightJson>(apiResponse)
val selectedProfileId = apiData.selectedProfileId
var selectedProfileEntry = apiData.profiles.find { it.profileId == selectedProfileId }
@@ -468,7 +493,7 @@ class FarmingWeightDisplay {
weight = selectedProfileEntry.totalWeight
localCounter.clear()
- dirtyCropWeight = true
+ weightNeedsRecalculating = true
return
}
@@ -517,7 +542,7 @@ class FarmingWeightDisplay {
private fun CropType.getLocalCounter() = localCounter[this] ?: 0L
private fun CropType.getFactor(): Double {
- return factorPerCrop[this] ?: backupFactors[this] ?: error("Crop $this not in backupFactors!")
+ return cropWeight[this] ?: backupCropWeights[this] ?: error("Crop $this not in backupFactors!")
}
fun lookUpCommand(it: Array<String>) {
@@ -536,24 +561,21 @@ class FarmingWeightDisplay {
ChatUtils.chat("Opening Farming Profile of player §b$name")
}
- private val factorPerCrop = mutableMapOf<CropType, Double>()
+ private val cropWeight = mutableMapOf<CropType, Double>()
private var attemptingCropWeightFetch = false
private var hasFetchedCropWeights = false
private fun getCropWeights() {
if (attemptingCropWeightFetch || hasFetchedCropWeights) return
attemptingCropWeightFetch = true
- val url = "https://api.elitebot.dev/weights"
+ val url = "https://api.elitebot.dev/weights/all"
val apiResponse = APIUtil.getJSONResponse(url)
try {
- val apiData = ConfigManager.gson.fromJson<Map<String, Double>>(
- apiResponse,
- object : TypeToken<Map<String, Double>>() {}.type
- )
- for (crop in apiData) {
- val cropType = CropType.getByName(crop.key)
- factorPerCrop[cropType] = crop.value
+ val apiData = eliteWeightApiGson.fromJson<EliteWeightsJson>(apiResponse)
+ apiData.crops
+ for (crop in apiData.crops) {
+ cropWeight[crop.key] = crop.value
}
hasFetchedCropWeights = true
} catch (e: Exception) {
@@ -565,7 +587,7 @@ class FarmingWeightDisplay {
}
// still needed when first joining garden and if they cant make https requests
- private val backupFactors by lazy {
+ private val backupCropWeights by lazy {
mapOf(
CropType.WHEAT to 100_000.0,
CropType.CARROT to 302_061.86,
diff --git a/src/main/java/at/hannibal2/skyhanni/features/garden/pests/PestType.kt b/src/main/java/at/hannibal2/skyhanni/features/garden/pests/PestType.kt
index 831ce71a7..3525b5e44 100644
--- a/src/main/java/at/hannibal2/skyhanni/features/garden/pests/PestType.kt
+++ b/src/main/java/at/hannibal2/skyhanni/features/garden/pests/PestType.kt
@@ -93,4 +93,13 @@ enum class PestType(
"PEST_SLUG_MONSTER".asInternalName(),
CropType.MUSHROOM,
),
+ ;
+
+ companion object {
+ fun getByNameOrNull(name: String): PestType? {
+ return PestType.entries.firstOrNull { it.displayName.lowercase() == name }
+ }
+
+ fun getByName(name: String) = getByNameOrNull(name) ?: error("No valid pest type '$name'")
+ }
}