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