diff --git a/src/main/java/ca/tweetzy/auctionhouse/auction/AuctionStatistic.java b/src/main/java/ca/tweetzy/auctionhouse/auction/AuctionStatistic.java index 2fd39a8..50ae17e 100644 --- a/src/main/java/ca/tweetzy/auctionhouse/auction/AuctionStatistic.java +++ b/src/main/java/ca/tweetzy/auctionhouse/auction/AuctionStatistic.java @@ -18,19 +18,40 @@ package ca.tweetzy.auctionhouse.auction; +import ca.tweetzy.auctionhouse.AuctionHouse; +import ca.tweetzy.auctionhouse.api.interfaces.Storeable; import ca.tweetzy.auctionhouse.auction.enums.AuctionStatisticType; import lombok.AllArgsConstructor; import lombok.Getter; import java.util.UUID; +import java.util.function.Consumer; @AllArgsConstructor @Getter -public final class AuctionStatistic { +public final class AuctionStatistic implements Storeable { + private final UUID id; private final UUID statOwner; private final AuctionStatisticType statisticType; private final double value; private final long time; + public AuctionStatistic(UUID statOwner, AuctionStatisticType type, double value) { + this(UUID.randomUUID(), statOwner, type, value, System.currentTimeMillis()); + } + + @Override + public void store(Consumer stored) { + AuctionHouse.getInstance().getDataManager().insertStatistic(this, (error, statistic) -> { + if (error != null) return; + + if (statistic != null) { + AuctionHouse.getInstance().getAuctionStatisticManager().addStatistic(statistic); + + if (stored != null) + stored.accept(statistic); + } + }); + } } diff --git a/src/main/java/ca/tweetzy/auctionhouse/database/DataManager.java b/src/main/java/ca/tweetzy/auctionhouse/database/DataManager.java index 8048d33..92c6716 100644 --- a/src/main/java/ca/tweetzy/auctionhouse/database/DataManager.java +++ b/src/main/java/ca/tweetzy/auctionhouse/database/DataManager.java @@ -3,10 +3,7 @@ package ca.tweetzy.auctionhouse.database; import ca.tweetzy.auctionhouse.AuctionHouse; import ca.tweetzy.auctionhouse.api.AuctionAPI; import ca.tweetzy.auctionhouse.auction.*; -import ca.tweetzy.auctionhouse.auction.enums.AdminAction; -import ca.tweetzy.auctionhouse.auction.enums.AuctionItemCategory; -import ca.tweetzy.auctionhouse.auction.enums.AuctionSaleType; -import ca.tweetzy.auctionhouse.auction.enums.AuctionSortType; +import ca.tweetzy.auctionhouse.auction.enums.*; import ca.tweetzy.auctionhouse.settings.Settings; import ca.tweetzy.auctionhouse.transaction.Transaction; import ca.tweetzy.core.database.DataManagerAbstract; @@ -393,6 +390,49 @@ public class DataManager extends DataManagerAbstract { })); } + public void insertStatistic(AuctionStatistic statistic, Callback callback) { + this.thread.execute(() -> this.databaseConnector.connect(connection -> { + try (PreparedStatement statement = connection.prepareStatement("INSERT INTO " + this.getTablePrefix() + "statistic (id, stat_owner, stat_type, value, time) VALUES (?, ?, ?, ?, ?)")) { + + PreparedStatement fetch = connection.prepareStatement("SELECT * FROM " + this.getTablePrefix() + "statistic WHERE id = ?"); + + fetch.setString(1, statistic.getId().toString()); + statement.setString(1, statistic.getId().toString()); + statement.setString(2, statistic.getStatOwner().toString()); + statement.setString(3, statistic.getStatisticType().name()); + statement.setDouble(4, statistic.getValue()); + statement.setLong(5, statistic.getTime()); + + if (callback != null) { + ResultSet res = fetch.executeQuery(); + res.next(); + callback.accept(null, extractAuctionStatistic(res)); + } + + } catch (Exception e) { + e.printStackTrace(); + resolveCallback(callback, e); + } + })); + } + + public void getStatistics(Callback> callback) { + List stats = new ArrayList<>(); + this.thread.execute(() -> this.databaseConnector.connect(connection -> { + try (PreparedStatement statement = connection.prepareStatement("SELECT * FROM " + this.getTablePrefix() + "statistic")) { + ResultSet resultSet = statement.executeQuery(); + while (resultSet.next()) { + stats.add(extractAuctionStatistic(resultSet)); + } + + callback.accept(null, stats); + } catch (Exception e) { + resolveCallback(callback, e); + } + })); + } + + @Deprecated public void getStats(Callback>> callback) { Map> stats = new HashMap<>(); this.async(() -> this.databaseConnector.connect(connection -> { @@ -415,6 +455,7 @@ public class DataManager extends DataManagerAbstract { })); } + @Deprecated public void updateStats(Map> stats, UpdateCallback callback) { this.databaseConnector.connect(connection -> { connection.setAutoCommit(false); @@ -588,6 +629,16 @@ public class DataManager extends DataManagerAbstract { })); } + private AuctionStatistic extractAuctionStatistic(ResultSet resultSet) throws SQLException { + return new AuctionStatistic( + UUID.fromString(resultSet.getString("uuid")), + UUID.fromString(resultSet.getString("stat_owner")), + AuctionStatisticType.valueOf(resultSet.getString("stat_type")), + resultSet.getDouble("value"), + resultSet.getLong("time") + ); + } + private AuctionPlayer extractAuctionPlayer(ResultSet resultSet) throws SQLException { return new AuctionPlayer( UUID.fromString(resultSet.getString("uuid")), diff --git a/src/main/java/ca/tweetzy/auctionhouse/database/migrations/_16_StatisticVersionTwoMigration.java b/src/main/java/ca/tweetzy/auctionhouse/database/migrations/_16_StatisticVersionTwoMigration.java index 62812c7..3a6220c 100644 --- a/src/main/java/ca/tweetzy/auctionhouse/database/migrations/_16_StatisticVersionTwoMigration.java +++ b/src/main/java/ca/tweetzy/auctionhouse/database/migrations/_16_StatisticVersionTwoMigration.java @@ -1,8 +1,6 @@ package ca.tweetzy.auctionhouse.database.migrations; -import ca.tweetzy.auctionhouse.AuctionHouse; import ca.tweetzy.core.database.DataMigration; -import ca.tweetzy.core.database.MySQLConnector; import java.sql.Connection; import java.sql.SQLException; @@ -23,11 +21,9 @@ public class _16_StatisticVersionTwoMigration extends DataMigration { @Override public void migrate(Connection connection, String tablePrefix) throws SQLException { - String autoIncrement = AuctionHouse.getInstance().getDatabaseConnector() instanceof MySQLConnector ? " AUTO_INCREMENT" : ""; - try (Statement statement = connection.createStatement()) { statement.execute("CREATE TABLE " + tablePrefix + "statistic (" + - "id INTEGER PRIMARY KEY" + autoIncrement + ", " + + "id VARCHAR(36) PRIMARY KEY, " + "stat_owner VARCHAR(36) NOT NULL, " + "stat_type VARCHAR(20) NOT NULL, " + "value DOUBLE NOT NULL, " +