aboutsummaryrefslogtreecommitdiff
path: root/src/hypixel.ts
blob: 99bc671e760d60f22e3426415319b2d254332ec1 (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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/**
 * Fetch the clean Hypixel API
 */

import { cleanSkyblockProfileResponse, CleanProfile, CleanBasicProfile, CleanFullProfile, CleanFullProfileBasicMembers } from './cleaners/skyblock/profile'
import { AccountCustomization, AccountSchema, fetchAccount, queueUpdateDatabaseMember, queueUpdateDatabaseProfile } from './database'
import { CleanBasicMember, CleanMemberProfile } from './cleaners/skyblock/member'
import { chooseApiKey, HypixelResponse, sendApiRequest } from './hypixelApi'
import { cleanSkyblockProfilesResponse } from './cleaners/skyblock/profiles'
import { CleanPlayer, cleanPlayerResponse } from './cleaners/player'
import * as cached from './hypixelCached'
import { debug } from '.'

export type Included = 'profiles' | 'player' | 'stats' | 'inventories'

// the interval at which the "last_save" parameter updates in the hypixel api, this is 3 minutes
export const saveInterval = 60 * 3 * 1000

// the highest level a minion can be
export const maxMinion = 11

/**
 *  Send a request to api.hypixel.net using a random key, clean it up to be more useable, and return it 
 */ 

export interface ApiOptions {
	mainMemberUuid?: string
	/** Only get the most basic information, like uuids and names */
	basic?: boolean
}

/** Sends an API request to Hypixel and cleans it up. */
export async function sendCleanApiRequest({ path, args }, included?: Included[], options?: ApiOptions): Promise<any> {
	const key = await chooseApiKey()
	const rawResponse = await sendApiRequest({ path, key, args })
	if (rawResponse.throttled) {
		// if it's throttled, wait a second and try again
		await new Promise(resolve => setTimeout(resolve, 1000))
		return await sendCleanApiRequest({ path, args }, included, options)
	}

	// clean the response
	return await cleanResponse({ path, data: rawResponse }, options ?? {})
}



async function cleanResponse({ path, data }: { path: string, data: HypixelResponse }, options: ApiOptions): Promise<any> {
	// Cleans up an api response
	switch (path) {
		case 'player': return await cleanPlayerResponse(data.player)
		case 'skyblock/profile': return await cleanSkyblockProfileResponse(data.profile, options)
		case 'skyblock/profiles': return await cleanSkyblockProfilesResponse(data.profiles)
	}
}

/* ----------------------------- */

export interface UserAny {
	user?: string
	uuid?: string
	username?: string
}

export interface CleanUser {
	player: CleanPlayer
	profiles?: CleanProfile[]
	activeProfile?: string
	online?: boolean
	customization?: AccountCustomization
}


/**
 * Higher level function that requests the api for a user, and returns the cleaned response
 * This is safe to fetch many times because the results are cached!
 * @param included lets you choose what is returned, so there's less processing required on the backend
 * used inclusions: player, profiles
 */
export async function fetchUser({ user, uuid, username }: UserAny, included: Included[]=['player'], customization?: boolean): Promise<CleanUser> {
	if (!uuid) {
		// If the uuid isn't provided, get it
		uuid = await cached.uuidFromUser(user || username)
	}
	const websiteAccountPromise = customization ? fetchAccount(uuid) : null
	if (!uuid) {
		// the user doesn't exist.
		if (debug) console.debug('error:', user, 'doesnt exist')
		return null
	}

	const includePlayers = included.includes('player')
	const includeProfiles = included.includes('profiles')

	let profilesData: CleanProfile[]
	let basicProfilesData: CleanBasicProfile[]
	let playerData: CleanPlayer

	if (includePlayers) {
		playerData = await cached.fetchPlayer(uuid)
		// if not including profiles, include lightweight profiles just in case
		if (!includeProfiles)
			basicProfilesData = playerData?.profiles
		if (playerData)
			delete playerData.profiles
	}
	if (includeProfiles) {
		profilesData = await cached.fetchSkyblockProfiles(uuid)
	}

	let activeProfile: CleanProfile = null
	let lastOnline: number = 0

	if (includeProfiles) {
		for (const profile of profilesData) {
			const member = profile.members.find(member => member.uuid === uuid)
			if (member.last_save > lastOnline) {
				lastOnline = member.last_save
				activeProfile = profile
			}
		}
	}
	let websiteAccount: AccountSchema = undefined

	if (websiteAccountPromise)
		websiteAccount = await websiteAccountPromise
	return {
		player: playerData ?? null,
		profiles: profilesData ?? basicProfilesData,
		activeProfile: includeProfiles ? activeProfile?.uuid : undefined,
		online: includeProfiles ? lastOnline > (Date.now() - saveInterval): undefined,
		customization: websiteAccount?.customization
	}
}

/**
 * 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
 * @param customization Whether stuff like the user's custom background will be returned
 */
export async function fetchMemberProfile(user: string, profile: string, customization: boolean): Promise<CleanMemberProfile> {
	const playerUuid = await cached.uuidFromUser(user)
	// we don't await the promise immediately so it can load while we do other stuff
	const websiteAccountPromise = customization ? fetchAccount(playerUuid) : null
	const profileUuid = await cached.fetchProfileUuid(user, profile)

	// if the profile doesn't have an id, just return
	if (!profileUuid) return null

	const player = await cached.fetchPlayer(playerUuid)

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

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

	// remove unnecessary member data
	const simpleMembers: CleanBasicMember[] = cleanProfile.members.map(m => {
		return {
			uuid: m.uuid,
			username: m.username,
			first_join: m.first_join,
			last_save: m.last_save,
			rank: m.rank
		}
	})

	cleanProfile.members = simpleMembers

	let websiteAccount: AccountSchema = undefined

	if (websiteAccountPromise)
		websiteAccount = await websiteAccountPromise

	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: cleanProfile,
		customization: websiteAccount?.customization
	}
}

