From fffea9121e6fa60403e44eb6bff606c77d7781a4 Mon Sep 17 00:00:00 2001 From: Kiran Hart Date: Thu, 8 Sep 2022 13:40:39 -0400 Subject: [PATCH] new Auction Statistic system properly stores data now after bin/auction sold/creation and money earned/spent events Took 4 minutes --- .../ca/tweetzy/auctionhouse/AuctionHouse.java | 6 ++ .../api/interfaces/Storeable.java | 26 +++++++++ .../auctionhouse/database/DataManager.java | 5 +- .../_16_StatisticVersionTwoMigration.java | 2 +- .../listeners/AuctionListeners.java | 50 ++++++++++------ .../managers/AuctionStatisticManager.java | 57 +++++++++++++++++++ src/main/test/Test.java | 38 ++++++------- 7 files changed, 141 insertions(+), 43 deletions(-) create mode 100644 src/main/java/ca/tweetzy/auctionhouse/api/interfaces/Storeable.java create mode 100644 src/main/java/ca/tweetzy/auctionhouse/managers/AuctionStatisticManager.java diff --git a/src/main/java/ca/tweetzy/auctionhouse/AuctionHouse.java b/src/main/java/ca/tweetzy/auctionhouse/AuctionHouse.java index 38fcc77..57d4135 100644 --- a/src/main/java/ca/tweetzy/auctionhouse/AuctionHouse.java +++ b/src/main/java/ca/tweetzy/auctionhouse/AuctionHouse.java @@ -89,6 +89,9 @@ public class AuctionHouse extends TweetyPlugin { @Getter private AuctionStatManager auctionStatManager; + @Getter + private AuctionStatisticManager auctionStatisticManager; + @Getter private MinItemPriceManager minItemPriceManager; @@ -213,6 +216,9 @@ public class AuctionHouse extends TweetyPlugin { this.auctionStatManager = new AuctionStatManager(); this.auctionStatManager.loadStats(); + this.auctionStatisticManager = new AuctionStatisticManager(); + this.auctionStatisticManager.loadStatistics(); + // auction players this.auctionPlayerManager = new AuctionPlayerManager(); this.auctionPlayerManager.loadPlayers(); diff --git a/src/main/java/ca/tweetzy/auctionhouse/api/interfaces/Storeable.java b/src/main/java/ca/tweetzy/auctionhouse/api/interfaces/Storeable.java new file mode 100644 index 0000000..f99e2f1 --- /dev/null +++ b/src/main/java/ca/tweetzy/auctionhouse/api/interfaces/Storeable.java @@ -0,0 +1,26 @@ +/* + * Auction House + * Copyright 2022 Kiran Hart + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package ca.tweetzy.auctionhouse.api.interfaces; + +import java.util.function.Consumer; + +public interface Storeable { + + void store(Consumer stored); +} diff --git a/src/main/java/ca/tweetzy/auctionhouse/database/DataManager.java b/src/main/java/ca/tweetzy/auctionhouse/database/DataManager.java index 92c6716..44a8182 100644 --- a/src/main/java/ca/tweetzy/auctionhouse/database/DataManager.java +++ b/src/main/java/ca/tweetzy/auctionhouse/database/DataManager.java @@ -392,9 +392,9 @@ 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 (?, ?, ?, ?, ?)")) { + try (PreparedStatement statement = connection.prepareStatement("INSERT INTO " + this.getTablePrefix() + "statistic (uuid, stat_owner, stat_type, value, time) VALUES (?, ?, ?, ?, ?)")) { - PreparedStatement fetch = connection.prepareStatement("SELECT * FROM " + this.getTablePrefix() + "statistic WHERE id = ?"); + PreparedStatement fetch = connection.prepareStatement("SELECT * FROM " + this.getTablePrefix() + "statistic WHERE uuid = ?"); fetch.setString(1, statistic.getId().toString()); statement.setString(1, statistic.getId().toString()); @@ -402,6 +402,7 @@ public class DataManager extends DataManagerAbstract { statement.setString(3, statistic.getStatisticType().name()); statement.setDouble(4, statistic.getValue()); statement.setLong(5, statistic.getTime()); + statement.executeUpdate(); if (callback != null) { ResultSet res = fetch.executeQuery(); 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 3a6220c..b1e9ba4 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 @@ -23,7 +23,7 @@ public class _16_StatisticVersionTwoMigration extends DataMigration { try (Statement statement = connection.createStatement()) { statement.execute("CREATE TABLE " + tablePrefix + "statistic (" + - "id VARCHAR(36) PRIMARY KEY, " + + "uuid VARCHAR(36) PRIMARY KEY, " + "stat_owner VARCHAR(36) NOT NULL, " + "stat_type VARCHAR(20) NOT NULL, " + "value DOUBLE NOT NULL, " + diff --git a/src/main/java/ca/tweetzy/auctionhouse/listeners/AuctionListeners.java b/src/main/java/ca/tweetzy/auctionhouse/listeners/AuctionListeners.java index 55c4b0a..ad81a79 100644 --- a/src/main/java/ca/tweetzy/auctionhouse/listeners/AuctionListeners.java +++ b/src/main/java/ca/tweetzy/auctionhouse/listeners/AuctionListeners.java @@ -6,8 +6,9 @@ import ca.tweetzy.auctionhouse.api.events.AuctionAdminEvent; import ca.tweetzy.auctionhouse.api.events.AuctionBidEvent; import ca.tweetzy.auctionhouse.api.events.AuctionEndEvent; import ca.tweetzy.auctionhouse.api.events.AuctionStartEvent; -import ca.tweetzy.auctionhouse.auction.AuctionStat; +import ca.tweetzy.auctionhouse.auction.AuctionStatistic; import ca.tweetzy.auctionhouse.auction.enums.AuctionSaleType; +import ca.tweetzy.auctionhouse.auction.enums.AuctionStatisticType; import ca.tweetzy.auctionhouse.settings.Settings; import ca.tweetzy.auctionhouse.transaction.Transaction; import org.bukkit.event.EventHandler; @@ -25,9 +26,14 @@ public class AuctionListeners implements Listener { @EventHandler public void onAuctionStart(AuctionStartEvent e) { - AuctionHouse.getInstance().getAuctionStatManager().insertOrUpdate(e.getSeller(), new AuctionStat<>( - 1, 0, 0, 0D, 0D - )); + + // legacy stat stuff +// AuctionHouse.getInstance().getAuctionStatManager().insertOrUpdate(e.getSeller(), new AuctionStat<>( +// 1, 0, 0, 0D, 0D +// )); + + // new stat system + new AuctionStatistic(e.getSeller().getUniqueId(), e.getAuctionItem().isBidItem() ? AuctionStatisticType.CREATED_AUCTION : AuctionStatisticType.CREATED_BIN, 1).store(null); if (Settings.DISCORD_ENABLED.getBoolean() && Settings.DISCORD_ALERT_ON_AUCTION_START.getBoolean()) { AuctionHouse.newChain().async(() -> { @@ -48,21 +54,29 @@ public class AuctionListeners implements Listener { @EventHandler public void onAuctionEnd(AuctionEndEvent e) { - AuctionHouse.getInstance().getAuctionStatManager().insertOrUpdate(e.getOriginalOwner(), new AuctionStat<>( - 0, - 1, - 0, - e.getSaleType() == AuctionSaleType.USED_BIDDING_SYSTEM ? e.getAuctionItem().getCurrentPrice() : e.getAuctionItem().getBasePrice(), - 0D - )); - AuctionHouse.getInstance().getAuctionStatManager().insertOrUpdate(e.getBuyer(), new AuctionStat<>( - 0, - 0, - 0, - 0D, - e.getSaleType() == AuctionSaleType.USED_BIDDING_SYSTEM ? e.getAuctionItem().getCurrentPrice() : e.getAuctionItem().getBasePrice() - )); + // legacy stat system +// AuctionHouse.getInstance().getAuctionStatManager().insertOrUpdate(e.getOriginalOwner(), new AuctionStat<>( +// 0, +// 1, +// 0, +// e.getSaleType() == AuctionSaleType.USED_BIDDING_SYSTEM ? e.getAuctionItem().getCurrentPrice() : e.getAuctionItem().getBasePrice(), +// 0D +// )); + + // legacy stat system +// AuctionHouse.getInstance().getAuctionStatManager().insertOrUpdate(e.getBuyer(), new AuctionStat<>( +// 0, +// 0, +// 0, +// 0D, +// e.getSaleType() == AuctionSaleType.USED_BIDDING_SYSTEM ? e.getAuctionItem().getCurrentPrice() : e.getAuctionItem().getBasePrice() +// )); + + // new stat system + new AuctionStatistic(e.getOriginalOwner().getUniqueId(), e.getAuctionItem().isBidItem() ? AuctionStatisticType.SOLD_AUCTION : AuctionStatisticType.SOLD_BIN, 1).store(null); + new AuctionStatistic(e.getOriginalOwner().getUniqueId(), AuctionStatisticType.MONEY_EARNED, e.getSaleType() == AuctionSaleType.USED_BIDDING_SYSTEM ? e.getAuctionItem().getCurrentPrice() : e.getAuctionItem().getBasePrice()).store(null); + new AuctionStatistic(e.getBuyer().getUniqueId(), AuctionStatisticType.MONEY_SPENT, e.getSaleType() == AuctionSaleType.USED_BIDDING_SYSTEM ? e.getAuctionItem().getCurrentPrice() : e.getAuctionItem().getBasePrice()).store(null); AuctionHouse.newChain().async(() -> { if (Settings.RECORD_TRANSACTIONS.getBoolean()) { diff --git a/src/main/java/ca/tweetzy/auctionhouse/managers/AuctionStatisticManager.java b/src/main/java/ca/tweetzy/auctionhouse/managers/AuctionStatisticManager.java new file mode 100644 index 0000000..ed504b6 --- /dev/null +++ b/src/main/java/ca/tweetzy/auctionhouse/managers/AuctionStatisticManager.java @@ -0,0 +1,57 @@ +/* + * Auction House + * Copyright 2022 Kiran Hart + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package ca.tweetzy.auctionhouse.managers; + +import ca.tweetzy.auctionhouse.AuctionHouse; +import ca.tweetzy.auctionhouse.auction.AuctionStatistic; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +public final class AuctionStatisticManager { + + private final List statistics = Collections.synchronizedList(new ArrayList<>()); + + public void addStatistic(AuctionStatistic statistic) { + synchronized (this.statistics) { + if (this.statistics.contains(statistic)) return; + this.statistics.add(statistic); + } + } + + public void addStatistics(List statisticsList) { + synchronized (this.statistics) { + statisticsList.forEach(this::addStatistic); + } + } + + public List getStatistics() { + synchronized (this.statistics) { + return this.statistics; + } + } + + public void loadStatistics() { + AuctionHouse.getInstance().getDataManager().getStatistics((error, all) -> { + if (error == null) + all.forEach(this::addStatistic); + }); + } +} diff --git a/src/main/test/Test.java b/src/main/test/Test.java index f3f0f5b..a89a171 100644 --- a/src/main/test/Test.java +++ b/src/main/test/Test.java @@ -1,4 +1,8 @@ -import ca.tweetzy.auctionhouse.api.AuctionAPI; +import lombok.AllArgsConstructor; +import lombok.Getter; + +import java.util.ArrayList; +import java.util.List; /** * The current file has been created by Kiran Hart @@ -9,33 +13,23 @@ import ca.tweetzy.auctionhouse.api.AuctionAPI; public class Test { public static void main(String[] args) { -// System.out.println(TimeUtils.makeReadable(getSecondsFromString("2y")*1000)); -// List enchants = new ArrayList<>(); -// enchants.add("Sharpness V"); -// enchants.add("Sharpness I"); -// enchants.add("Fire Aspect IV"); -// System.out.println(StringUtils.join(enchants, ";=;")); + final List list = new ArrayList<>(); -// final String uIDPartOne = "%%__US"; -// final String uIDPartTwo = "ER__%%"; -// -// final String UID = "%%__USER_%%"; + list.add(new TestObject(1, "Kiran")); + list.add(new TestObject(2, "Carl")); + list.add(new TestObject(3, "Orlando")); -// System.out.println(UID.contains(uIDPartOne) && UID.contains(uIDPartTwo)); + System.out.println(list.size()); + } -// System.out.println(AuctionAPI.toTicks("1 day")); + @AllArgsConstructor + @Getter + static class TestObject { -// String arguments = "3d"; -// -// System.out.println(getSecondsFromString(arguments)); + private final int id; + private final String name; -// long future = System.currentTimeMillis() + 1000L * 10; - -// System.out.println("16".compareTo(System.getProperty("java.version")) <= 0); - -// final double max = 99999999999999999999999999999.0D; - System.out.println(AuctionAPI.getInstance().getFriendlyNumber(0)); } public static long getSecondsFromString(String time) {