|
3 | 3 | import java.util.*; |
4 | 4 |
|
5 | 5 | import de.unistuttgart.iste.meitrex.common.dapr.TopicPublisher; |
| 6 | +import de.unistuttgart.iste.meitrex.common.event.HexadPlayerType; |
6 | 7 | import de.unistuttgart.iste.meitrex.common.event.ServerSource; |
| 8 | +import de.unistuttgart.iste.meitrex.common.event.UserHexadPlayerTypeSetEvent; |
7 | 9 | import de.unistuttgart.iste.meitrex.gamification_service.persistence.entity.PlayerHexadScoreQuestionEntity; |
8 | 10 | import de.unistuttgart.iste.meitrex.gamification_service.persistence.entity.UserEntity; |
9 | 11 | import de.unistuttgart.iste.meitrex.gamification_service.persistence.repository.IPlayerHexadScoreQuestionRepository; |
10 | 12 | import de.unistuttgart.iste.meitrex.gamification_service.service.internal.IUserCreator; |
11 | | -import io.dapr.client.DaprClient; |
12 | 13 | import org.springframework.stereotype.Service; |
13 | 14 |
|
14 | 15 | import de.unistuttgart.iste.meitrex.gamification_service.persistence.entity.PlayerHexadScoreEntity; |
@@ -69,6 +70,8 @@ public PlayerHexadScore evaluate(UUID userId, PlayerAnswerInput input, String us |
69 | 70 |
|
70 | 71 | playerHexadScoreEntity.setUser(user); |
71 | 72 |
|
| 73 | + sendUserHexadPlayerTypeSetEvent(userId, playerHexadScore); |
| 74 | + |
72 | 75 | return playerHexadScore; |
73 | 76 | } |
74 | 77 |
|
@@ -173,5 +176,51 @@ public Boolean hasHexadScore(UUID userId) { |
173 | 176 | return user.getPlayerHexadScore() != null; |
174 | 177 | } |
175 | 178 |
|
| 179 | + /** |
| 180 | + * Publishes an event with the userId, PlayerHexadScore of the user and his primary player type |
| 181 | + * @param userId the ID of the user |
| 182 | + * @param playerHexadScore the updated player hexad score |
| 183 | + */ |
| 184 | + private void sendUserHexadPlayerTypeSetEvent(UUID userId, PlayerHexadScore playerHexadScore) { |
| 185 | + HexadPlayerType primaryPlayerType = null; |
| 186 | + Map<HexadPlayerType, Double> scoresMap = new HashMap<>(); |
| 187 | + |
| 188 | + double maxScore = Double.NEGATIVE_INFINITY; |
| 189 | + |
| 190 | + for (PlayerTypeScore score : playerHexadScore.getScores()) { |
| 191 | + HexadPlayerType hexadType = mapPlayerTypeToHexadPlayerType(score.getType()); |
| 192 | + scoresMap.put(hexadType, score.getValue()); |
| 193 | + |
| 194 | + if (score.getValue() > maxScore) { |
| 195 | + maxScore = score.getValue(); |
| 196 | + primaryPlayerType = hexadType; |
| 197 | + } |
| 198 | + } |
| 199 | + |
| 200 | + UserHexadPlayerTypeSetEvent event = UserHexadPlayerTypeSetEvent.builder() |
| 201 | + .userId(userId) |
| 202 | + .primaryPlayerType(primaryPlayerType) |
| 203 | + .playerTypePercentages(scoresMap) |
| 204 | + .build(); |
| 205 | + |
| 206 | + topicPublisher.notifyUserHexadPlayerTypeSet(event); |
| 207 | + } |
| 208 | + |
| 209 | + /** |
| 210 | + * Maps the GraphQL PlayerType enum to the common HexadPlayerType enum |
| 211 | + * @param playerType the GraphQL player type |
| 212 | + * @return the corresponding HexadPlayerType |
| 213 | + */ |
| 214 | + private HexadPlayerType mapPlayerTypeToHexadPlayerType(PlayerType playerType) { |
| 215 | + return switch (playerType) { |
| 216 | + case ACHIEVER -> HexadPlayerType.ACHIEVER; |
| 217 | + case PLAYER -> HexadPlayerType.PLAYER; |
| 218 | + case SOCIALISER -> HexadPlayerType.SOCIALISER; |
| 219 | + case FREE_SPIRIT -> HexadPlayerType.FREE_SPIRIT; |
| 220 | + case PHILANTHROPIST -> HexadPlayerType.PHILANTHROPIST; |
| 221 | + case DISRUPTOR -> HexadPlayerType.DISRUPTOR; |
| 222 | + }; |
| 223 | + } |
| 224 | + |
176 | 225 |
|
177 | 226 | } |
0 commit comments