aboutsummaryrefslogtreecommitdiff
path: root/src/hypixel.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/hypixel.ts')
-rw-r--r--src/hypixel.ts39
1 files changed, 38 insertions, 1 deletions
diff --git a/src/hypixel.ts b/src/hypixel.ts
index 745e036..a9a0233 100644
--- a/src/hypixel.ts
+++ b/src/hypixel.ts
@@ -24,6 +24,7 @@ import * as cached from './hypixelCached.js'
import { debug } from './index.js'
import { sleep } from './util.js'
import { WithId } from 'mongodb'
+import { cleanElectionResponse, ElectionData } from './cleaners/skyblock/election.js'
export type Included = 'profiles' | 'player' | 'stats' | 'inventories' | undefined
@@ -64,6 +65,7 @@ async function cleanResponse({ path, data }: { path: string, data: HypixelRespon
case 'player': return await cleanPlayerResponse(data.player)
case 'skyblock/profile': return await cleanSkyblockProfileResponse(data.profile, options)
case 'skyblock/profiles': return await cleanSkyblockProfilesResponse(data.profiles)
+ case 'resources/skyblock/election': return await cleanElectionResponse(data)
}
}
@@ -267,4 +269,39 @@ export async function fetchMemberProfilesUncached(playerUuid: string): Promise<C
queueUpdateDatabaseProfile(profile)
}
return profiles
-} \ No newline at end of file
+}
+
+let isFetchingElection = false
+let cachedElectionData: ElectionData | null = null
+let nextElectionUpdate: Date = new Date(0)
+
+export async function fetchElection(): Promise<ElectionData> {
+ if (cachedElectionData && nextElectionUpdate > new Date())
+ return cachedElectionData
+
+ // if it's currently fetching the election data and it doesn't have it,
+ // wait until we do have the election data
+ if (isFetchingElection && !cachedElectionData) {
+ await new Promise(resolve => {
+ const interval = setInterval(() => {
+ if (cachedElectionData) {
+ clearInterval(interval)
+ resolve(cachedElectionData)
+ }
+ }, 100)
+ })
+ }
+
+ isFetchingElection = true
+ const election: ElectionData = await sendCleanApiRequest({
+ path: 'resources/skyblock/election',
+ args: {}
+ })
+ isFetchingElection = false
+
+ cachedElectionData = election
+ // updates every 10 minutes
+ nextElectionUpdate = new Date((election.last_updated + 10 * 60) * 1000)
+ return election
+}
+