Skip to content

Commit 129fcd2

Browse files
committed
retrieve members in QOTW leaderboard
1 parent cbb7ca8 commit 129fcd2

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

src/main/java/net/discordjug/javabot/systems/qotw/QOTWPointsService.java

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,18 @@
77
import net.discordjug.javabot.util.Pair;
88
import net.dv8tion.jda.api.entities.Guild;
99
import net.dv8tion.jda.api.entities.Member;
10-
1110
import org.springframework.dao.DataAccessException;
1211
import org.springframework.stereotype.Service;
1312
import org.springframework.transaction.annotation.Transactional;
1413

1514
import java.time.LocalDate;
1615
import java.time.YearMonth;
16+
import java.util.Comparator;
1717
import java.util.List;
18+
import java.util.Map;
1819
import java.util.Optional;
20+
import java.util.function.Function;
21+
import java.util.stream.Collectors;
1922

2023
/**
2124
* Service class which is used to get and manipulate other {@link QOTWAccount}s.
@@ -110,10 +113,18 @@ public long getPoints(long userId) {
110113
public List<Pair<QOTWAccount, Member>> getTopMembers(int n, Guild guild) {
111114
try {
112115
List<QOTWAccount> accounts = pointsRepository.getTopAccounts(getCurrentMonth(),1,(int)Math.ceil(n*1.5));
113-
return accounts.stream()
114-
.map(s -> new Pair<>(s, guild.getMemberById(s.getUserId())))
115-
.filter(p->p.first().getPoints() > 0)
116-
.filter(p -> p.second() != null)
116+
Map<Long, QOTWAccount> accountPerId = accounts
117+
.stream()
118+
.collect(Collectors.toMap(
119+
QOTWAccount::getUserId,
120+
Function.identity()));
121+
122+
return guild.retrieveMembersByIds(accountPerId.keySet())
123+
.get()
124+
.stream()
125+
.map(m -> new Pair<>(accountPerId.get(m.getIdLong()), m))
126+
.sorted(Comparator.comparingLong(pair -> -pair.first().getPoints()))
127+
.filter(p -> p.first().getPoints() > 0)
117128
.limit(n)
118129
.toList();
119130
} catch (DataAccessException e) {

0 commit comments

Comments
 (0)