Transaction logging seems to work, kind of.

This commit is contained in:
AppleDash 2016-09-21 02:23:38 -04:00
parent 92350e6d25
commit d1b302e779
3 changed files with 24 additions and 3 deletions

View File

@ -142,9 +142,9 @@ public class EconomyManager {
if (saneEconomy.shouldLogTransactions() && reason != TransactionReason.PLAYER_PAY) { // Player pay is handled in the transfer() method.
if (oldAmount > amount) { // Lower amount now
saneEconomy.getTransactionLogger().logSubtraction(targetPlayer, amount, reason);
saneEconomy.getTransactionLogger().logSubtraction(targetPlayer, oldAmount - amount, reason);
} else if (oldAmount < amount) { // Higher amount now
saneEconomy.getTransactionLogger().logAddition(targetPlayer, amount, reason);
saneEconomy.getTransactionLogger().logAddition(targetPlayer, amount - oldAmount, reason);
}
}
}

View File

@ -5,6 +5,7 @@ import org.appledash.saneeconomy.economy.economable.Economable;
import org.appledash.saneeconomy.utils.DatabaseCredentials;
import org.appledash.saneeconomy.utils.MySQLConnection;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
@ -41,6 +42,7 @@ public class TransactionLoggerMySQL implements TransactionLogger {
ps.setString(1, from);
ps.setString(2, to);
ps.setDouble(3, change);
ps.executeUpdate();
} catch (SQLException e) {
throw new RuntimeException("Error occurred logging addition", e);
}
@ -48,6 +50,20 @@ public class TransactionLoggerMySQL implements TransactionLogger {
}
public boolean testConnection() {
return dbConn.testConnection();
if (dbConn.testConnection()) {
createTables();
return true;
}
return false;
}
private void createTables() {
try (Connection conn = dbConn.openConnection()) {
PreparedStatement ps = conn.prepareStatement("CREATE TABLE IF NOT EXISTS `transaction_logs` (`source` VARCHAR(128), `destination` VARCHAR(128), `amount` DECIMAL(18, 2))");
ps.executeUpdate();
} catch (SQLException e) {
throw new RuntimeException("Failed to create transaction logger tables", e);
}
}
}

View File

@ -121,6 +121,11 @@ public class SaneEconomyConfiguration {
logger.info("Attempting to load transaction logger...");
if (rootConfig.getConfigurationSection("logger-database") == null) {
logger.severe("No transaction logger database defined, cannot possibly connect!");
return null;
}
DatabaseCredentials credentials = loadCredentials(rootConfig.getConfigurationSection("logger-database"));
TransactionLoggerMySQL transactionLoggerMySQL = new TransactionLoggerMySQL(credentials);