protected override async Task PerformInternal(CommandParametersModel parameters)
        {
            this.runParameters = parameters;

            bool useCustomQuestion = (RandomHelper.GenerateProbability() >= 50);

            if (this.CustomQuestions.Count > 0 && !this.UseRandomOnlineQuestions)
            {
                useCustomQuestion = true;
            }
            else if (this.CustomQuestions.Count == 0 && this.UseRandomOnlineQuestions)
            {
                useCustomQuestion = false;
            }

            if (!useCustomQuestion)
            {
                using (AdvancedHttpClient client = new AdvancedHttpClient())
                {
                    OpenTDBResults openTDBResults = await client.GetAsync <OpenTDBResults>(OpenTDBUrl);

                    if (openTDBResults != null && openTDBResults.results != null && openTDBResults.results.Count > 0)
                    {
                        List <string> answers = new List <string>();
                        answers.Add(HttpUtility.HtmlDecode(openTDBResults.results[0].correct_answer));
                        answers.AddRange(openTDBResults.results[0].incorrect_answers.Select(a => HttpUtility.HtmlDecode(a)));
                        this.question = new TriviaGameQuestionModel()
                        {
                            Question = HttpUtility.HtmlDecode(openTDBResults.results[0].question),
                            Answers  = answers,
                        };
                    }
                }
            }

            if (this.CustomQuestions.Count > 0 && (useCustomQuestion || this.question == null))
            {
                this.question = this.CustomQuestions.Random();
            }

            if (this.question == null)
            {
                this.question = FallbackQuestion;
            }

            int i = 1;

            foreach (string answer in this.question.Answers.Shuffle())
            {
                this.numbersToAnswers[i] = answer;
                i++;
            }

            int correctAnswerNumber = 0;

            foreach (var kvp in this.numbersToAnswers)
            {
                if (kvp.Value.Equals(this.question.CorrectAnswer))
                {
                    correctAnswerNumber = kvp.Key;
                    break;
                }
            }

            this.runParameters.SpecialIdentifiers[TriviaGameCommandModel.GameTriviaQuestionSpecialIdentifier] = this.question.Question;
            foreach (var kvp in this.numbersToAnswers)
            {
                this.runParameters.SpecialIdentifiers[TriviaGameCommandModel.GameTriviaSpecificAnswerHeaderSpecialIdentifier + kvp.Key] = kvp.Value;
            }
            this.runParameters.SpecialIdentifiers[TriviaGameCommandModel.GameTriviaAnswersSpecialIdentifier]       = string.Join(", ", this.numbersToAnswers.OrderBy(a => a.Key).Select(a => $"{a.Key}) {a.Value}"));
            this.runParameters.SpecialIdentifiers[TriviaGameCommandModel.GameTriviaCorrectAnswerSpecialIdentifier] = this.question.CorrectAnswer;

            GlobalEvents.OnChatMessageReceived += GlobalEvents_OnChatMessageReceived;

            await this.StartedCommand.Perform(this.runParameters);

            await Task.Delay(this.TimeLimit * 1000);

            GlobalEvents.OnChatMessageReceived -= GlobalEvents_OnChatMessageReceived;

            List <CommandParametersModel> winners = new List <CommandParametersModel>();

            foreach (var kvp in this.runUserSelections.ToList())
            {
                CommandParametersModel participant = this.runUsers[kvp.Key];
                if (kvp.Value == correctAnswerNumber)
                {
                    winners.Add(participant);
                    this.PerformPrimarySetPayout(participant.User, this.WinAmount);
                    participant.SpecialIdentifiers[GameCommandModelBase.GamePayoutSpecialIdentifier] = this.WinAmount.ToString();
                    await this.UserSuccessCommand.Perform(participant);
                }
                else
                {
                    await this.UserFailureCommand.Perform(participant);
                }
            }

            this.runParameters.SpecialIdentifiers[GameCommandModelBase.GameWinnersCountSpecialIdentifier] = winners.Count.ToString();
            this.runParameters.SpecialIdentifiers[GameCommandModelBase.GameWinnersSpecialIdentifier]      = string.Join(", ", winners.Select(u => "@" + u.User.Username));
            this.runParameters.SpecialIdentifiers[GameCommandModelBase.GamePayoutSpecialIdentifier]       = this.WinAmount.ToString();
            this.runParameters.SpecialIdentifiers[GameCommandModelBase.GameAllPayoutSpecialIdentifier]    = (this.WinAmount * winners.Count).ToString();
            await this.CorrectAnswerCommand.Perform(this.runParameters);

            await this.PerformCooldown(this.runParameters);

            this.ClearData();
        }
