aboutsummaryrefslogtreecommitdiff
path: root/src/cleaners/achievements.ts
blob: 3e4189a46ffc59ddedce9d0744df6744c56eb135 (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 | null
	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.toLowerCase()}`] ?? null
			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.toLowerCase()}`),
				description: achievementData.description
			})
		}

		return { tiered: tieredAchievements, oneTime: oneTimeAchievements }
	}

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