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