public IActionResult ShowStatistics(int quizId, string responses, int currentQuestion, string response) { int correctAnswers = 0; Quiz quiz = _quizRepository.GetQuizById(quizId); List <string> tmpResponses = QuizViewModel.ResponsesFromJson(responses); tmpResponses[currentQuestion] = response; for (int i = 0; i < quiz.Questions.Count(); i++) { if (quiz.Questions[i].CorrectAnswer.Equals(tmpResponses[i])) { correctAnswers++; } } ViewBag.CorrectAnswers = correctAnswers; ViewBag.Total = quiz.Questions.Count(); ViewBag.Accurate = Math.Round((double)(TOTAL_PERCENT * correctAnswers / quiz.Questions.Count()), PRECISION); return(View()); }
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)); } }