/// <summary> /// Calculates the result. /// </summary> /// <param name="votingProcess">The voting process.</param> /// <returns></returns> /// <exception cref="VoteInException">Impossible to calculate the results : The voting process is null</exception> public IResultatModel CalculateResult(VotingProcessModel votingProcess) { if (votingProcess == null) { throw new VoteInException("Impossible to calculate the results : The voting process is null"); } decimal nbTotalVote = votingProcess.Options.SelectMany(o => o.Suffrages).Count(s => s.Value == ChoosenValue); var resultat = new ResultatMajorityVotingProcessModel { IndividualResults = votingProcess.Options .Select(o => CreateResultatIndividuel(nbTotalVote, o)) .OrderByDescending(ri => ri.Votes) .ToList() }; resultat.IsValidResult = IsResultatValide(resultat, votingProcess); var first = resultat.IndividualResults.FirstOrDefault(); if (resultat.IsValidResult && first != null && (first.Percentage > 50m || votingProcess.IsLastRound)) { resultat.Winner = first.Option; } return(resultat); }
/// <summary> /// Creates the tour suivant. /// </summary> /// <param name="idScutin">The identifier scutin.</param> /// <param name="resultatModel">The resultat model.</param> /// <returns></returns> private int CreateTourSuivant(int idScutin, ResultatMajorityVotingProcessModel resultatModel) { var ancienScrutin = GetScrutin(idScutin); var nouveauScrutin = ancienScrutin.CopyForNextTurn(); nouveauScrutin.VotingProcessOption = GetOptionsQualifiees(resultatModel, ancienScrutin); VotingProcessRepository.Add(nouveauScrutin); return(nouveauScrutin.Id); }
/// <summary> /// Gets the options qualifiees. /// </summary> /// <param name="resultatModel">The resultat model.</param> /// <param name="ancienScrutin">The ancien scrutin.</param> /// <returns></returns> private List <VotingProcessOption> GetOptionsQualifiees(ResultatMajorityVotingProcessModel resultatModel, VotingProcess ancienScrutin) { Option GetOptionAncienScrutin(OptionsModel o) => ancienScrutin.VotingProcessOption.First(os => os.Option.Id == o.Id).Option; var resultatsQualifies = resultatModel.IndividualResults.Take(2); return(resultatsQualifies.Select(o => new VotingProcessOption { Option = GetOptionAncienScrutin(o.Option) }).ToList()); }
public void MapScrutinMajoritaireResultatModelToResultat() { var scrutinModel = new VotingProcessModel { Id = 52, Mode = "scrutin-majoritaire" }; var optionVainqueur = new OptionsModel { Id = 28, Name = "Winner" }; var optionPerdant = new OptionsModel { Id = 32, Name = "Loser" }; var resultatGagnant = new ResultatIndividualMajorityVotingProcessModel { Option = optionVainqueur, Votes = 3, Percentage = 75m }; var resultatPerdant = new ResultatIndividualMajorityVotingProcessModel { Option = optionPerdant, Votes = 1, Percentage = 25m }; var resultatModel = new ResultatMajorityVotingProcessModel { Id = 96, IsValidResult = true, Voters = 4, Winner = optionVainqueur, IndividualResults = { resultatGagnant, resultatPerdant } }; var resultat = _mapper.MapResultatModelToResultat(scrutinModel, resultatModel); Check.That(resultat.IdVotingProcess).IsEqualTo(scrutinModel.Id); Check.That(resultat.IsValid).IsEqualTo(resultatModel.IsValidResult); Check.That(resultat.NbVoters).IsEqualTo(resultatModel.Voters); Check.That(resultat.IdWinningOption).IsEqualTo(resultatModel.Winner.Id); var scoreDetails = JsonConvert.DeserializeObject <List <ResultatIndividualMajorityVotingProcessModel> >(resultat.ScoreDetail); Check.That(scoreDetails[0].Votes).IsEqualTo(resultatGagnant.Votes); Check.That(scoreDetails[0].Percentage).IsEqualTo(resultatGagnant.Percentage); Check.That(scoreDetails[0].Option.Id).IsEqualTo(resultatGagnant.Option.Id); Check.That(scoreDetails[1].Votes).IsEqualTo(resultatPerdant.Votes); Check.That(scoreDetails[1].Percentage).IsEqualTo(resultatPerdant.Percentage); Check.That(scoreDetails[1].Option.Id).IsEqualTo(resultatPerdant.Option.Id); }
/// <summary> /// Determines whether [is resultat valide] [the specified resultat]. /// </summary> /// <param name="resultat">The resultat.</param> /// <param name="votingProcess">The voting process.</param> /// <returns> /// <c>true</c> if [is resultat valide] [the specified resultat]; otherwise, <c>false</c>. /// </returns> private static bool IsResultatValide(ResultatMajorityVotingProcessModel resultat, VotingProcessModel votingProcess) { var first = resultat.IndividualResults.FirstOrDefault(); var second = resultat.IndividualResults.ElementAtOrDefault(1); var third = resultat.IndividualResults.ElementAtOrDefault(2); if (first == null || first.Option.IsBlankVote) { return(false); } if ((resultat.IndividualResults.Count == 2 || votingProcess.IsLastRound) && first.Votes == second.Votes) { return(false); } var isFirstRoundWithoutWinner = !votingProcess.IsLastRound && first.Percentage <= 50m; return(!isFirstRoundWithoutWinner || (second?.Votes != third?.Votes && !second.Option.IsBlankVote)); }