diff options
author | mat <github@matdoes.dev> | 2022-02-12 22:34:17 -0600 |
---|---|---|
committer | mat <github@matdoes.dev> | 2022-02-12 22:34:17 -0600 |
commit | aa4d561f1663c85e9dec8e3d2e76c14cbea997bb (patch) | |
tree | c8d887e37f952f068a2297769a9ae3d60ea99412 /src/hypixel.ts | |
parent | e10181e7763ebb3ec7b16e0612875727665df552 (diff) | |
download | skyblock-api-aa4d561f1663c85e9dec8e3d2e76c14cbea997bb.tar.gz skyblock-api-aa4d561f1663c85e9dec8e3d2e76c14cbea997bb.tar.bz2 skyblock-api-aa4d561f1663c85e9dec8e3d2e76c14cbea997bb.zip |
Election API
Diffstat (limited to 'src/hypixel.ts')
-rw-r--r-- | src/hypixel.ts | 39 |
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 +} + |