diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/cleaners/skyblock/election.ts | 64 | ||||
-rw-r--r-- | src/hypixel.ts | 39 | ||||
-rw-r--r-- | src/index.ts | 13 |
3 files changed, 114 insertions, 2 deletions
diff --git a/src/cleaners/skyblock/election.ts b/src/cleaners/skyblock/election.ts new file mode 100644 index 0000000..78937ec --- /dev/null +++ b/src/cleaners/skyblock/election.ts @@ -0,0 +1,64 @@ + +const candidateColors = { + barry: 'e', + paul: '4', + aatrox: 'a', + foxy: '3', + cole: 'e', + marina: '5', + diaz: '5', + diana: '3', +} + +export interface MayorPerk { + name: string + description: string +} + +export interface Candidate { + name: string + perks: MayorPerk[] + votes: number + color: string | null +} + +export interface ElectionData { + last_updated: number + previous: { + year: number + winner: Candidate + candidates: Candidate[] + } + current: { + year: number + candidates: Candidate[] + } | null +} + +function cleanCandidate(data: any): Candidate { + return { + name: data.name, + perks: data.perks, + votes: data.votes, + color: candidateColors[data.name.toLowerCase()], + } +} + +export function cleanElectionResponse(data: any): ElectionData { + return { + last_updated: data.lastUpdated / 1000, + previous: { + year: data.mayor.election.year, + winner: cleanCandidate({ + name: data.mayor.name, + perks: data.mayor.perks, + votes: data.mayor.election.candidates.find(c => c.key === data.mayor.key).votes, + }), + candidates: data.mayor.election.candidates.map(cleanCandidate) + }, + current: data.current ? { + year: data.current.year, + candidates: data.current.candidates.map(cleanCandidate) + } : null + } +}
\ No newline at end of file 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 +} + diff --git a/src/index.ts b/src/index.ts index 7332805..6ac23cd 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,5 +1,5 @@ import { createSession, fetchAccountFromDiscord, fetchAllLeaderboardsCategorized, fetchLeaderboard, fetchMemberLeaderboardSpots, fetchSession, finishedCachingRawLeaderboards, leaderboardUpdateMemberQueue, leaderboardUpdateProfileQueue, updateAccount, fetchServerStatus } from './database.js' -import { fetchMemberProfile, fetchUser } from './hypixel.js' +import { fetchElection, fetchMemberProfile, fetchUser } from './hypixel.js' import rateLimit from 'express-rate-limit' import * as constants from './constants.js' import * as discord from './discord.js' @@ -141,6 +141,17 @@ app.get('/constants', async (req, res) => { } }) +app.get('/election', async (req, res) => { + try { + res.json( + await fetchElection() + ) + } catch (err) { + console.error(err) + res.json({ ok: false }) + } +}) + app.post('/accounts/createsession', async (req, res) => { try { const { code } = req.body |