示例#1
0
        private static Kind DecideTransitionKind(ElectionStateChange transition)
        {
            // ReSharper disable once PossibleInvalidOperationException (already checked)
            ElectionState precedingState = transition.PreviousState.Value;
            ElectionState targetState    = transition.TargetState;

            if (precedingState == ElectionState.PreNominations && targetState == ElectionState.Nominations)
            {
                return(Kind.NominationsStart);
            }

            if (precedingState == ElectionState.Nominations && targetState == ElectionState.PreVoting)
            {
                return(Kind.NominationsEnd);
            }

            if (precedingState == ElectionState.PreVoting && targetState == ElectionState.Voting)
            {
                return(Kind.VotingStart);
            }

            if (precedingState == ElectionState.Voting && targetState == ElectionState.Closed)
            {
                return(Kind.VotingEnd);
            }

            throw new Exception($"This {nameof(ElectionStateChange)} is not a background one");
        }
示例#2
0
        public Election(GenericReader reader)
        {
            int version = reader.ReadEncodedInt();

            switch ( version )
            {
                case 0:
                    {
                        this.m_Faction = Faction.ReadReference(reader);

                        this.m_LastStateTime = reader.ReadDateTime();
                        this.m_State = (ElectionState)reader.ReadEncodedInt();

                        this.m_Candidates = new List<Candidate>();

                        int count = reader.ReadEncodedInt();

                        for (int i = 0; i < count; ++i)
                        {
                            Candidate cd = new Candidate(reader);

                            if (cd.Mobile != null)
                                this.m_Candidates.Add(cd);
                        }

                        break;
                    }
            }

            this.StartTimer();
        }
        public Election(GenericReader reader)
        {
            int version = reader.ReadEncodedInt();

            switch (version)
            {
            case 0:
            {
                m_Faction = Faction.ReadReference(reader);

                m_LastStateTime = reader.ReadDateTime();
                m_State         = (ElectionState)reader.ReadEncodedInt();

                m_Candidates = new CandidateCollection();

                int count = reader.ReadEncodedInt();

                for (int i = 0; i < count; ++i)
                {
                    Candidate cd = new Candidate(reader);

                    if (cd.Mobile != null)
                    {
                        m_Candidates.Add(cd);
                    }
                }

                break;
            }
            }

            StartTimer();
        }
示例#4
0
 private static void ValidateNormalOrder(ElectionState state, string argumentName)
 {
     if (!NormalOrder.Contains(state))
     {
         throw new ArgumentOutOfRangeException(
                   argumentName, state,
                   "The election state is not part of normal lifecycle order and cannot be used in lifecycle context"
                   );
     }
 }
示例#5
0
 private static void ProhibitState(
     this SimpleValidationResult result,
     Election election,
     ElectionState prohibitedState
     )
 {
     if (election.State == prohibitedState)
     {
         result.Violations.Add(new ProhibitedState(prohibitedState));
     }
 }
示例#6
0
 private static void RequireCurrentState(
     this SimpleValidationResult result,
     Election election,
     ElectionState requiredState
     )
 {
     if (election.State != requiredState)
     {
         result.Violations.Add(new NotInRequiredState(requiredState));
     }
 }
示例#7
0
        public static ElectionState After(ElectionState state)
        {
            // This will also check if the state is in normal order
            if (IsLast(state))
            {
                throw new ArgumentOutOfRangeException(nameof(state), state,
                                                      "There is nothing after the last state in lifecycle");
            }

            return(NormalOrder[Array.IndexOf(NormalOrder, state) + 1]);
        }
示例#8
0
        private static int Compare(Election election, ElectionState referenceState)
        {
            if (IsInactive(election))
            {
                throw new InactiveElectionNoLifecycleComparisonException();
            }

            ValidateNormalOrder(election.State, nameof(election));
            ValidateNormalOrder(referenceState, nameof(referenceState));

            int current   = Array.IndexOf(NormalOrder, election.State);
            int reference = Array.IndexOf(NormalOrder, referenceState);

            return(current.CompareTo(reference));
        }
示例#9
0
        private void ChangeStateByUserAndRecord(Election election, ElectionState newState)
        {
            ElectionStateChange stateChange = new ElectionStateChange()
            {
                Election           = election,
                PreviousState      = election.State,
                TargetState        = newState,
                IsCausedByUser     = true,
                InstigatorUsername = User.Identity.GetUserId(),
                CompletedAt        = DateTime.Now
            };

            db.ElectionStateChanges.Add(stateChange);
            election.State = newState;

            AuditLogManager.RecordElectionStateChange(stateChange);
        }
        public ElectionPhaseBase GetPhaseByState(ElectionState stateInQuestion)
        {
            // ReSharper disable once SwitchStatementMissingSomeCases
            switch (stateInQuestion)
            {
            case ElectionState.Nominations:
                return(Nominations);

            case ElectionState.Voting:
                return(Voting);

            default:
                throw new ArgumentOutOfRangeException(
                          nameof(stateInQuestion),
                          $"Only {ElectionState.Nominations} and {ElectionState.Voting} are allowed"
                          );
            }
        }
示例#11
0
        public ServerHandler(int term, int _id, NodeRole _role, List <ClusterNodeSocket> _ExClusterNodeList)
        {
            TestNum = 0;

            id_                 = _id;
            role_               = _role;
            term_               = term;
            state_              = ServerState.Running;
            isVoteInThisTerm_   = false;
            leader_should_stop_ = false;
            is_election_done_   = false;
            lastApplied_        = -1; //与论文不同,论文下标从1开始,所以初始值为0
            commitIndex_        = -1;
            election_state_     = ElectionState.Stop;
            ExClusterNodeList_  = new List <ClusterNodeSocket>();
            ExClusterNodeList_  = _ExClusterNodeList;
            nextIndex_          = new int[ExClusterNodeList_.Count];
            matchIndex_         = new int[ExClusterNodeList_.Count];
            log_                = new List <LogEntryStruct>();
            rand_               = new Random();
            int seed = rand_.Next() * _id;

            rand_ = new Random(seed);
        }
