new Auction Statistic system properly stores data now after bin/auction sold/creation and money earned/spent events

Took 4 minutes
This commit is contained in:
Kiran Hart 2022-09-08 13:40:39 -04:00
parent 39177a1fbd
commit fffea9121e
No known key found for this signature in database
GPG Key ID: 5F36C7BC79D3EBC3
7 changed files with 141 additions and 43 deletions

View File

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

View File

@ -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 <https://www.gnu.org/licenses/>.
*/
package ca.tweetzy.auctionhouse.api.interfaces;
import java.util.function.Consumer;
public interface Storeable<T> {
void store(Consumer<T> stored);
}

View File

@ -392,9 +392,9 @@ public class DataManager extends DataManagerAbstract {
public void insertStatistic(AuctionStatistic statistic, Callback<AuctionStatistic> 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();

View File

@ -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, " +

View File

@ -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()) {

View File

@ -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 <https://www.gnu.org/licenses/>.
*/
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<AuctionStatistic> 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<AuctionStatistic> statisticsList) {
synchronized (this.statistics) {
statisticsList.forEach(this::addStatistic);
}
}
public List<AuctionStatistic> getStatistics() {
synchronized (this.statistics) {
return this.statistics;
}
}
public void loadStatistics() {
AuctionHouse.getInstance().getDataManager().getStatistics((error, all) -> {
if (error == null)
all.forEach(this::addStatistic);
});
}
}

View File

@ -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<String> enchants = new ArrayList<>();
// enchants.add("Sharpness V");
// enchants.add("Sharpness I");
// enchants.add("Fire Aspect IV");
// System.out.println(StringUtils.join(enchants, ";=;"));
final List<TestObject> 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) {