diff options
author | mat <27899617+mat-1@users.noreply.github.com> | 2021-02-13 14:35:58 -0600 |
---|---|---|
committer | mat <27899617+mat-1@users.noreply.github.com> | 2021-02-13 14:35:58 -0600 |
commit | 68c07e7a5ab8acf5ba7785eed594497e05467061 (patch) | |
tree | bf29688d541e817ea1d7e1dbdb0c83233a643530 /src | |
parent | f1c18020d5d86aac6119c842e6a108d99fae6e51 (diff) | |
download | skyblock-api-68c07e7a5ab8acf5ba7785eed594497e05467061.tar.gz skyblock-api-68c07e7a5ab8acf5ba7785eed594497e05467061.tar.bz2 skyblock-api-68c07e7a5ab8acf5ba7785eed594497e05467061.zip |
create member, profile, and profiles cleaners
Diffstat (limited to 'src')
-rw-r--r-- | src/cleaners/player.ts | 3 | ||||
-rw-r--r-- | src/cleaners/skyblock/member.ts | 52 | ||||
-rw-r--r-- | src/cleaners/skyblock/profile.ts | 79 | ||||
-rw-r--r-- | src/cleaners/skyblock/profiles.ts | 24 | ||||
-rw-r--r-- | src/hypixel.ts | 144 | ||||
-rw-r--r-- | src/hypixelCached.ts | 2 |
6 files changed, 161 insertions, 143 deletions
diff --git a/src/cleaners/player.ts b/src/cleaners/player.ts index c3afd6f..02eed90 100644 --- a/src/cleaners/player.ts +++ b/src/cleaners/player.ts @@ -1,8 +1,9 @@ -import { CleanBasicProfile, cleanPlayerSkyblockProfiles, Included } from '../hypixel' import { CleanSocialMedia, parseSocialMedia } from './socialmedia' import { CleanRank, parseRank } from './rank' import { HypixelPlayer } from '../hypixelApi' import { undashUuid } from '../util' +import { CleanBasicProfile } from './skyblock/profile' +import { cleanPlayerSkyblockProfiles } from './skyblock/profiles' export interface CleanBasicPlayer { uuid: string diff --git a/src/cleaners/skyblock/member.ts b/src/cleaners/skyblock/member.ts new file mode 100644 index 0000000..774dace --- /dev/null +++ b/src/cleaners/skyblock/member.ts @@ -0,0 +1,52 @@ +import { Included } from '../../hypixel' +import * as cached from '../../hypixelCached' +import { CleanPlayer } from '../player' +import { CleanMinion, cleanMinions } from './minions' +import { CleanProfileStats, cleanProfileStats } from './stats' + +export interface CleanBasicMember { + uuid: string + username: string + last_save: number + first_join: number +} + +export interface CleanMember extends CleanBasicMember { + stats?: CleanProfileStats + minions?: CleanMinion[] +} + + +/** Cleans up a member (from skyblock/profile) */ +export async function cleanSkyBlockProfileMemberResponse(member, included: Included[] = null): Promise<CleanMember> { + // profiles.members[] + const statsIncluded = included == null || included.includes('stats') + return { + uuid: member.uuid, + username: await cached.usernameFromUser(member.uuid), + last_save: member.last_save, + first_join: member.first_join, + // last_death: ??? idk how this is formatted, + stats: statsIncluded ? cleanProfileStats(member.stats) : undefined, + minions: statsIncluded ? cleanMinions(member.crafted_generators) : undefined, + } +} + + +export interface CleanMemberProfilePlayer extends CleanPlayer { + // The profile name may be different for each player, so we put it here + profileName: string + first_join: number + last_save: number + bank?: { + balance: number + history: any[] + } +} + +export interface CleanMemberProfile { + member: CleanMemberProfilePlayer + profile: { + + } +} diff --git a/src/cleaners/skyblock/profile.ts b/src/cleaners/skyblock/profile.ts new file mode 100644 index 0000000..8b59a9d --- /dev/null +++ b/src/cleaners/skyblock/profile.ts @@ -0,0 +1,79 @@ +import { CleanBasicMember, CleanMember, cleanSkyBlockProfileMemberResponse } from './member' +import { CleanMinion, combineMinionArrays } from './minions' + + +export interface CleanProfile extends CleanBasicProfile { + members?: CleanBasicMember[] +} + +export interface CleanFullProfile extends CleanProfile { + members: CleanMember[] + bank?: { + balance: number + history: any[] + } + 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: { + balance: data?.banking?.balance ?? 0, + + // TODO: make transactions good + history: data?.banking?.transactions ?? [] + }, + 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 +} + diff --git a/src/cleaners/skyblock/profiles.ts b/src/cleaners/skyblock/profiles.ts new file mode 100644 index 0000000..c942a14 --- /dev/null +++ b/src/cleaners/skyblock/profiles.ts @@ -0,0 +1,24 @@ +import { HypixelPlayerStatsSkyBlockProfiles } from "../../hypixelApi" +import { CleanBasicProfile, CleanProfile, cleanSkyblockProfileResponseLighter } from "./profile" + +export function cleanPlayerSkyblockProfiles(rawProfiles: HypixelPlayerStatsSkyBlockProfiles): CleanBasicProfile[] { + let profiles: CleanBasicProfile[] = [] + for (const profile of Object.values(rawProfiles)) { + profiles.push({ + uuid: profile.profile_id, + name: profile.cute_name + }) + } + console.log('cleanPlayerSkyblockProfiles', profiles) + return profiles +} + +/** Convert an array of raw profiles into clean profiles */ +export async function cleanSkyblockProfilesResponse(data: any[]): Promise<CleanProfile[]> { + const cleanedProfiles: CleanProfile[] = [] + for (const profile of data) { + let cleanedProfile = await cleanSkyblockProfileResponseLighter(profile) + cleanedProfiles.push(cleanedProfile) + } + return cleanedProfiles +} diff --git a/src/hypixel.ts b/src/hypixel.ts index f17dc9e..105e627 100644 --- a/src/hypixel.ts +++ b/src/hypixel.ts @@ -3,6 +3,9 @@ import { CleanProfileStats, cleanProfileStats } from './cleaners/skyblock/stats' import { CleanPlayer, cleanPlayerResponse } from './cleaners/player' import { chooseApiKey, HypixelPlayerStatsSkyBlockProfiles, HypixelResponse, sendApiRequest } from './hypixelApi' import * as cached from './hypixelCached' +import { CleanMemberProfile } from './cleaners/skyblock/member' +import { cleanSkyblockProfileResponse, CleanProfile, CleanBasicProfile } from './cleaners/skyblock/profile' +import { cleanSkyblockProfilesResponse } from './cleaners/skyblock/profiles' export type Included = 'profiles' | 'player' | 'stats' @@ -34,148 +37,7 @@ export async function sendCleanApiRequest({ path, args }, included?: Included[], } } -export interface CleanBasicMember { - uuid: string - username: string - last_save: number - first_join: number -} - -interface CleanMember extends CleanBasicMember { - stats?: CleanProfileStats - minions?: CleanMinion[] -} - -async function cleanSkyBlockProfileMemberResponse(member, included: Included[] = null): Promise<CleanMember> { - // Cleans up a member (from skyblock/profile) - // profiles.members[] - const statsIncluded = included == null || included.includes('stats') - return { - uuid: member.uuid, - username: await cached.usernameFromUser(member.uuid), - last_save: member.last_save, - first_join: member.first_join, - // last_death: ??? idk how this is formatted, - stats: statsIncluded ? cleanProfileStats(member.stats) : undefined, - minions: statsIncluded ? cleanMinions(member.crafted_generators) : undefined, - } -} - - -export interface CleanMemberProfilePlayer extends CleanPlayer { - // The profile name may be different for each player, so we put it here - profileName: string - first_join: number - last_save: number - bank?: { - balance: number - history: any[] - } -} - -export interface CleanMemberProfile { - member: CleanMemberProfilePlayer - profile: { - - } -} - -export interface CleanProfile extends CleanBasicProfile { - members?: CleanBasicMember[] -} - -export interface CleanFullProfile extends CleanProfile { - members: CleanMember[] - bank?: { - balance: number - history: any[] - } - minions: CleanMinion[] -} - -/** Return a `CleanProfile` instead of a `CleanFullProfile`, useful when we need to get members but don't want to waste much ram */ -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 very costly and shouldn't be called often. Use cleanSkyblockProfileResponseLighter if you don't need all the data */ -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: { - balance: data?.banking?.balance ?? 0, - - // TODO: make transactions good - history: data?.banking?.transactions ?? [] - }, - 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 -} - -export function cleanPlayerSkyblockProfiles(rawProfiles: HypixelPlayerStatsSkyBlockProfiles): CleanBasicProfile[] { - let profiles: CleanBasicProfile[] = [] - for (const profile of Object.values(rawProfiles)) { - profiles.push({ - uuid: profile.profile_id, - name: profile.cute_name - }) - } - console.log('cleanPlayerSkyblockProfiles', profiles) - return profiles -} - -/** Convert an array of raw profiles into clean profiles */ -async function cleanSkyblockProfilesResponse(data: any[]): Promise<CleanProfile[]> { - const cleanedProfiles: CleanProfile[] = [] - for (const profile of data) { - let cleanedProfile = await cleanSkyblockProfileResponseLighter(profile) - cleanedProfiles.push(cleanedProfile) - } - return cleanedProfiles -} async function cleanResponse({ path, data }: { path: string, data: HypixelResponse }, included?: Included[]) { // Cleans up an api response diff --git a/src/hypixelCached.ts b/src/hypixelCached.ts index 5fe65a3..7f0305e 100644 --- a/src/hypixelCached.ts +++ b/src/hypixelCached.ts @@ -2,8 +2,8 @@ import NodeCache from 'node-cache' import * as mojang from './mojang' import * as hypixel from './hypixel' import { CleanPlayer } from './cleaners/player' -import { CleanBasicProfile, CleanFullProfile, CleanProfile } from './hypixel' import { undashUuid } from './util' +import { CleanProfile, CleanFullProfile, CleanBasicProfile } from './cleaners/skyblock/profile' /** Hypixel... but with caching |