Skip to content

Commit

Permalink
GH-495 AutoMessage fix (#528)
Browse files Browse the repository at this point in the history
* Fixed showing messages

* Update eternalcore-core/src/main/java/com/eternalcode/core/database/wrapper/AutoMessageRepositoryOrmLite.java

Co-authored-by: Norbert Dejlich <[email protected]>

* Use Set instead of List

* fix

---------

Co-authored-by: Norbert Dejlich <[email protected]>
  • Loading branch information
P1otrulla and Rollczi authored Sep 14, 2023
1 parent e8c376a commit 3fba755
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ public Completable<Blank> unIgnoreAll(UUID by) {
}

@Override
public Completable<List<UUID>> findReceivers(List<UUID> onlineUniqueIds) {
return Completable.completed(Collections.emptyList());
public Completable<Set<UUID>> findReceivers(Set<UUID> onlineUniqueIds) {
return Completable.completed(Collections.emptySet());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@
import panda.std.reactive.Completable;

import java.sql.SQLException;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.UUID;
import java.util.stream.Collectors;

public class AutoMessageRepositoryOrmLite extends AbstractRepositoryOrmLite implements AutoMessageRepository {

Expand All @@ -21,7 +24,7 @@ private AutoMessageRepositoryOrmLite(DatabaseManager databaseManager, Scheduler
}

@Override
public Completable<List<UUID>> findReceivers(List<UUID> onlineUniqueIds) {
public Completable<Set<UUID>> findReceivers(Set<UUID> onlineUniqueIds) {
if (onlineUniqueIds.isEmpty()) {
return Completable.completed(onlineUniqueIds);
}
Expand All @@ -38,9 +41,21 @@ public Completable<List<UUID>> findReceivers(List<UUID> onlineUniqueIds) {
return where.query();
});

return wrapperList.thenApply(ignores -> ignores.stream()
.map(wrapper -> wrapper.uniqueId)
.toList());
return wrapperList.thenApply(ignores -> {
Set<UUID> ignoredIds = ignores.stream()
.map(wrapper -> wrapper.uniqueId)
.collect(Collectors.toSet());

Set<UUID> onlineUniqueIdsWithoutIngores = new HashSet<>();

for (UUID onlineUniqueId : onlineUniqueIds) {
if (!ignoredIds.contains(onlineUniqueId)) {
onlineUniqueIdsWithoutIngores.add(onlineUniqueId);
}
}

return onlineUniqueIdsWithoutIngores;
});
}

@Override
Expand All @@ -52,12 +67,12 @@ public Completable<Boolean> isReceiving(UUID uniqueId) {
public Completable<Boolean> switchReceiving(UUID uniqueId) {
return this.selectSafe(AutoMessageIgnoreWrapper.class, uniqueId).thenCompose(optional -> {
if (optional.isEmpty()) {
return this.save(AutoMessageIgnoreWrapper.class, new AutoMessageIgnoreWrapper(uniqueId)).thenApply(result -> true);
return this.save(AutoMessageIgnoreWrapper.class, new AutoMessageIgnoreWrapper(uniqueId)).thenApply(result -> false);
}

AutoMessageIgnoreWrapper wrapper = optional.get();

return this.delete(AutoMessageIgnoreWrapper.class, wrapper).thenApply(state -> false);
return this.delete(AutoMessageIgnoreWrapper.class, wrapper).thenApply(state -> true);
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

import panda.std.reactive.Completable;

import java.util.List;
import java.util.Set;
import java.util.UUID;

public interface AutoMessageRepository {

Completable<List<UUID>> findReceivers(List<UUID> onlineUniqueIds);
Completable<Set<UUID>> findReceivers(Set<UUID> onlineUniqueIds);

// for nearby feafure like placeholders etc
Completable<Boolean> isReceiving(UUID uniqueId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@

import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

@FeatureDocs(name = "AutoMessage", description = "Automatically sends messages to players at a given time interval.")
public class AutoMessageService {
Expand Down Expand Up @@ -42,9 +44,9 @@ public Completable<Boolean> switchReceiving(UUID uniqueId) {
}

public void broadcastNextMessage() {
List<UUID> onlineUniqueIds = this.server.getOnlinePlayers().stream()
Set<UUID> onlineUniqueIds = this.server.getOnlinePlayers().stream()
.map(Entity::getUniqueId)
.toList();
.collect(Collectors.toSet());

this.repository.findReceivers(onlineUniqueIds).then(receivers -> {
if (receivers.isEmpty()) {
Expand Down

0 comments on commit 3fba755

Please sign in to comment.