aboutsummaryrefslogtreecommitdiff
path: root/src/cleaners/skyblock/profile.ts
blob: 8389a0dd3e3680171ce689ed09cc99dbf425147f (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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import { CleanBasicMember, CleanMember, CleanMemberProfile, cleanSkyBlockProfileMemberResponse } from './member'
import { CleanMinion, combineMinionArrays, countUniqueMinions } from './minions'
import * as cached from '../../hypixelCached'
import { Bank, cleanBank } from './bank'
import { cleanFairySouls, FairySouls } from './fairysouls'

export interface CleanProfile extends CleanBasicProfile {
    members?: CleanBasicMember[]
}

export interface CleanFullProfile extends CleanProfile {
    members: CleanMember[]
    bank?: Bank
    minions: CleanMinion[]
}

/** Return a `CleanProfile` instead of a `CleanFullProfile`, useful when we need to get members but don't want to waste much ram */
export async function cleanSkyblockProfileResponseLighter(data): Promise<CleanProfile> {
    // We use Promise.all so it can fetch all the usernames at once instead of waiting for the previous promise to complete
    const promises: Promise<CleanMember>[] = []

    for (const memberUUID in data.members) {
        const memberRaw = data.members[memberUUID]
        memberRaw.uuid = memberUUID
        // we pass an empty array to make it not check stats
        promises.push(cleanSkyBlockProfileMemberResponse(memberRaw, []))
    }

    const cleanedMembers: CleanMember[] = await Promise.all(promises)

    return {
        uuid: data.profile_id,
        name: data.cute_name,
        members: cleanedMembers,
    }
}

/** This function is somewhat costly and shouldn't be called often. Use cleanSkyblockProfileResponseLighter if you don't need all the data */
export async function cleanSkyblockProfileResponse(data: any): Promise<CleanFullProfile> {
    const cleanedMembers: CleanMember[] = []

    for (const memberUUID in data.members) {
        const memberRaw = data.members[memberUUID]
        memberRaw.uuid = memberUUID
        const member: CleanMember = await cleanSkyBlockProfileMemberResponse(memberRaw, ['stats'])
        cleanedMembers.push(member)
    }

    const memberMinions: CleanMinion[][] = []

    for (const member of cleanedMembers) {
        memberMinions.push(member.minions)
    }
    const minions: CleanMinion[] = combineMinionArrays(memberMinions)

    // return more detailed info
    return {
        uuid: data.profile_id,
        name: data.cute_name,
        members: cleanedMembers,
        bank: cleanBank(data),
        minions
    }
}

/** A basic profile that only includes the profile uuid and name */
export interface CleanBasicProfile {
    uuid: string

    // the name depends on the user, so its sometimes not included
    name?: string
}

// TODO: this should be moved and split up
/**
 * Fetch a CleanMemberProfile from a user and string
 * This is safe to use many times as the results are cached!
 * @param user A username or uuid
 * @param profile A profile name or profile uuid
 */
export async function fetchMemberProfile(user: string, profile: string): Promise<CleanMemberProfile> {
    const playerUuid = await cached.uuidFromUser(user)
    const profileUuid = await cached.fetchProfileUuid(user, profile)

    const player = await cached.fetchPlayer(playerUuid)

    const cleanProfile = await cached.fetchProfile(playerUuid, profileUuid)

    const member = cleanProfile.members.find(m => m.uuid === playerUuid)

    return {
        member: {
			// the profile name is in member rather than profile since they sometimes differ for each member
            profileName: cleanProfile.name,
			// add all the member data
            ...member,
            // add all other data relating to the hypixel player, such as username, rank, etc
            ...player
        },
        profile: {
			uuid: cleanProfile.uuid,
			bank: cleanProfile.bank,
            minions: cleanProfile.minions,
			minion_count: countUniqueMinions(cleanProfile.minions)
        }
    }
}