コード例 #1
0
        private async Task StartGame()
        {
            while (!ShouldStopGame)
            {
                // reset the cancellation source
                triviaCancelSource = new CancellationTokenSource();
                var token = triviaCancelSource.Token;
                // load question
                CurrentQuestion = TriviaQuestionPool.Instance.GetRandomQuestion(oldQuestions);
                if (CurrentQuestion == null)
                {
                    await channel.SendMessage($":exclamation: Failed loading a trivia question").ConfigureAwait(false);
                    await End().ConfigureAwait(false);

                    return;
                }
                oldQuestions.Add(CurrentQuestion); //add it to exclusion list so it doesn't show up again
                                                   //sendquestion
                await channel.SendMessage($":question: **{CurrentQuestion.Question}**").ConfigureAwait(false);

                //receive messages
                WizBot.Client.MessageReceived += PotentialGuess;

                //allow people to guess
                GameActive = true;

                try
                {
                    //hint
                    await Task.Delay(HintTimeoutMiliseconds, token).ConfigureAwait(false);

                    if (ShowHints)
                    {
                        await channel.SendMessage($":exclamation:**Hint:** {CurrentQuestion.GetHint()}").ConfigureAwait(false);
                    }

                    //timeout
                    await Task.Delay(QuestionDurationMiliseconds - HintTimeoutMiliseconds, token).ConfigureAwait(false);
                }
                catch (TaskCanceledException)
                {
                    Console.WriteLine("Trivia cancelled");
                }
                GameActive = false;
                if (!triviaCancelSource.IsCancellationRequested)
                {
                    await channel.Send($":clock2: :question: **Time's up!** The correct answer was **{CurrentQuestion.Answer}**").ConfigureAwait(false);
                }
                WizBot.Client.MessageReceived -= PotentialGuess;
                // load next question if game is still running
                await Task.Delay(2000).ConfigureAwait(false);
            }
            await End().ConfigureAwait(false);
        }
