feat: 支持记录日志

This commit is contained in:
2024-08-08 06:24:34 +08:00
parent 15cd8cc692
commit d5f3a24eae
2 changed files with 65 additions and 17 deletions

View File

@@ -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();
}
}
}
}

View File

@@ -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;
}
}