use of com.faforever.api.data.domain.VotingAnswer 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);
}
use of com.faforever.api.data.domain.VotingAnswer in project faf-java-api by FAForever.
the class VotingSubjectEnricher method getWinners.
private List<VotingChoice> getWinners(VotingQuestion votingQuestion) {
if (!votingQuestion.isAlternativeQuestion()) {
OptionalInt max = votingQuestion.getVotingChoices().stream().mapToInt(value -> value.getVotingAnswers().size()).max();
if (max.isPresent()) {
return votingQuestion.getVotingChoices().stream().filter(votingChoice -> votingChoice.getVotingAnswers().size() == max.getAsInt()).collect(toList());
}
return Collections.emptyList();
}
// All the answers sorted by their choice, but only those that are the 1st choice
Map<VotingChoice, List<VotingAnswer>> votersByChoice = votingQuestion.getVotingChoices().stream().collect(Collectors.toMap(Function.identity(), choice -> new ArrayList<>(choice.getVotingAnswers().stream().filter(votingAnswer -> votingAnswer.getAlternativeOrdinal() == 0).collect(toList()))));
while (votersByChoice.size() > 1) {
OptionalInt min = votersByChoice.values().stream().mapToInt(List::size).min();
List<VotingChoice> candidatesToEliminate = votersByChoice.entrySet().stream().filter(votingChoiceListEntry -> votingChoiceListEntry.getValue().size() == min.getAsInt()).map(Entry::getKey).collect(toList());
if (candidatesToEliminate.size() == votersByChoice.size()) {
// We got a problem here, we would eliminate all the candidates if we went on normally
return candidatesToEliminate;
}
candidatesToEliminate.forEach(candidate -> {
List<VotingAnswer> votingAnswersForCandidate = votersByChoice.get(candidate);
// Lets distribute the answers of the candidate that is eliminated
votingAnswersForCandidate.forEach(votingAnswer -> {
int newAlternativeOrdinal = votingAnswer.getAlternativeOrdinal() + 1;
votingAnswer.getVote().getVotingAnswers().stream().filter(votingAnswer1 -> votingAnswer1.getVotingChoice().getVotingQuestion().equals(votingAnswer.getVotingChoice().getVotingQuestion()) && votingAnswer1.getAlternativeOrdinal() == newAlternativeOrdinal).findFirst().ifPresent(votingAnswer1 -> {
VotingChoice votingChoice1 = votingAnswer1.getVotingChoice();
votersByChoice.get(votingChoice1).add(votingAnswer1);
});
});
votersByChoice.remove(candidate);
});
}
Optional<Entry<VotingChoice, List<VotingAnswer>>> first = votersByChoice.entrySet().stream().findFirst();
return first.map(votingChoiceListEntry -> Collections.singletonList(votingChoiceListEntry.getKey())).orElse(Collections.emptyList());
}
Aggregations