コード例 #2
0
ファイル: TriviaGame.cs プロジェクト: rumlefisk/NadekoBot
        private async Task StartGame()
        {
            while (!ShouldStopGame)
            {
                // reset the cancellation source
                triviaCancelSource = new CancellationTokenSource();
                var token = triviaCancelSource.Token;
                // load question
                CurrentQuestion = TriviaQuestionPool.Instance.GetRandomQuestion(oldQuestions);
                if (CurrentQuestion == null)
                {
                    try { await channel.SendErrorAsync($":exclamation: Failed loading a trivia question.").ConfigureAwait(false); } catch (Exception ex) { _log.Warn(ex); }
                    await End().ConfigureAwait(false);

                    return;
                }
                oldQuestions.Add(CurrentQuestion); //add it to exclusion list so it doesn't show up again
                                                   //sendquestion
                try { await channel.SendConfirmAsync($":question: Question", $"**{CurrentQuestion.Question}**").ConfigureAwait(false); }
                catch (HttpException ex) when(ex.StatusCode == System.Net.HttpStatusCode.NotFound || ex.StatusCode == System.Net.HttpStatusCode.Forbidden)
                {
                    break;
                }
                catch (Exception ex) { _log.Warn(ex); }

                //receive messages
                NadekoBot.Client.MessageReceived += PotentialGuess;

                //allow people to guess
                GameActive = true;

                try
                {
                    //hint
                    await Task.Delay(HintTimeoutMiliseconds, token).ConfigureAwait(false);

                    if (ShowHints)
                    {
                        try { await channel.SendConfirmAsync($":exclamation: Hint", CurrentQuestion.GetHint()).ConfigureAwait(false); }
                        catch (HttpException ex) when(ex.StatusCode == System.Net.HttpStatusCode.NotFound || ex.StatusCode == System.Net.HttpStatusCode.Forbidden)
                        {
                            break;
                        }
                    }
                    catch (Exception ex) { _log.Warn(ex); }

                    //timeout
                    await Task.Delay(QuestionDurationMiliseconds - HintTimeoutMiliseconds, token).ConfigureAwait(false);
                }
コード例 #3
0
        public async Task StartGame()
        {
            while (!ShouldStopGame)
            {
                // reset the cancellation source
                triviaCancelSource = new CancellationTokenSource();

                // load question
                CurrentQuestion = TriviaQuestionPool.Instance.GetRandomQuestion(oldQuestions);
                if (CurrentQuestion == null ||
                    string.IsNullOrWhiteSpace(CurrentQuestion.Answer) ||
                    string.IsNullOrWhiteSpace(CurrentQuestion.Question))
                {
                    await channel.SendErrorAsync("Trivia Game", "Failed loading a question.").ConfigureAwait(false);

                    return;
                }
                oldQuestions.Add(CurrentQuestion); //add it to exclusion list so it doesn't show up again

                EmbedBuilder questionEmbed;
                IUserMessage questionMessage;
                try
                {
                    questionEmbed = new EmbedBuilder().WithOkColor()
                                    .WithTitle("Trivia Game")
                                    .AddField(eab => eab.WithName("Category").WithValue(CurrentQuestion.Category))
                                    .AddField(eab => eab.WithName("Question").WithValue(CurrentQuestion.Question));

                    questionMessage = await channel.EmbedAsync(questionEmbed).ConfigureAwait(false);
                }
                catch (HttpException ex) when(ex.HttpCode == System.Net.HttpStatusCode.NotFound ||
                                              ex.HttpCode == System.Net.HttpStatusCode.Forbidden ||
                                              ex.HttpCode == System.Net.HttpStatusCode.BadRequest)
                {
                    return;
                }
                catch (Exception ex)
                {
                    _log.Warn(ex);
                    await Task.Delay(2000).ConfigureAwait(false);

                    continue;
                }

                //receive messages
                try
                {
                    NadekoBot.Client.MessageReceived += PotentialGuess;

                    //allow people to guess
                    GameActive = true;
                    try
                    {
                        //hint
                        await Task.Delay(HintTimeoutMiliseconds, triviaCancelSource.Token).ConfigureAwait(false);

                        if (ShowHints)
                        {
                            try
                            {
                                await questionMessage.ModifyAsync(m => m.Embed = questionEmbed.WithFooter(efb => efb.WithText(CurrentQuestion.GetHint())).Build())
                                .ConfigureAwait(false);
                            }
                            catch (HttpException ex) when(ex.HttpCode == System.Net.HttpStatusCode.NotFound || ex.HttpCode == System.Net.HttpStatusCode.Forbidden)
                            {
                                break;
                            }
                        }
                        catch (Exception ex) { _log.Warn(ex); }

                        //timeout
                        await Task.Delay(QuestionDurationMiliseconds - HintTimeoutMiliseconds, triviaCancelSource.Token).ConfigureAwait(false);
                    }
                    catch (TaskCanceledException) { } //means someone guessed the answer
                }
コード例 #4
0
        public async Task StartGame()
        {
            while (!ShouldStopGame)
            {
                // reset the cancellation source
                _triviaCancelSource = new CancellationTokenSource();

                // load question
                CurrentQuestion = _questionPool.GetRandomQuestion(OldQuestions, _options.IsPokemon);
                if (string.IsNullOrWhiteSpace(CurrentQuestion?.Answer) || string.IsNullOrWhiteSpace(CurrentQuestion.Question))
                {
                    await Channel.SendErrorAsync(GetText("trivia_game"), GetText("failed_loading_question")).ConfigureAwait(false);

                    return;
                }
                OldQuestions.Add(CurrentQuestion); //add it to exclusion list so it doesn't show up again

                EmbedBuilder questionEmbed;
                IUserMessage questionMessage;
                try
                {
                    questionEmbed = new EmbedBuilder().WithOkColor()
                                    .WithTitle(GetText("trivia_game"))
                                    .AddField(eab => eab.WithName(GetText("category")).WithValue(CurrentQuestion.Category))
                                    .AddField(eab => eab.WithName(GetText("question")).WithValue(CurrentQuestion.Question));
                    if (Uri.IsWellFormedUriString(CurrentQuestion.ImageUrl, UriKind.Absolute))
                    {
                        questionEmbed.WithImageUrl(CurrentQuestion.ImageUrl);
                    }

                    questionMessage = await Channel.EmbedAsync(questionEmbed).ConfigureAwait(false);
                }
                catch (HttpException ex) when(ex.HttpCode == System.Net.HttpStatusCode.NotFound ||
                                              ex.HttpCode == System.Net.HttpStatusCode.Forbidden ||
                                              ex.HttpCode == System.Net.HttpStatusCode.BadRequest)
                {
                    return;
                }
                catch (Exception ex)
                {
                    _log.Warn(ex);
                    await Task.Delay(2000).ConfigureAwait(false);

                    continue;
                }

                //receive messages
                try
                {
                    _client.MessageReceived += PotentialGuess;

                    //allow people to guess
                    GameActive = true;
                    try
                    {
                        //hint
                        await Task.Delay(_options.QuestionTimer * 1000 / 2, _triviaCancelSource.Token).ConfigureAwait(false);

                        if (!_options.NoHint)
                        {
                            try
                            {
                                await questionMessage.ModifyAsync(m => m.Embed = questionEmbed.WithFooter(efb => efb.WithText(CurrentQuestion.GetHint())).Build())
                                .ConfigureAwait(false);
                            }
                            catch (HttpException ex) when(ex.HttpCode == System.Net.HttpStatusCode.NotFound || ex.HttpCode == System.Net.HttpStatusCode.Forbidden)
                            {
                                break;
                            }
                        }
                        catch (Exception ex) { _log.Warn(ex); }

                        //timeout
                        await Task.Delay(_options.QuestionTimer * 1000 / 2, _triviaCancelSource.Token).ConfigureAwait(false);
                    }
                    catch (TaskCanceledException) { _timeoutCount = 0; } //means someone guessed the answer
                }