diff --git a/src/main/java/cn/hamster3/mc/plugin/currency/core/CurrencyDataManager.java b/src/main/java/cn/hamster3/mc/plugin/currency/core/CurrencyDataManager.java index d61379f..9197d2c 100644 --- a/src/main/java/cn/hamster3/mc/plugin/currency/core/CurrencyDataManager.java +++ b/src/main/java/cn/hamster3/mc/plugin/currency/core/CurrencyDataManager.java @@ -24,14 +24,25 @@ public final class CurrencyDataManager { try (Connection connection = CoreAPI.getInstance().getConnection()) { try (Statement statement = connection.createStatement()) { statement.execute("CREATE TABLE IF NOT EXISTS hamster_currency_type(" + - "`id` VARCHAR(36) PRIMARY KEY," + - "`data` MEDIUMTEXT NOT NULL" + + "id VARCHAR(36) PRIMARY KEY," + + "data MEDIUMTEXT NOT NULL" + ") CHARSET utf8mb4;"); statement.execute("CREATE TABLE IF NOT EXISTS hamster_currency_data(" + - "`uuid` CHAR(36) NOT NULL," + - "`currency` VARCHAR(36) NOT NULL," + - "`amount` DOUBLE NOT NULL," + - "PRIMARY KEY `player_currency` (`uuid`, `currency`)" + + "uuid CHAR(36) NOT NULL," + + "currency VARCHAR(36) NOT NULL," + + "amount DOUBLE NOT NULL," + + "PRIMARY KEY `player_currency` (`uuid`, `currency`)," + + "INDEX idx_currency(currency)" + + ") CHARSET utf8mb4;"); + statement.execute("CREATE TABLE IF NOT EXISTS hamster_currency_logs(" + + "uuid CHAR(36) NOT NULL," + + "currency VARCHAR(36) NOT NULL," + + "action ENUM('MODIFY', 'SET')," + + "amount DOUBLE NOT NULL," + + "balance DOUBLE NOT NULL," + + "time DATETIME NOT NULL," + + "INDEX idx_uuid(uuid)," + + "INDEX idx_uuid_currency(uuid, currency)" + ") CHARSET utf8mb4;"); try (ResultSet set = statement.executeQuery("SELECT * FROM hamster_currency_type;")) { while (set.next()) { @@ -66,16 +77,20 @@ public final class CurrencyDataManager { public static double getPlayerCurrency(@NotNull UUID uuid, @NotNull CurrencyType type) throws SQLException { try (Connection connection = CoreAPI.getInstance().getConnection()) { - try (PreparedStatement statement = connection.prepareStatement( - "SELECT currency, amount FROM hamster_currency_data WHERE `uuid`=?;" - )) { - statement.setString(1, uuid.toString()); - try (ResultSet set = statement.executeQuery()) { - if (set.next()) { - return set.getDouble("amount"); - } - return type.getDefaultAmount(); + return getPlayerCurrency(connection, uuid, type); + } + } + + private static double getPlayerCurrency(@NotNull Connection connection, @NotNull UUID uuid, @NotNull CurrencyType type) throws SQLException { + try (PreparedStatement statement = connection.prepareStatement( + "SELECT currency, amount FROM hamster_currency_data WHERE `uuid`=?;" + )) { + statement.setString(1, uuid.toString()); + try (ResultSet set = statement.executeQuery()) { + if (set.next()) { + return set.getDouble("amount"); } + return type.getDefaultAmount(); } } } @@ -92,6 +107,19 @@ public final class CurrencyDataManager { statement.setDouble(4, amount); statement.executeUpdate(); } + if (type.isEnableLog()) { + double currency = getPlayerCurrency(connection, uuid, type); + try (PreparedStatement statement = connection.prepareStatement( + "INSERT INTO hamster_currency_logs VALUES(?, ?, ?, ?, ?, NOW());" + )) { + statement.setString(1, uuid.toString()); + statement.setString(2, type.getId()); + statement.setString(3, "MODIFY"); + statement.setDouble(4, amount); + statement.setDouble(5, currency); + statement.executeUpdate(); + } + } } } @@ -110,6 +138,19 @@ public final class CurrencyDataManager { statement.setDouble(4, amount); statement.executeUpdate(); } + if (type.isEnableLog()) { + double currency = getPlayerCurrency(connection, uuid, type); + try (PreparedStatement statement = connection.prepareStatement( + "INSERT INTO hamster_currency_logs VALUES(?, ?, ?, ?, ?, NOW());" + )) { + statement.setString(1, uuid.toString()); + statement.setString(2, type.getId()); + statement.setString(3, "SET"); + statement.setDouble(4, amount); + statement.setDouble(5, currency); + statement.executeUpdate(); + } + } } } diff --git a/src/main/java/cn/hamster3/mc/plugin/currency/data/CurrencyType.java b/src/main/java/cn/hamster3/mc/plugin/currency/data/CurrencyType.java index dbb038e..ce3ca0c 100644 --- a/src/main/java/cn/hamster3/mc/plugin/currency/data/CurrencyType.java +++ b/src/main/java/cn/hamster3/mc/plugin/currency/data/CurrencyType.java @@ -22,8 +22,15 @@ public class CurrencyType { */ private double defaultAmount; - public CurrencyType(@NotNull String id, @NotNull String name) { + /** + * 是否启用日志 + */ + private boolean enableLog; + + public CurrencyType(@NotNull String id) { this.id = id; - this.name = name; + name = id; + defaultAmount = 0; + enableLog = false; } }