private Quiz CreateQuizModel(JsonQuiz jsonQuiz) { List <Question> questions = new List <Question>(); foreach (JsonQuestion jsonQuestion in jsonQuiz.Questions) { Question question = CreateQuestionModel(jsonQuestion); questions.Add(question); } return(new Quiz() { Questions = questions }); }
public IActionResult PlayGame(GameOptionsViewModel gameOptions, bool newGame, int currentQuestion, int quizId, string response, string move, string responses) { if (newGame) { Difficulty difficulty; Models.Type type; CreatingQuizFromOptions(gameOptions, out difficulty, out type); string apiUrl = WebService.CreateApiUrl(gameOptions.Count, difficulty, type, System.Int32.Parse(gameOptions.SelectedCategory)); JsonQuiz jsonQuiz = WebService.GetQuizFromUrl(apiUrl); if (jsonQuiz == null) { //return ErrorPageView(); NOT IMPLEMENTED YET return(NotFound()); } if (jsonQuiz.ResponseCode != 0) { return(RedirectToAction("NewGame")); } Quiz quiz = CreateQuizModel(jsonQuiz); _quizRepository.InsertQuiz(quiz); Question question = quiz.Questions[0]; QuizViewModel quizViewModel = new QuizViewModel() { QuizId = quiz.QuizId, CurrentQuestion = 0, QuestionStatement = question.QuestionStatement, Answers = SortAllAnswers(question.IncorrectAnswers, question.CorrectAnswer), Responses = CreateEmptyList(quiz.Questions.Count()), Category = question.Category1, Difficulty = question.Difficulty.DifficultyName }; return(View(quizViewModel)); } else { Quiz quiz = _quizRepository.GetQuizById(quizId); List <string> tmpResponses = QuizViewModel.ResponsesFromJson(responses); tmpResponses[currentQuestion] = response; if (move == "next") { if (currentQuestion < quiz.Questions.Count() - 1) { currentQuestion++; } } if (move == "previous") { if (currentQuestion > 0) { currentQuestion--; } } Question question = quiz.Questions[currentQuestion]; QuizViewModel quizViewModel = new QuizViewModel() { QuizId = quiz.QuizId, CurrentQuestion = currentQuestion, QuestionStatement = question.QuestionStatement, Answers = SortAllAnswers(question.IncorrectAnswers, question.CorrectAnswer), Responses = tmpResponses, Category = question.Category1, Difficulty = question.Difficulty.DifficultyName }; return(View(quizViewModel)); } }