aboutsummaryrefslogtreecommitdiff
path: root/src/cleaners/skyblock/profiles.ts
blob: da40ee303cb4430b7c297a949b10df3a9167211e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import { HypixelPlayerStatsSkyBlockProfiles } from '../../hypixelApi.js'
import {
    CleanBasicProfile,
    CleanFullProfile,
    cleanSkyblockProfileResponse
} from './profile.js'
import typedHypixelApi from 'typed-hypixel-api'
import { ApiOptions } from '../../hypixel.js'

export function cleanPlayerSkyblockProfiles(rawProfiles: HypixelPlayerStatsSkyBlockProfiles | undefined): CleanBasicProfile[] {
    if (!rawProfiles) return []

    let profiles: CleanBasicProfile[] = []
    for (const profile of Object.values(rawProfiles ?? {})) {
        profiles.push({
            uuid: profile.profile_id,
            name: profile.cute_name
        })
    }
    return profiles
}

/** Convert an array of raw profiles into clean profiles */
export async function cleanSkyblockProfilesResponse(
    data: typedHypixelApi.SkyBlockProfilesResponse['profiles'],
    options: ApiOptions
): Promise<CleanFullProfile[] | null> {
    if (!data) return null

    const promises: Promise<CleanFullProfile | null>[] = []
    for (const profile of data) {
        promises.push(cleanSkyblockProfileResponse(profile, options))
    }
    const cleanedProfiles: CleanFullProfile[] = (await Promise.all(promises)).filter((p): p is CleanFullProfile => p !== null)
    return cleanedProfiles
}