use of com.faforever.api.error.ApiException in project faf-java-api by FAForever.
the class MapService method checkLua.
private void checkLua(MapUploadData progressData) {
List<Error> errors = new ArrayList<>();
LuaValue scenarioInfo = progressData.getLuaScenarioInfo();
if (scenarioInfo.get(ScenarioMapInfo.NAME) == LuaValue.NIL) {
errors.add(new Error(ErrorCode.MAP_NAME_MISSING));
}
if (scenarioInfo.get(ScenarioMapInfo.DESCRIPTION) == LuaValue.NIL) {
errors.add(new Error(ErrorCode.MAP_DESCRIPTION_MISSING));
}
if (invalidTeam(scenarioInfo)) {
errors.add(new Error(ErrorCode.MAP_FIRST_TEAM_FFA));
}
if (scenarioInfo.get(ScenarioMapInfo.TYPE) == LuaValue.NIL) {
errors.add(new Error(ErrorCode.MAP_TYPE_MISSING));
}
if (scenarioInfo.get(ScenarioMapInfo.SIZE) == LuaValue.NIL) {
errors.add(new Error(ErrorCode.MAP_SIZE_MISSING));
}
if (scenarioInfo.get(ScenarioMapInfo.MAP_VERSION) == LuaValue.NIL) {
errors.add(new Error(ErrorCode.MAP_VERSION_MISSING));
}
if (!errors.isEmpty()) {
throw new ApiException(errors.toArray(new Error[errors.size()]));
}
}
use of com.faforever.api.error.ApiException 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.ApiException 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.ApiException 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.ApiException 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);
}
Aggregations