use of com.faforever.api.data.domain.Achievement 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.Achievement 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.Achievement in project faf-java-api by FAForever.
the class EventsServiceTest method mockAchievement.
private void mockAchievement(String achievementId, AchievementType type, Integer totalSteps) {
Achievement achievement = new Achievement();
achievement.setId(achievementId);
achievement.setType(type);
achievement.setTotalSteps(totalSteps);
when(achievementRepository.getOne(achievementId)).thenReturn(achievement);
}
Aggregations