public IActionResult PlayPost(SinglePlayerGameFilledBlank obj) { if (ModelState.IsValid) { //Save Answer _db.SinglePlayerGameFilledBlank.Update(obj); _db.SaveChanges(); //Update Game SinglePlayerGame game = _db.SinglePlayerGame.Where(g => g.Id == obj.SinglePlayerGameId).FirstOrDefault(); game.ActiveMadlibBlankIndex++; //If it's the last one, show the results if (game.ActiveMadlibBlankIndex > _db.MadlibBlank.Where(b => b.MadlibId == game.MadlibId).Select(b => b.Index).Max()) { return(RedirectToAction("DisplayResults", game)); } //Otherwise move to the next one SinglePlayerGameFilledBlank nextBlank = AddNextBlankToAnswers(game); _db.SinglePlayerGame.Update(obj.SinglePlayerGame); _db.SaveChanges(); return(RedirectToAction("Play", nextBlank)); } else { return(View(obj)); } }
public IActionResult Play(SinglePlayerGameFilledBlank nextBlank) { if (nextBlank == null) { return(NotFound()); } nextBlank.Category = _db.Category.Find(nextBlank.CategoryId); nextBlank.SinglePlayerGame = _db.SinglePlayerGame.Find(nextBlank.SinglePlayerGameId); return(View(nextBlank)); }
private SinglePlayerGameFilledBlank AddNextBlankToAnswers(SinglePlayerGame game) { MadlibBlank currentBlank = _db.MadlibBlank.Where(b => b.MadlibId == game.MadlibId && b.Index == game.ActiveMadlibBlankIndex).FirstOrDefault(); SinglePlayerGameFilledBlank nextBlank = new SinglePlayerGameFilledBlank() { MadlibBlankIndex = currentBlank.Index, CategoryId = currentBlank.CategoryId, SinglePlayerGameId = game.Id }; _db.SinglePlayerGameFilledBlank.Add(nextBlank); _db.SaveChanges(); return(nextBlank); }
public IActionResult Start(Madlib.Models.Madlib obj) { if (obj == null) { return(NotFound()); } SinglePlayerGame NewGame = new SinglePlayerGame() { MadlibId = obj.Id, ActiveMadlibBlankIndex = 0 }; if (ModelState.IsValid) { _db.SinglePlayerGame.Add(NewGame); _db.SaveChanges(); SinglePlayerGameFilledBlank nextBlank = AddNextBlankToAnswers(NewGame); return(RedirectToAction("Play", nextBlank)); } else { return(View(obj)); } }