Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

request elections api less frequently #966

Merged
merged 1 commit into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 16 additions & 14 deletions src/main/java/at/hannibal2/skyhanni/data/MayorElection.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@ import at.hannibal2.skyhanni.events.LorenzTickEvent
import at.hannibal2.skyhanni.utils.APIUtil
import at.hannibal2.skyhanni.utils.LorenzUtils
import at.hannibal2.skyhanni.utils.LorenzUtils.put
import at.hannibal2.skyhanni.utils.SimpleTimeMark
import io.github.moulberry.notenoughupdates.util.SkyBlockTime
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
import kotlin.time.Duration.Companion.minutes

class MayorElection {
private var lastUpdate = 0L
private var lastUpdate = SimpleTimeMark.farPast()
private var dispatcher = Dispatchers.IO

companion object {
Expand All @@ -37,20 +39,20 @@ class MayorElection {
}

private fun check() {
if (System.currentTimeMillis() > lastUpdate + 60_000 * 5) {
lastUpdate = System.currentTimeMillis()
SkyHanniMod.coroutineScope.launch {
val url = "https://api.hypixel.net/v2/resources/skyblock/election"
val jsonObject = withContext(dispatcher) { APIUtil.getJSONResponse(url) }
rawMayorData = ConfigManager.gson.fromJson(jsonObject, MayorJson::class.java)
val data = rawMayorData ?: return@launch
val map = mutableMapOf<Int, MayorJson.Candidate>()
map put data.mayor.election.getPairs()
data.current?.let {
map put data.current.getPairs()
}
candidates = map
if (lastUpdate.passedSince() < 20.minutes) return
lastUpdate = SimpleTimeMark.now()

SkyHanniMod.coroutineScope.launch {
val url = "https://api.hypixel.net/v2/resources/skyblock/election"
val jsonObject = withContext(dispatcher) { APIUtil.getJSONResponse(url) }
rawMayorData = ConfigManager.gson.fromJson(jsonObject, MayorJson::class.java)
val data = rawMayorData ?: return@launch
val map = mutableMapOf<Int, MayorJson.Candidate>()
map put data.mayor.election.getPairs()
data.current?.let {
map put data.current.getPairs()
}
candidates = map
}

checkCurrentMayor()
Expand Down
15 changes: 9 additions & 6 deletions src/main/java/at/hannibal2/skyhanni/utils/APIUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,11 @@ object APIUtil {
e.printStackTrace()

} else {
ErrorManager.logError(
Error("$apiName error for url: '$urlString'", e),
"Failed to load data from $apiName"
ErrorManager.logErrorWithData(
e, "$apiName error for url: '$urlString'",
"apiName" to apiName,
"urlString" to urlString,
"returnedData" to retSrc
)
}
}
Expand All @@ -83,9 +85,10 @@ object APIUtil {
if (silentError) {
throw throwable
} else {
ErrorManager.logError(
Error("$apiName error for url: '$urlString'", throwable),
"Failed to load data from $apiName"
ErrorManager.logErrorWithData(
throwable, "$apiName error for url: '$urlString'",
"apiName" to apiName,
"urlString" to urlString,
)
}
} finally {
Expand Down
Loading