Beispiel #1
0
        public static async Task <RootObject> GetQuizzesFromExternal(OpentdbParams opentdbParams)
        {
            string uriString  = "https://opentdb.com/api.php?amount=" + opentdbParams.Amount + opentdbParams.Category + opentdbParams.Difficulty + "&type=multiple";
            Uri    opentdbUri = new Uri(uriString);

            var result = await httpClient.GetAsync(opentdbUri);

            string json = "";

            if (result.IsSuccessStatusCode)
            {
                json = await result.Content.ReadAsStringAsync();
            }
            RootObject rootObject = JsonConvert.DeserializeObject <RootObject>(json);

            return(rootObject);
        }
        public async void ClickedNext()
        {
            OpentdbParams opentdbParams = new OpentdbParams();

            if (SelectedCategoryIndex > 0)
            {
                opentdbParams.Category = "&category=" + (SelectedCategoryIndex + 8);
            }
            else
            {
                opentdbParams.Category = "";
            }

            if (SelectedDifficulty == null || SelectedDifficulty.Equals("All difficulties", StringComparison.Ordinal))
            {
                opentdbParams.Difficulty = "";
            }
            else
            {
                opentdbParams.Difficulty = "&difficulty=" + SelectedDifficulty.ToLower(new CultureInfo("en-Us", false));
            }

            if (SelectedAmount == 0)
            {
                opentdbParams.Amount = "&amount=3";
            }
            else
            {
                opentdbParams.Amount = "&amount=" + selectedAmount;
            }

            RootObject rootObject = await ExternalRequest.GetQuizzesFromExternal(opentdbParams).ConfigureAwait(true);


            if (rootObject == null)
            {
                DisplayErrorMessageAsync("Didn't find enough quizzes to match your request!");
                return;
            }

            Quiz quiz = new Quiz();

            quiz.QuizName     = SelectedCategoryItem + " Quiz";
            quiz.QuizCategory = SelectedCategoryItem;
            foreach (var opentdbQuestion in rootObject.results)
            {
                Question question = new Question();
                question.QuestionText        = System.Web.HttpUtility.HtmlDecode(opentdbQuestion.question);
                question.CorrectAnswerNumber = 1;
                question.AnswersList.Add(new Answer(System.Web.HttpUtility.HtmlDecode(opentdbQuestion.correct_answer), question, 1, 0));
                int i = 2;
                foreach (var answer in opentdbQuestion.incorrect_answers)
                {
                    question.AnswersList.Add(new Answer(System.Web.HttpUtility.HtmlDecode(answer), question, i++, 0));
                }
                quiz.QuestionList.Add(question);
            }
            QuizCompletionParams q = new QuizCompletionParams();

            q.Quiz             = quiz;
            q.QuestionToHandle = 1;

            if (quiz.QuestionList.Count == 0)
            {
                DisplayErrorMessageAsync("Didn't find enough quizzes to match your request!");
                return;
            }
            foreach (var question in quiz.QuestionList)
            {
                ScrambleAnswers(question);
            }

            NavigationService.Navigate(typeof(TakeQuiz), q);
        }