public void ProcessDayChange(int newDay) { var countries = countryRepository.GetAll(); foreach (var country in countries) { var policy = country.CountryPolicy; var congressCandidateVoting = congressCandidateVotingRepository.GetLastVotingForCountry(country.ID); if (congressCandidateVoting.VotingStatusID == (int)VotingStatusEnum.NotStarted) { if (congressCandidateVoting.CongressCandidates.Count == 0) { congressCandidateVoting.VotingDay = GameHelper.CurrentDay + 7; } else if (newDay >= congressCandidateVoting.VotingDay) { DismissCongressmen(country); congressCandidateVoting.VotingStatusID = (int)VotingStatusEnum.Ongoing; } } else if (congressCandidateVoting.VotingStatusID == (int)VotingStatusEnum.Ongoing) { var RegionsOfCandidates = congressCandidateVoting .CongressCandidates .Where(c => c.CongressCandidateVoting.CountryID == country.ID) .Where(c => c.CongressCandidateStatusID == (int)CongressCandidateStatusEnum.Approved) .GroupBy(c => c.RegionID) .ToList(); foreach (var region in RegionsOfCandidates) { var votes = region .Select(c => new { CandidateID = c.ID, VoteCount = c.CongressCandidateVotes.Count() }) .OrderByDescending(v => v.VoteCount) .ThenByDescending(v => v.CandidateID) // candidates which candidated first can win with candidate with same amount of votes .ToList(); for (int i = 0; i < Constants.ElectedCongressCandidatesByRegion && i < votes.Count; ++i) { var candidate = congressCandidateVotingRepository.GetCandidate(votes[i].CandidateID); country.Congressmen.Add(createCongressman(candidate.Citizen.ID, country.ID)); var gold = GetGoldForCadency(policy.CongressCadenceLength); citizenService.ReceiveCongressMedal(candidate.Citizen, gold); } } congressCandidateVoting.VotingStatusID = (int)VotingStatusEnum.Finished; if (RegionsOfCandidates.Count == 0) { CreateNewCongressCandidateVoting(country, newDay + 7); } else { CreateNewCongressCandidateVoting(country, newDay + policy.CongressCadenceLength); } } } congressCandidateVotingRepository.SaveChanges(); }