Fix transaction logging, hopefully

This commit is contained in:
AppleDash 2016-10-02 11:33:30 -04:00
parent 7f5740e5f1
commit f9b53695d6
9 changed files with 63 additions and 36 deletions

2
.gitignore vendored
View File

@ -14,3 +14,5 @@ RemoteSystemsTempFiles/.project
out/
target/
dependency-reduced-pom.xml
SaneEconomySignShop/

View File

@ -142,6 +142,10 @@ public class SaneEconomy extends JavaPlugin implements ISaneEconomy {
*/
@Override
public TransactionLogger getTransactionLogger() {
if (!shouldLogTransactions()) {
throw new IllegalStateException("TransactionLogger should not be retrieved if we aren't logging transactions!");
}
return transactionLogger;
}

View File

@ -81,7 +81,7 @@ public class EconomyAdminCommand extends SaneEconomyCommand {
}
if (subCommand.equalsIgnoreCase("give")) {
Transaction transaction = new Transaction(Economable.wrap(sender), Economable.wrap(targetPlayer), amount, TransactionReason.ADMIN);
Transaction transaction = new Transaction(Economable.wrap(sender), Economable.wrap(targetPlayer), amount, TransactionReason.ADMIN_GIVE);
TransactionResult result = ecoMan.transact(transaction);
double newAmount = result.getToBalance();
@ -117,12 +117,12 @@ public class EconomyAdminCommand extends SaneEconomyCommand {
// FIXME: This is a silly hack to get it to log.
if (oldBal > 0.0) {
saneEconomy.getTransactionLogger().logTransaction(new Transaction(
economable, Economable.CONSOLE, oldBal, TransactionReason.ADMIN
economable, Economable.CONSOLE, oldBal, TransactionReason.ADMIN_GIVE
));
}
saneEconomy.getTransactionLogger().logTransaction(new Transaction(
Economable.CONSOLE, economable, amount, TransactionReason.ADMIN
Economable.CONSOLE, economable, amount, TransactionReason.ADMIN_GIVE
));
}

View File

@ -4,7 +4,6 @@ import org.appledash.saneeconomy.ISaneEconomy;
import org.appledash.saneeconomy.economy.backend.EconomyStorageBackend;
import org.appledash.saneeconomy.economy.economable.Economable;
import org.appledash.saneeconomy.economy.transaction.Transaction;
import org.appledash.saneeconomy.economy.transaction.TransactionReason;
import org.appledash.saneeconomy.economy.transaction.TransactionResult;
import org.appledash.saneeconomy.utils.NumberUtils;
import org.bukkit.Bukkit;
@ -165,17 +164,15 @@ public class EconomyManager {
Economable receiver = transaction.getReceiver();
double amount = transaction.getAmount(); // This amount is validated upon creation of Transaction
if (!transaction.isFree()) { // If the transaction is occurring because of another plugin or because of an admin.
// If the sender doesn't have the balance AND we're not testing, throw an error.
// I don't really know why we check if they're testing, but it breaks if we don't. FIXME.
if (!hasBalance(sender, amount) && transaction.getReason() != TransactionReason.TEST && transaction.getReason() != TransactionReason.ADMIN_TAKE) {
if (transaction.isSenderAffected()) { // Sender should have balance taken from them
if (!hasBalance(sender, amount)) {
return new TransactionResult(transaction, TransactionResult.Status.ERR_NOT_ENOUGH_FUNDS);
}
subtractBalance(sender, amount);
}
if (transaction.getReason() != TransactionReason.ADMIN_TAKE) {
if (transaction.isReceiverAffected()) { // Receiver should have balance added to them
addBalance(receiver, amount);
}

View File

@ -1,6 +1,7 @@
package org.appledash.saneeconomy.economy.transaction;
import org.appledash.saneeconomy.economy.economable.Economable;
import org.appledash.saneeconomy.economy.transaction.TransactionReason.AffectedParties;
/**
* Created by appledash on 9/21/16.
@ -39,7 +40,11 @@ public class Transaction {
return reason;
}
public boolean isFree() {
return (sender == Economable.CONSOLE) || (sender == Economable.PLUGIN) || (reason == TransactionReason.ADMIN);
public boolean isSenderAffected() {
return (reason.getAffectedParties() == AffectedParties.SENDER) || (reason.getAffectedParties() == AffectedParties.BOTH);
}
public boolean isReceiverAffected() {
return (reason.getAffectedParties() == AffectedParties.RECEIVER) || (reason.getAffectedParties() == AffectedParties.BOTH);
}
}

View File

@ -8,22 +8,40 @@ public enum TransactionReason {
/**
* A player paying another player.
*/
PLAYER_PAY,
PLAYER_PAY(AffectedParties.BOTH),
/**
* An admin giving a player money.
*/
ADMIN,
ADMIN_TAKE,
ADMIN_GIVE(AffectedParties.RECEIVER),
ADMIN_TAKE(AffectedParties.SENDER),
/**
* Another plugin using the API.
*/
PLUGIN,
PLUGIN_GIVE(AffectedParties.RECEIVER),
PLUGIN_TAKE(AffectedParties.SENDER),
/**
* Initial starting balance on join.
*/
STARTING_BALANCE,
STARTING_BALANCE(AffectedParties.RECEIVER),
/**
* Used in unit tests.
*/
TEST
TEST_GIVE(AffectedParties.RECEIVER),
TEST_TAKE(AffectedParties.SENDER);
private final AffectedParties affectedParties;
TransactionReason(AffectedParties affectedParties) {
this.affectedParties = affectedParties;
}
public AffectedParties getAffectedParties() {
return affectedParties;
}
public enum AffectedParties {
SENDER,
RECEIVER,
BOTH
}
}

View File

@ -149,7 +149,7 @@ public class EconomySaneEconomy implements Economy {
}
return transact(new Transaction(
economable, Economable.PLUGIN, v, TransactionReason.PLUGIN
economable, Economable.PLUGIN, v, TransactionReason.PLUGIN_TAKE
));
}
@ -164,7 +164,7 @@ public class EconomySaneEconomy implements Economy {
}
return transact(new Transaction(
Economable.wrap(offlinePlayer), Economable.PLUGIN, v, TransactionReason.PLUGIN
Economable.wrap(offlinePlayer), Economable.PLUGIN, v, TransactionReason.PLUGIN_TAKE
));
}
@ -192,7 +192,7 @@ public class EconomySaneEconomy implements Economy {
}
return transact(new Transaction(
Economable.PLUGIN, economable, v, TransactionReason.PLUGIN
Economable.PLUGIN, economable, v, TransactionReason.PLUGIN_GIVE
));
}
@ -203,7 +203,7 @@ public class EconomySaneEconomy implements Economy {
}
return transact(new Transaction(
Economable.PLUGIN, Economable.wrap(offlinePlayer), v, TransactionReason.PLUGIN
Economable.PLUGIN, Economable.wrap(offlinePlayer), v, TransactionReason.PLUGIN_GIVE
));
}

View File

@ -37,8 +37,8 @@ public class EconomyManagerTest {
// Accounts should not exist
Assert.assertFalse(economyManager.accountExists(playerOne));
Assert.assertFalse(economyManager.accountExists(playerTwo));
Assert.assertEquals(economyManager.getBalance(playerOne), 0.0D, 0.0);
Assert.assertEquals(economyManager.getBalance(playerTwo), 0.0D, 0.0);
Assert.assertEquals(0.0D, economyManager.getBalance(playerOne), 0.0);
Assert.assertEquals(0.0D, economyManager.getBalance(playerTwo), 0.0);
economyManager.setBalance(playerOne, 100.0D);
@ -47,28 +47,28 @@ public class EconomyManagerTest {
Assert.assertFalse(economyManager.accountExists(playerTwo));
// One should have balance, two should not
Assert.assertEquals(economyManager.getBalance(playerOne), 100.0, 0.0);
Assert.assertEquals(economyManager.getBalance(playerTwo), 0.0, 0.0);
Assert.assertEquals(100.0, economyManager.getBalance(playerOne), 0.0);
Assert.assertEquals(0.0, economyManager.getBalance(playerTwo), 0.0);
// One should be able to transfer to two
Assert.assertTrue(economyManager.transact(new Transaction(playerOne, playerTwo, 50.0, TransactionReason.PLAYER_PAY)).getStatus() == TransactionResult.Status.SUCCESS);
// One should now have only 50 left, two should have 50 now
Assert.assertEquals(economyManager.getBalance(playerOne), 50.0, 0.0);
Assert.assertEquals(economyManager.getBalance(playerTwo), 50.0, 0.0);
Assert.assertEquals(50.0, economyManager.getBalance(playerOne), 0.0);
Assert.assertEquals(50.0, economyManager.getBalance(playerTwo), 0.0);
// Ensure that balance addition and subtraction works...
Assert.assertEquals(economyManager.transact(
new Transaction(playerOne, Economable.CONSOLE, 25.0, TransactionReason.TEST)
).getFromBalance(), 25.0, 0.0);
Assert.assertEquals(25.0, economyManager.transact(
new Transaction(playerOne, Economable.CONSOLE, 25.0, TransactionReason.TEST_TAKE)
).getFromBalance(), 0.0);
Assert.assertEquals(economyManager.transact(
new Transaction(Economable.CONSOLE, playerOne, 25.0, TransactionReason.TEST)
).getToBalance(), 50.0, 0.0);
Assert.assertEquals(50.0, economyManager.transact(
new Transaction(Economable.CONSOLE, playerOne, 25.0, TransactionReason.TEST_GIVE)
).getToBalance(), 0.0);
Assert.assertEquals(economyManager.transact(
new Transaction(playerTwo, Economable.CONSOLE, Double.MAX_VALUE, TransactionReason.TEST)
).getFromBalance(), 0.0, 0.0);
Assert.assertEquals(TransactionResult.Status.ERR_NOT_ENOUGH_FUNDS, economyManager.transact(
new Transaction(playerTwo, Economable.CONSOLE, Double.MAX_VALUE, TransactionReason.TEST_TAKE)
).getStatus());
// Ensure that hasBalance works
Assert.assertTrue(economyManager.hasBalance(playerOne, 50.0));

View File

@ -11,6 +11,7 @@
<modules>
<module>SaneEconomyCore</module>
<module>SaneEconomySignShop</module>
</modules>
<repositories>