|
7 | 7 | import net.discordjug.javabot.util.Pair; |
8 | 8 | import net.dv8tion.jda.api.entities.Guild; |
9 | 9 | import net.dv8tion.jda.api.entities.Member; |
10 | | - |
11 | 10 | import org.springframework.dao.DataAccessException; |
12 | 11 | import org.springframework.stereotype.Service; |
13 | 12 | import org.springframework.transaction.annotation.Transactional; |
14 | 13 |
|
15 | 14 | import java.time.LocalDate; |
16 | 15 | import java.time.YearMonth; |
| 16 | +import java.util.Comparator; |
17 | 17 | import java.util.List; |
| 18 | +import java.util.Map; |
18 | 19 | import java.util.Optional; |
| 20 | +import java.util.function.Function; |
| 21 | +import java.util.stream.Collectors; |
19 | 22 |
|
20 | 23 | /** |
21 | 24 | * Service class which is used to get and manipulate other {@link QOTWAccount}s. |
@@ -110,10 +113,18 @@ public long getPoints(long userId) { |
110 | 113 | public List<Pair<QOTWAccount, Member>> getTopMembers(int n, Guild guild) { |
111 | 114 | try { |
112 | 115 | 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) |
117 | 128 | .limit(n) |
118 | 129 | .toList(); |
119 | 130 | } catch (DataAccessException e) { |
|
0 commit comments