From 6dadf95683a8b8574976c9d024b0b148521012f7 Mon Sep 17 00:00:00 2001 From: mat <27899617+mat-1@users.noreply.github.com> Date: Sun, 28 Feb 2021 01:23:18 -0600 Subject: Add leaderboards --- src/cleaners/rank.ts | 84 ++++++++++++++++++++------------------- src/cleaners/skyblock/member.ts | 9 ++++- src/cleaners/skyblock/profile.ts | 25 ++++++++---- src/cleaners/skyblock/profiles.ts | 5 ++- 4 files changed, 70 insertions(+), 53 deletions(-) (limited to 'src/cleaners') diff --git a/src/cleaners/rank.ts b/src/cleaners/rank.ts index d565502..0a3a4a7 100644 --- a/src/cleaners/rank.ts +++ b/src/cleaners/rank.ts @@ -22,48 +22,50 @@ export interface CleanRank { /** Response cleaning (reformatting to be nicer) */ export function cleanRank({ - packageRank, - newPackageRank, - monthlyPackageRank, - rankPlusColor, - rank, - prefix + packageRank, + newPackageRank, + monthlyPackageRank, + rankPlusColor, + rank, + prefix }: HypixelPlayer): CleanRank { - let name - let color - let colored - if (prefix) { // derive values from prefix - colored = prefix - color = minecraftColorCodes[colored.match(/§./)[0][1]] - name = colored.replace(/§./g, '').replace(/[\[\]]/g, '') - } else { - name = monthlyPackageRank - || rank - || newPackageRank?.replace('_PLUS', '+') - || packageRank?.replace('_PLUS', '+') + let name + let color + let colored + if (prefix) { // derive values from prefix + colored = prefix + color = minecraftColorCodes[colored.match(/§./)[0][1]] + name = colored.replace(/§./g, '').replace(/[\[\]]/g, '') + } else { + if (monthlyPackageRank !== 'NONE') + name = monthlyPackageRank + else + name = rank + || newPackageRank?.replace('_PLUS', '+') + || packageRank?.replace('_PLUS', '+') - // MVP++ is called Superstar for some reason - if (name === 'SUPERSTAR') name = 'MVP++' - // YouTube rank is called YouTuber, change this to the proper name - else if (name === 'YOUTUBER') name = 'YOUTUBE' - else if (name === undefined) name = 'NONE' + // MVP++ is called Superstar for some reason + if (name === 'SUPERSTAR') name = 'MVP++' + // YouTube rank is called YouTuber, change this to the proper name + else if (name === 'YOUTUBER') name = 'YOUTUBE' + else if (name === undefined) name = 'NONE' - const plusColor = rankPlusColor ? colorCodeFromName(rankPlusColor) : null - color = minecraftColorCodes[rankColors[name]] - const rankColorPrefix = rankColors[name] ? '§' + rankColors[name] : '' - const nameWithoutPlus = name.split('+')[0] - const plusesInName = '+'.repeat(name.split('+').length - 1) - if (plusColor && plusesInName.length >= 1) - colored = `${rankColorPrefix}[${nameWithoutPlus}§${plusColor}${plusesInName}${rankColorPrefix}]` - else if (name !== 'NONE') - colored = `${rankColorPrefix}[${name}]` - else - // nons don't have a prefix - colored = `${rankColorPrefix}` - } - return { - name, - color, - colored - } + const plusColor = rankPlusColor ? colorCodeFromName(rankPlusColor) : null + color = minecraftColorCodes[rankColors[name]] + const rankColorPrefix = rankColors[name] ? '§' + rankColors[name] : '' + const nameWithoutPlus = name.split('+')[0] + const plusesInName = '+'.repeat(name.split('+').length - 1) + if (plusColor && plusesInName.length >= 1) + colored = `${rankColorPrefix}[${nameWithoutPlus}§${plusColor}${plusesInName}${rankColorPrefix}]` + else if (name !== 'NONE') + colored = `${rankColorPrefix}[${name}]` + else + // nons don't have a prefix + colored = `${rankColorPrefix}` + } + return { + name, + color, + colored + } } diff --git a/src/cleaners/skyblock/member.ts b/src/cleaners/skyblock/member.ts index 154ff22..a4ca053 100644 --- a/src/cleaners/skyblock/member.ts +++ b/src/cleaners/skyblock/member.ts @@ -5,7 +5,7 @@ import { cleanObjectives, Objective } from './objectives' import { CleanMinion, cleanMinions } from './minions' import { cleanSkills, Skill } from './skills' import * as cached from '../../hypixelCached' -import { CleanFullProfile } from './profile' +import { CleanFullProfile, CleanFullProfileBasicMembers } from './profile' import { Included } from '../../hypixel' import { CleanPlayer } from '../player' import { Bank } from './bank' @@ -25,6 +25,7 @@ export interface CleanBasicMember { export interface CleanMember extends CleanBasicMember { purse: number stats: CleanProfileStats + rawHypixelStats?: { [ key: string ]: number } minions: CleanMinion[] fairy_souls: FairySouls inventories: Inventories @@ -61,6 +62,10 @@ export async function cleanSkyBlockProfileMemberResponse(member, included: Inclu purse: member.coin_purse, stats: cleanProfileStats(member), + + // this is used for leaderboards + rawHypixelStats: member.stats ?? {}, + minions: cleanMinions(member), fairy_souls: cleanFairySouls(member), inventories: inventoriesIncluded ? await cleanInventories(member) : undefined, @@ -83,5 +88,5 @@ export interface CleanMemberProfilePlayer extends CleanPlayer { export interface CleanMemberProfile { member: CleanMemberProfilePlayer - profile: CleanFullProfile + profile: CleanFullProfileBasicMembers } diff --git a/src/cleaners/skyblock/profile.ts b/src/cleaners/skyblock/profile.ts index 2b092a1..6e98f8f 100644 --- a/src/cleaners/skyblock/profile.ts +++ b/src/cleaners/skyblock/profile.ts @@ -8,7 +8,14 @@ export interface CleanProfile extends CleanBasicProfile { } export interface CleanFullProfile extends CleanProfile { - members: (CleanMember|CleanBasicMember)[] + members: CleanMember[] + bank: Bank + minions: CleanMinion[] + minion_count: number +} + +export interface CleanFullProfileBasicMembers extends CleanProfile { + members: CleanBasicMember[] bank: Bank minions: CleanMinion[] minion_count: number @@ -38,19 +45,21 @@ export async function cleanSkyblockProfileResponseLighter(data): Promise { - const cleanedMembers: CleanMember[] = [] - +export async function cleanSkyblockProfileResponse(data: any, options?: ApiOptions): Promise { + // We use Promise.all so it can fetch all the users at once instead of waiting for the previous promise to complete + const promises: Promise[] = [] + for (const memberUUID in data.members) { const memberRaw = data.members[memberUUID] memberRaw.uuid = memberUUID - const member: CleanMember = await cleanSkyBlockProfileMemberResponse( + promises.push(cleanSkyBlockProfileMemberResponse( memberRaw, - ['stats', mainMemberUuid === memberUUID ? 'inventories' : undefined] - ) - cleanedMembers.push(member) + ['stats', options?.mainMemberUuid === memberUUID ? 'inventories' : undefined] + )) } + const cleanedMembers: CleanMember[] = await Promise.all(promises) + const memberMinions: CleanMinion[][] = [] for (const member of cleanedMembers) { diff --git a/src/cleaners/skyblock/profiles.ts b/src/cleaners/skyblock/profiles.ts index ea290f6..c9f5628 100644 --- a/src/cleaners/skyblock/profiles.ts +++ b/src/cleaners/skyblock/profiles.ts @@ -1,5 +1,5 @@ import { HypixelPlayerStatsSkyBlockProfiles } from "../../hypixelApi" -import { CleanBasicProfile, CleanProfile, cleanSkyblockProfileResponseLighter } from "./profile" +import { CleanBasicProfile, CleanProfile, cleanSkyblockProfileResponse, cleanSkyblockProfileResponseLighter } from "./profile" export function cleanPlayerSkyblockProfiles(rawProfiles: HypixelPlayerStatsSkyBlockProfiles): CleanBasicProfile[] { let profiles: CleanBasicProfile[] = [] @@ -16,7 +16,8 @@ export function cleanPlayerSkyblockProfiles(rawProfiles: HypixelPlayerStatsSkyBl export async function cleanSkyblockProfilesResponse(data: any[]): Promise { const cleanedProfiles: CleanProfile[] = [] for (const profile of data ?? []) { - let cleanedProfile = await cleanSkyblockProfileResponseLighter(profile) + // let cleanedProfile = await cleanSkyblockProfileResponseLighter(profile) + let cleanedProfile = await cleanSkyblockProfileResponse(profile) cleanedProfiles.push(cleanedProfile) } return cleanedProfiles -- cgit