/**
 * Fetches the Hypixel API to get a CleanFullProfile. This doesn't do any caching and you should use hypixelCached.fetchProfile instead
 * @param playerUuid The UUID of the Minecraft player
 * @param profileUuid The UUID of the Hypixel SkyBlock profile
 */
 export async function fetchMemberProfileUncached(playerUuid: string, profileUuid: string): Promise<CleanFullProfile> {
	const profile: CleanFullProfile = await sendCleanApiRequest(
		{
			path: 'skyblock/profile',
			args: { profile: profileUuid }
		},
		null,
		{ mainMemberUuid: playerUuid }
	)

	// queue updating the leaderboard positions for the member, eventually
	for (const member of profile.members)
		queueUpdateDatabaseMember(member, profile)
	queueUpdateDatabaseProfile(profile)

	return profile
}

/**
 * Fetches the Hypixel API to get a CleanProfile from its id. This doesn't do any caching and you should use hypixelCached.fetchBasicProfileFromUuid instead
 * @param playerUuid The UUID of the Minecraft player
 * @param profileUuid The UUID of the Hypixel SkyBlock profile
 */
 export async function fetchBasicProfileFromUuidUncached(profileUuid: string): Promise<CleanProfile> {
	const profile: CleanFullProfile = await sendCleanApiRequest(
		{
			path: 'skyblock/profile',
			args: { profile: profileUuid }
		},
		null,
		{ basic: true }
	)

	return profile
}


export async function fetchMemberProfilesUncached(playerUuid: string): Promise<CleanFullProfile[]> {
	const profiles: CleanFullProfile[] = await sendCleanApiRequest({
		path: 'skyblock/profiles',
		args: {
			uuid: playerUuid
		}},
		null,
		{
			// only the inventories for the main player are generated, this is for optimization purposes
			mainMemberUuid: playerUuid
		}
	)
	for (const profile of profiles) {
		for (const member of profile.members) {
			queueUpdateDatabaseMember(member, profile)
		}
		queueUpdateDatabaseProfile(profile)
	}
	return profiles
}