protected bool FlagsToQuoteFilter(CommandMatch cmd, out QuoteRating requestedRating, out bool addMyRating) { int anyCount = cmd.Options.Count(o => o.Key == "--any"); int badCount = cmd.Options.Count(o => o.Key == "--bad"); int addRatingCount = cmd.Options.Count(o => o.Key == "-r"); if (anyCount + badCount == 0) { requestedRating = QuoteRating.High; } else if (anyCount + badCount > 1) { // too many "--any"s and/or "--bad"s requestedRating = 0; addMyRating = false; return(false); } else if (anyCount == 1) { requestedRating = QuoteRating.Any; } else { Debug.Assert(badCount == 1); requestedRating = QuoteRating.Low; } if (addRatingCount == 0) { addMyRating = false; } else if (addRatingCount == 1) { addMyRating = true; } else { // too many "-r"s requestedRating = 0; addMyRating = false; return(false); } return(true); }
/// <summary> /// Posts a random quote. /// </summary> /// <param name="requestor">The nickname of the person who requested this quote.</param> /// <param name="location">The channel (for channel messages) or nickname (for private messages) in which the /// request has been placed.</param> /// <param name="quotes">The Queryable of quotes.</param> /// <param name="votes">The Queryable of votes.</param> /// <param name="requestedRating">Whether to display all quotes, quotes rated equal to or above a certain threshold, or quotes rated below this threshold.</param> /// <param name="addMyRating">If <c>true</c>, shows how the requestor voted on this quote.</param> /// <param name="postReply">Action to invoke to post a reply.</param> protected virtual void PostRandomQuote(string requestor, string location, IQueryable <Quote> quotesWithVotes, QuoteRating requestedRating, bool addMyRating, Action <string> postReply) { var filteredQuotes = GetFilteredQuotes(quotesWithVotes, requestedRating); int quoteCount = filteredQuotes.Count(); if (quoteCount > 0) { int index = Randomizer.Next(quoteCount); var quote = filteredQuotes .OrderBy(q => q.ID) .Skip(index) .FirstOrDefault(); PostQuote(quote, requestor, location, addMyRating, postReply); } else { postReply(string.Format("Sorry, {0}, I don't have any matching quotes.", requestor)); } }
protected virtual IQueryable <Quote> GetFilteredQuotes(IQueryable <Quote> quotesWithVotes, QuoteRating requestedRating) { switch (requestedRating) { case QuoteRating.Low: return(quotesWithVotes.Where(q => (q.Votes.Any() ? q.Votes.Sum(v => v.Points) : 0) < Config.VoteThreshold)); case QuoteRating.Any: return(quotesWithVotes); case QuoteRating.High: return(quotesWithVotes.Where(q => (q.Votes.Any() ? q.Votes.Sum(v => v.Points) : 0) >= Config.VoteThreshold)); default: throw new ArgumentOutOfRangeException(nameof(requestedRating)); } }