Search in sources :

Example 6 with Error

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

the class VotingService method ableToVote.

private List<Error> ableToVote(Player player, int votingSubjectId) {
    VotingSubject subject = votingSubjectRepository.findById(votingSubjectId).orElseThrow(() -> new ApiException(new Error(ErrorCode.VOTING_SUBJECT_DOES_NOT_EXIST, votingSubjectId)));
    List<Error> errors = new ArrayList<>();
    Optional<Vote> byPlayerAndVotingSubject = voteRepository.findByPlayerAndVotingSubjectId(player, votingSubjectId);
    if (byPlayerAndVotingSubject.isPresent()) {
        errors.add(new Error(ErrorCode.VOTED_TWICE));
    }
    int gamesPlayed = gamePlayerStatsRepository.countByPlayerAndGameValidity(player, Validity.VALID);
    if (subject.getBeginOfVoteTime().isAfter(OffsetDateTime.now())) {
        errors.add(new Error(ErrorCode.VOTE_DID_NOT_START_YET, subject.getBeginOfVoteTime()));
    }
    if (subject.getEndOfVoteTime().isBefore(OffsetDateTime.now())) {
        errors.add(new Error(ErrorCode.VOTE_ALREADY_ENDED, subject.getEndOfVoteTime()));
    }
    if (gamesPlayed < subject.getMinGamesToVote()) {
        errors.add(new Error(ErrorCode.NOT_ENOUGH_GAMES, gamesPlayed, subject.getMinGamesToVote()));
    }
    return errors;
}
Also used : Vote(com.faforever.api.data.domain.Vote) ArrayList(java.util.ArrayList) Error(com.faforever.api.error.Error) VotingSubject(com.faforever.api.data.domain.VotingSubject) ApiException(com.faforever.api.error.ApiException)

Example 7 with Error

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

the class ModService method processUploadedMod.

@SneakyThrows
@Transactional
@CacheEvict(value = { Mod.TYPE_NAME, ModVersion.TYPE_NAME }, allEntries = true)
public void processUploadedMod(Path uploadedFile, Player uploader) {
    log.debug("Player '{}' uploaded a mod", uploader);
    ModReader modReader = new ModReader();
    com.faforever.commons.mod.Mod modInfo = modReader.readZip(uploadedFile);
    validateModInfo(modInfo);
    validateModStructure(uploadedFile);
    log.debug("Mod uploaded by user '{}' is valid: {}", uploader, modInfo);
    String displayName = modInfo.getName().trim();
    short version = (short) Integer.parseInt(modInfo.getVersion().toString());
    if (!canUploadMod(displayName, uploader)) {
        Mod mod = modRepository.findOneByDisplayName(displayName).orElseThrow(() -> new IllegalStateException("Mod could not be found"));
        throw new ApiException(new Error(ErrorCode.MOD_NOT_ORIGINAL_AUTHOR, mod.getAuthor(), displayName));
    }
    if (modExists(displayName, version)) {
        throw new ApiException(new Error(ErrorCode.MOD_VERSION_EXISTS, displayName, version));
    }
    String uuid = modInfo.getUid();
    if (modUidExists(uuid)) {
        throw new ApiException(new Error(ErrorCode.MOD_UID_EXISTS, uuid));
    }
    String zipFileName = generateZipFileName(displayName, version);
    Path targetPath = properties.getMod().getTargetDirectory().resolve(zipFileName);
    if (Files.exists(targetPath)) {
        throw new ApiException(new Error(ErrorCode.MOD_NAME_CONFLICT, zipFileName));
    }
    Optional<Path> thumbnailPath = extractThumbnail(uploadedFile, version, displayName, modInfo.getIcon());
    log.debug("Moving uploaded mod '{}' to: {}", modInfo.getName(), targetPath);
    Files.createDirectories(targetPath.getParent(), FilePermissionUtil.directoryPermissionFileAttributes());
    Files.move(uploadedFile, targetPath);
    FilePermissionUtil.setDefaultFilePermission(targetPath);
    try {
        store(modInfo, thumbnailPath, uploader, zipFileName);
    } catch (Exception exception) {
        try {
            Files.delete(targetPath);
        } catch (IOException ioException) {
            log.warn("Could not delete file " + targetPath, ioException);
        }
        throw exception;
    }
}
Also used : Path(java.nio.file.Path) Mod(com.faforever.api.data.domain.Mod) Error(com.faforever.api.error.Error) IOException(java.io.IOException) ApiException(com.faforever.api.error.ApiException) IOException(java.io.IOException) ModReader(com.faforever.commons.mod.ModReader) ApiException(com.faforever.api.error.ApiException) CacheEvict(org.springframework.cache.annotation.CacheEvict) SneakyThrows(lombok.SneakyThrows) Transactional(org.springframework.transaction.annotation.Transactional)

