Пример #1
0
        /// <summary>
        /// Gets the partitions supported by an identity.
        /// </summary>
        /// <param name="identity">The identity.</param>
        /// <returns>Returns the list of vote partitions a given identity supports.</returns>
        public List <VotePartition> GetPartitionsForIdentity(Identity identity)
        {
            if (identity == null)
            {
                throw new ArgumentNullException(nameof(identity));
            }

            if (IdentityVotesLookup.TryGetValue(identity, out List <VotePartition> votes))
            {
                return(votes);
            }

            return(new List <VotePartition>());
        }
Пример #2
0
        /// <summary>
        /// Add any number of vote fragments for a given voter, from a given post.
        /// </summary>
        /// <param name="partitions">A list of vote fragments.</param>
        /// <param name="voterName">The voter.</param>
        /// <param name="postID">The post the vote fragments are from.</param>
        /// <exception cref="ArgumentNullException"/>
        public void AddVoteEntries(IEnumerable <VotePartition> partitions, Identity identity)
        {
            if (partitions == null)
            {
                throw new ArgumentNullException(nameof(partitions));
            }
            if (identity == null)
            {
                throw new ArgumentNullException(nameof(identity));
            }

            if (!partitions.Any())
            {
                return;
            }

            // All existing partitions have this identity removed.
            bool changedVoters = RemoveVoterSupport(identity);
            bool addedAnyVotes = false;

            foreach (var partition in partitions)
            {
                AddTask(partition.Task);

                var votes = GetVoteEntries(partition.VoteType);

                // Add supporters to partitions
                if (votes.TryGetValue(partition, out var identitySet))
                {
                    changedVoters = identitySet.Add(identity) || changedVoters;
                }
                else
                {
                    votes[partition] = new IdentitySet {
                        identity
                    };
                    addedAnyVotes = true;
                }

                // Add partitions to supporters
                if (IdentityVotesLookup.TryGetValue(identity, out var identVotes))
                {
                    identVotes.Add(partition);
                }
                else
                {
                    IdentityVotesLookup.Add(identity, new List <VotePartition> {
                        partition
                    });
                }
            }

            if (addedAnyVotes)
            {
                OnPropertyChanged("Votes");
            }
            if (changedVoters)
            {
                OnPropertyChanged("Voters");
            }
        }