From cd707234194b41df933a3a287b5fbec3657063d5 Mon Sep 17 00:00:00 2001 From: mat <27899617+mat-1@users.noreply.github.com> Date: Sat, 13 Feb 2021 20:11:53 -0600 Subject: add objectives and skills --- src/cleaners/skyblock/bank.ts | 3 ++- src/cleaners/skyblock/member.ts | 30 ++++++++++++++++++++---------- src/cleaners/skyblock/objectives.ts | 17 +++++++++++++++++ src/cleaners/skyblock/profile.ts | 10 +++++----- src/cleaners/skyblock/skills.ts | 19 +++++++++++++++++++ src/cleaners/skyblock/stats.ts | 8 +++++--- 6 files changed, 68 insertions(+), 19 deletions(-) create mode 100644 src/cleaners/skyblock/objectives.ts create mode 100644 src/cleaners/skyblock/skills.ts (limited to 'src/cleaners/skyblock') diff --git a/src/cleaners/skyblock/bank.ts b/src/cleaners/skyblock/bank.ts index 302f392..4735bec 100644 --- a/src/cleaners/skyblock/bank.ts +++ b/src/cleaners/skyblock/bank.ts @@ -7,6 +7,7 @@ export function cleanBank(data: any): Bank { return { balance: data?.banking?.balance ?? 0, // TODO: make transactions good - history: data?.banking?.transactions ?? [] + // history: data?.banking?.transactions ?? [] + history: [] } } \ No newline at end of file diff --git a/src/cleaners/skyblock/member.ts b/src/cleaners/skyblock/member.ts index d7d0cd1..04aa476 100644 --- a/src/cleaners/skyblock/member.ts +++ b/src/cleaners/skyblock/member.ts @@ -5,6 +5,7 @@ import { Bank } from './bank' import { cleanFairySouls, FairySouls } from './fairysouls' import { cleanInventories, INVENTORIES } from './inventory' import { CleanMinion, cleanMinions } from './minions' +import { cleanObjectives, Objective } from './objectives' import { CleanFullProfile } from './profile' import { CleanProfileStats, cleanProfileStats } from './stats' @@ -16,29 +17,38 @@ export interface CleanBasicMember { } export interface CleanMember extends CleanBasicMember { - stats?: CleanProfileStats - minions?: CleanMinion[] - bank?: Bank - fairy_souls?: FairySouls + stats: CleanProfileStats + minions: CleanMinion[] + fairy_souls: FairySouls inventories: typeof INVENTORIES + objectives: Objective[] } +export async function cleanSkyBlockProfileMemberResponseBasic(member, included: Included[] = null): Promise { + return { + uuid: member.uuid, + username: await cached.usernameFromUser(member.uuid), + last_save: member.last_save, + first_join: member.first_join, + } +} /** Cleans up a member (from skyblock/profile) */ export async function cleanSkyBlockProfileMemberResponse(member, included: Included[] = null): Promise { // profiles.members[] - const statsIncluded = included == null || included.includes('stats') const inventoriesIncluded = included == null || included.includes('inventories') 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) : undefined, - fairy_souls: statsIncluded ? cleanFairySouls(member) : undefined, - inventories: inventoriesIncluded ? await cleanInventories(member) : undefined, + + stats: cleanProfileStats(member), + minions: cleanMinions(member), + fairy_souls: cleanFairySouls(member), + inventories: inventoriesIncluded ? await cleanInventories(member) : undefined, + objectives: cleanObjectives(member), + // skills: statsIncluded ? } } diff --git a/src/cleaners/skyblock/objectives.ts b/src/cleaners/skyblock/objectives.ts new file mode 100644 index 0000000..3427f1f --- /dev/null +++ b/src/cleaners/skyblock/objectives.ts @@ -0,0 +1,17 @@ +export interface Objective { + name: string + completed: boolean +} + +export function cleanObjectives(data: any): Objective[] { + const rawObjectives = data?.objectives || {} + const objectives: Objective[] = [] + for (const rawObjectiveName in rawObjectives) { + const rawObjectiveValue = rawObjectives[rawObjectiveName] + objectives.push({ + name: rawObjectiveName, + completed: rawObjectiveValue.status === 'COMPLETE', + }) + } + return objectives +} diff --git a/src/cleaners/skyblock/profile.ts b/src/cleaners/skyblock/profile.ts index b4a8574..2b092a1 100644 --- a/src/cleaners/skyblock/profile.ts +++ b/src/cleaners/skyblock/profile.ts @@ -1,4 +1,4 @@ -import { CleanBasicMember, CleanMember, cleanSkyBlockProfileMemberResponse } from './member' +import { CleanBasicMember, CleanMember, cleanSkyBlockProfileMemberResponse, cleanSkyBlockProfileMemberResponseBasic } from './member' import { CleanMinion, combineMinionArrays, countUniqueMinions } from './minions' import { Bank, cleanBank } from './bank' import { ApiOptions } from '../../hypixel' @@ -8,7 +8,7 @@ export interface CleanProfile extends CleanBasicProfile { } export interface CleanFullProfile extends CleanProfile { - members: CleanMember[] + members: (CleanMember|CleanBasicMember)[] bank: Bank minions: CleanMinion[] minion_count: number @@ -17,16 +17,16 @@ export interface CleanFullProfile extends CleanProfile { /** 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 { // 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[] = [] + const promises: Promise[] = [] 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, [])) + promises.push(cleanSkyBlockProfileMemberResponseBasic(memberRaw)) } - const cleanedMembers: CleanMember[] = await Promise.all(promises) + const cleanedMembers: CleanBasicMember[] = await Promise.all(promises) return { uuid: data.profile_id, diff --git a/src/cleaners/skyblock/skills.ts b/src/cleaners/skyblock/skills.ts new file mode 100644 index 0000000..e283379 --- /dev/null +++ b/src/cleaners/skyblock/skills.ts @@ -0,0 +1,19 @@ +export interface Skill { + name: string + xp: number +} + +export function cleanSkills(data: any): Skill[] { + const skills: Skill[] = [] + for (const item in data) { + if (item.startsWith('experience_skill_')) { + const skillName = item.substr('experience_skill_'.length) + const skillValue = data[item] + skills.push({ + name: skillName, + xp: skillValue + }) + } + } + return skills +} diff --git a/src/cleaners/skyblock/stats.ts b/src/cleaners/skyblock/stats.ts index 1222582..99aa1f7 100644 --- a/src/cleaners/skyblock/stats.ts +++ b/src/cleaners/skyblock/stats.ts @@ -61,13 +61,15 @@ export interface CleanProfileStats { } } -export function cleanProfileStats(statsRaw: any): CleanProfileStats { +export function cleanProfileStats(data: any): CleanProfileStats { // TODO: add type for statsRaw (probably in hypixelApi.ts since its coming from there) const stats: CleanProfileStats = {} - for (let statNameRaw in statsRaw) { + const rawStats = data?.stats ?? {} + for (const statNameRaw in rawStats) { + const statValue = rawStats[statNameRaw] let { category: statCategory, name: statName } = categorizeStat(statNameRaw) if (!stats[statCategory]) stats[statCategory] = {} - stats[statCategory][statName || 'total'] = statsRaw[statNameRaw] + stats[statCategory][statName || 'total'] = statValue } return stats } -- cgit