Example 8 with Error

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

the class MapService method updateMapEntities.

private void updateMapEntities(MapUploadData progressData) {
    LuaValue scenarioInfo = progressData.getLuaScenarioInfo();
    Map map = progressData.getMapEntity();
    if (map == null) {
        map = new Map();
    }
    map.setDisplayName(scenarioInfo.get(ScenarioMapInfo.NAME).toString()).setMapType(scenarioInfo.get(ScenarioMapInfo.TYPE).tojstring()).setBattleType(scenarioInfo.get(ScenarioMapInfo.CONFIGURATIONS).get(ScenarioMapInfo.CONFIGURATION_STANDARD).get(ScenarioMapInfo.CONFIGURATION_STANDARD_TEAMS).get(1).get(ScenarioMapInfo.CONFIGURATION_STANDARD_TEAMS_NAME).tojstring()).setAuthor(progressData.getAuthorEntity());
    LuaValue size = scenarioInfo.get(ScenarioMapInfo.SIZE);
    MapVersion version = new MapVersion().setDescription(scenarioInfo.get(ScenarioMapInfo.DESCRIPTION).tojstring().replaceAll("<LOC .*?>", "")).setWidth(size.get(1).toint()).setHeight(size.get(2).toint()).setHidden(false).setRanked(progressData.isRanked()).setMaxPlayers(scenarioInfo.get(ScenarioMapInfo.CONFIGURATIONS).get(ScenarioMapInfo.CONFIGURATION_STANDARD).get(ScenarioMapInfo.CONFIGURATION_STANDARD_TEAMS).get(1).get(ScenarioMapInfo.CONFIGURATION_STANDARD_TEAMS_ARMIES).length()).setVersion(scenarioInfo.get(ScenarioMapInfo.MAP_VERSION).toint());
    map.getVersions().add(version);
    version.setMap(map);
    progressData.setMapEntity(map);
    progressData.setMapVersionEntity(version);
    version.setFilename(STUPID_MAP_FOLDER_PREFIX + progressData.getFinalZipName());
    progressData.setFinalZipFile(this.fafApiProperties.getMap().getTargetDirectory().resolve(progressData.getFinalZipName()));
    if (Files.exists(progressData.getFinalZipFile())) {
        throw new ApiException(new Error(ErrorCode.MAP_NAME_CONFLICT, progressData.getFinalZipName()));
    }
    // this triggers validation
    mapRepository.save(map);
}
Also used : ProgrammingError(com.faforever.api.error.ProgrammingError) Error(com.faforever.api.error.Error) MapVersion(com.faforever.api.data.domain.MapVersion) LuaValue(org.luaj.vm2.LuaValue) Map(com.faforever.api.data.domain.Map) ApiException(com.faforever.api.error.ApiException)

Example 9 with Error

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

the class MapService method postProcessLuaFile.

private void postProcessLuaFile(MapUploadData progressData) {
    LuaValue scenarioInfo = progressData.getLuaScenarioInfo();
    Optional<Map> mapEntity = mapRepository.findOneByDisplayName(scenarioInfo.get(ScenarioMapInfo.NAME).toString());
    if (!mapEntity.isPresent()) {
        return;
    }
    Player author = mapEntity.get().getAuthor();
    if (author == null) {
        return;
    }
    if (author.getId() != progressData.getAuthorEntity().getId()) {
        throw new ApiException(new Error(ErrorCode.MAP_NOT_ORIGINAL_AUTHOR, mapEntity.get().getDisplayName()));
    }
    int newVersion = scenarioInfo.get(ScenarioMapInfo.MAP_VERSION).toint();
    if (mapEntity.get().getVersions().stream().anyMatch(mapVersion -> mapVersion.getVersion() == newVersion)) {
        throw new ApiException(new Error(ErrorCode.MAP_VERSION_EXISTS, mapEntity.get().getDisplayName(), newVersion));
    }
    progressData.setMapEntity(mapEntity.get());
}
Also used : Player(com.faforever.api.data.domain.Player) ProgrammingError(com.faforever.api.error.ProgrammingError) Error(com.faforever.api.error.Error) LuaValue(org.luaj.vm2.LuaValue) Map(com.faforever.api.data.domain.Map) ApiException(com.faforever.api.error.ApiException)

Example 10 with Error

use of com.faforever.api.error.Error 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()]));
    }
}
Also used : ArrayList(java.util.ArrayList) ProgrammingError(com.faforever.api.error.ProgrammingError) Error(com.faforever.api.error.Error) LuaValue(org.luaj.vm2.LuaValue) 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