Example #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("null").ConfigureAwait(false);
                    await End().ConfigureAwait(false);

                    return;
                }
                //add current question to the exclusion list
                OldQuestions.Add(CurrentQuestion);
                await Channel.SendMessage(CurrentQuestion.ToString());

                //add PotentialGuess to OnMessageReceived
                Client.OnMessageRecieved += PotentialGuess;
                //allow people to guess
                GameActive = true;

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

                    if (ShowHints)
                    {
                        await Channel.SendMessage("hint");
                    }
                    await
                    Task.Delay(QuestionDurationMilliseconds - HintTimeoutMilliseconds, token).ConfigureAwait(false);
                }
                catch (TaskCanceledException) { }
                GameActive = false;
                if (!TriviaCancelSource.IsCancellationRequested)
                {
                    await Channel.SendMessage("correct answer was : asdf").ConfigureAwait(false);
                }
            }
        }
Example #2
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
                }