Skip to content

Commit

Permalink
display collect event rewards and fix disabling bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaktushose committed Aug 7, 2024
1 parent e71f99c commit eb92728
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ public CollectEventListener(Database database, EmbedCache embedCache) {
@Override
public void onMessageReceived(@NotNull MessageReceivedEvent event) {
log.debug("Received message event");
if (!eventService.isCollectEventActive()) {
return;
}
var author = event.getAuthor();

if (author.isBot()) {
Expand All @@ -62,6 +65,10 @@ public void onMessageReceived(@NotNull MessageReceivedEvent event) {
return;
}

if (!rankService.isValidMessage(event.getMessage())) {
return;
}

if (!permissionsService.hasUserPermissions(event.getMember())) {
return;
}
Expand Down Expand Up @@ -108,10 +115,6 @@ private void onCollectPointChange(int oldPoints, int newPoints, Member member, G
}

private void onCollectLootDrop(MessageReceivedEvent event) {
if (!eventService.isCollectEventActive()) {
return;
}

var points = eventService.getCollectLootDrop(event.getMessage());

if (points < 1) {
Expand All @@ -128,7 +131,9 @@ public void onMessageReactionAdd(@NotNull MessageReactionAddEvent event) {
if (event.getUser().isBot()) {
return;
}

if (!eventService.isCollectEventActive()) {
return;
}
var messageId = event.getMessageIdLong();
if (!collectLootDrops.contains(messageId)) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,22 @@ public int getCollectPoints(UserSnowflake user) {
}
}

public String getEventName() {
try (Connection connection = dataSource.getConnection()) {
var statement = connection.prepareStatement("""
SELECT collect_event_name
FROM event_settings
WHERE guild_id = ?
""");
statement.setLong(1, guild.getIdLong());
var result = statement.executeQuery();
result.next();
return result.getString("collect_event_name");
} catch (SQLException e) {
throw new RuntimeException(e);
}
}

public record CollectCurrency(String name, String emoji) {
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,19 @@ private void sendReply(UserInfo userInfo, User user, CommandEvent event) {
if (database.getCollectEventService().isCollectEventActive()) {
var currency = database.getCollectEventService().getCollectCurrency();
var points = database.getCollectEventService().getCollectPoints(user);
embed.addField(currency.name(), String.format("%s %d", currency.emoji(), points), false);
embed.addField(String.format("%s:", currency.name()), String.format("%s %d", currency.emoji(), points), false);

StringBuilder rewardText = new StringBuilder();
database.getCollectEventService().getCollectRewards().forEach(reward -> {
if (userInfo.collectPoints() >= reward.threshold()) {
rewardText.append(reward.name()).append("\n");
}
});
if (rewardText.isEmpty()) {
rewardText.append("❌ noch keine Belohnung");
}
var name = database.getCollectEventService().getEventName();
embed.addField(String.format("%s Belohnungen: ", name), rewardText.toString(), false);
}

var transactions = database.getItemService().getTransactions(user);
Expand All @@ -61,10 +73,8 @@ private void sendReply(UserInfo userInfo, User user, CommandEvent event) {
.append(")\n");
}
});
embed.addField("Items", items.toString(), false);
embed.addField("Items:", items.toString(), false);
}

event.reply(embed);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
import java.util.Optional;

public record UserInfo(long id, int currentXp, RankInfo currentRank, Optional<RankInfo> nextRank, int messageCount,
int xpGain,
int karma, int lastKarma, long lastValidMessage) {
int xpGain, int karma, int lastKarma, long lastValidMessage, int collectPoints) {

@SuppressWarnings("OptionalUsedAsFieldOrParameterType")
public static UserInfo fromResultSet(ResultSet result, RankInfo currentRank, Optional<RankInfo> nextRank) throws SQLException {
Expand All @@ -23,7 +22,8 @@ public static UserInfo fromResultSet(ResultSet result, RankInfo currentRank, Opt
result.getInt("xp") - result.getInt("start_xp"),
result.getInt("karma_points"),
result.getInt("last_karma"),
result.getLong("last_valid_message")
result.getLong("last_valid_message"),
result.getInt("collect_points")
);
}

Expand Down

0 comments on commit eb92728

Please sign in to comment.