コード例 #1
0
ファイル: VoteCounter.cs プロジェクト: MizMahem/NetTally
        /// <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);
        }
コード例 #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);
            }
        }