used implemented version of AuctionStatistic

Took 25 minutes
This commit is contained in:
Kiran Hart 2024-08-07 13:34:57 -04:00
parent e0ef86ac0f
commit f792b82a59
No known key found for this signature in database
GPG Key ID: 5F36C7BC79D3EBC3
9 changed files with 50 additions and 98 deletions

View File

@ -83,7 +83,6 @@ public class AuctionHouse extends TweetyPlugin {
private DatabaseConnector databaseConnector;
private DataManager dataManager;
private final CurrencyManager currencyManager = new CurrencyManager();
private final CommandManager commandManager = new CommandManager(this);
private final GuiManager guiManager = new GuiManager(this);
@ -99,8 +98,6 @@ public class AuctionHouse extends TweetyPlugin {
private final PaymentsManager paymentsManager = new PaymentsManager();
// the default vault economy
private Economy economy = null;
@ -120,6 +117,7 @@ public class AuctionHouse extends TweetyPlugin {
@Override
public void onPluginLoad() {
}
@Override
@ -145,7 +143,6 @@ public class AuctionHouse extends TweetyPlugin {
Translations.init();
ca.tweetzy.auctionhouse.settings.v3.Settings.init();
// Setup the database if enabled
this.databaseConnector = Settings.DATABASE_USE.getBoolean() ? new MySQLConnector(
this,

View File

@ -23,6 +23,7 @@ import lombok.Getter;
import lombok.NonNull;
@AllArgsConstructor
@Getter
public enum AuctionHousePermission {
COMMAND_SELL(cmd("sell"), "Allows the user to use /ah sell"),
@ -33,12 +34,12 @@ public enum AuctionHousePermission {
COMMAND_ADMIN(cmd("admin"), "Allows the user to use /ah admin"),
COMMAND_RELOAD(cmd("reload"), "Allows the user to use /ah reload"),
UNLIMITED_LISTINGS(wild("auctionhouse.maxallowedlistings"), "Allows the user to have unlimited listings");
UNLIMITED_LISTINGS(wild("auctionhouse.maxallowedlistings"), "Allows the user to have unlimited listings"),
;
@Getter
private final String permission;
@Getter
private final String description;
private static String cmd(@NonNull final String value) {

View File

@ -1,58 +0,0 @@
/*
* Auction House
* Copyright 2018-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.auction;
import ca.tweetzy.auctionhouse.AuctionHouse;
import ca.tweetzy.auctionhouse.api.sync.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 implements Storeable<AuctionStatistic> {
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<AuctionStatistic> stored) {
final AuctionHouse instance = AuctionHouse.getInstance();
instance.getDataManager().insertStatistic(this, (error, statistic) -> {
if (error != null) return;
if (statistic != null) {
instance.getAuctionStatisticManager().addStatistic(statistic);
if (stored != null)
stored.accept(statistic);
}
});
}
}

View File

@ -22,22 +22,20 @@ import ca.tweetzy.auctionhouse.AuctionHouse;
import ca.tweetzy.auctionhouse.api.AuctionAPI;
import ca.tweetzy.auctionhouse.api.ban.Ban;
import ca.tweetzy.auctionhouse.api.ban.BanType;
import ca.tweetzy.auctionhouse.api.statistic.Statistic;
import ca.tweetzy.auctionhouse.auction.*;
import ca.tweetzy.auctionhouse.auction.enums.*;
import ca.tweetzy.auctionhouse.impl.AuctionBan;
import ca.tweetzy.auctionhouse.impl.AuctionStatistic;
import ca.tweetzy.auctionhouse.settings.Settings;
import ca.tweetzy.auctionhouse.transaction.Transaction;
import ca.tweetzy.auctionhouse.transaction.TransactionViewFilter;
import ca.tweetzy.flight.comp.enums.CompMaterial;
import ca.tweetzy.flight.database.*;
import ca.tweetzy.flight.nbtapi.NBT;
import ca.tweetzy.flight.nbtapi.NbtApiException;
import ca.tweetzy.flight.utils.Common;
import ca.tweetzy.flight.utils.QuickItem;
import lombok.NonNull;
import org.bukkit.Bukkit;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.Plugin;
import org.jetbrains.annotations.NotNull;
@ -647,7 +645,7 @@ public class DataManager extends DataManagerAbstract {
}));
}
public void insertStatistic(AuctionStatistic statistic, Callback<AuctionStatistic> callback) {
public void insertStatistic(Statistic statistic, Callback<Statistic> callback) {
this.runAsync(() -> this.databaseConnector.connect(connection -> {
try (PreparedStatement statement = connection.prepareStatement("INSERT INTO " + this.getTablePrefix() + "statistic (uuid, stat_owner, stat_type, value, time) VALUES (?, ?, ?, ?, ?)")) {
@ -655,10 +653,10 @@ public class DataManager extends DataManagerAbstract {
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.setString(2, statistic.getOwner().toString());
statement.setString(3, statistic.getType().name());
statement.setDouble(4, statistic.getValue());
statement.setLong(5, statistic.getTime());
statement.setLong(5, statistic.getTimeCreated());
statement.executeUpdate();
if (callback != null) {
@ -674,8 +672,8 @@ public class DataManager extends DataManagerAbstract {
}));
}
public void getStatistics(Callback<List<AuctionStatistic>> callback) {
List<AuctionStatistic> stats = new ArrayList<>();
public void getStatistics(Callback<List<Statistic>> callback) {
List<Statistic> stats = new ArrayList<>();
this.runAsync(() -> this.databaseConnector.connect(connection -> {
try (PreparedStatement statement = connection.prepareStatement("SELECT * FROM " + this.getTablePrefix() + "statistic")) {
ResultSet resultSet = statement.executeQuery();
@ -886,11 +884,11 @@ public class DataManager extends DataManagerAbstract {
);
}
private AuctionStatistic extractAuctionStatistic(ResultSet resultSet) throws SQLException {
private Statistic 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")),
UUID.fromString(resultSet.getString("stat_owner")),
resultSet.getDouble("value"),
resultSet.getLong("time")
);

View File

@ -86,8 +86,8 @@ public class GUIPaymentCollection extends AuctionPagedGUI<AuctionPayment> {
}
auctionPayment.pay(click.player);
AuctionHouse.getInstance().getDataManager().deletePayments(Collections.singleton(auctionPayment.getId()));
AuctionHouse.getInstance().getPaymentsManager().remove(auctionPayment.getId());
AuctionHouse.getDataManager().deletePayments(Collections.singleton(auctionPayment.getId()));
AuctionHouse.getPaymentsManager().remove(auctionPayment.getId());
click.manager.showGUI(click.player, new GUIPaymentCollection(this.auctionPlayer, this.lastClicked));
}
@ -113,8 +113,8 @@ public class GUIPaymentCollection extends AuctionPagedGUI<AuctionPayment> {
auctionPayment.pay(e.player);
}
AuctionHouse.getInstance().getDataManager().deletePayments(this.items.stream().map(AuctionPayment::getId).collect(Collectors.toList()));
this.items.forEach(payment -> AuctionHouse.getInstance().getPaymentsManager().remove(payment.getId()));
AuctionHouse.getDataManager().deletePayments(this.items.stream().map(AuctionPayment::getId).collect(Collectors.toList()));
this.items.forEach(payment -> AuctionHouse.getPaymentsManager().remove(payment.getId()));
e.manager.showGUI(e.player, new GUIPaymentCollection(this.auctionPlayer, this.lastClicked));
});
}

View File

@ -18,6 +18,7 @@
package ca.tweetzy.auctionhouse.impl;
import ca.tweetzy.auctionhouse.AuctionHouse;
import ca.tweetzy.auctionhouse.api.statistic.Statistic;
import ca.tweetzy.auctionhouse.auction.enums.AuctionStatisticType;
import lombok.AllArgsConstructor;
@ -35,6 +36,10 @@ public final class AuctionStatistic implements Statistic {
private final double value;
private final long createdAt;
public AuctionStatistic(UUID statOwner, AuctionStatisticType type, double value) {
this(UUID.randomUUID(), type, statOwner, value, System.currentTimeMillis());
}
@Override
public @NonNull UUID getId() {
return this.id;
@ -67,6 +72,15 @@ public final class AuctionStatistic implements Statistic {
@Override
public void store(Consumer<Statistic> stored) {
AuctionHouse.getDataManager().insertStatistic(this, (error, statistic) -> {
if (error != null) return;
if (statistic != null) {
AuctionHouse.getAuctionStatisticManager().addStatistic(statistic);
if (stored != null)
stored.accept(statistic);
}
});
}
}

View File

@ -19,7 +19,6 @@
package ca.tweetzy.auctionhouse.listeners;
import ca.tweetzy.auctionhouse.AuctionHouse;
import ca.tweetzy.auctionhouse.auction.AuctionStatistic;
import ca.tweetzy.auctionhouse.auction.AuctionedItem;
import ca.tweetzy.auctionhouse.auction.enums.AuctionSaleType;
import ca.tweetzy.auctionhouse.auction.enums.AuctionStatisticType;
@ -27,6 +26,7 @@ import ca.tweetzy.auctionhouse.events.AuctionAdminEvent;
import ca.tweetzy.auctionhouse.events.AuctionBidEvent;
import ca.tweetzy.auctionhouse.events.AuctionEndEvent;
import ca.tweetzy.auctionhouse.events.AuctionStartEvent;
import ca.tweetzy.auctionhouse.impl.AuctionStatistic;
import ca.tweetzy.auctionhouse.model.discord.DiscordMessageCreator;
import ca.tweetzy.auctionhouse.settings.Settings;
import ca.tweetzy.auctionhouse.transaction.Transaction;

View File

@ -19,7 +19,7 @@
package ca.tweetzy.auctionhouse.managers;
import ca.tweetzy.auctionhouse.AuctionHouse;
import ca.tweetzy.auctionhouse.auction.AuctionStatistic;
import ca.tweetzy.auctionhouse.api.statistic.Statistic;
import ca.tweetzy.auctionhouse.auction.enums.AuctionStatisticType;
import java.time.Instant;
@ -30,7 +30,7 @@ import java.util.stream.Collectors;
public final class AuctionStatisticManager {
private final List<AuctionStatistic> statistics = Collections.synchronizedList(new ArrayList<>());
private final List<Statistic> statistics = Collections.synchronizedList(new ArrayList<>());
// 'cheat' way of doing this leaderboard thing, I will fix this eventually...
private final Map<UUID, Double> createdAuctionCount = new ConcurrentHashMap<>();
@ -41,15 +41,15 @@ public final class AuctionStatisticManager {
private final Map<UUID, Double> moneyEarnedCount = new ConcurrentHashMap<>();
public void addStatistic(AuctionStatistic statistic) {
public void addStatistic(Statistic statistic) {
synchronized (this.statistics) {
if (this.statistics.contains(statistic)) return;
this.statistics.add(statistic);
final UUID owner = statistic.getStatOwner();
final UUID owner = statistic.getOwner();
final double value = statistic.getValue();
switch (statistic.getStatisticType()) {
switch (statistic.getType()) {
case CREATED_AUCTION:
this.createdAuctionCount.put(owner, this.createdAuctionCount.getOrDefault(owner, 0D) + value);
break;
@ -72,13 +72,13 @@ public final class AuctionStatisticManager {
}
}
public void addStatistics(List<AuctionStatistic> statisticsList) {
public void addStatistics(List<Statistic> statisticsList) {
synchronized (this.statistics) {
statisticsList.forEach(this::addStatistic);
}
}
public List<AuctionStatistic> getStatistics() {
public List<Statistic> getStatistics() {
synchronized (this.statistics) {
return this.statistics;
}
@ -103,39 +103,39 @@ public final class AuctionStatisticManager {
return new HashMap<>();
}
public List<AuctionStatistic> getStatistics(AuctionStatisticType statisticType) {
public List<Statistic> getStatistics(AuctionStatisticType statisticType) {
synchronized (this.statistics) {
return this.statistics.stream().filter(stat -> stat.getStatisticType() == statisticType).collect(Collectors.toList());
return this.statistics.stream().filter(stat -> stat.getType() == statisticType).collect(Collectors.toList());
}
}
public double getStatistic(AuctionStatisticType statisticType) {
synchronized (this.statistics) {
return this.statistics.stream().filter(stat -> stat.getStatisticType() == statisticType).mapToDouble(AuctionStatistic::getValue).sum();
return this.statistics.stream().filter(stat -> stat.getType() == statisticType).mapToDouble(Statistic::getValue).sum();
}
}
public double getStatistic(AuctionStatisticType statisticType, ChronoUnit timeUnit, int timeAmount) {
synchronized (this.statistics) {
final List<AuctionStatistic> globalStats = this.statistics.stream().filter(stat -> stat.getStatisticType() == statisticType).collect(Collectors.toList());
final List<Statistic> globalStats = this.statistics.stream().filter(stat -> stat.getType() == statisticType).collect(Collectors.toList());
final long time = Instant.now().minus(timeAmount, timeUnit).toEpochMilli();
return globalStats.stream().filter(stat -> stat.getTime() >= time).mapToDouble(AuctionStatistic::getValue).sum();
return globalStats.stream().filter(stat -> stat.getTimeCreated() >= time).mapToDouble(Statistic::getValue).sum();
}
}
public double getStatisticByPlayer(UUID playerUuid, AuctionStatisticType statisticType) {
synchronized (this.statistics) {
return this.statistics.stream().filter(stat -> stat.getStatOwner().equals(playerUuid) && stat.getStatisticType() == statisticType).mapToDouble(AuctionStatistic::getValue).sum();
return this.statistics.stream().filter(stat -> stat.getOwner().equals(playerUuid) && stat.getType() == statisticType).mapToDouble(Statistic::getValue).sum();
}
}
public double getStatisticByPlayer(UUID playerUuid, AuctionStatisticType statisticType, ChronoUnit timeUnit, int timeAmount) {
synchronized (this.statistics) {
final List<AuctionStatistic> playerStats = this.statistics.stream().filter(stat -> stat.getStatOwner().equals(playerUuid) && stat.getStatisticType() == statisticType).collect(Collectors.toList());
final List<Statistic> playerStats = this.statistics.stream().filter(stat -> stat.getOwner().equals(playerUuid) && stat.getType() == statisticType).collect(Collectors.toList());
final long time = Instant.now().minus(timeAmount, timeUnit).toEpochMilli();
return playerStats.stream().filter(stat -> stat.getTime() >= time).mapToDouble(AuctionStatistic::getValue).sum();
return playerStats.stream().filter(stat -> stat.getTimeCreated() >= time).mapToDouble(Statistic::getValue).sum();
}
}

View File

@ -191,6 +191,6 @@ public final class Settings extends SettingTemp {
public static ConfigEntry CONFIRM_CANCEL_LISTING = create("settings.confirmation.cancel listing", false, "Ask for confirmation before canceling a listing?");
public static void init() {
AuctionHouse.getInstance().getMigrationCoreConfig().init();
AuctionHouse.getMigrationCoreConfig().init();
}
}