Experimental transaction logger

This commit is contained in:
AppleDash 2016-09-20 22:47:25 -04:00
parent 6642c5b703
commit f9cf1269fb
4 changed files with 63 additions and 2 deletions

View File

@ -173,7 +173,7 @@ public class EconomyManager {
addBalance(to, amount, TransactionReason.PLAYER_PAY);
if (saneEconomy.shouldLogTransactions()) {
saneEconomy.getTransactionLogger().logTransfer(from, to, amount, TransactionReason.PLAYER_PAY);
saneEconomy.getTransactionLogger().logTransfer(from, to, amount);
}
return true;

View File

@ -10,5 +10,5 @@ import org.appledash.saneeconomy.economy.economable.Economable;
public interface TransactionLogger {
void logAddition(Economable economable, double amount, TransactionReason reason);
void logSubtraction(Economable economable, double amount, TransactionReason reason);
void logTransfer(Economable from, Economable to, double amount, TransactionReason reason);
void logTransfer(Economable from, Economable to, double amount);
}

View File

@ -0,0 +1,49 @@
package org.appledash.saneeconomy.economy.logger;
import org.appledash.saneeconomy.economy.TransactionReason;
import org.appledash.saneeconomy.economy.economable.Economable;
import org.appledash.saneeconomy.utils.DatabaseCredentials;
import org.appledash.saneeconomy.utils.MySQLConnection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
/**
* Created by appledash on 9/20/16.
* Blackjack is best pony.
*/
public class TransactionLoggerMySQL implements TransactionLogger {
private MySQLConnection dbConn;
public TransactionLoggerMySQL(DatabaseCredentials credentials) {
this.dbConn = new MySQLConnection(credentials);
}
@Override
public void logAddition(Economable economable, double amount, TransactionReason reason) {
logGeneric(reason.toString(), economable.getUniqueIdentifier(), amount);
}
@Override
public void logSubtraction(Economable economable, double amount, TransactionReason reason) {
logGeneric(reason.toString(), economable.getUniqueIdentifier(), -amount);
}
@Override
public void logTransfer(Economable from, Economable to, double amount) {
logGeneric(from.getUniqueIdentifier(), to.getUniqueIdentifier(), amount);
}
private void logGeneric(String from, String to, double change) {
this.dbConn.executeAsyncOperation((conn) -> {
try {
PreparedStatement ps = conn.prepareStatement("INSERT INTO transaction_logs (`source`, `destination`, `amount`) VALUES (?, ?, ?)");
ps.setString(1, from);
ps.setString(2, to);
ps.setDouble(3, change);
} catch (SQLException e) {
throw new RuntimeException("Error occurred logging addition", e);
}
});
}
}

View File

@ -8,6 +8,8 @@ import org.appledash.saneeconomy.economy.backend.EconomyStorageBackend;
import org.appledash.saneeconomy.economy.backend.type.EconomyStorageBackendFlatfile;
import org.appledash.saneeconomy.economy.backend.type.EconomyStorageBackendMySQL;
import org.appledash.saneeconomy.economy.economable.EconomableGeneric;
import org.appledash.saneeconomy.economy.logger.TransactionLogger;
import org.appledash.saneeconomy.economy.logger.TransactionLoggerMySQL;
import org.bukkit.configuration.Configuration;
import org.bukkit.configuration.ConfigurationSection;
@ -112,6 +114,16 @@ public class SaneEconomyConfiguration {
newer.waitUntilFlushed();
}
private TransactionLogger loadLogger() {
if (!rootConfig.getBoolean("log-transactions", false)) {
return null;
}
DatabaseCredentials credentials = loadCredentials(rootConfig.getConfigurationSection("logger-database"));
return new TransactionLoggerMySQL(credentials);
}
/**
* Load database host, port, username, password, and db name from a ConfigurationSection
* @param config ConfigurationSection containing the right fields.