diff --git a/TallyCore/Tally.cs b/TallyCore/Tally.cs index e5df9ece..0d1261f6 100644 --- a/TallyCore/Tally.cs +++ b/TallyCore/Tally.cs @@ -196,9 +196,6 @@ public void ClearPageCache() PageProvider.ClearPageCache(); } - #endregion - - #region Functions for constructing the tally results output. /// /// Compose the tallied results into a string to put in the TallyResults property, /// for display in the UI. @@ -209,385 +206,7 @@ public void ConstructResults(IQuest quest) return; TallyResults = TextResults.BuildOutput(quest, VoteCounter, DisplayMode); - - /* - StringBuilder sb = new StringBuilder(); - - AddHeader(sb); - - ConstructRankedOutput(sb, quest); - - ConstructNormalOutput(sb, quest); - - TallyResults = sb.ToString(); - */ - } - - /// - /// Construct the output of ranked votes for the quest. - /// - /// The string builder to add the results to. - /// The quest being tallied. - private void ConstructRankedOutput(StringBuilder sb, IQuest quest) - { - if (VoteCounter.HasRankedVotes) - { - // Get ranked results, and order them by task name - var results = RankVotes.Rank(VoteCounter).OrderBy(a => a.Key); - - // output the ranking result - foreach (var result in results) - { - AddTaskLabel(sb, result.Key); - - AddRankedOptions(sb, result.Key); - - AddRankedWinner(sb, result.Value.First()); - - AddRankedVoters(sb, quest, result); - - AddRunnersUp(sb, result.Value.Skip(1)); - - sb.AppendLine(""); - } - } - } - - /// - /// Construct the output of normal votes for the quest. - /// - /// The string builder to add the results to. - /// The quest being tallied. - private void ConstructNormalOutput(StringBuilder sb, IQuest quest) - { - var groupedVotesWithSupporters = GroupVotes(VoteCounter.VotesWithSupporters); - bool firstTask = true; - - foreach (var taskGroup in groupedVotesWithSupporters) - { - if (!firstTask) - { - AddLineBreak(sb); - } - - firstTask = false; - - AddTaskLabel(sb, taskGroup.Key); - - foreach (var vote in taskGroup.OrderByDescending(v => v.Value.Count(vc => VoteCounter.PlanNames.Contains(vc) == false))) - { - sb.Append(vote.Key); - - AddVoteCount(sb, vote.Value); - - if (UseSpoilerForVoters) - { - sb.AppendLine("[spoiler=Voters]"); - } - - AddVoters(sb, vote.Value, quest); - - if (UseSpoilerForVoters) - { - sb.AppendLine("[/spoiler]"); - } - - sb.AppendLine(""); - } - } - - AddTotalVoterCount(sb); - } - #endregion - - #region Functions for adding pieces of text to the output results. - /// - /// Construct the header text for the tally results. - /// - /// The string builder to add the results to. - private void AddHeader(StringBuilder sb) - { - var assembly = Assembly.GetExecutingAssembly(); - var product = (AssemblyProductAttribute)assembly.GetCustomAttribute(typeof(AssemblyProductAttribute)); - var version = (AssemblyInformationalVersionAttribute)assembly.GetCustomAttribute(typeof(AssemblyInformationalVersionAttribute)); - - sb.AppendFormat("[b]Vote Tally[/b] : {0}\r\n", VoteCounter.Title); - sb.AppendFormat("[color=transparent]##### {0} {1}[/color]\r\n\r\n", - product.Product, - version.InformationalVersion); - } - - /// - /// Add a line break to the output. - /// - /// The string builder to add the results to. - private void AddLineBreak(StringBuilder sb) - { - //sb.AppendLine("[hr][/hr]"); - //sb.AppendLine("---------------------------------------------------------\r\n"); - sb.AppendLine("—————————————————————————————————————————————————————————\r\n"); - } - - /// - /// Add the total number of user votes to the output. - /// - /// The string builder to add the results to. - /// The set of voters voting for this item. - private void AddVoteCount(StringBuilder sb, HashSet voters) - { - sb.Append("[b]No. of Votes: "); - sb.Append(voters.Count(vc => VoteCounter.PlanNames.Contains(vc) == false)); - sb.AppendLine("[/b]"); - } - - /// - /// Add a task label line to the string builder. - /// - /// The string builder to add the results to. - /// The name of the task. - private void AddTaskLabel(StringBuilder sb, string task) - { - if (task.Length > 0) - { - sb.AppendFormat("[b]Task: {0}[/b]\r\n\r\n", task); - } - } - - /// - /// Add all voters from the provided list of voters to the output string. - /// Plans are placed before users, and each group (after the first voter) - /// is alphabetized. - /// - /// The string builder to add the results to. - /// The set of voters being added. - /// The quest being tallied. - private void AddVoters(StringBuilder sb, HashSet voters, IQuest quest) - { - string firstVoter = voters.First(); - - AddVoter(sb, firstVoter, quest); - - var remainder = voters.Skip(1); - - var remainingPlans = remainder.Where(vc => VoteCounter.PlanNames.Contains(vc) == true); - - foreach (var supporter in remainingPlans.OrderBy(v => v)) - { - AddVoter(sb, supporter, quest); - } - - var remainingVoters = remainder.Except(remainingPlans); - - foreach (var supporter in remainingVoters.OrderBy(v => v)) - { - AddVoter(sb, supporter, quest); - } - } - - /// - /// Add an individual voter to the output. - /// - /// The string builder to add the results to. - /// The name of the voter being added. - /// The quest being tallied. - private void AddVoter(StringBuilder sb, string voter, IQuest quest) - { - sb.Append(GenerateSupporterUrl(quest, voter)); - } - - /// - /// Add an individual voter to the output. - /// - /// The string builder to add the results to. - /// The name of the voter being added. - /// The rank that the voter rated the current vote. - /// The quest being tallied. - private void AddRankedVoter(StringBuilder sb, string voter, string marker, IQuest quest) - { - sb.Append(GenerateSupporterUrl(quest, voter, marker)); - } - - /// - /// Add the the total number of voters to the tally results. - /// - /// The string builder to add the results to. - private void AddTotalVoterCount(StringBuilder sb) - { - int totalVoterCount = VoteCounter.VoterMessageId.Count - VoteCounter.PlanNames.Count; - if (totalVoterCount > 0) - { - sb.AppendLine(""); - sb.AppendFormat("Total No. of Voters: {0}\r\n", totalVoterCount); - } } - - /// - /// Generate a line for a supporter (that's possibly a plan), including the - /// link to the original post that user voted in. - /// - /// The quest being tallied. - /// The supporter of a given plan. - /// Returns a url'ized string for the voter's post. - private string GenerateSupporterUrl(IQuest quest, string supporter) - { - StringBuilder sb = new StringBuilder(); - - string tail = string.Empty; - if (VoteCounter.PlanNames.Contains(supporter)) - { - sb.Append("[b]Plan: "); - tail = "[/b]"; - } - - AddSupporterUrl(sb, supporter, VoteCounter.VoterMessageId, quest); - - sb.AppendLine(tail); - - return sb.ToString(); - } - - /// - /// Generate a line for a voter that ranked a vote with a specific value, including the - /// link to the original post that user voted in. - /// - /// The quest being tallied. - /// The supporter of a given plan. - /// Returns a url'ized string for the voter's post. - private string GenerateSupporterUrl(IQuest quest, string supporter, string marker) - { - StringBuilder sb = new StringBuilder(); - - sb.AppendFormat("[{0}] ", marker); - AddSupporterUrl(sb, supporter, VoteCounter.RankedVoterMessageId, quest); - sb.AppendLine(""); - - return sb.ToString(); - } - - /// - /// Adds a [url] entry to the provided string builder for the supporter, - /// within a given quest. - /// - /// The string builder to add the results to. - /// The supporter of a given plan. - /// The quest being tallied. - private void AddSupporterUrl(StringBuilder sb, string supporter, Dictionary idLookup, IQuest quest) - { - sb.Append("[url=\""); - sb.Append(quest.GetForumAdapter().GetPostUrlFromId(quest.ThreadName, idLookup[supporter])); - sb.Append("\"]"); - sb.Append(supporter); - sb.Append("[/url]"); - } - - /// - /// Add the list of options available for the given ranked task. - /// - /// The string builder to add the results to. - /// - private void AddRankedOptions(StringBuilder sb, string task) - { - var voteContents = VoteCounter.RankedVotesWithSupporters. - Where(v => VoteLine.GetVoteTask(v.Key) == task). - Select(v => VoteLine.GetVoteContent(v.Key)); - - HashSet uniqueOptions = new HashSet(voteContents, StringComparer.OrdinalIgnoreCase); - - sb.AppendLine("[b]Options:[/b]"); - - foreach (var option in uniqueOptions.OrderBy(a => a)) - { - sb.AppendLine(option); - } - - sb.AppendLine(""); - } - - /// - /// Add the winner of the runoff for the given task's options. - /// - /// The string builder to add the results to. - /// The winning choice. - private void AddRankedWinner(StringBuilder sb, string winningChoice) - { - sb.AppendFormat("[b]Winner:[/b] {0}\r\n\r\n", winningChoice); - } - - /// - /// Add the list of voters who voted for the winning vote for the current task. - /// - /// The string builder to add the results to. - /// The quest being tallied. - /// The task and winning vote. - private void AddRankedVoters(StringBuilder sb, IQuest quest, KeyValuePair> result) - { - if (UseSpoilerForVoters) - { - sb.AppendLine("[spoiler=Voters]"); - } - - string winningChoice = result.Value.First(); - - var whoVoted = from v in VoteCounter.RankedVotesWithSupporters - where VoteLine.GetVoteTask(v.Key) == result.Key && - VoteLine.GetVoteContent(v.Key) == winningChoice - select new { marker = VoteLine.GetVoteMarker(v.Key), voters = v.Value }; - - var markerOrder = whoVoted.OrderBy(a => a.marker); - - foreach (var mark in markerOrder) - { - var sortedVoters = mark.voters.OrderBy(a => a); - foreach (var voter in sortedVoters) - { - AddRankedVoter(sb, voter, mark.marker, quest); - } - } - - if (UseSpoilerForVoters) - { - sb.AppendLine("[/spoiler]"); - } - - sb.AppendLine(""); - } - - - /// - /// Add the top two runners-up in the tally. - /// - /// The string builder to add the results to. - /// The list of runners-up, in order. - private void AddRunnersUp(StringBuilder sb, IEnumerable runnersUp) - { - if (runnersUp.Count() > 0) - { - sb.AppendLine("Runners Up:"); - - foreach (var ranker in runnersUp) - { - sb.AppendLine(ranker); - } - - sb.AppendLine(""); - } - } - #endregion - - #region Utility functions for constructing chunks of the output. - private IOrderedEnumerable>>> GroupVotes(Dictionary> votesWithSupporters) - { - var grouped = from v in votesWithSupporters - group v by VoteLine.GetVoteTask(v.Key) into g - orderby g.Key - select g; - - return grouped; - } - - #endregion - - } }