Skip to content

Commit

Permalink
Add checks to prevent duplicate when undo a group of auctions
Browse files Browse the repository at this point in the history
  • Loading branch information
EpiCanard committed Jan 27, 2024
1 parent eb29806 commit 946dde4
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,18 @@ private void undoEveryAuction(InventoryGUI i) {
);
final List<AuctionInfo> auctions = this.current.auctions.subList(0, pos.get());

if (GlobalMarketChest.plugin.auctionManager.undoGroupOfPlayerAuctions(i.getPlayer(), shop.getGroup(), Utils.mapList(auctions, act -> act.getId()))) {
for (AuctionInfo auction : auctions)
i.getPlayer().getInventory().addItem(auction.getRealItemStack());
PlayerUtils.sendMessageConfig(i.getPlayer(), "InfoMessages.UndoEveryAuction");
} else
throw new WarnException("CantUndoEveryAuction");
if (auctions.size() > 0) {
List<Integer> ids = GlobalMarketChest.plugin.auctionManager
.undoGroupOfPlayerAuctions(i.getPlayer(), shop.getGroup(), Utils.mapList(auctions, AuctionInfo::getId));

if (ids.size() > 0) {
for (AuctionInfo auction : auctions)
if (ids.contains(auction.getId()))
i.getPlayer().getInventory().addItem(auction.getRealItemStack());
PlayerUtils.sendMessageConfig(i.getPlayer(), "InfoMessages.UndoEveryAuction");
} else
throw new WarnException("CantUndoEveryAuction");
}

if (!ret)
throw new WarnException("NotEnoughSpace");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,17 +149,23 @@ public Boolean renewAuction(final int id, final Integer expirationDays) {
* @param group Shop group name target
* @param auctions List of auctions to undo
*/
public Boolean undoGroupOfPlayerAuctions(final Player player, final String group, final List<Integer> auctions) {
public List<Integer> undoGroupOfPlayerAuctions(final Player player, final String group, final List<Integer> auctions) {
final String playerUuid = PlayerUtils.getUUIDToString(player);
final QueryExecutor executor = QueryExecutor.of();

final UpdateBuilder builder = update()
.addCondition("id", auctions, ConditionType.IN)
.addCondition("playerStarter", playerUuid)
.addCondition("group", group)
.addValue("status", StatusAuction.ABANDONED.getValue())
.addValue("playerEnder", playerUuid)
.addValue("end", DatabaseUtils.getTimestamp().toString());
return QueryExecutor.of().execute(builder);
final List<Integer> finalAuctions = getEditableAuctions(executor, auctions);

if (!finalAuctions.isEmpty()) {
final UpdateBuilder builder = update()
.addCondition("id", finalAuctions, ConditionType.IN)
.addCondition("playerStarter", playerUuid)
.addCondition("group", group)
.addValue("status", StatusAuction.ABANDONED.getValue())
.addValue("playerEnder", playerUuid)
.addValue("end", DatabaseUtils.getTimestamp().toString());
executor.execute(builder);
}
return finalAuctions;
}

/**
Expand Down Expand Up @@ -550,6 +556,26 @@ private Boolean canEditAuction(final QueryExecutor executor, final int id) {
return end.get();
}

/**
* Request the database to know if the auctions specified are ended or not
*
* @param executor instance of QueryExecutor to us
* @param id list of ids to verify
*/
private List<Integer> getEditableAuctions(final QueryExecutor executor, final List<Integer> ids) {
final SelectBuilder select = select()
.addField("id")
.addCondition("id", ids, ConditionType.IN)
.addCondition("status", StatusAuction.IN_PROGRESS.getValue(), ConditionType.EQUAL);
List<Integer> finalIds = new ArrayList<>();

executor.execute(select, res -> {
while (res.next())
finalIds.add(res.getInt(1));
}, Exception::printStackTrace);
return finalIds;
}

/**
* Execute the query that listing auctions
*
Expand Down

0 comments on commit 946dde4

Please sign in to comment.