use of com.faforever.api.data.domain.PlayerAchievement in project faf-java-api by FAForever.
the class AchievementService method unlock.
UpdatedAchievementResponse unlock(int playerId, String achievementId) {
Achievement achievement = achievementRepository.getOne(achievementId);
if (achievement.getType() != AchievementType.STANDARD) {
throw new ApiException(new Error(ACHIEVEMENT_NOT_STANDARD, achievementId));
}
PlayerAchievement playerAchievement = getOrCreatePlayerAchievement(playerId, achievement, AchievementState.REVEALED);
boolean newlyUnlocked = playerAchievement.getState() != AchievementState.UNLOCKED;
if (newlyUnlocked) {
playerAchievement.setState(AchievementState.UNLOCKED);
playerAchievementRepository.save(playerAchievement);
}
return new UpdatedAchievementResponse(achievementId, newlyUnlocked, playerAchievement.getState());
}
use of com.faforever.api.data.domain.PlayerAchievement in project faf-java-api by FAForever.
the class AchievementService method updateSteps.
private UpdatedAchievementResponse updateSteps(int playerId, String achievementId, int steps, BiFunction<Integer, Integer, Integer> stepsFunction) {
Achievement achievement = achievementRepository.getOne(achievementId);
if (achievement.getType() != AchievementType.INCREMENTAL) {
throw new ApiException(new Error(ACHIEVEMENT_NOT_INCREMENTAL, achievementId));
}
PlayerAchievement playerAchievement = getOrCreatePlayerAchievement(playerId, achievement, AchievementState.REVEALED);
int currentSteps = MoreObjects.firstNonNull(playerAchievement.getCurrentSteps(), 0);
int newCurrentSteps = stepsFunction.apply(currentSteps, steps);
boolean newlyUnlocked = false;
if (newCurrentSteps >= achievement.getTotalSteps()) {
playerAchievement.setState(AchievementState.UNLOCKED);
playerAchievement.setCurrentSteps(achievement.getTotalSteps());
newlyUnlocked = playerAchievement.getState() != AchievementState.UNLOCKED;
} else {
playerAchievement.setCurrentSteps(newCurrentSteps);
}
playerAchievementRepository.save(playerAchievement);
return new UpdatedAchievementResponse(achievementId, newlyUnlocked, playerAchievement.getState(), playerAchievement.getCurrentSteps());
}
use of com.faforever.api.data.domain.PlayerAchievement in project faf-java-api by FAForever.
the class EventsServiceTest method setStepsAtLeastExistingMoreSteps.
/**
* Tests whether an achievement's steps are updated if the existing steps are lower than the new steps.
*/
@Test
public void setStepsAtLeastExistingMoreSteps() throws Exception {
mockAchievement("111", AchievementType.INCREMENTAL, 10);
when(playerAchievementRepository.findOneByAchievementIdAndPlayerId("111", PLAYER_ID)).thenReturn(Optional.of(createPlayerAchievement(5, REVEALED)));
instance.setStepsAtLeast(PLAYER_ID, "111", 6);
PlayerAchievement playerAchievement = captureSaveEvent();
assertThat(playerAchievement.getCurrentSteps(), is(6));
assertThat(playerAchievement.getState(), is(REVEALED));
}
use of com.faforever.api.data.domain.PlayerAchievement in project faf-java-api by FAForever.
the class EventsServiceTest method createPlayerAchievement.
private static PlayerAchievement createPlayerAchievement(Integer currentSteps, AchievementState state) {
PlayerAchievement playerAchievement = new PlayerAchievement();
playerAchievement.setState(state);
playerAchievement.setCurrentSteps(currentSteps);
return playerAchievement;
}
Aggregations