Exemplo n.º 1
0
        public async Task <ActionResult> StartGame(Guid?id)
        {
            if (id == null)
            {
                return(Redirect("/Error/400"));
            }
            string userId = User.FindFirstValue(ClaimTypes.NameIdentifier);
            //alle history van gespeelde games verwijderen
            await gameRepo.ClearUserGameHistory(userId);

            //alle oude foutief afgesloten quizzen verwijderen
            await gameRepo.RemoveUsersActiveGames(userId);

            //quiz starten
            var q = await quizRepo.GetQuizQuestionsAsync(id ?? Guid.Empty);

            List <Question> questions = q.Cast <Question>().ToList();

            if (questions.Count() == 0)
            {
                return(Redirect("/Error/404"));
            }
            Game game = new Game()
            {
                QuizId = id ?? Guid.Empty,
                UserId = userId,
            };

            if (await gameRepo.Create(game) == null)
            {
                return(Redirect("/Error/0"));
            }
            Game_VM vm = convertGame(game.GameId, questions[0], 0);

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