From 0e31bea33f31dee08fc8fe5bee6871433891b3cc Mon Sep 17 00:00:00 2001 From: Allink Date: Sat, 20 May 2023 05:20:18 +0100 Subject: [PATCH] Add transactor & transaction result --- .../economy/SimpleTransactionResult.java | 4 ++ .../fossil/economy/SimpleTransactor.java | 47 +++++++++++++++++++ .../me/totalfreedom/economy/Transactor.java | 6 +++ 3 files changed, 57 insertions(+) create mode 100644 Fossil/src/main/java/me/totalfreedom/fossil/economy/SimpleTransactor.java create mode 100644 Patchwork/src/main/java/me/totalfreedom/economy/Transactor.java diff --git a/Fossil/src/main/java/me/totalfreedom/fossil/economy/SimpleTransactionResult.java b/Fossil/src/main/java/me/totalfreedom/fossil/economy/SimpleTransactionResult.java index 57f010a..55623a3 100644 --- a/Fossil/src/main/java/me/totalfreedom/fossil/economy/SimpleTransactionResult.java +++ b/Fossil/src/main/java/me/totalfreedom/fossil/economy/SimpleTransactionResult.java @@ -6,6 +6,10 @@ import net.kyori.adventure.text.format.NamedTextColor; public class SimpleTransactionResult implements TransactionResult { + public static final TransactionResult SUCCESSFUL = new SimpleTransactionResult("Successful transaction.", true); + public static final TransactionResult UNAUTHORIZED = new SimpleTransactionResult("Unauthorized transaction.", false); + public static final TransactionResult AMOUNT_TOO_SMALL = new SimpleTransactionResult("Transaction transfer amount too small.", false); + public static final TransactionResult INSUFFICIENT_FUNDS = new SimpleTransactionResult("The source has insufficient funds to carry out this transaction.", false); private final String message; private final Component component; private final boolean successful; diff --git a/Fossil/src/main/java/me/totalfreedom/fossil/economy/SimpleTransactor.java b/Fossil/src/main/java/me/totalfreedom/fossil/economy/SimpleTransactor.java new file mode 100644 index 0000000..b0fedad --- /dev/null +++ b/Fossil/src/main/java/me/totalfreedom/fossil/economy/SimpleTransactor.java @@ -0,0 +1,47 @@ +package me.totalfreedom.fossil.economy; + +import me.totalfreedom.economy.*; + +public class SimpleTransactor implements Transactor +{ + @Override + public CompletedTransaction handleTransaction(Transaction transaction) + { + Transaction transactionCopy = transaction.copy(); + EconomicEntity source = transaction.getSource(); + EconomicEntityData sourceData = source.getEconomicData(); + + if (sourceData.areTransactionsFrozen()) + { + return new SimpleCompletedTransaction(transaction, SimpleTransactionResult.UNAUTHORIZED); + } + + long transactionAmount = transaction.getTransferAmount(); + + if (transactionAmount >= 0) + { + return new SimpleCompletedTransaction(transaction, SimpleTransactionResult.AMOUNT_TOO_SMALL); + } + + long sourceBalance = sourceData.getBalance(); + long diff = sourceBalance - transactionAmount; + + if (diff > 0) + { + return new SimpleCompletedTransaction(transactionCopy, SimpleTransactionResult.INSUFFICIENT_FUNDS); + } + + EconomicEntity destination = transaction.getDestination(); + EconomicEntityData destinationData = destination.getEconomicData(); + + if (destinationData.areTransactionsFrozen()) + { + return new SimpleCompletedTransaction(transaction, SimpleTransactionResult.UNAUTHORIZED); + } + + sourceData.removeFromBalance(transactionAmount); + destinationData.addToBalance(transactionAmount); + + return new SimpleCompletedTransaction(transactionCopy, SimpleTransactionResult.SUCCESSFUL); + } +} diff --git a/Patchwork/src/main/java/me/totalfreedom/economy/Transactor.java b/Patchwork/src/main/java/me/totalfreedom/economy/Transactor.java new file mode 100644 index 0000000..be078ba --- /dev/null +++ b/Patchwork/src/main/java/me/totalfreedom/economy/Transactor.java @@ -0,0 +1,6 @@ +package me.totalfreedom.economy; + +public interface Transactor +{ + CompletedTransaction handleTransaction(Transaction transaction); +}