Search in sources :

Example 6 with PlayerAchievement

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());
}
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 7 with PlayerAchievement

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());
}
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 8 with PlayerAchievement

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

Example 9 with PlayerAchievement

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

Aggregations

PlayerAchievement (com.faforever.api.data.domain.PlayerAchievement)9 Test (org.junit.Test)6 Achievement (com.faforever.api.data.domain.Achievement)2 ApiException (com.faforever.api.error.ApiException)2 Error (com.faforever.api.error.Error)2