Ejemplo n.º 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;
                }
            }
        }