Beispiel #1
0
        public async Task <ActionResult> Play(Game_VM vm, IFormCollection collection)
        {
            Guid vraagId     = new Guid(collection["vraagId"].ToString());
            int  vraagNr     = Int32.Parse(collection["vraagNr"].ToString());
            Game currentgame = await gameRepo.GetGameByIdAsync(vm.GameId);

            Question currentquestion = await questionRepo.GetQuestionByIdAsync(vraagId);

            // controleren of het antwoord juist was
            bool correct = true;
            //controleren of ze wel iets hebben aangeduid
            bool filled = false;

            foreach (var input in vm.Options)
            {
                if (input.Value == true)
                {
                    filled = true;
                }
            }
            if (!filled)
            {
                vm = convertGame(currentgame.GameId, currentquestion, vm.questionNr);
                ViewBag.questionNr = vraagNr;
                ViewBag.questionId = vraagId;
                ModelState.AddModelError("", "Je moet een optie kiezen");
                return(View(vm));
            }
            List <Option> histoptions = new List <Option>();

            foreach (Option option in currentquestion.PossibleOptions)
            {
                //var guessedval = vm.Options[option.OptionDescription];
                //var correctval = option.CorrectAnswer;
                if (vm.Options[option.OptionDescription] != option.CorrectAnswer)
                {
                    correct = false;
                }
                //ondertussen options opslaan voor bij te houden voor de uitslag
                Option histoption = new Option()
                {
                    OptionDescription = option.OptionDescription,
                    CorrectAnswer     = vm.Options[option.OptionDescription]
                };
                histoptions.Add(histoption);
            }
            if (correct)
            {
                currentgame.CorrectAnswers += 1;
                if (await gameRepo.Update(currentgame) == null)
                {
                    return(Redirect("/Error/0"));
                }
            }

            //question opslaan voor later als bij uitslag weer te geven
            Question histquestion = new Question()
            {
                PossibleOptions = histoptions
            };

            if (await questionRepo.Create(histquestion) != null)
            {
                if (await gameRepo.AddQuestionToGameAsync(vm.GameId, histquestion.QuestionId, currentquestion.QuestionId) == null)
                {
                    return(Redirect("/Error/0"));
                }
            }

            //getting next question
            var allquestions = await quizRepo.GetQuizQuestionsAsync(currentgame.QuizId);

            bool     save         = false;
            Question nextQuestion = new Question();

            foreach (Question question in allquestions)
            {
                if (question.Description == currentquestion.Description)
                {
                    save = true;
                }
                else if (save)
                {
                    nextQuestion = question;
                    save         = false;
                    break;
                }
            }
            //als save nog aanstaat betekend dat de we aan de laatste vraag zitten.
            if (save)
            {
                //finish the quiz
                QuizClass currentQuiz = await quizRepo.GetQuizByIdAsync(currentgame.QuizId);

                DateTime finishedtime = DateTime.Now;
                TimeSpan completetime = finishedtime.Subtract(currentgame.TimeStarted);
                //vm voor uitslag pagina opvullen
                var gameresults = await gameRepo.GetGameResults(vm.GameId);

                Finished_VM finished_vm = new Finished_VM()
                {
                    QuizName        = currentQuiz.Name,
                    QuizDescription = currentQuiz.Description,
                    userscore       = currentgame.CorrectAnswers,
                    maxscore        = allquestions.Count(),
                    completetime    = finishedtime.Subtract(currentgame.TimeStarted),
                    gameresults     = gameresults
                };
                currentgame.TimeFinished = finishedtime;
                if (await gameRepo.Update(currentgame) == null)
                {
                    return(Redirect("/Error/0"));
                }
                return(View("Finished", finished_vm));
            }
            vm = convertGame(currentgame.GameId, nextQuestion, vm.questionNr + 1);

            ViewBag.questionNr = vm.questionNr;
            ViewBag.questionId = nextQuestion.QuestionId;
            return(View("Play", vm));
        }