Exemple #1
0
        /// <summary>
        /// Wrapper for handling replace task, but without adding to merge records.
        /// </summary>
        /// <param name="vote">The vote being modified.</param>
        /// <param name="task">The new task to apply to the vote.</param>
        /// <returns>Returns true if the task replacement was successfully completed.</returns>
        private bool ReplaceTaskImplWrapper(VoteLineBlock vote, string task)
        {
            if (!VoteStorage.TryGetValue(vote, out var supporters))
            {
                return(false);
            }

            // Incoming parameter may be an entry in storage, or a copy of a vote.
            // Adjust so that we're always pointing at an actual vote.
            // If the vote isn't found in VoteStorage, just use the one provided.
            vote = VoteStorage.GetVoteMatching(vote) ?? vote;

            // Remove the version of the vote we're starting with.
            VoteStorage.Remove(vote);

            string originalTask = vote.Task;

            vote.Task = task;

            // If there's a conflict with the newly-tasked vote, we need to merge with the existing vote.
            if (VoteStorage.ContainsKey(vote))
            {
                if (VoteStorage.TryGetValue(vote, out var toSupport))
                {
                    foreach (var(supporterName, supporterVote) in supporters)
                    {
                        if (!toSupport.ContainsKey(supporterName))
                        {
                            supporterVote.Task = task;
                            toSupport.Add(supporterName, supporterVote);
                        }
                    }
                }
                else
                {
                    // Undo the attempt if we couldn't get the conflicting vote data
                    vote.Task = originalTask;

                    VoteStorage.Add(vote, supporters);

                    return(false);
                }
            }
            // If there's no conflict, update the tasks in the supporter votes and add the revised vote.
            else
            {
                foreach (var(_, supporterVote) in supporters)
                {
                    supporterVote.Task = task;
                }

                VoteStorage.Add(vote, supporters);
            }

            return(true);
        }
Exemple #2
0
        public UndoAction(UndoActionType actionType, VoteStorage currentState,
                          VoteLineBlock?storageVote = null)
        {
            ActionType = actionType;

            // Clone the current vote repository.
            storage = new VoteStorage(currentState);

            // Utilize a cloned storage vote if we need to track a changed VoteLineBlock
            // that had internal properties changed.  Otherwise those will be propogated
            // to our storage collection.
            if (storageVote != null)
            {
                var storedVote = storageVote.Clone();
                storage.TryGetValue(storageVote, out var storedVoteSupporters);
                storage.Remove(storageVote);
                storage.Add(storedVote, storedVoteSupporters);
            }
        }
Exemple #3
0
        private bool SplitImplWrapper(VoteLineBlock fromVote, List <VoteLineBlock> toVotes)
        {
            if (!VoteStorage.TryGetValue(fromVote, out var fromSupport))
            {
                return(false);
            }

            foreach (var toVote in toVotes)
            {
                if (!VoteStorage.TryGetValue(toVote, out var toSupport))
                {
                    return(false);
                }

                MergeImpl(fromVote, toVote, fromSupport, toSupport);
            }

            // But we still want to remove the from vote.
            return(VoteStorage.Remove(fromVote));
        }
Exemple #4
0
        /// <summary>
        /// The wrapper handles the process of extracting the vote support from
        /// the storage before passing the pieces on to the implementation.
        /// </summary>
        /// <param name="fromVote">The vote being merged.</param>
        /// <param name="toVote">The vote being merged into.</param>
        /// <returns>Returns true if there was a successful merge.</returns>
        private bool MergeImplWrapper(VoteLineBlock fromVote, VoteLineBlock toVote)
        {
            if (fromVote == toVote)
            {
                return(false);
            }

            if (!VoteStorage.TryGetValue(fromVote, out var fromSupport))
            {
                return(false);
            }

            if (!VoteStorage.TryGetValue(toVote, out var toSupport))
            {
                return(false);
            }

            // Theoretically, all the supporters in the from vote could already
            // be in the to vote, in which case no merging would happen.
            MergeImpl(fromVote, toVote, fromSupport, toSupport);

            // But we still want to remove the from vote.
            return(VoteStorage.Remove(fromVote));
        }
Exemple #5
0
        public bool Undo(IVoteCounter voteCounter)
        {
            if (undone)
            {
                return(false);
            }

            var currentVotes = voteCounter.VoteStorage;

            // Remove pass - Remove all current votes or supporters that are not
            // in the archived version of the vote repository.

            HashSet <VoteLineBlock> voteRemovals = new HashSet <VoteLineBlock>();

            foreach (var(currentVote, currentSupporters) in currentVotes)
            {
                if (!storage.TryGetValue(currentVote, out var storageSupporters))
                {
                    voteRemovals.Add(currentVote);
                }
                else
                {
                    HashSet <Origin> voterRemovals = new HashSet <Origin>();

                    foreach (var(currentSupporter, _) in currentSupporters)
                    {
                        if (!storageSupporters.ContainsKey(currentSupporter))
                        {
                            voterRemovals.Add(currentSupporter);
                        }
                    }

                    foreach (var removal in voterRemovals)
                    {
                        currentSupporters.Remove(removal);
                    }
                }
            }

            foreach (var removal in voteRemovals)
            {
                currentVotes.Remove(removal);
            }


            // Replace pass - Add all archived votes or supporters that are not
            // in the current version of the vote repository.

            foreach (var(storageVote, storageSupporters) in storage)
            {
                if (!currentVotes.TryGetValue(storageVote, out var currentSupporters))
                {
                    currentVotes.Add(storageVote, storageSupporters);
                }
                else
                {
                    foreach (var(storageSupporter, storageSupporterVote) in storageSupporters)
                    {
                        if (!currentSupporters.ContainsKey(storageSupporter))
                        {
                            currentSupporters.Add(storageSupporter, storageSupporterVote);
                        }
                    }
                }
            }

            return(undone = true);
        }