Search in sources :

Example 11 with Error

use of com.faforever.api.error.Error in project faf-java-api by FAForever.

the class MapService method parseScenarioLua.

@SneakyThrows
private void parseScenarioLua(MapUploadData progressData) {
    try (Stream<Path> mapFilesStream = Files.list(progressData.getOriginalMapFolder())) {
        Path scenarioLuaPath = noCatch(() -> mapFilesStream).filter(myFile -> myFile.toString().endsWith("_scenario.lua")).findFirst().orElseThrow(() -> new ApiException(new Error(ErrorCode.MAP_SCENARIO_LUA_MISSING)));
        LuaValue root = noCatch(() -> LuaLoader.loadFile(scenarioLuaPath), IllegalStateException.class);
        progressData.setLuaRoot(root);
    }
}
Also used : Path(java.nio.file.Path) ProgrammingError(com.faforever.api.error.ProgrammingError) Error(com.faforever.api.error.Error) LuaValue(org.luaj.vm2.LuaValue) ApiException(com.faforever.api.error.ApiException) SneakyThrows(lombok.SneakyThrows)

Example 12 with Error

use of com.faforever.api.error.Error in project faf-java-api by FAForever.

the class ClanService method acceptPlayerInvitationToken.

@SneakyThrows
void acceptPlayerInvitationToken(String stringToken, Authentication authentication) {
    Jwt token = jwtService.decodeAndVerify(stringToken);
    InvitationResult invitation = objectMapper.readValue(token.getClaims(), InvitationResult.class);
    if (invitation.isExpired()) {
        throw new ApiException(new Error(ErrorCode.CLAN_ACCEPT_TOKEN_EXPIRE));
    }
    final Integer clanId = invitation.getClan().getId();
    Player player = playerService.getPlayer(authentication);
    Clan clan = clanRepository.findById(clanId).orElseThrow(() -> new ApiException(new Error(ErrorCode.CLAN_NOT_EXISTS, clanId)));
    Player newMember = playerRepository.findById(invitation.getNewMember().getId()).orElseThrow(() -> new ProgrammingError("ClanMember does not exist: " + invitation.getNewMember().getId()));
    if (player.getId() != newMember.getId()) {
        throw new ApiException(new Error(ErrorCode.CLAN_ACCEPT_WRONG_PLAYER));
    }
    if (newMember.getClan() != null) {
        throw new ApiException(new Error(ErrorCode.CLAN_ACCEPT_PLAYER_IN_A_CLAN));
    }
    ClanMembership membership = new ClanMembership();
    membership.setClan(clan);
    membership.setPlayer(newMember);
    clanMembershipRepository.save(membership);
}
Also used : Player(com.faforever.api.data.domain.Player) Jwt(org.springframework.security.jwt.Jwt) Clan(com.faforever.api.data.domain.Clan) Error(com.faforever.api.error.Error) ProgrammingError(com.faforever.api.error.ProgrammingError) ProgrammingError(com.faforever.api.error.ProgrammingError) ClanMembership(com.faforever.api.data.domain.ClanMembership) InvitationResult(com.faforever.api.clan.result.InvitationResult) ApiException(com.faforever.api.error.ApiException) SneakyThrows(lombok.SneakyThrows)

Example 13 with Error

use of com.faforever.api.error.Error in project faf-java-api by FAForever.

the class ClanService method generatePlayerInvitationToken.

@SneakyThrows
String generatePlayerInvitationToken(Player requester, int newMemberId, int clanId) {
    Clan clan = clanRepository.findById(clanId).orElseThrow(() -> new ApiException(new Error(ErrorCode.CLAN_NOT_EXISTS, clanId)));
    if (requester.getId() != clan.getLeader().getId()) {
        throw new ApiException(new Error(ErrorCode.CLAN_NOT_LEADER, clanId));
    }
    Player newMember = playerRepository.findById(newMemberId).orElseThrow(() -> new ApiException(new Error(ErrorCode.CLAN_GENERATE_LINK_PLAYER_NOT_FOUND, newMemberId)));
    long expire = Instant.now().plus(fafApiProperties.getClan().getInviteLinkExpireDurationMinutes(), ChronoUnit.MINUTES).toEpochMilli();
    InvitationResult result = new InvitationResult(expire, ClanResult.of(clan), PlayerResult.of(newMember));
    return jwtService.sign(result);
}
Also used : Player(com.faforever.api.data.domain.Player) Clan(com.faforever.api.data.domain.Clan) Error(com.faforever.api.error.Error) ProgrammingError(com.faforever.api.error.ProgrammingError) InvitationResult(com.faforever.api.clan.result.InvitationResult) ApiException(com.faforever.api.error.ApiException) SneakyThrows(lombok.SneakyThrows)

Example 14 with Error

use of com.faforever.api.error.Error in project faf-java-api by FAForever.

the class AvatarService method copyAvatarFile.

private void copyAvatarFile(InputStream imageDataInputStream, Path imageTargetPath, boolean overwrite) throws IOException {
    CopyOption[] copyOptions = overwrite ? new CopyOption[] { StandardCopyOption.REPLACE_EXISTING } : new CopyOption[0];
    if (!overwrite && Files.exists(imageTargetPath)) {
        throw new ApiException(new Error(ErrorCode.AVATAR_NAME_CONFLICT, imageTargetPath.getFileName().toString()));
    }
    Files.createDirectories(imageTargetPath.getParent(), FilePermissionUtil.directoryPermissionFileAttributes());
    Files.copy(imageDataInputStream, imageTargetPath, copyOptions);
    FilePermissionUtil.setDefaultFilePermission(imageTargetPath);
}
Also used : StandardCopyOption(java.nio.file.StandardCopyOption) CopyOption(java.nio.file.CopyOption) ProgrammingError(com.faforever.api.error.ProgrammingError) Error(com.faforever.api.error.Error) ApiException(com.faforever.api.error.ApiException) NotFoundApiException(com.faforever.api.error.NotFoundApiException)

Example 15 with Error

use of com.faforever.api.error.Error 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)

Aggregations

ApiException (com.faforever.api.error.ApiException)29 Error (com.faforever.api.error.Error)29 ProgrammingError (com.faforever.api.error.ProgrammingError)13 SneakyThrows (lombok.SneakyThrows)11 Path (java.nio.file.Path)6 ArrayList (java.util.ArrayList)6 Player (com.faforever.api.data.domain.Player)5 NotFoundApiException (com.faforever.api.error.NotFoundApiException)5 LuaValue (org.luaj.vm2.LuaValue)4 Transactional (org.springframework.transaction.annotation.Transactional)4 Clan (com.faforever.api.data.domain.Clan)3 User (com.faforever.api.data.domain.User)3 InvitationResult (com.faforever.api.clan.result.InvitationResult)2 Achievement (com.faforever.api.data.domain.Achievement)2 Avatar (com.faforever.api.data.domain.Avatar)2 ClanMembership (com.faforever.api.data.domain.ClanMembership)2 Map (com.faforever.api.data.domain.Map)2 PlayerAchievement (com.faforever.api.data.domain.PlayerAchievement)2 Vote (com.faforever.api.data.domain.Vote)2 VotingSubject (com.faforever.api.data.domain.VotingSubject)2