private void _endVoting(IDbConnection connection, IHubContext <MainGameHub> hubContext = null,
                                bool notifyClients = true)
        {
            if (VotingInProgress)
            {
                throw new Exception(Error.TimeVotingIsNotOver);
            }
            if (_voteSate == VoteStates.EndedFinished)
            {
                throw new Exception(Error.VotingRunnerVoteIsFinishedBefore);
            }
            if (_voteSate == VoteStates.EndInProgress)
            {
                throw new Exception(Error.VotingRunnerEndInProgress);
            }
            try
            {
                _voteSate         = VoteStates.EndInProgress;
                _activeCandidates = null;
                List <IUserOfficerOut> officers = _endVotingDb(connection);

                if (officers == null || !officers.Any())
                {
                    _voteSate = VoteStates.EndedFinished;
                    return;
                }

                _rebuildWeekOfficers(officers);

                if (notifyClients)
                {
                    _notifyClientsOnVotingEnded(connection, hubContext);
                }
                _voteSate = VoteStates.EndedFinished;
            }
            catch (Exception e)
            {
                if (e.Message == VoteStates.EndedFinished.ToString())
                {
                    if (notifyClients)
                    {
                        _notifyClientsOnVotingEnded(connection, hubContext);
                    }
                    _voteSate = VoteStates.EndedFinished;
                }
                else
                {
                    _voteSate = VoteStates.EndedErrorInFinished;
                    throw;
                }
            }
        }
        private void _startVoting(IDbConnection connection, IHubContext <MainGameHub> hubContext = null,
                                  bool notifyClients = true)
        {
            if (!VotingInProgress)
            {
                throw new Exception(Error.IsNotVotePeriod);
            }
            if (_voteSate == VoteStates.StartedRunned)
            {
                throw new Exception(Error.VotingRunnerVoteIsFinishedBefore);
            }
            if (_voteSate == VoteStates.StartInProgress)
            {
                throw new Exception(Error.VotingRunnerStartInProgress);
            }
            List <CandidatOut> finalzers = null;

            try
            {
                _voteSate = VoteStates.StartInProgress;

                finalzers         = _startVotingDb(connection);
                _activeCandidates = new ActiveCandidates(finalzers);
                //_notyfyOnVoteEndedComplete
                if (notifyClients)
                {
                    _notifyClientsOnVoteStarted(hubContext);
                }
                _voteSate = VoteStates.StartedRunned;
            }
            catch (Exception e)
            {
                if (e.Message == VoteStates.StartedRunned.ToString())
                {
                    finalzers = _officerCandidatRepository.GetRegistredOfficerCandidatesByTopPvp(connection,
                                                                                                 MAX_CANDIDATES_COUNT);
                    _activeCandidates = new ActiveCandidates(finalzers);

                    if (notifyClients)
                    {
                        _notifyClientsOnVoteStarted(hubContext);
                    }
                    _voteSate = VoteStates.StartedRunned;
                }
                else
                {
                    _voteSate = VoteStates.StartedErrorInRun;
                    throw;
                }
            }
        }
        private void _fillActiveCandidatesFromDb(IDbConnection connection)
        {
            var finalzers = _officerCandidatRepository.GetRegistredOfficerCandidatesByTopPvp(connection, 0);

            if (finalzers == null || !finalzers.Any())
            {
                throw new NotImplementedException("finalzers == null || !finalzers.Any()");
            }
            if (finalzers.Count > MAX_CANDIDATES_COUNT)
            {
                finalzers = finalzers.Take(MAX_CANDIDATES_COUNT).ToList();
            }

            _activeCandidates = new ActiveCandidates(finalzers);
        }
 public void OnAppStop()
 {
     _voteSate = VoteStates.Undefined;
     if (_weekOfficersStorage != null)
     {
         _weekOfficersStorage.Clear();
         _weekOfficersStorage = null;
     }
     // ReSharper disable once RedundantCheckBeforeAssignment
     if (_activeCandidates == null)
     {
         return;
     }
     _activeCandidates.Dispose();
     _activeCandidates = null;
 }