Example #1
0
        public void ProcessPresidentVoting(int newDay, Country country, PresidentVoting presidentVoting)
        {
            if (presidentVoting.VotingStatusID == (int)VotingStatusEnum.NotStarted)
            {
                if (presidentVoting.PresidentCandidates.Count == 0)
                {
                    presidentVoting.StartDay = GameHelper.CurrentDay + 7;
                }
                else if (newDay >= presidentVoting.StartDay)
                {
                    dismissPresident(country);
                    presidentVoting.VotingStatusID = (int)VotingStatusEnum.Ongoing;
                }
            }
            else if (presidentVoting.VotingStatusID == (int)VotingStatusEnum.Ongoing)
            {
                var votes = presidentVoting.PresidentVotes
                            .GroupBy(v => v.CandidateID)
                            .Select(v => new { CandidateID = v.Key, VoteCount = v.Count() })
                            .OrderByDescending(v => v.VoteCount)
                            .ToList();

                foreach (var vote in votes)
                {
                    var candidate = presidentVotingRepository.GetCandidateByID(vote.CandidateID);
                    candidate.CandidateStatusID = (int)PresidentCandidateStatusEnum.Rejected;
                }

                if (votes.Count == 0)
                {
                    presidentVoting.VotingStatusID = (int)VotingStatusEnum.Finished;
                    CreateNewPresidentVoting(country, newDay + 7);

                    var message = $"No one was elected in last president elections in {presidentVoting.Country.Entity.Name} due to lack of votes on candidates. If you still want to be a president candidate you need to candidate again";
                    foreach (var candidate in presidentVoting.PresidentCandidates)
                    {
                        warningService.AddWarning(candidate.CandidateID, message);
                    }
                }
                else if (votes.Count != 1 && votes[0].VoteCount == votes[1].VoteCount)
                {
                    //reelection in next week
                    presidentVoting.VotingStatusID = (int)VotingStatusEnum.Finished;
                    CreateNewPresidentVoting(country, newDay + 7);
                }
                else
                {
                    var policy = country.CountryPolicy;

                    var candidate = presidentVotingRepository.GetCandidateByID(votes[0].CandidateID);
                    country.PresidentID = candidate.CandidateID;
                    var gold = countryPresidentService.GetGoldForCadency(policy.PresidentCadenceLength);
                    citizenService.ReceivePresidentMedal(candidate.Citizen, gold);

                    CreateNewPresidentVoting(country, newDay + policy.PresidentCadenceLength);
                    presidentVoting.VotingStatusID = (int)VotingStatusEnum.Finished;
                    candidate.CandidateStatusID    = (int)PresidentCandidateStatusEnum.Approved;
                }
            }
        }
Example #2
0
        public ActionResult VoteOnPresident(int candidateID)
        {
            var candidate = presidentVotingRepository.GetCandidateByID(candidateID);

            if (candidate == null)
            {
                return(RedirectToHomeWithError("Candidate does not exist!"));
            }

            var entity = SessionHelper.CurrentEntity;

            if (entity.Citizen == null)
            {
                return(RedirectBackWithError("You cannot vote"));
            }

            MethodResult result;

            if ((result = countryService.CanVoteOnPresidentCandidate(entity.Citizen, candidate)).IsError)
            {
                return(RedirectBackWithError(result));
            }

            countryService.VoteOnPresidentCandidate(entity.Citizen, candidate);
            AddInfo(string.Format("You successfully voted on {0}", candidate.Citizen.Entity.Name));
            return(RedirectBack());
        }