Skip to content

Commit bd3a56b

Browse files
committed
sends UserHexadPlayerTypeSet event when the PlayerType is evaluated
1 parent 0657b68 commit bd3a56b

File tree

2 files changed

+51
-2
lines changed

2 files changed

+51
-2
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ repositories {
119119

120120
dependencies {
121121

122-
implementation 'de.unistuttgart.iste.meitrex:meitrex-common:1.4.11'
122+
implementation 'de.unistuttgart.iste.meitrex:meitrex-common:1.4.12pre2'
123123
implementation 'de.unistuttgart.iste.meitrex:content_service:1.6.0'
124124
implementation 'de.unistuttgart.iste.meitrex:course_service:1.1.0rc2'
125125
implementation 'de.unistuttgart.iste.meitrex:user_service:1.0.0rc1'

src/main/java/de/unistuttgart/iste/meitrex/gamification_service/service/PlayerHexadScoreService.java

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33
import java.util.*;
44

55
import de.unistuttgart.iste.meitrex.common.dapr.TopicPublisher;
6+
import de.unistuttgart.iste.meitrex.common.event.HexadPlayerType;
67
import de.unistuttgart.iste.meitrex.common.event.ServerSource;
8+
import de.unistuttgart.iste.meitrex.common.event.UserHexadPlayerTypeSetEvent;
79
import de.unistuttgart.iste.meitrex.gamification_service.persistence.entity.PlayerHexadScoreQuestionEntity;
810
import de.unistuttgart.iste.meitrex.gamification_service.persistence.entity.UserEntity;
911
import de.unistuttgart.iste.meitrex.gamification_service.persistence.repository.IPlayerHexadScoreQuestionRepository;
1012
import de.unistuttgart.iste.meitrex.gamification_service.service.internal.IUserCreator;
11-
import io.dapr.client.DaprClient;
1213
import org.springframework.stereotype.Service;
1314

1415
import de.unistuttgart.iste.meitrex.gamification_service.persistence.entity.PlayerHexadScoreEntity;
@@ -69,6 +70,8 @@ public PlayerHexadScore evaluate(UUID userId, PlayerAnswerInput input, String us
6970

7071
playerHexadScoreEntity.setUser(user);
7172

73+
sendUserHexadPlayerTypeSetEvent(userId, playerHexadScore);
74+
7275
return playerHexadScore;
7376
}
7477

@@ -173,5 +176,51 @@ public Boolean hasHexadScore(UUID userId) {
173176
return user.getPlayerHexadScore() != null;
174177
}
175178

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+
176225

177226
}

0 commit comments

Comments
 (0)