aboutsummaryrefslogtreecommitdiff
path: root/src/cleaners/skyblock/election.ts
blob: 78937ec1f879f1fd658dc42acc1cf4f096084877 (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
63
64
const candidateColors = {
	barry: 'e',
	paul: '4',
	aatrox: 'a',
	foxy: '3',
	cole: 'e',
	marina: '5',
	diaz: '5',
	diana: '3',
}

export interface MayorPerk {
	name: string
	description: string
}

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

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

function cleanCandidate(data: any): Candidate {
	return {
		name: data.name,
		perks: data.perks,
		votes: data.votes,
		color: candidateColors[data.name.toLowerCase()],
	}
}

export function cleanElectionResponse(data: any): ElectionData {
	return {
		last_updated: data.lastUpdated / 1000,
		previous: {
			year: data.mayor.election.year,
			winner: cleanCandidate({
				name: data.mayor.name,
				perks: data.mayor.perks,
				votes: data.mayor.election.candidates.find(c => c.key === data.mayor.key).votes,
			}),
			candidates: data.mayor.election.candidates.map(cleanCandidate)
		},
		current: data.current ? {
			year: data.current.year,
			candidates: data.current.candidates.map(cleanCandidate)
		} : null
	}
}