feat: 支持记录日志
This commit is contained in:
@@ -24,14 +24,25 @@ public final class CurrencyDataManager {
|
|||||||
try (Connection connection = CoreAPI.getInstance().getConnection()) {
|
try (Connection connection = CoreAPI.getInstance().getConnection()) {
|
||||||
try (Statement statement = connection.createStatement()) {
|
try (Statement statement = connection.createStatement()) {
|
||||||
statement.execute("CREATE TABLE IF NOT EXISTS hamster_currency_type(" +
|
statement.execute("CREATE TABLE IF NOT EXISTS hamster_currency_type(" +
|
||||||
"`id` VARCHAR(36) PRIMARY KEY," +
|
"id VARCHAR(36) PRIMARY KEY," +
|
||||||
"`data` MEDIUMTEXT NOT NULL" +
|
"data MEDIUMTEXT NOT NULL" +
|
||||||
") CHARSET utf8mb4;");
|
") CHARSET utf8mb4;");
|
||||||
statement.execute("CREATE TABLE IF NOT EXISTS hamster_currency_data(" +
|
statement.execute("CREATE TABLE IF NOT EXISTS hamster_currency_data(" +
|
||||||
"`uuid` CHAR(36) NOT NULL," +
|
"uuid CHAR(36) NOT NULL," +
|
||||||
"`currency` VARCHAR(36) NOT NULL," +
|
"currency VARCHAR(36) NOT NULL," +
|
||||||
"`amount` DOUBLE NOT NULL," +
|
"amount DOUBLE NOT NULL," +
|
||||||
"PRIMARY KEY `player_currency` (`uuid`, `currency`)" +
|
"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;");
|
") CHARSET utf8mb4;");
|
||||||
try (ResultSet set = statement.executeQuery("SELECT * FROM hamster_currency_type;")) {
|
try (ResultSet set = statement.executeQuery("SELECT * FROM hamster_currency_type;")) {
|
||||||
while (set.next()) {
|
while (set.next()) {
|
||||||
@@ -66,6 +77,11 @@ public final class CurrencyDataManager {
|
|||||||
|
|
||||||
public static double getPlayerCurrency(@NotNull UUID uuid, @NotNull CurrencyType type) throws SQLException {
|
public static double getPlayerCurrency(@NotNull UUID uuid, @NotNull CurrencyType type) throws SQLException {
|
||||||
try (Connection connection = CoreAPI.getInstance().getConnection()) {
|
try (Connection connection = CoreAPI.getInstance().getConnection()) {
|
||||||
|
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(
|
try (PreparedStatement statement = connection.prepareStatement(
|
||||||
"SELECT currency, amount FROM hamster_currency_data WHERE `uuid`=?;"
|
"SELECT currency, amount FROM hamster_currency_data WHERE `uuid`=?;"
|
||||||
)) {
|
)) {
|
||||||
@@ -78,7 +94,6 @@ public final class CurrencyDataManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public static void givePlayerCurrency(@NotNull UUID uuid, @NotNull CurrencyType type, double amount) throws SQLException {
|
public static void givePlayerCurrency(@NotNull UUID uuid, @NotNull CurrencyType type, double amount) throws SQLException {
|
||||||
String typeID = type.getId();
|
String typeID = type.getId();
|
||||||
@@ -92,6 +107,19 @@ public final class CurrencyDataManager {
|
|||||||
statement.setDouble(4, amount);
|
statement.setDouble(4, amount);
|
||||||
statement.executeUpdate();
|
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.setDouble(4, amount);
|
||||||
statement.executeUpdate();
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -22,8 +22,15 @@ public class CurrencyType {
|
|||||||
*/
|
*/
|
||||||
private double defaultAmount;
|
private double defaultAmount;
|
||||||
|
|
||||||
public CurrencyType(@NotNull String id, @NotNull String name) {
|
/**
|
||||||
|
* 是否启用日志
|
||||||
|
*/
|
||||||
|
private boolean enableLog;
|
||||||
|
|
||||||
|
public CurrencyType(@NotNull String id) {
|
||||||
this.id = id;
|
this.id = id;
|
||||||
this.name = name;
|
name = id;
|
||||||
|
defaultAmount = 0;
|
||||||
|
enableLog = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user