Skip to content

Commit

Permalink
changed api endpoint + saved last crop/skill to config
Browse files Browse the repository at this point in the history
  • Loading branch information
catgirlseraid committed May 19, 2024
1 parent ea26a8b commit 7f27a16
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 88 deletions.
10 changes: 10 additions & 0 deletions src/main/java/at/hannibal2/skyhanni/config/ConfigManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import at.hannibal2.skyhanni.data.jsonobjects.local.KnownFeaturesJson
import at.hannibal2.skyhanni.data.jsonobjects.local.VisualWordsJson
import at.hannibal2.skyhanni.events.LorenzEvent
import at.hannibal2.skyhanni.features.fishing.trophy.TrophyRarity
import at.hannibal2.skyhanni.features.garden.CropType
import at.hannibal2.skyhanni.features.misc.update.UpdateManager
import at.hannibal2.skyhanni.test.command.ErrorManager
import at.hannibal2.skyhanni.utils.ChatUtils
Expand Down Expand Up @@ -150,6 +151,15 @@ class ConfigManager {
return reader.nextString().toLong().asTimeMark()
}
}.nullSafe())
.registerTypeAdapter(CropType::class.java, object : TypeAdapter<CropType>() {
override fun write(out: JsonWriter, value: CropType?) {
out.value(value?.name)
}

override fun read(reader: JsonReader): CropType {
return CropType.valueOf(reader.nextString())
}
})
.enableComplexMapKeySerialization()
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package at.hannibal2.skyhanni.config.storage;

