diff options
author | mat <github@matdoes.dev> | 2022-03-20 20:58:01 -0500 |
---|---|---|
committer | mat <github@matdoes.dev> | 2022-03-20 20:58:01 -0500 |
commit | c467866d8eb9634f2cc7c441ee3735c49e6970d2 (patch) | |
tree | ab7252d91e3b1887b967fece368797663fe15bd7 /src/cleaners | |
parent | 30fc35f74e8692db533250aa8114fcdd0aa297f9 (diff) | |
download | skyblock-api-c467866d8eb9634f2cc7c441ee3735c49e6970d2.tar.gz skyblock-api-c467866d8eb9634f2cc7c441ee3735c49e6970d2.tar.bz2 skyblock-api-c467866d8eb9634f2cc7c441ee3735c49e6970d2.zip |
Add bank history
Diffstat (limited to 'src/cleaners')
-rw-r--r-- | src/cleaners/skyblock/bank.ts | 33 |
1 files changed, 29 insertions, 4 deletions
diff --git a/src/cleaners/skyblock/bank.ts b/src/cleaners/skyblock/bank.ts index 4735bec..b1a9e38 100644 --- a/src/cleaners/skyblock/bank.ts +++ b/src/cleaners/skyblock/bank.ts @@ -1,13 +1,38 @@ export interface Bank { balance: number - history: any[] + 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, - // TODO: make transactions good - // history: data?.banking?.transactions ?? [] - history: [] + history } }
\ No newline at end of file |