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/cleaners/skyblock | |
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/cleaners/skyblock')
-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 |
3 files changed, 155 insertions, 0 deletions
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 +} |