fixed future handling in message cache, so methods return futures instead of requiring them as parameters

and adapted the code using this
fixed config loading still relying on double, where the values were changed to long
This commit is contained in:
Sheldan
2020-05-12 09:32:18 +02:00
parent 1634f5e9cc
commit 8d8f735672
7 changed files with 31 additions and 32 deletions

View File

@@ -140,9 +140,9 @@ public class AUserExperienceServiceBean implements AUserExperienceService {
public void handleExperienceGain(List<AServer> servers) {
servers.forEach(serverExp -> {
log.trace("Handling experience for server {}", serverExp.getId());
int minExp = configService.getDoubleValue("minExp", serverExp.getId()).intValue();
int maxExp = configService.getDoubleValue("maxExp", serverExp.getId()).intValue();
Integer multiplier = configService.getDoubleValue("expMultiplier", serverExp.getId()).intValue();
int minExp = configService.getLongValue("minExp", serverExp.getId()).intValue();
int maxExp = configService.getLongValue("maxExp", serverExp.getId()).intValue();
Double multiplier = configService.getDoubleValue("expMultiplier", serverExp.getId());
PrimitiveIterator.OfInt iterator = new Random().ints(serverExp.getUsers().size(), minExp, maxExp + 1).iterator();
List<AExperienceLevel> levels = experienceLevelManagementService.getLevelConfig();
levels.sort(Comparator.comparing(AExperienceLevel::getExperienceNeeded));
@@ -150,7 +150,7 @@ public class AUserExperienceServiceBean implements AUserExperienceService {
roles.sort(Comparator.comparing(role -> role.getLevel().getLevel()));
serverExp.getUsers().forEach(userInAServer -> {
Integer gainedExperience = iterator.next();
gainedExperience *= multiplier;
gainedExperience = (int) Math.floor(gainedExperience * multiplier);
log.trace("Handling {}. The user gains {}", userInAServer.getUserReference().getId(), gainedExperience);
AUserExperience aUserExperience = userExperienceManagementService.incrementExpForUser(userInAServer, gainedExperience.longValue(), 1L);
updateUserlevel(aUserExperience, levels);

View File

@@ -111,8 +111,8 @@ public class WarnServiceBean implements WarnService {
@Override
@Transactional
public void decayWarningsForServer(AServer server) {
Double days = configService.getDoubleValue("decayDays", server.getId());
Instant cutOffDay = Instant.now().minus(days.longValue(), ChronoUnit.DAYS);
Long days = configService.getLongValue("decayDays", server.getId());
Instant cutOffDay = Instant.now().minus(days, ChronoUnit.DAYS);
List<Warning> warningsToDecay = warnManagementService.getActiveWarningsInServerOlderThan(server, cutOffDay);
decayWarnings(warningsToDecay);
logDecayedWarnings(server, warningsToDecay);

View File

@@ -77,7 +77,7 @@ public class StarboardListener implements ReactedAddedListener, ReactedRemovedLi
if(reaction != null) {
AUserInAServer author = userInServerManagementService.loadUser(message.getServerId(), message.getAuthorId());
List<AUserInAServer> userExceptAuthor = getUsersExcept(reaction.getUserInServersIds(), author);
Double starMinimum = getFromConfig("starLvl1", message.getServerId());
Long starMinimum = getFromConfig("starLvl1", message.getServerId());
if (userExceptAuthor.size() >= starMinimum) {
log.info("Post reached starboard minimum. Message {} in channel {} in server {} will be starred/updated.",
message.getMessageId(), message.getChannelId(), message.getServerId());
@@ -133,8 +133,8 @@ public class StarboardListener implements ReactedAddedListener, ReactedRemovedLi
}
}
private Double getFromConfig(String key, Long guildId) {
return configManagementService.loadConfig(guildId, key).getDoubleValue();
private Long getFromConfig(String key, Long guildId) {
return configManagementService.loadConfig(guildId, key).getLongValue();
}
private List<AUserInAServer> getUsersExcept(List<Long> users, AUserInAServer author) {

View File

@@ -208,7 +208,7 @@ public class StarboardServiceBean implements StarboardService {
private String getAppropriateEmote(Long serverId, Integer starCount) {
for(int i = starboardConfig.getLvl().size(); i > 0; i--) {
Double starMinimum = configService.getDoubleValue("starLvl" + i, serverId);
Long starMinimum = configService.getLongValue("starLvl" + i, serverId);
if(starCount >= starMinimum) {
return emoteService.getUsableEmoteOrDefault(serverId, "star" + i);
}

View File

@@ -57,9 +57,7 @@ public class ReactionUpdatedListener extends ListenerAdapter {
}
CompletableFuture<CachedMessage> asyncMessageFromCache = messageCache.getMessageFromCache(event.getGuild().getIdLong(), event.getChannel().getIdLong(), event.getMessageIdLong());
asyncMessageFromCache.thenAccept(cachedMessage -> {
CompletableFuture<CachedReaction> future = new CompletableFuture<>();
messageCache.getCachedReactionFromReaction(future, event.getReaction());
future.thenAccept(reaction -> {
messageCache.getCachedReactionFromReaction(event.getReaction()).thenAccept(reaction -> {
self.callAddedListeners(event, cachedMessage, reaction);
messageCache.putMessageInCache(cachedMessage);
}).exceptionally(throwable -> {
@@ -123,9 +121,7 @@ public class ReactionUpdatedListener extends ListenerAdapter {
}
CompletableFuture<CachedMessage> asyncMessageFromCache = messageCache.getMessageFromCache(event.getGuild().getIdLong(), event.getChannel().getIdLong(), event.getMessageIdLong());
asyncMessageFromCache.thenAccept(cachedMessage -> {
CompletableFuture<CachedReaction> future = new CompletableFuture<>();
messageCache.getCachedReactionFromReaction(future, event.getReaction());
future.thenAccept(reaction ->
messageCache.getCachedReactionFromReaction(event.getReaction()).thenAccept(reaction ->
self.callRemoveListeners(event, cachedMessage, reaction)
) .exceptionally(throwable -> {
log.error("Failed to retrieve cached reaction for message {} ", event.getMessageIdLong(), throwable);

View File

@@ -53,9 +53,7 @@ public class MessageCacheBean implements MessageCache {
@CachePut(key = "#message.id")
public CompletableFuture<CachedMessage> putMessageInCache(Message message) {
log.info("Adding message {} to cache", message.getId());
CompletableFuture<CachedMessage> future = new CompletableFuture<>();
self.buildCachedMessageFromMessage(future, message);
return future;
return self.buildCachedMessageFromMessage(message);
}
@@ -78,20 +76,21 @@ public class MessageCacheBean implements MessageCache {
public CompletableFuture<CachedMessage> getMessageFromCache(Long guildId, Long textChannelId, Long messageId) {
log.info("Retrieving message with parameters");
CompletableFuture<CachedMessage> cachedMessageCompletableFuture = new CompletableFuture<>();
self.loadMessage(cachedMessageCompletableFuture, guildId, textChannelId, messageId);
return cachedMessageCompletableFuture;
return self.loadMessage(guildId, textChannelId, messageId);
}
@Override
public void loadMessage(CompletableFuture<CachedMessage> future, Long guildId, Long textChannelId, Long messageId) {
public CompletableFuture<CachedMessage> loadMessage(Long guildId, Long textChannelId, Long messageId) {
CompletableFuture<CachedMessage> future = new CompletableFuture<>();
Optional<Guild> guildOptional = botService.getGuildById(guildId);
if(guildOptional.isPresent()) {
Optional<TextChannel> textChannelByIdOptional = botService.getTextChannelFromServer(guildOptional.get(), textChannelId);
if(textChannelByIdOptional.isPresent()) {
TextChannel textChannel = textChannelByIdOptional.get();
textChannel.retrieveMessageById(messageId).queue(message ->
buildCachedMessageFromMessage(future, message)
{
buildCachedMessageFromMessage(message).thenAccept(future::complete);
}
);
} else {
log.error("Not able to load message {} in channel {} in guild {}. Text channel not found.", messageId, textChannelId, guildId);
@@ -102,10 +101,13 @@ public class MessageCacheBean implements MessageCache {
future.completeExceptionally(new GuildException(String.format("Not able to load message %s. Guild %s not found.", messageId, guildId)));
}
return future;
}
@Override
public void buildCachedMessageFromMessage(CompletableFuture<CachedMessage> future, Message message) {
public CompletableFuture<CachedMessage> buildCachedMessageFromMessage(Message message) {
CompletableFuture<CachedMessage> future = new CompletableFuture<>();
List<String> attachmentUrls = new ArrayList<>();
message.getAttachments().forEach(attachment ->
attachmentUrls.add(attachment.getProxyUrl())
@@ -117,9 +119,7 @@ public class MessageCacheBean implements MessageCache {
List<CompletableFuture<CachedReaction>> futures = new ArrayList<>();
message.getReactions().forEach(messageReaction -> {
CompletableFuture<CachedReaction> future1 = new CompletableFuture<>();
self.getCachedReactionFromReaction(future1, messageReaction);
futures.add(future1);
futures.add(self.getCachedReactionFromReaction(messageReaction));
});
CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).thenAccept(aVoid ->
@@ -138,6 +138,7 @@ public class MessageCacheBean implements MessageCache {
log.error("Failed to load reactions for message {}. ", message.getId(), throwable);
return null;
});
return future;
}
private List<CachedReaction> getFutures(List<CompletableFuture<CachedReaction>> futures) {
@@ -154,7 +155,8 @@ public class MessageCacheBean implements MessageCache {
}
@Override
public void getCachedReactionFromReaction(CompletableFuture<CachedReaction> future, MessageReaction reaction) {
public CompletableFuture<CachedReaction> getCachedReactionFromReaction(MessageReaction reaction) {
CompletableFuture<CachedReaction> future = new CompletableFuture<>();
ReactionPaginationAction users = reaction.retrieveUsers().cache(false);
CachedReaction.CachedReactionBuilder builder = CachedReaction.builder();
@@ -169,6 +171,7 @@ public class MessageCacheBean implements MessageCache {
});
builder.userInServersIds(ausers);
builder.emote(emoteService.buildAEmoteFromReaction(reaction.getReactionEmote()));
return future;
}
@Transactional

View File

@@ -13,7 +13,7 @@ public interface MessageCache {
CompletableFuture<CachedMessage> getMessageFromCache(Long guildId, Long textChannelId, Long messageId);
CompletableFuture<CachedMessage> getMessageFromCache(Message message);
CompletableFuture<CachedMessage> putMessageInCache(CachedMessage message);
void loadMessage(CompletableFuture<CachedMessage> future, Long guildId, Long textChannelId, Long messageId);
void getCachedReactionFromReaction(CompletableFuture<CachedReaction> future, MessageReaction reaction);
void buildCachedMessageFromMessage(CompletableFuture<CachedMessage> future, Message message);
CompletableFuture<CachedMessage> loadMessage(Long guildId, Long textChannelId, Long messageId);
CompletableFuture<CachedReaction> getCachedReactionFromReaction(MessageReaction reaction);
CompletableFuture<CachedMessage> buildCachedMessageFromMessage(Message message);
}