From 6cb86433e0190cdead1e213fa3d11391ba9c56e4 Mon Sep 17 00:00:00 2001 From: mat Date: Thu, 19 May 2022 18:55:26 +0000 Subject: better achievements --- src/cleaners/achievements.ts | 48 ++++++++++++++++++++++++++++++++++++-------- 1 file changed, 40 insertions(+), 8 deletions(-) (limited to 'src/cleaners/achievements.ts') diff --git a/src/cleaners/achievements.ts b/src/cleaners/achievements.ts index 9e4c7ae..d556f74 100644 --- a/src/cleaners/achievements.ts +++ b/src/cleaners/achievements.ts @@ -1,18 +1,50 @@ import typedHypixelApi from 'typed-hypixel-api' +import { fetchAchievements } from '../hypixelCached.js' + +interface Achievement { + id: string + name: string + value: number | boolean + description: string +} export interface Achievements { - skyblock: Record + skyblock: Achievement[] } -export function cleanPlayerAchievements(data: typedHypixelApi.PlayerDataResponse['player']): Achievements { - const achievements: Achievements = { - skyblock: {} +export async function cleanPlayerAchievements(data: typedHypixelApi.PlayerDataResponse['player']): Promise { + const playerAchievements: Achievements = { + skyblock: [] } - for (const [id, value] of Object.entries(data.achievements)) { - if (id.startsWith('skyblock_')) - achievements.skyblock[id.substring(9)] = value + const gameAchievements: typedHypixelApi.AchievementsResponse['achievements'] = await fetchAchievements() + + for (const [gameId, achievementsData] of Object.entries(gameAchievements)) { + if (gameId !== 'skyblock') continue + + let tieredAchievements: Achievement[] = [] + for (const [achievementId, achievementData] of Object.entries(achievementsData.tiered)) { + const value = data.achievements[`skyblock_${achievementId}`] ?? 0 + tieredAchievements.push({ + id: achievementId.toLowerCase(), + name: achievementData.name, + value, + description: achievementData.description.replace(/%s/g, value.toString()) + }) + } + + let oneTimeAchievements: Achievement[] = [] + for (const [achievementId, achievementData] of Object.entries(achievementsData.one_time)) { + oneTimeAchievements.push({ + id: achievementId.toLowerCase(), + name: achievementData.name, + value: data.achievementsOneTime.includes(`skyblock_${achievementId}`), + description: achievementData.description + }) + } + + playerAchievements[gameId] = [...tieredAchievements, ...oneTimeAchievements] } - return achievements + return playerAchievements } -- cgit