aboutsummaryrefslogtreecommitdiff
path: root/src/cleaners/achievements.ts
blob: 372ab777ac9be3ec94f5c054188c2e9d921a52d2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import typedHypixelApi from 'typed-hypixel-api'
import { fetchAchievements } from '../hypixelCached.js'

interface TieredAchievement {
	id: string
	name: string
	value?: number
	description: string
}

interface OneTimeAchievement {
	id: string
	name: string
	achieved: boolean
	description: string
}

export interface Achievements {
	tiered: TieredAchievement[]
	oneTime: OneTimeAchievement[]
}

export async function cleanPlayerAchievements(data: typedHypixelApi.PlayerDataResponse['player']): Promise<Achievements> {
	const gameAchievements: typedHypixelApi.AchievementsResponse['achievements'] = await fetchAchievements()

	for (const [gameId, achievementsData] of Object.entries(gameAchievements)) {
		if (gameId !== 'skyblock') continue


		let tieredAchievements: TieredAchievement[] = []
		for (const [achievementId, achievementData] of Object.entries(achievementsData.tiered)) {
			const value = data.achievements[`skyblock_${achievementId}`] ?? undefined
			tieredAchievements.push({
				id: achievementId.toLowerCase(),
				name: achievementData.name,
				value,
				description: value ? achievementData.description.replace(/%s/g, value.toString()) : achievementData.description
			})
		}

		let oneTimeAchievements: OneTimeAchievement[] = []
		for (const [achievementId, achievementData] of Object.entries(achievementsData.one_time)) {
			oneTimeAchievements.push({
				id: achievementId.toLowerCase(),
				name: achievementData.name,
				achieved: data.achievementsOneTime.includes(`skyblock_${achievementId}`),
				description: achievementData.description
			})
		}

		return { tiered: tieredAchievements, oneTime: oneTimeAchievements }
	}

	// this shouldn't be possible
	console.debug('skyblock not found in achievements?')
	return { tiered: [], oneTime: [] }
}