public void Delete(int id) { var ID = db.Set <T>().Find(id); db.Set <T>().Remove(ID); db.SaveChanges(); }
public ActionResult SubscribeGroup(TrainingGroup model) { var group = QuizDb.TrainingGroups.SingleOrDefault(x => x.Id == model.Id); if (group == null) { return(RedirectToAction("Index")); } var user = GetCurrentUser(); if (group.AccessToken.Equals(model.AccessToken)) { var subscription = new TrainingGroupSubscription { TrainingGroup = group, TimeStamp = DateTime.Now, Userid = user.Id }; QuizDb.TrainingGroupSubscriptions.Add(subscription); QuizDb.SaveChanges(); return(RedirectToAction("Index")); } ModelState.AddModelError("AccessToken", "Falscher Schlüssel"); return(View(model)); }
public ActionResult SubscribeGroup(Guid id) { var group = QuizDb.TrainingGroups.SingleOrDefault(x => x.Id == id); if (group == null) { return(RedirectToAction("Index")); } var user = GetCurrentUser(); if (group.IsPublic) { var subscription = new TrainingGroupSubscription { TrainingGroup = group, TimeStamp = DateTime.Now, Userid = user.Id }; QuizDb.TrainingGroupSubscriptions.Add(subscription); QuizDb.SaveChanges(); return(RedirectToAction("Index")); } var model = new TrainingGroup(); return(View(model)); }
public ActionResult ClearLog(Guid id) { var allGames = QuizDb.QuizGames.Where(x => x.Levels.Any(y => y.Quiz.Id == id)).ToList();; foreach (var game in allGames) { foreach (var level in game.Levels.ToList()) { QuizDb.GameLevels.Remove(level); } foreach (var player in game.Players.ToList()) { QuizDb.GamePlayers.Remove(player); } } var allLogs = QuizDb.GameLogs.Where(x => x.Game.Levels.Any(y => y.Quiz.Id == id)).ToList(); foreach (var log in allLogs) { QuizDb.GameLogs.Remove(log); } QuizDb.SaveChanges(); return(RedirectToAction("Index")); }
public PartialViewResult Answer(Guid questionId, Guid answerId) { var gameState = Session["GameState"] as GamePlayState; var answer = QuizDb.Answers.SingleOrDefault(x => x.Id == answerId); var game = QuizDb.QuizGames.SingleOrDefault(x => x.Id == gameState.Game.Id); var player = QuizDb.GamePlayers.SingleOrDefault(x => x.Id == gameState.Player.Id); var log = new GameLog(); log.Answer = answer; log.Game = game; log.Player = player; log.LogDateTime = DateTime.Now; QuizDb.GameLogs.Add(log); QuizDb.SaveChanges(); // Update var statService = new StatisticsService(QuizDb); statService.NotifyUpdates(); var questionToDelete = gameState.QuestionsLeft.SingleOrDefault(x => x.Id == answer.Question.Id); gameState.QuestionsLeft.Remove(questionToDelete); // jetzt die Antwort mit Ergebnis zurückschicken // die Frage // die Antwort var model = new QuestionAnswerViewModel(); model.Answers = gameState.CurrentQuestion.Answers; model.GivenAnswer = answer; model.CorrectAnswer = questionToDelete.Answers.FirstOrDefault(x => x.IsCorrect); return(PartialView("_Answer", model)); }
/// <summary> /// /// </summary> /// <param name="exId">ExerciseId</param> /// <param name="subId">Subscription</param> /// <returns></returns> public ActionResult StartExercise(Guid exId, Guid subId) { // User identifizieren var user = GetCurrentUser(); var exercise = QuizDb.TrainingExercises.SingleOrDefault(x => x.Id == exId); var subscription = QuizDb.TrainingGroupSubscriptions.SingleOrDefault(x => x.Id == subId); // Spiel anlegen var gameService = new GamePlayService(QuizDb); var game = gameService.CreateSingleUserGame(user.Id, exercise.Quiz.Id); var practice = new TrainingPractice { Exercise = exercise, Game = game, Subscription = subscription }; QuizDb.TrainingPractices.Add(practice); QuizDb.SaveChanges(); var gameState = new GamePlayState { Game = game, Player = game.Players.First(), Quiz = game.Levels.First().Quiz, Practice = practice }; // Den Fragenkatalog sortiert aufbauen foreach (var quizSection in gameState.Quiz.Sections.OrderBy(x => x.Order)) { foreach (var quizQuestion in quizSection.Questions.OrderBy(x => x.Order)) { gameState.QuestionsLeft.Add(quizQuestion.Question); } } // TODO: Hier jetzt die Option des Quiz einbauen // Fragen mischen gameState.QuestionsLeft.Shuffle(); // Statistik erstellen gameState.QuestionTotalCount = gameState.QuestionsLeft.Count; // In einer Session sichern // oder in DB? Session["GameState"] = gameState; var question = gameState.QuestionsLeft.FirstOrDefault(); var model = new QuestionViewModel { Question = question, Answers = question.Answers.ToList() }; model.Answers.Shuffle(); model.Total = gameState.QuestionTotalCount; model.Index = gameState.QuestionTotalCount - gameState.QuestionsLeft.Count + 1; gameState.CurrentQuestion = model; //return View("PlayGameSPA", model); return(View("PlayGame", model)); }