Search in sources :

Example 11 with VotingSubject

use of com.faforever.api.data.domain.VotingSubject in project faf-java-api by FAForever.

the class VotingController method redirectToFilteredVotingSubjects.

private void redirectToFilteredVotingSubjects(HttpServletResponse response, List<VotingSubject> votingSubjects, HttpServletRequest request) throws IOException {
    boolean empty = true;
    StringBuilder filter = new StringBuilder();
    filter.append("id=in=(");
    for (VotingSubject votingSubject : votingSubjects) {
        if (empty) {
            empty = false;
        } else {
            filter.append(',');
        }
        filter.append(votingSubject.getId());
    }
    if (empty) {
        filter.append("-1");
    }
    filter.append(')');
    String queryString = request.getQueryString();
    if (!Strings.isNullOrEmpty(queryString)) {
        response.sendRedirect(String.format("/data/votingSubject?%s&filter=%s", queryString, filter));
    } else {
        response.sendRedirect(String.format("/data/votingSubject?filter=%s", filter));
    }
}
Also used : VotingSubject(com.faforever.api.data.domain.VotingSubject)

Example 12 with VotingSubject

use of com.faforever.api.data.domain.VotingSubject in project faf-java-api by FAForever.

the class VotingService method saveVote.

@Transactional
public void saveVote(Vote vote, Player player) {
    vote.setPlayer(player);
    Assert.notNull(vote.getVotingSubject(), "You must specify a subject");
    List<Error> errors = ableToVote(player, vote.getVotingSubject().getId());
    if (vote.getVotingAnswers() == null) {
        vote.setVotingAnswers(Collections.emptySet());
    }
    VotingSubject subject = votingSubjectRepository.findById(vote.getVotingSubject().getId()).orElseThrow(() -> new IllegalArgumentException("Subject of vote not found"));
    vote.getVotingAnswers().forEach(votingAnswer -> {
        VotingChoice votingChoice = votingAnswer.getVotingChoice();
        VotingChoice one = votingChoiceRepository.findById(votingChoice.getId()).orElseThrow(() -> new ApiException(new Error(ErrorCode.VOTING_CHOICE_DOES_NOT_EXIST, votingChoice.getId())));
        votingAnswer.setVotingChoice(one);
        votingAnswer.setVote(vote);
    });
    subject.getVotingQuestions().forEach(votingQuestion -> {
        List<VotingAnswer> votingAnswers = vote.getVotingAnswers().stream().filter(votingAnswer -> votingAnswer.getVotingChoice().getVotingQuestion().equals(votingQuestion)).collect(Collectors.toList());
        long countOfAnswers = votingAnswers.size();
        int maxAnswers = votingQuestion.getMaxAnswers();
        if (maxAnswers < countOfAnswers) {
            errors.add(new Error(ErrorCode.TOO_MANY_ANSWERS, countOfAnswers, maxAnswers));
        }
        if (votingQuestion.isAlternativeQuestion()) {
            for (int i = 0; i < countOfAnswers; i++) {
                int finalI = i;
                long answersWithOrdinal = votingAnswers.stream().filter(votingAnswer -> Objects.equals(votingAnswer.getAlternativeOrdinal(), finalI)).count();
                if (answersWithOrdinal == 1) {
                    continue;
                }
                errors.add(new Error(ErrorCode.MALFORMATTED_ALTERNATIVE_ORDINALS));
            }
        }
    });
    if (!errors.isEmpty()) {
        throw new ApiException(errors.toArray(new Error[0]));
    }
    voteRepository.save(vote);
}
Also used : Error(com.faforever.api.error.Error) ErrorCode(com.faforever.api.error.ErrorCode) VotingAnswer(com.faforever.api.data.domain.VotingAnswer) Transactional(javax.transaction.Transactional) ApiException(com.faforever.api.error.ApiException) GamePlayerStatsRepository(com.faforever.api.game.GamePlayerStatsRepository) Vote(com.faforever.api.data.domain.Vote) Player(com.faforever.api.data.domain.Player) Collectors(java.util.stream.Collectors) VotingChoice(com.faforever.api.data.domain.VotingChoice) ArrayList(java.util.ArrayList) Objects(java.util.Objects) List(java.util.List) OffsetDateTime(java.time.OffsetDateTime) Validity(com.faforever.api.data.domain.Validity) Service(org.springframework.stereotype.Service) Optional(java.util.Optional) VotingSubject(com.faforever.api.data.domain.VotingSubject) Collections(java.util.Collections) Assert(org.springframework.util.Assert) VotingAnswer(com.faforever.api.data.domain.VotingAnswer) Error(com.faforever.api.error.Error) VotingSubject(com.faforever.api.data.domain.VotingSubject) VotingChoice(com.faforever.api.data.domain.VotingChoice) ApiException(com.faforever.api.error.ApiException) Transactional(javax.transaction.Transactional)

Example 13 with VotingSubject

use of com.faforever.api.data.domain.VotingSubject in project faf-java-api by FAForever.

the class VotingSubjectEnricher method calculateWinners.

@VisibleForTesting
void calculateWinners(VotingQuestion votingQuestion) {
    VotingSubject votingSubject = votingQuestion.getVotingSubject();
    boolean ended = votingSubject.getEndOfVoteTime().isBefore(OffsetDateTime.now());
    List<VotingChoice> winners = votingQuestion.getWinners();
    if ((winners == null || winners.isEmpty()) && ended && votingSubject.getRevealWinner()) {
        votingQuestion.setWinners(getWinners(votingQuestion));
    } else {
        votingQuestion.setWinners(Collections.emptyList());
    }
}
Also used : VotingSubject(com.faforever.api.data.domain.VotingSubject) VotingChoice(com.faforever.api.data.domain.VotingChoice) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Aggregations

VotingSubject (com.faforever.api.data.domain.VotingSubject)13 Vote (com.faforever.api.data.domain.Vote)11 Player (com.faforever.api.data.domain.Player)10 Test (org.junit.Test)9 VotingChoice (com.faforever.api.data.domain.VotingChoice)8 VotingQuestion (com.faforever.api.data.domain.VotingQuestion)8 VotingAnswer (com.faforever.api.data.domain.VotingAnswer)5 ApiException (com.faforever.api.error.ApiException)5 Error (com.faforever.api.error.Error)2 ArrayList (java.util.ArrayList)2 Validity (com.faforever.api.data.domain.Validity)1 ErrorCode (com.faforever.api.error.ErrorCode)1 GamePlayerStatsRepository (com.faforever.api.game.GamePlayerStatsRepository)1 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 OffsetDateTime (java.time.OffsetDateTime)1 Collections (java.util.Collections)1 List (java.util.List)1 Objects (java.util.Objects)1 Optional (java.util.Optional)1 Collectors (java.util.stream.Collectors)1