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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
|
/**
* Fetch the clean Hypixel API
*/
import {
cleanSkyblockProfileResponse,
CleanProfile,
CleanBasicProfile,
CleanFullProfile,
CleanFullProfileBasicMembers
} from './cleaners/skyblock/profile.js'
import {
AccountCustomization,
AccountSchema,
fetchAccount,
fetchItemsAuctions,
fetchItemsAuctionsIds,
ItemAuctionsSchema,
queueUpdateDatabaseMember,
queueUpdateDatabaseProfile,
removeDeletedProfilesFromLeaderboards,
SimpleAuctionSchema,
updateItemAuction
} from './database.js'
import { cleanElectionResponse, ElectionData } from './cleaners/skyblock/election.js'
import { cleanItemListResponse, ItemListData } from './cleaners/skyblock/itemList.js'
import { CleanBasicMember, CleanMemberProfile } from './cleaners/skyblock/member.js'
import { cleanSkyblockProfilesResponse } from './cleaners/skyblock/profiles.js'
import { CleanPlayer, cleanPlayerResponse } from './cleaners/player.js'
import { chooseApiKey, sendApiRequest } from './hypixelApi.js'
import typedHypixelApi from 'typed-hypixel-api'
import * as cached from './hypixelCached.js'
import { debug } from './index.js'
import { WithId } from 'mongodb'
import { cleanEndedAuctions } from './cleaners/skyblock/endedAuctions.js'
import { cleanAuctions } from './cleaners/skyblock/auctions.js'
import { string } from 'prismarine-nbt'
export type Included = 'profiles' | 'player' | 'stats' | 'inventories' | undefined
// the interval at which the "last_save" parameter updates in the hypixel api, this is 3 minutes
export const saveInterval = 60 * 3 * 1000
/**
* 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<P extends keyof typeof cleanResponseFunctions>(path: P, args: Omit<typedHypixelApi.Requests[P]['options'], 'key'>, options?: ApiOptions): Promise<Awaited<ReturnType<typeof cleanResponseFunctions[P]>>> {
const key = await chooseApiKey()
const data = await sendApiRequest(path, { key, ...args })
if (!data)
throw new Error(`No data returned from ${path}`)
// clean the response
return await cleanResponse(path, data, options ?? {})
}
const cleanResponseFunctions = {
'player': (data, options) => cleanPlayerResponse(data.player),
'skyblock/profile': (data: typedHypixelApi.SkyBlockProfileResponse, options) => cleanSkyblockProfileResponse(data.profile, options),
'skyblock/profiles': (data, options) => cleanSkyblockProfilesResponse(data.profiles),
'skyblock/auctions_ended': (data, options) => cleanEndedAuctions(data),
'skyblock/auction': (data, options) => cleanAuctions(data),
'resources/skyblock/election': (data, options) => cleanElectionResponse(data),
'resources/skyblock/items': (data, options) => cleanItemListResponse(data),
} as const
async function cleanResponse<P extends keyof typeof cleanResponseFunctions>(
path: P,
data: typedHypixelApi.Requests[P]['response']['data'],
options: ApiOptions
): Promise<Awaited<ReturnType<typeof cleanResponseFunctions[P]>>> {
// Cleans up an api response
const cleaningFunction: typeof cleanResponseFunctions[P] = cleanResponseFunctions[path]
// we do `as any` because typescript unfortunately doesn't know which path it is
const cleanedData = await cleaningFunction(data as any, options)
return cleanedData as Awaited<ReturnType<typeof cleanResponseFunctions[P]>>
}
/* ----------------------------- */
export interface UserAny {
user?: string
uuid?: string
username?: string
}
export interface CleanUser {
player: CleanPlayer | null
profiles?: CleanProfile[] | CleanBasicProfile[]
activeProfile?: string
online?: boolean
customization?: AccountCustomization
}
/**
* Higher level function that requests the api for a user, and returns the
* cleaned response. This is used by the /player/<name> route.
* 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 | null> {
if (!uuid) {
// If the uuid isn't provided, get it
if (!username && !user) return null
uuid = await cached.uuidFromUser((user ?? username)!)
}
if (!uuid) {
// the user doesn't exist.
if (debug) console.debug('error:', user, 'doesnt exist')
return null
}
const websiteAccountPromise = customization ? fetchAccount(uuid) : null
const includePlayers = included.includes('player')
const includeProfiles = included.includes('profiles')
let profilesData: CleanProfile[] | undefined
let basicProfilesData: CleanBasicProfile[] | undefined
let playerData: CleanPlayer | null = null
if (includePlayers) {
playerData = await cached.fetchPlayer(uuid, true)
// if not including profiles, include lightweight profiles just in case
if (!includeProfiles)
basicProfilesData = playerData?.profiles
// we don't want the `profiles` field in `player`
if (playerData)
delete playerData.profiles
}
if (includeProfiles)
profilesData = await cached.fetchSkyblockProfiles(uuid) ?? []
let activeProfile: CleanProfile
let lastOnline: number = 0
if (includeProfiles && profilesData !== undefined) {
for (const profile of profilesData) {
const member = profile.members?.find(member => member.uuid === uuid)
if (member && member.lastSave && member.lastSave > lastOnline) {
lastOnline = member.lastSave
activeProfile = profile
}
}
// we don't await so it happens in the background
removeDeletedProfilesFromLeaderboards(uuid, profilesData.map(p => p.uuid))
}
let websiteAccount: WithId<AccountSchema> | null = null
if (websiteAccountPromise)
websiteAccount = await websiteAccountPromise
return {
player: playerData,
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 | null> {
const playerUuid = await cached.uuidFromUser(user)
if (!playerUuid) return null
// 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 or player doesn't have an id, just return
if (!profileUuid) return null
if (!playerUuid) return null
const player: CleanPlayer | null = await cached.fetchPlayer(playerUuid, true)
if (!player) return null // this should never happen, but if it does just return null
const cleanProfile = await cached.fetchProfile(playerUuid, profileUuid)
if (!cleanProfile) return null
const member = cleanProfile.members.find(m => m.uuid === playerUuid)
if (!member) return null // this should never happen, but if it does just return null
// remove unnecessary member data
const simpleMembers: CleanBasicMember[] = cleanProfile.members.map(m => {
return {
uuid: m.uuid,
username: m.username,
firstJoin: m.firstJoin,
lastSave: m.lastSave,
rank: m.rank,
left: m.left
}
})
const cleanProfileBasicMembers: CleanFullProfileBasicMembers = {
...cleanProfile,
members: simpleMembers
}
let websiteAccount: WithId<AccountSchema> | null = null
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: cleanProfileBasicMembers,
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<null | CleanFullProfile> {
const profile = await sendCleanApiRequest(
'skyblock/profile',
{ profile: profileUuid },
{ mainMemberUuid: playerUuid }
)
// we check for minions in profile to filter out the CleanProfile type (as opposed to CleanFullProfile)
if (!profile || !('minions' in profile)) return null
// queue updating the leaderboard positions for the member, eventually
if (profile.members)
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 | null> {
const profile = await sendCleanApiRequest(
'skyblock/profile',
{ profile: profileUuid },
{ basic: true }
)
return profile
}
export async function fetchMemberProfilesUncached(playerUuid: string): Promise<CleanFullProfile[] | null> {
const profiles = await sendCleanApiRequest(
'skyblock/profiles',
{ uuid: playerUuid },
{
// only the inventories for the main player are generated, this is for optimization purposes
mainMemberUuid: playerUuid
}
)
if (profiles === null)
return null
for (const profile of profiles) {
for (const member of profile.members) {
queueUpdateDatabaseMember(member, profile)
}
queueUpdateDatabaseProfile(profile)
}
return profiles
}
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(
'resources/skyblock/election',
{}
)
isFetchingElection = false
cachedElectionData = election
// updates every 10 minutes
nextElectionUpdate = new Date((election.lastUpdated + 10 * 60) * 1000)
return election
}
let isFetchingItemList = false
let cachedItemListData: ItemListData | null = null
let nextItemListUpdate: Date = new Date(0)
export async function fetchItemList() {
if (cachedItemListData && nextItemListUpdate > new Date())
return cachedItemListData
// if it's currently fetching the election data and it doesn't have it,
// wait until we do have the election data
if (isFetchingItemList && !cachedItemListData) {
await new Promise(resolve => {
const interval = setInterval(() => {
if (cachedItemListData) {
clearInterval(interval)
resolve(cachedItemListData)
}
}, 100)
})
}
isFetchingItemList = true
const itemList: ItemListData = await sendCleanApiRequest(
'resources/skyblock/items',
{}
)
isFetchingItemList = false
cachedItemListData = itemList
// updates every 60 minutes
nextItemListUpdate = new Date((itemList.lastUpdated + 60 * 60) * 1000)
return itemList
}
export async function fetchAuctionUncached(uuid: string) {
return await sendCleanApiRequest(
'skyblock/auction',
{ uuid }
)
}
// this function is called from database.ts so it starts when we connect to the database
// it should only ever be called once!
export async function periodicallyFetchRecentlyEndedAuctions() {
let previousAuctionIds = new Set()
while (true) {
const endedAuctions = await sendCleanApiRequest(
'skyblock/auctions_ended',
{}
)
let newAuctionUuids: Set<string> = new Set()
let newAuctionItemIds: Set<string> = new Set()
for (const auction of endedAuctions.auctions) {
if (previousAuctionIds.has(auction.id)) continue
newAuctionUuids.add(auction.id)
newAuctionItemIds.add(auction.item.id)
}
let updatedDatabaseAuctionItems: Map<string, ItemAuctionsSchema> = new Map()
const itemsAuctions = await fetchItemsAuctions(Array.from(newAuctionItemIds))
for (const itemAuctions of itemsAuctions) {
updatedDatabaseAuctionItems.set(itemAuctions.id, itemAuctions)
}
for (const auction of endedAuctions.auctions) {
if (previousAuctionIds.has(auction.id)) continue
let auctions: SimpleAuctionSchema[]
if (!updatedDatabaseAuctionItems.has(auction.item.id)) {
auctions = []
} else {
auctions = updatedDatabaseAuctionItems.get(auction.item.id)!.auctions
}
const simpleAuction: SimpleAuctionSchema = {
s: true,
coins: auction.coins,
id: auction.id,
ts: Math.floor(auction.timestamp / 1000),
bin: auction.bin,
}
// make sure the auction isn't already in there
if (!auctions.find((a) => a.id === simpleAuction.id)) {
auctions.push(simpleAuction)
// keep only the last 100 items
if (auctions.length > 100)
auctions = auctions.slice(-100)
}
updatedDatabaseAuctionItems.set(auction.item.id, {
id: auction.item.id,
auctions,
})
}
// we use a promise pool to set all the things fast but not overload the database
let tasks = Array.from(updatedDatabaseAuctionItems.values()).map(t => updateItemAuction(t))
async function doTasks() {
let hasTask = true
while (hasTask) {
const task = tasks.pop()
if (task) {
await task
} else
hasTask = false
}
}
// Promise.all 5 cycles
await Promise.all(Array(5).fill(0).map(_ => doTasks()))
previousAuctionIds = newAuctionUuids
let endedAgo = Date.now() - endedAuctions.lastUpdated
// +10 seconds just so we're sure we'll get the update
let refetchIn = 60 * 1000 - endedAgo + 10000
await new Promise(resolve => setTimeout(resolve, refetchIn))
}
}
let isFetchingAuctionItemList = false
let cachedAuctionItemListData: Record<string, string> | null = null
let nextAuctionItemListUpdate: Date = new Date(0)
export async function fetchAuctionItems() {
if (cachedAuctionItemListData && nextAuctionItemListUpdate > new Date())
return cachedAuctionItemListData
// if it's currently fetching the election data and it doesn't have it,
// wait until we do have the election data
if (isFetchingAuctionItemList && !cachedAuctionItemListData) {
await new Promise(resolve => {
const interval = setInterval(() => {
if (cachedAuctionItemListData) {
clearInterval(interval)
resolve(cachedAuctionItemListData)
}
}, 100)
})
}
isFetchingAuctionItemList = true
const itemList = await fetchAuctionItemsUncached()
isFetchingAuctionItemList = false
if (!itemList) return undefined
cachedAuctionItemListData = Object.fromEntries(itemList)
// updates every 60 minutes
nextAuctionItemListUpdate = new Date(Date.now() + 10 * 60 * 1000)
return cachedAuctionItemListData
}
async function fetchAuctionItemsUncached() {
const auctionItemIds = await fetchItemsAuctionsIds()
if (!auctionItemIds) return undefined
const itemList = await fetchItemList()
const idsToNames: Map<string, string> = new Map()
for (const item of itemList.list)
// we only return items in auctionItemIds so the response isn't too big,
// since usually it would contain stuff that we don't care about like
// minions
if (auctionItemIds.includes(item.id))
idsToNames.set(item.id, item.display.name)
// if the item in the database isn't in the items api, just set the name to the id
for (const item of auctionItemIds)
if (!idsToNames.has(item))
idsToNames.set(item, item)
return idsToNames
}
|