|
3 | 3 | import lombok.RequiredArgsConstructor; |
4 | 4 | import net.dv8tion.jda.api.entities.Guild; |
5 | 5 | import net.dv8tion.jda.api.entities.Member; |
| 6 | +import net.dv8tion.jda.api.entities.Role; |
| 7 | +import net.javadiscord.javabot.data.config.BotConfig; |
6 | 8 | import net.javadiscord.javabot.systems.qotw.dao.QuestionPointsRepository; |
7 | 9 | import net.javadiscord.javabot.systems.qotw.model.QOTWAccount; |
8 | 10 | import net.javadiscord.javabot.util.ExceptionLogger; |
|
24 | 26 | @Service |
25 | 27 | public class QOTWPointsService { |
26 | 28 | private final QuestionPointsRepository pointsRepository; |
| 29 | + private final BotConfig botConfig; |
27 | 30 |
|
28 | 31 | /** |
29 | 32 | * Creates a new QOTW Account if none exists. |
@@ -121,15 +124,34 @@ public List<QOTWAccount> getTopAccounts(int amount, int page) { |
121 | 124 | /** |
122 | 125 | * Increments a single user's QOTW-Points. |
123 | 126 | * |
124 | | - * @param userId The discord Id of the user. |
| 127 | + * @param member The {@link Member} whose points shall be incremented |
125 | 128 | * @return The total points after the update. |
126 | 129 | */ |
127 | | - public long increment(long userId) { |
| 130 | + public long increment(Member member) { |
| 131 | + long userId = member.getIdLong(); |
128 | 132 | try { |
129 | 133 | LocalDate date=LocalDate.now(); |
130 | 134 | int points = pointsRepository.getPointsAtDate(userId, date)+1; |
131 | 135 | pointsRepository.setPointsAtDate(userId, date, points); |
132 | | - return pointsRepository.getByUserId(userId, getCurrentMonth()).map(QOTWAccount::getPoints).orElse(0L); |
| 136 | + Role qotwChampionRole = botConfig.get(member.getGuild()).getQotwConfig().getQOTWChampionRole(); |
| 137 | + LocalDate month = getCurrentMonth(); |
| 138 | + long newScore = pointsRepository.getByUserId(userId, month).map(QOTWAccount::getPoints).orElse(0L); |
| 139 | + if (qotwChampionRole != null) { |
| 140 | + pointsRepository.getTopAccounts(month, 0, 1) |
| 141 | + .stream() |
| 142 | + .findFirst() |
| 143 | + .ifPresent(best -> { |
| 144 | + if (newScore >= best.getPoints()) { |
| 145 | + member.getGuild().addRoleToMember(member, qotwChampionRole).queue(); |
| 146 | + for (Member m : member.getGuild().getMembersWithRoles(qotwChampionRole)) { |
| 147 | + if (getOrCreateAccount(m.getIdLong()).getPoints() < best.getPoints()) { |
| 148 | + m.getGuild().removeRoleFromMember(m, qotwChampionRole).queue(); |
| 149 | + } |
| 150 | + } |
| 151 | + } |
| 152 | + }); |
| 153 | + } |
| 154 | + return newScore; |
133 | 155 | } catch (DataAccessException e) { |
134 | 156 | ExceptionLogger.capture(e, getClass().getSimpleName()); |
135 | 157 | return 0; |
|
0 commit comments