aboutsummaryrefslogtreecommitdiff
path: root/src/cleaners/skyblock/bank.ts
blob: b1a9e38d8d3a2352794f7aacdd0dc2990490367c (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
export interface Bank {
	balance: number
	history: BankHistoryItem[]
}

export interface BankHistoryItem {
	change: number
	total: number
	timestamp: number
	name: string
}

export function cleanBank(data: any): Bank {
	let history: BankHistoryItem[] = []
	
	if (data?.banking?.transactions) {
		let bankBalance = Math.round(data.banking.balance * 10) / 10
		// we go in reverse so we can simulate the bank transactions
		for (const transaction of data.banking.transactions.sort((a, b) => b.timestamp - a.timestamp)) {
			const change = transaction.action === 'DEPOSIT' ? transaction.amount : -transaction.amount
			// since we're going in reverse, we remove from the total balance when adding to the history
			bankBalance -= change
			history.push({
				change: change,
				total: bankBalance,
				timestamp: transaction.timestamp,
				name: transaction.initiator_name,
			})
		}
	}

	history.reverse()

	return {
		balance: data?.banking?.balance ?? 0,
		history
	}
}