import at.hannibal2.skyhanni.features.garden.CropType;
import at.hannibal2.skyhanni.features.misc.visualwords.VisualWord;
import at.hannibal2.skyhanni.utils.LorenzVec;
import at.hannibal2.skyhanni.utils.tracker.SkyHanniTracker;
Expand Down Expand Up @@ -50,4 +51,11 @@ public class Storage {

@Expose
public List<String> blacklistedUsers = new ArrayList<>();

@Expose
public CropType lastCropBroken = null;

@Expose
public String lastSkillObtained = null;

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ data class EliteLeaderboardJson(

data class EliteLeaderboard(
@Expose val rank: Int,
@Expose val amount: Long,
@Expose val upcomingRank: Int,
@Expose val upcomingPlayers: List<UpcomingLeaderboardPlayer>
)
Expand All @@ -45,11 +46,6 @@ 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>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ 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 @@ -36,17 +35,16 @@ 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
import kotlin.time.Duration.Companion.seconds

object FarmingCollectionDisplay {

private val config get() = SkyHanniMod.feature.garden.eliteFarmingCollection

private var checkDuration = 10.minutes
private var worldSwapRefresh = true
private var settingToggleCooldown = SimpleTimeMark.farPast()

@SubscribeEvent
fun onRepoReload(event: RepositoryReloadEvent) {
Expand Down Expand Up @@ -74,8 +72,11 @@ object FarmingCollectionDisplay {
private var currentCollections = mutableMapOf<CropType, Long>()
private var lastFetchedCrop: CropType? = null

private var lastBrokenCrop: CropType? = null
private var hasFetchedCollection = false
private var lastBrokenCrop: CropType?
get() = SkyHanniMod.feature.storage.lastCropBroken
set(value) {
SkyHanniMod.feature.storage.lastCropBroken = value
}
private var lastLeaderboardFetch = SimpleTimeMark.farPast()

private var display = emptyList<Renderable>()
Expand All @@ -99,10 +100,7 @@ object FarmingCollectionDisplay {
@SubscribeEvent
fun onConfigLoad(event: ConfigLoadEvent) {
config.crop.afterChange {
if (settingToggleCooldown.passedSince() < 30.seconds) {
settingToggleCooldown = SimpleTimeMark.now()
lastLeaderboardFetch = SimpleTimeMark.farPast()
}
lastLeaderboardFetch = SimpleTimeMark.farPast()
}
}

Expand All @@ -111,13 +109,6 @@ object FarmingCollectionDisplay {
if (!isEnabled()) return
if (profileID == null) return

if (!hasFetchedCollection) {
SkyHanniMod.coroutineScope.launch {
getCurrentCollection()
}
hasFetchedCollection = true
}

if (lastLeaderboardFetch.passedSince() > checkDuration) {
lastLeaderboardFetch = SimpleTimeMark.now()
val crop = if (config.crop.get() == CropDisplay.AUTO) {
Expand Down Expand Up @@ -172,7 +163,6 @@ object FarmingCollectionDisplay {
}

private fun resetData() {
hasFetchedCollection = false
lastLeaderboardFetch = SimpleTimeMark.farPast()
collectionRanks.clear()
collectionPlacements.clear()
Expand Down Expand Up @@ -217,7 +207,6 @@ object FarmingCollectionDisplay {
listOf("§bClick to refresh."),
onClick = {
lastLeaderboardFetch = SimpleTimeMark.farPast()
hasFetchedCollection = false
ChatUtils.chat("Collection leaderboard updating...")
}
)
Expand Down Expand Up @@ -260,6 +249,7 @@ object FarmingCollectionDisplay {
}
collectionPlacements[crop] = placements
lastFetchedCrop = crop
currentCollections[crop] = data.amount

} catch (e: Exception) {
ErrorManager.logErrorWithData(
Expand All @@ -274,37 +264,17 @@ 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,7 +5,6 @@ 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 @@ -31,6 +30,7 @@ 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 All @@ -41,7 +41,6 @@ object SkillRankDisplay {

private var checkDuration = 10.minutes
private var worldSwapRefresh = true
private var settingToggleCooldown = SimpleTimeMark.farPast()

@SubscribeEvent
fun onRepoReload(event: RepositoryReloadEvent) {
Expand All @@ -60,9 +59,13 @@ object SkillRankDisplay {
private val skillRanks = mutableMapOf<String, Int>()
private var currentSkills = mutableMapOf<String, Long>()

private var lastSkillGained: String? = null
private var lastSkillGained: String?
get() = SkyHanniMod.feature.storage.lastSkillObtained
set(value) {
SkyHanniMod.feature.storage.lastSkillObtained = value
}

private var lastSkillFetched: String? = null
private var hasFetchedSkills = false
private var lastLeaderboardFetch = SimpleTimeMark.farPast()
private var lastXPGained = SimpleTimeMark.farPast()

Expand All @@ -87,10 +90,7 @@ object SkillRankDisplay {
@SubscribeEvent
fun onConfigLoad(event: ConfigLoadEvent) {
config.skill.afterChange {
if (settingToggleCooldown.passedSince() < 30.seconds) {
settingToggleCooldown = SimpleTimeMark.now()
lastLeaderboardFetch = SimpleTimeMark.farPast()
}
lastLeaderboardFetch = SimpleTimeMark.farPast()
}
}

Expand All @@ -99,13 +99,6 @@ object SkillRankDisplay {
if (!isEnabled()) return
if (profileID == null) return

if (!hasFetchedSkills) {
SkyHanniMod.coroutineScope.launch {
getCurrentSkills()
}
hasFetchedSkills = true
}

if (lastLeaderboardFetch.passedSince() > checkDuration) {
lastLeaderboardFetch = SimpleTimeMark.now()
val skill = if (config.skill.get() == SkillDisplay.AUTO) {
Expand Down Expand Up @@ -152,7 +145,6 @@ object SkillRankDisplay {
}

private fun resetData() {
hasFetchedSkills = false
lastLeaderboardFetch = SimpleTimeMark.farPast()
skillRanks.clear()
skillPlacements.clear()
Expand Down Expand Up @@ -197,7 +189,6 @@ object SkillRankDisplay {
listOf("§bClick to refresh."),
onClick = {
lastLeaderboardFetch = SimpleTimeMark.farPast()
hasFetchedSkills = false
ChatUtils.chat("Skills leaderboard updating...")
}
)
Expand Down Expand Up @@ -240,6 +231,7 @@ object SkillRankDisplay {
}
skillPlacements[skill] = placements
lastSkillFetched = skill
currentSkills[skill] = data.amount

} catch (e: Exception) {
ErrorManager.logErrorWithData(
Expand All @@ -254,29 +246,9 @@ object SkillRankDisplay {
}
}

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,
)
}
@SubscribeEvent
fun playerLogout(event: FMLNetworkEvent.ClientDisconnectionFromServerEvent) {
SkyHanniMod.feature.storage.lastSkillObtained = lastSkillGained
}

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

0 comments on commit 7f27a16

Please sign in to comment.