aboutsummaryrefslogtreecommitdiff
path: root/src/cleaners/skyblock/election.ts
blob: 1688c66932d20daf120e8f4230668b9f865b825e (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
58
59
60
61
62
import typedHypixelApi from 'typed-hypixel-api'


const candidateColors = [
	'4', 'a', '3', 'e', '5',
]

export interface MayorPerk {
	name: string
	description: string
}

export interface Candidate {
	name: string
	perks: MayorPerk[]
	votes: number
	color: string | null
}

export interface ElectionData {
	lastUpdated: number
	previous: {
		year: number
		winner: string
		candidates: Candidate[]
	}
	current: {
		year: number
		candidates: Candidate[]
	} | null
}

function cleanCandidate(
	data: typedHypixelApi.Candidate & { votes: number },
	index: number
): Candidate {
	return {
		name: data.name,
		perks: data.perks.map(perk => ({
			name: perk.name,
			description: '§7' + perk.description,
		})),
		votes: data.votes,
		color: candidateColors[index],
	}
}

export async function cleanElectionResponse(data: typedHypixelApi.SkyBlockElectionResponse): Promise<ElectionData> {
	const previousCandidates = data.mayor.election.candidates.map(cleanCandidate)
	return {
		lastUpdated: data.lastUpdated,
		previous: {
			year: data.mayor.election.year,
			winner: data.mayor.name,
			candidates: previousCandidates
		},
		current: data.current ? {
			year: data.current.year,
			candidates: data.current.candidates.map(cleanCandidate)
		} : null
	}
}