Example #2
0
        public override async Task CustomRun(CommandParametersModel parameters)
        {
            this.runParameters = parameters;

            bool useCustomQuestion = (RandomHelper.GenerateProbability() >= 50);

            if (this.CustomQuestions.Count > 0 && !this.UseRandomOnlineQuestions)
            {
                useCustomQuestion = true;
            }
            else if (this.CustomQuestions.Count == 0 && this.UseRandomOnlineQuestions)
            {
                useCustomQuestion = false;
            }

            if (!useCustomQuestion)
            {
                using (AdvancedHttpClient client = new AdvancedHttpClient())
                {
                    OpenTDBResults openTDBResults = await client.GetAsync <OpenTDBResults>(OpenTDBUrl);

                    if (openTDBResults != null && openTDBResults.results != null && openTDBResults.results.Count > 0)
                    {
                        List <string> answers = new List <string>();
                        answers.Add(HttpUtility.HtmlDecode(openTDBResults.results[0].correct_answer));
                        answers.AddRange(openTDBResults.results[0].incorrect_answers.Select(a => HttpUtility.HtmlDecode(a)));
                        this.question = new TriviaGameQuestionModel()
                        {
                            Question = HttpUtility.HtmlDecode(openTDBResults.results[0].question),
                            Answers  = answers,
                        };
                    }
                }
            }

            if (this.CustomQuestions.Count > 0 && (useCustomQuestion || this.question == null))
            {
                this.question = this.CustomQuestions.Random();
            }

            if (this.question == null)
            {
                this.question = FallbackQuestion;
            }

            int i = 1;

            foreach (string answer in this.question.Answers.Shuffle())
            {
                this.numbersToAnswers[i] = answer;
                i++;
            }

            int correctAnswerNumber = 0;

            foreach (var kvp in this.numbersToAnswers)
            {
                if (kvp.Value.Equals(this.question.CorrectAnswer))
                {
                    correctAnswerNumber = kvp.Key;
                    break;
                }
            }

            this.runParameters.SpecialIdentifiers[TriviaGameCommandModel.GameTriviaQuestionSpecialIdentifier] = this.question.Question;
            foreach (var kvp in this.numbersToAnswers)
            {
                this.runParameters.SpecialIdentifiers[TriviaGameCommandModel.GameTriviaSpecificAnswerHeaderSpecialIdentifier + kvp.Key] = kvp.Value;
            }
            this.runParameters.SpecialIdentifiers[TriviaGameCommandModel.GameTriviaAnswersSpecialIdentifier]       = string.Join(", ", this.numbersToAnswers.OrderBy(a => a.Key).Select(a => $"{a.Key}) {a.Value}"));
            this.runParameters.SpecialIdentifiers[TriviaGameCommandModel.GameTriviaCorrectAnswerSpecialIdentifier] = this.question.CorrectAnswer;

#pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
            AsyncRunner.RunAsyncBackground(async(cancellationToken) =>
            {
                GlobalEvents.OnChatMessageReceived += GlobalEvents_OnChatMessageReceived;

                await this.DelayNoThrow(this.TimeLimit * 1000, cancellationToken);

                GlobalEvents.OnChatMessageReceived -= GlobalEvents_OnChatMessageReceived;

                List <CommandParametersModel> winners = new List <CommandParametersModel>();
                foreach (var kvp in this.runUserSelections.ToList())
                {
                    CommandParametersModel participant = this.runUsers[kvp.Key];
                    if (kvp.Value == correctAnswerNumber)
                    {
                        winners.Add(participant);
                        this.PerformPrimarySetPayout(participant.User, this.WinAmount);
                        participant.SpecialIdentifiers[GameCommandModelBase.GamePayoutSpecialIdentifier] = this.WinAmount.ToString();
                        await this.RunSubCommand(this.UserSuccessCommand, participant);
                    }
                    else
                    {
                        await this.RunSubCommand(this.UserFailureCommand, participant);
                    }
                }

                this.SetGameWinners(this.runParameters, winners);
                this.runParameters.SpecialIdentifiers[GameCommandModelBase.GamePayoutSpecialIdentifier]    = this.WinAmount.ToString();
                this.runParameters.SpecialIdentifiers[GameCommandModelBase.GameAllPayoutSpecialIdentifier] = (this.WinAmount * winners.Count).ToString();
                await this.RunSubCommand(this.CorrectAnswerCommand, this.runParameters);

                await this.PerformCooldown(this.runParameters);
                this.ClearData();
            }, new CancellationToken());
#pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed

            await this.RunSubCommand(this.StartedCommand, this.runParameters);
        }