Skip to content

Commit

Permalink
default back to graph api if not in top 5000
Browse files Browse the repository at this point in the history
  • Loading branch information
catgirlseraid committed May 19, 2024
1 parent 7f27a16 commit 3f4918a
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,14 @@ data class PestWeightData(
@Expose @SerializedName("values") val pestWeights: Map<PestType, Map<Int, Double>>
)

data class EliteCollectionGraphEntry(
@Expose val timestamp: Long,
@Expose val crops: Map<CropType, Long>,
)

data class EliteSkillGraphEntry(
@Expose val timestamp: Long,
@Expose val skills: Map<String, Long>,
)


Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import at.hannibal2.skyhanni.SkyHanniMod
import at.hannibal2.skyhanni.config.ConfigManager
import at.hannibal2.skyhanni.config.features.garden.EliteFarmingCollectionConfig.CropDisplay
import at.hannibal2.skyhanni.data.ClickType
import at.hannibal2.skyhanni.data.jsonobjects.other.EliteCollectionGraphEntry
import at.hannibal2.skyhanni.data.jsonobjects.other.EliteLeaderboard
import at.hannibal2.skyhanni.data.jsonobjects.repo.EliteAPISettingsJson
import at.hannibal2.skyhanni.events.BlockClickEvent
Expand Down Expand Up @@ -35,7 +36,6 @@ import com.google.gson.stream.JsonReader
import com.google.gson.stream.JsonWriter
import kotlinx.coroutines.launch
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
import net.minecraftforge.fml.common.network.FMLNetworkEvent
import java.util.UUID
import kotlin.time.Duration.Companion.minutes

Expand Down Expand Up @@ -72,6 +72,8 @@ object FarmingCollectionDisplay {
private var currentCollections = mutableMapOf<CropType, Long>()
private var lastFetchedCrop: CropType? = null

private var hasCollectionBeenFetched = false

private var lastBrokenCrop: CropType?
get() = SkyHanniMod.feature.storage.lastCropBroken
set(value) {
Expand Down Expand Up @@ -163,6 +165,7 @@ object FarmingCollectionDisplay {
}

private fun resetData() {
hasCollectionBeenFetched = false
lastLeaderboardFetch = SimpleTimeMark.farPast()
collectionRanks.clear()
collectionPlacements.clear()
Expand Down Expand Up @@ -251,6 +254,10 @@ object FarmingCollectionDisplay {
lastFetchedCrop = crop
currentCollections[crop] = data.amount

if (!hasCollectionBeenFetched && data.amount == 0L) {
hasCollectionBeenFetched = true
getCurrentCollection()
}
} catch (e: Exception) {
ErrorManager.logErrorWithData(
e,
Expand All @@ -264,17 +271,37 @@ object FarmingCollectionDisplay {
}
}

private fun getCurrentCollection() {
if (profileID == null) return
val url =
"https://api.elitebot.dev/Graph/${LorenzUtils.getPlayerUuid()}/${profileID!!.toDashlessUUID()}/crops?days=1"
val response = APIUtil.getJSONResponseAsElement(url)

try {
val data = eliteCollectionApiGson.fromJson<Array<EliteCollectionGraphEntry>>(response)

data.sortBy { it.timestamp }
currentCollections = data.lastOrNull()?.crops?.toMutableMap() ?: mutableMapOf()

} catch (e: Exception) {
ErrorManager.logErrorWithData(
e,
"Error loading user farming collection\n" +
"§eLoading the farming collection data from elitebot.dev failed!\n" +
"§eYou can re-enter the garden to try to fix the problem.\n" +
"§cIf this message repeats, please report it on Discord!\n",
"url" to url,
"apiResponse" to response,
)
}
}

private fun getEliteBotLeaderboardForCrop(crop: CropType) = when (crop) {
CropType.NETHER_WART -> "netherwart"
CropType.SUGAR_CANE -> "sugarcane"
else -> crop.simpleName
}

@SubscribeEvent
fun playerLogout(event: FMLNetworkEvent.ClientDisconnectionFromServerEvent) {
SkyHanniMod.feature.storage.lastCropBroken = lastBrokenCrop
}

private fun isEnabled() =
config.display && LorenzUtils.inSkyBlock && (GardenAPI.inGarden() || config.showOutsideGarden)
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import at.hannibal2.skyhanni.config.ConfigManager
import at.hannibal2.skyhanni.config.features.skillprogress.EliteSkillsDisplayConfig.SkillDisplay
import at.hannibal2.skyhanni.data.SkillExperience
import at.hannibal2.skyhanni.data.jsonobjects.other.EliteLeaderboard
import at.hannibal2.skyhanni.data.jsonobjects.other.EliteSkillGraphEntry
import at.hannibal2.skyhanni.data.jsonobjects.repo.EliteAPISettingsJson
import at.hannibal2.skyhanni.events.ConfigLoadEvent
import at.hannibal2.skyhanni.events.GuiRenderEvent
Expand All @@ -30,7 +31,6 @@ import at.hannibal2.skyhanni.utils.fromJson
import at.hannibal2.skyhanni.utils.renderables.Renderable
import kotlinx.coroutines.launch
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
import net.minecraftforge.fml.common.network.FMLNetworkEvent
import java.util.UUID
import kotlin.time.Duration.Companion.minutes
import kotlin.time.Duration.Companion.seconds
Expand Down Expand Up @@ -68,6 +68,7 @@ object SkillRankDisplay {
private var lastSkillFetched: String? = null
private var lastLeaderboardFetch = SimpleTimeMark.farPast()
private var lastXPGained = SimpleTimeMark.farPast()
private var hasSkillsBeenFetched = false

private var display = emptyList<Renderable>()

Expand Down Expand Up @@ -145,6 +146,7 @@ object SkillRankDisplay {
}

private fun resetData() {
hasSkillsBeenFetched = false
lastLeaderboardFetch = SimpleTimeMark.farPast()
skillRanks.clear()
skillPlacements.clear()
Expand Down Expand Up @@ -233,6 +235,10 @@ object SkillRankDisplay {
lastSkillFetched = skill
currentSkills[skill] = data.amount

if (!hasSkillsBeenFetched && data.amount == 0L) {
hasSkillsBeenFetched = true
getCurrentSkills()
}
} catch (e: Exception) {
ErrorManager.logErrorWithData(
e,
Expand All @@ -246,9 +252,29 @@ object SkillRankDisplay {
}
}

@SubscribeEvent
fun playerLogout(event: FMLNetworkEvent.ClientDisconnectionFromServerEvent) {
SkyHanniMod.feature.storage.lastSkillObtained = lastSkillGained
private fun getCurrentSkills() {
if (profileID == null) return
val url =
"https://api.elitebot.dev/Graph/${LorenzUtils.getPlayerUuid()}/${profileID!!.toDashlessUUID()}/skills?days=1"
val response = APIUtil.getJSONResponseAsElement(url)

try {
val data = eliteCollectionApiGson.fromJson<Array<EliteSkillGraphEntry>>(response)

data.sortBy { it.timestamp }
currentSkills = data.lastOrNull()?.skills?.toMutableMap() ?: mutableMapOf()

} catch (e: Exception) {
ErrorManager.logErrorWithData(
e,
"Error loading user skill\n" +
"§eLoading the skill data from elitebot.dev failed!\n" +
"§eYou can switch worlds to try to fix the problem.\n" +
"§cIf this message repeats, please report it on Discord!\n",
"url" to url,
"apiResponse" to response,
)
}
}

private fun isEnabled() = config.display && LorenzUtils.inSkyBlock
Expand Down

0 comments on commit 3f4918a

Please sign in to comment.