Search in sources :

Example 1 with Achievement

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());
}
Also used : Error(com.faforever.api.error.Error) PlayerAchievement(com.faforever.api.data.domain.PlayerAchievement) PlayerAchievement(com.faforever.api.data.domain.PlayerAchievement) Achievement(com.faforever.api.data.domain.Achievement) ApiException(com.faforever.api.error.ApiException)

Example 2 with Achievement

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());
}
Also used : Error(com.faforever.api.error.Error) PlayerAchievement(com.faforever.api.data.domain.PlayerAchievement) PlayerAchievement(com.faforever.api.data.domain.PlayerAchievement) Achievement(com.faforever.api.data.domain.Achievement) ApiException(com.faforever.api.error.ApiException)

Example 3 with Achievement

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);
}
Also used : Achievement(com.faforever.api.data.domain.Achievement) PlayerAchievement(com.faforever.api.data.domain.PlayerAchievement)

Aggregations

Achievement (com.faforever.api.data.domain.Achievement)3 PlayerAchievement (com.faforever.api.data.domain.PlayerAchievement)3 ApiException (com.faforever.api.error.ApiException)2 Error (com.faforever.api.error.Error)2