示例#12
0
 public NotInRequiredState(ElectionState requiredState)
 {
     RequiredState = requiredState;
 }
示例#13
0
 public ProhibitedState(ElectionState prohibitedState)
 {
     CurrentState = prohibitedState;
 }
示例#14
0
        public void Slice()
        {
            if ( m_Faction.Election != this )
            {
                if ( m_Timer != null )
                    m_Timer.Stop();

                m_Timer = null;

                return;
            }

            switch ( m_State )
            {
                case ElectionState.Pending:
                {
                    if ( (m_LastStateTime + PendingPeriod) > Core.Now )
                        break;

                    m_Faction.Broadcast( 1038023 ); // Campaigning for the Faction Commander election has begun.

                    m_Candidates.Clear();
                    State = ElectionState.Campaign;

                    break;
                }
                case ElectionState.Campaign:
                {
                    if ( (m_LastStateTime + CampaignPeriod) > Core.Now )
                        break;

                    if ( m_Candidates.Count == 0 )
                    {
                        m_Faction.Broadcast( 1038025 ); // Nobody ran for office.
                        State = ElectionState.Pending;
                    }
                    else if ( m_Candidates.Count == 1 )
                    {
                        m_Faction.Broadcast( 1038029 ); // Only one member ran for office.

                        Candidate winner = m_Candidates[0];

                        Mobile mob = winner.Mobile;
                        PlayerState pl = PlayerState.Find( mob );

                        if ( pl == null || pl.Faction != m_Faction || mob == m_Faction.Commander )
                        {
                            m_Faction.Broadcast( 1038026 ); // Faction leadership has not changed.
                        }
                        else
                        {
                            m_Faction.Broadcast( 1038028 ); // The faction has a new commander.
                            m_Faction.Commander = mob;
                        }

                        m_Candidates.Clear();
                        State = ElectionState.Pending;
                    }
                    else
                    {
                        m_Faction.Broadcast( 1038030 );
                        State = ElectionState.Election;
                    }

                    break;
                }
                case ElectionState.Election:
                {
                    if ( (m_LastStateTime + VotingPeriod) > Core.Now )
                        break;

                    m_Faction.Broadcast( 1038024 ); // The results for the Faction Commander election are in

                    Candidate winner = null;

                    for ( int i = 0; i < m_Candidates.Count; ++i )
                    {
                        Candidate cd = m_Candidates[i];

                        PlayerState pl = PlayerState.Find( cd.Mobile );

                        if ( pl == null || pl.Faction != m_Faction )
                            continue;

                        //cd.CleanMuleVotes();

                        if ( winner == null || cd.Votes > winner.Votes )
                            winner = cd;
                    }

                    if ( winner == null )
                    {
                        m_Faction.Broadcast( 1038026 ); // Faction leadership has not changed.
                    }
                    else if ( winner.Mobile == m_Faction.Commander )
                    {
                        m_Faction.Broadcast( 1038027 ); // The incumbent won the election.
                    }
                    else
                    {
                        m_Faction.Broadcast( 1038028 ); // The faction has a new commander.
                        m_Faction.Commander = winner.Mobile;
                    }

                    m_Candidates.Clear();
                    State = ElectionState.Pending;

                    break;
                }
            }
        }
示例#15
0
        public void RemoveCandidate( Mobile mob )
        {
            Candidate cd = FindCandidate( mob );

            if ( cd == null )
                return;

            m_Candidates.Remove( cd );
            mob.SendLocalizedMessage( 1038031 );

            if ( m_State == ElectionState.Election )
            {
                if ( m_Candidates.Count == 1 )
                {
                    m_Faction.Broadcast( 1038031 ); // There are no longer any valid candidates in the Faction Commander election.

                    Candidate winner = m_Candidates[0];

                    Mobile winMob = winner.Mobile;
                    PlayerState pl = PlayerState.Find( winMob );

                    if ( pl == null || pl.Faction != m_Faction || winMob == m_Faction.Commander )
                    {
                        m_Faction.Broadcast( 1038026 ); // Faction leadership has not changed.
                    }
                    else
                    {
                        m_Faction.Broadcast( 1038028 ); // The faction has a new commander.
                        m_Faction.Commander = winMob;
                    }

                    m_Candidates.Clear();
                    State = ElectionState.Pending;
                }
                else if ( m_Candidates.Count == 0 ) // well, I guess this'll never happen
                {
                    m_Faction.Broadcast( 1038031 ); // There are no longer any valid candidates in the Faction Commander election.

                    m_Candidates.Clear();
                    State = ElectionState.Pending;
                }
            }
        }
示例#16
0
        public static bool IsFirst(ElectionState state)
        {
            ValidateNormalOrder(state, nameof(state));

            return(NormalOrder[0] == state);
        }
示例#17
0
 public static bool IsAfter(Election election, ElectionState referenceState)
 {
     return(Compare(election, referenceState) == 1);
 }
示例#18
0
        public static bool IsLast(ElectionState state)
        {
            ValidateNormalOrder(state, nameof(state));

            return(NormalOrder.Last() == state);
        }
        public override bool ShouldBeScheduled()
        {
            ElectionState intendedState = IsStart ? ElectionLifecycleInfo.Before(PhaseState) : PhaseState;

            return(Entity.State == intendedState);
        }
示例#20
0
 public static bool IsBefore(Election election, ElectionState referenceState)
 {
     return(Compare(election, referenceState) == -1);
 }