Esempio n. 1
0
 private static OTDBQuestion CleanQuestion(OTDBQuestion x)
 {
     return(new OTDBQuestion
     {
         Category = MonkeyHelpers.CleanHtmlString(x.Category),
         Question = MonkeyHelpers.CleanHtmlString(x.Question),
         CorrectAnswer = MonkeyHelpers.CleanHtmlString(x.CorrectAnswer),
         IncorrectAnswers = x.IncorrectAnswers.Select(MonkeyHelpers.CleanHtmlString).ToList(),
         Type = x.Type,
         Difficulty = x.Difficulty
     });
 }
Esempio n. 2
0
        private async Task GetNextQuestionAsync()
        {
            if (status == TriviaStatus.Stopped)
            {
                return;
            }
            if (currentQuestionMessage != null)
            {
                interactiveService.RemoveReactionCallback(currentQuestionMessage);
                int points = QuestionToPoints(currentQuestion);

                var embedBuilder = new EmbedBuilder()
                                   .WithColor(new Color(46, 191, 84))
                                   .WithTitle("Time is up")
                                   .WithDescription($"The correct answer was: **{ currentQuestion.CorrectAnswer}**");

                string msg = "";
                if (correctAnswerUsers.Count > 0)
                {
                    correctAnswerUsers.ForEach(async usr => await AddPointsToUserAsync(usr, points).ConfigureAwait(false));
                    msg = $"*{string.Join(", ", correctAnswerUsers.Select(u => u.Username))}* had it right! Here, have {points} point{(points == 1 ? "" : "s")}.";
                }
                else
                {
                    msg = "No one had it right";
                }
                embedBuilder.AddField("Correct answers", msg, true);
                if (wrongAnswerUsers.Count > 0)
                {
                    wrongAnswerUsers.ForEach(async usr => await AddPointsToUserAsync(usr, -1).ConfigureAwait(false));
                    msg = $"*{string.Join(", ", wrongAnswerUsers.Select(u => u.Username))}* had it wrong! You lose 1 point.";
                }
                else
                {
                    msg = "No one had it wrong.";
                }
                embedBuilder.AddField("Incorrect answers", msg, true);

                var highScores = GetCurrentHighScores(3);
                if (!highScores.IsEmptyOrWhiteSpace())
                {
                    embedBuilder.AddField("Top 3:", highScores, true);
                }

                await MonkeyHelpers.SendChannelMessageAsync(discordClient, guildID, channelID, "", embed : embedBuilder.Build()).ConfigureAwait(false);

                correctAnswerUsers.Clear();
                wrongAnswerUsers.Clear();
            }
            if (currentIndex < questionsToPlay)
            {
                if (currentIndex >= questions.Count)                    // we want to play more questions than available
                {
                    await LoadQuestionsAsync(10).ConfigureAwait(false); // load more questions
                }
                currentQuestion = questions.ElementAt(currentIndex);
                var builder = new EmbedBuilder
                {
                    Color = new Color(26, 137, 185),
                    Title = $"Question {currentIndex + 1}"
                };
                int points = QuestionToPoints(currentQuestion);
                builder.Description = $"{currentQuestion.Category} - {currentQuestion.Difficulty} : {points} point{(points == 1 ? "" : "s")}";
                if (currentQuestion.Type == TriviaQuestionType.TrueFalse)
                {
                    builder.AddField($"{currentQuestion.Question}", "True or false?");
                    var trueEmoji          = new Emoji("👍");
                    var falseEmoji         = new Emoji("👎");
                    var correctAnswerEmoji = currentQuestion.CorrectAnswer.Equals("true", StringComparison.OrdinalIgnoreCase) ? trueEmoji : falseEmoji;

                    currentQuestionMessage = await interactiveService.SendMessageWithReactionCallbacksAsync(commandContext,
                                                                                                            new ReactionCallbackData("", builder.Build(), false, true, true, timeout, _ => GetNextQuestionAsync())
                                                                                                            .WithCallback(trueEmoji, (c, r) => CheckAnswer(r, correctAnswerEmoji))
                                                                                                            .WithCallback(falseEmoji, (c, r) => CheckAnswer(r, correctAnswerEmoji)),
                                                                                                            false
                                                                                                            ).ConfigureAwait(false);
                }
                else if (currentQuestion.Type == TriviaQuestionType.MultipleChoice)
                {
                    // add the correct answer to the list of correct answers to form the list of possible answers
                    var    answers = currentQuestion.IncorrectAnswers.Append(currentQuestion.CorrectAnswer);
                    Random rand    = new Random();
                    // randomize the order of the answers
                    var randomizedAnswers  = answers.OrderBy(_ => rand.Next()).ToList();
                    var correctAnswerEmoji = new Emoji(MonkeyHelpers.GetUnicodeRegionalLetter(randomizedAnswers.IndexOf(currentQuestion.CorrectAnswer)));
                    builder.AddField($"{currentQuestion.Question}", string.Join(Environment.NewLine, randomizedAnswers.Select((s, i) => $"{MonkeyHelpers.GetUnicodeRegionalLetter(i)} {s}")));

                    currentQuestionMessage = await interactiveService.SendMessageWithReactionCallbacksAsync(commandContext,
                                                                                                            new ReactionCallbackData("", builder.Build(), false, true, true, timeout, _ => GetNextQuestionAsync())
                                                                                                            .WithCallback(new Emoji(MonkeyHelpers.GetUnicodeRegionalLetter(0)), (c, r) => CheckAnswer(r, correctAnswerEmoji))
                                                                                                            .WithCallback(new Emoji(MonkeyHelpers.GetUnicodeRegionalLetter(1)), (c, r) => CheckAnswer(r, correctAnswerEmoji))
                                                                                                            .WithCallback(new Emoji(MonkeyHelpers.GetUnicodeRegionalLetter(2)), (c, r) => CheckAnswer(r, correctAnswerEmoji))
                                                                                                            .WithCallback(new Emoji(MonkeyHelpers.GetUnicodeRegionalLetter(3)), (c, r) => CheckAnswer(r, correctAnswerEmoji))
                                                                                                            , false).ConfigureAwait(false);
                }
                currentIndex++;
            }
            else
            {
                await StopTriviaAsync().ConfigureAwait(false);
            }
        }