Merge branch 'banning-update' of https://github.com/SimplexDevelopment/FreedomNetworkSuite into banning-update

This commit is contained in:
Paul Reilly
2023-05-21 21:47:54 -05:00
22 changed files with 904 additions and 9 deletions

View File

@ -0,0 +1,47 @@
package me.totalfreedom.fossil.economy;
import me.totalfreedom.economy.CompletedTransaction;
import me.totalfreedom.economy.EconomicEntity;
import me.totalfreedom.economy.Transaction;
import me.totalfreedom.economy.TransactionResult;
public class SimpleCompletedTransaction implements CompletedTransaction
{
private final TransactionResult transactionResult;
private final EconomicEntity source;
private final EconomicEntity destination;
private final long balance;
public SimpleCompletedTransaction(Transaction transaction, TransactionResult transactionResult)
{
this.source = transaction.getSource();
this.destination = transaction.getDestination();
this.balance = transaction.getBalance();
this.transactionResult = transactionResult;
}
@Override
public TransactionResult getResult()
{
return transactionResult;
}
@Override
public EconomicEntity getSource()
{
return source;
}
@Override
public EconomicEntity getDestination()
{
return destination;
}
@Override
public long getBalance()
{
return balance;
}
}

View File

@ -0,0 +1,37 @@
package me.totalfreedom.fossil.economy;
import me.totalfreedom.economy.CompletedTransaction;
import me.totalfreedom.economy.MutableTransaction;
import me.totalfreedom.economy.TransactionLogger;
import me.totalfreedom.economy.Transactor;
public class SimpleLoggedTransactor implements Transactor
{
private final Transactor transactor;
private final TransactionLogger transactionLogger;
public SimpleLoggedTransactor()
{
this(new SimpleTransactor(), new SimpleTransactionLogger());
}
public SimpleLoggedTransactor(Transactor transactor, TransactionLogger transactionLogger)
{
this.transactor = transactor;
this.transactionLogger = transactionLogger;
}
@Override
public CompletedTransaction handleTransaction(MutableTransaction transaction)
{
CompletedTransaction completedTransaction = transactor.handleTransaction(transaction);
transactionLogger.logTransaction(completedTransaction);
return completedTransaction;
}
public TransactionLogger getTransactionLogger()
{
return this.transactionLogger;
}
}

View File

@ -0,0 +1,30 @@
package me.totalfreedom.fossil.economy;
import me.totalfreedom.economy.EconomicEntity;
import me.totalfreedom.economy.MutableTransaction;
public class SimpleMutableTransaction extends SimpleTransaction implements MutableTransaction
{
public SimpleMutableTransaction(EconomicEntity source, EconomicEntity destination, long balance)
{
super(source, destination, balance);
}
@Override
public long addToBalance(long amount)
{
return balance.addAndGet(amount);
}
@Override
public long removeFromBalance(long amount)
{
return this.addToBalance(-amount);
}
@Override
public void setBalance(long newBalance)
{
balance.set(newBalance);
}
}

View File

@ -0,0 +1,38 @@
package me.totalfreedom.fossil.economy;
import me.totalfreedom.economy.EconomicEntity;
import me.totalfreedom.economy.Transaction;
import java.util.concurrent.atomic.AtomicLong;
public class SimpleTransaction implements Transaction
{
private final EconomicEntity source;
private final EconomicEntity destination;
protected final AtomicLong balance;
public SimpleTransaction(EconomicEntity source, EconomicEntity destination, long balance)
{
this.source = source;
this.destination = destination;
this.balance = new AtomicLong(balance);
}
@Override
public EconomicEntity getSource()
{
return source;
}
@Override
public EconomicEntity getDestination()
{
return destination;
}
@Override
public long getBalance()
{
return balance.get();
}
}

View File

@ -0,0 +1,45 @@
package me.totalfreedom.fossil.economy;
import me.totalfreedom.audience.MutableAudienceForwarder;
import me.totalfreedom.economy.*;
import me.totalfreedom.utils.FreedomLogger;
import net.kyori.adventure.text.Component;
public class SimpleTransactionLogger implements TransactionLogger
{
private final MutableAudienceForwarder audience = MutableAudienceForwarder.from(FreedomLogger.getLogger("Fossil"));
@Override
public void logTransaction(CompletedTransaction completedTransaction)
{
StringBuilder transactionLoggingStatementBuilder = new StringBuilder();
TransactionResult result = completedTransaction.getResult();
boolean resultSuccess = result.isSuccessful();
String resultMessage = result.getMessage();
EconomicEntity source = completedTransaction.getSource();
EconomicEntity destination = completedTransaction.getDestination();
long transactionAmount = completedTransaction.getBalance();
transactionLoggingStatementBuilder.append(resultSuccess ? "Successful" : "Unsuccessful")
.append(" (")
.append(resultMessage)
.append(") ")
.append(" transaction between ")
.append(source.getName())
.append(" ")
.append(destination.getName())
.append(" where the volume of currency transferred was $")
.append(transactionAmount)
.append(".");
Component message = Component.text(transactionLoggingStatementBuilder.toString());
audience.sendMessage(message);
}
public MutableAudienceForwarder getAudienceForwarder()
{
return audience;
}
}

View File

@ -0,0 +1,46 @@
package me.totalfreedom.fossil.economy;
import me.totalfreedom.economy.TransactionResult;
import net.kyori.adventure.text.Component;
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 balance too small.", false);
public static final TransactionResult INSUFFICIENT_FUNDS = new SimpleTransactionResult("The source has an insufficient balance to carry out this transaction.", false);
private final String message;
private final Component component;
private final boolean successful;
public SimpleTransactionResult(String message, boolean successful)
{
this(message, Component.text(message, successful ? NamedTextColor.GREEN : NamedTextColor.RED), successful);
}
public SimpleTransactionResult(String message, Component component, boolean successful)
{
this.message = message;
this.component = component;
this.successful = successful;
}
@Override
public String getMessage()
{
return message;
}
@Override
public boolean isSuccessful()
{
return successful;
}
@Override
public Component getComponent()
{
return component;
}
}

View File

@ -0,0 +1,46 @@
package me.totalfreedom.fossil.economy;
import me.totalfreedom.economy.*;
public class SimpleTransactor implements Transactor
{
@Override
public CompletedTransaction handleTransaction(MutableTransaction transaction)
{
EconomicEntity source = transaction.getSource();
EconomicEntityData sourceData = source.getEconomicData();
if (sourceData.areTransactionsFrozen())
{
return new SimpleCompletedTransaction(transaction, SimpleTransactionResult.UNAUTHORIZED);
}
long transactionAmount = transaction.getBalance();
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(transaction, 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(transaction, SimpleTransactionResult.SUCCESSFUL);
}
}