Skip to content

Commit

Permalink
Add withdraw and transfer logic
Browse files Browse the repository at this point in the history
  • Loading branch information
ericgrandt committed Jan 14, 2024
1 parent 6a7327f commit 4700cb7
Show file tree
Hide file tree
Showing 4 changed files with 846 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.ericgrandt.totaleconomy.impl;

import java.math.BigDecimal;
import java.util.HashSet;
import java.util.Set;
import org.spongepowered.api.service.context.Context;
import org.spongepowered.api.service.economy.Currency;
Expand All @@ -18,6 +19,6 @@ public record TransactionResultImpl(
) implements TransactionResult {
@Override
public Set<Context> contexts() {
throw new UnsupportedOperationException();
return new HashSet<>();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.ericgrandt.totaleconomy.impl;

import java.math.BigDecimal;
import java.util.HashSet;
import java.util.Set;
import org.spongepowered.api.service.context.Context;
import org.spongepowered.api.service.economy.Currency;
import org.spongepowered.api.service.economy.account.Account;
import org.spongepowered.api.service.economy.transaction.ResultType;
import org.spongepowered.api.service.economy.transaction.TransactionType;
import org.spongepowered.api.service.economy.transaction.TransferResult;

public record TransferResultImpl(
Account account,
Account accountTo,
Currency currency,
BigDecimal amount,
ResultType result,
TransactionType type
) implements TransferResult {
@Override
public Set<Context> contexts() {
return new HashSet<>();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -181,22 +181,87 @@ public TransactionResult deposit(Currency currency, BigDecimal amount, Cause cau

@Override
public TransactionResult withdraw(Currency currency, BigDecimal amount, Set<Context> contexts) {
return null;
if (!hasBalance(currency, contexts)) {
return new TransactionResultImpl(this, currency, amount, ResultType.FAILED, null);
}

BigDecimal currentBalance = balances.get(currency);
BigDecimal newBalance = currentBalance.subtract(amount);
balances.put(currency, newBalance);

try {
balanceData.updateBalance(
accountId,
currencyDto.id(),
newBalance.doubleValue()
);
} catch (SQLException e) {
logger.error(
String.format(
"[Total Economy] Error calling updateBalance (accountId: %s, currencyId: %s, amount: %s)",
accountId,
currencyDto.id(),
newBalance
),
e
);
return new TransactionResultImpl(this, currency, amount, ResultType.FAILED, null);
}

return new TransactionResultImpl(this, currency, amount, ResultType.SUCCESS, null);
}

@Override
public TransactionResult withdraw(Currency currency, BigDecimal amount, Cause cause) {
return null;
return withdraw(currency, amount, new HashSet<>());
}

@Override
public TransferResult transfer(Account to, Currency currency, BigDecimal amount, Set<Context> contexts) {
return null;
if (amount.compareTo(BigDecimal.ZERO) <= 0
|| !hasBalance(currency, contexts)
|| !to.hasBalance(currency, contexts)
) {
return new TransferResultImpl(this, to, currency, amount, ResultType.FAILED, null);
}

BigDecimal currentFromBalance = balances.get(currency);
if (currentFromBalance.compareTo(amount) < 0) {
return new TransferResultImpl(this, to, currency, amount, ResultType.ACCOUNT_NO_FUNDS, null);
}

balances.put(currency, currentFromBalance.subtract(amount));

BigDecimal currentToBalance = to.balance(currency, contexts);
to.balances(contexts).put(currency, currentToBalance.add(amount));

try {
balanceData.transfer(
accountId,
UUID.fromString(to.identifier()),
currencyDto.id(),
amount.doubleValue()
);
} catch (SQLException e) {
logger.error(
String.format(
"[Total Economy] Error calling transfer (fromAccountId: %s, toAccountId: %s, currencyId: %s, amount: %s)",
accountId,
to.identifier(),
currencyDto.id(),
amount
),
e
);
return new TransferResultImpl(this, to, currency, amount, ResultType.FAILED, null);
}

return new TransferResultImpl(this, to, currency, amount, ResultType.SUCCESS, null);
}

@Override
public TransferResult transfer(Account to, Currency currency, BigDecimal amount, Cause cause) {
return null;
return transfer(to, currency, amount, new HashSet<>());
}

@Override
Expand Down
Loading

0 comments on commit 4700cb7

Please sign in to comment.