public void ProcessDayChange(int newDay)
        {
            var unfinishedVotings = congressVotingRepository.Where(v => v.VotingStatusID == (int)VotingStatusEnum.Ongoing).ToList();

            foreach (var voting in unfinishedVotings)
            {
                var country = voting.Country;

                var timeLeft = voting.GetTimeLeft(GameHelper.CurrentDay);

                if (timeLeft.TotalSeconds < 0)
                {
                    var votes        = voting.CongressVotes.ToList();
                    var supportVotes = votes.Where(v => v.VoteTypeID == (int)VoteTypeEnum.Yes);
                    var againstVotes = votes.Where(v => v.VoteTypeID == (int)VoteTypeEnum.No);

                    var supportersIDs = supportVotes.Select(s => s.CitizenID);
                    var againstsIDs   = againstVotes.Select(a => a.CitizenID);

                    var abstained     = country.Congressmen.Where(c => supportersIDs.Contains(c.CitizenID) == false && againstsIDs.Contains(c.CitizenID) == false);
                    var winPercentage = (double)country.CountryPolicy.NormalCongressVotingWinPercentage;

                    foreach (var vote in CreateAbstainedVotes(voting, abstained))
                    {
                        congressVotingRepository.AddVote(vote);
                    }

                    double percentage = (double)supportVotes.Count() / (double)(Math.Max(votes.Count(), 1));

                    if (percentage >= winPercentage)
                    {
                        using (NoSaveChanges)
                        {
                            FinishVoting(voting);
                        }
                    }
                    else
                    {
                        RejectVoting(voting, CongressVotingRejectionReasonEnum.NotEnoughVotes);
                    }

                    using (NoSaveChanges)
                    {
                        var    votingLink = CongressVotingLinkCreator.Create(voting);
                        string message    = $"{votingLink} has ended with result: {voting.GetStatus()}.";

                        if (voting.GetStatusEnum() == VotingStatusEnum.Rejected)
                        {
                            message += $"{voting.GetRejectionReason().ToHumanReadable().FirstUpper()}.";
                        }

                        warningService.SendWarningToCongress(voting.Country, message);
                    }
                }
            }

            congressVotingRepository.SaveChanges();
        }
Exemplo n.º 2
0
        public PartialViewResult VotingsPartial(int countryID, PagingParam pagingParam)
        {
            pagingParam = pagingParam ?? new PagingParam();

            var votingsQuery = congressVotingRepository.Where(cv => cv.CountryID == countryID);

            var vm = new CongressVotingsPartialViewModel(countryID, votingsQuery, pagingParam);

            return(PartialView(vm));
        }