public ActionResult Join(JoinViewModel model) { var db = new Entities(); Game game = db.Games.Single(g => g.ID == model.JoinInfo.GameID); if (game.HasStarted) { ModelState.AddModelError(string.Empty, "You cannot join this game, because it has already started."); } if (game.Password != null) { if (string.IsNullOrEmpty(model.JoinInfo.Password)) ModelState.AddModelError("JoinInfo.Password", "You must enter the password to join this game."); else if (!GameService.CheckPassword(model.JoinInfo.Password, game)) ModelState.AddModelError("JoinInfo.Password", "The password you entered is incorrect."); } if (!ModelState.IsValid) return View("Lobby", new LobbyViewModel(game, User.Identity.GetUserId(), model.JoinInfo)); GameService.JoinGame(game, User.Identity.GetUserId(), model.JoinInfo.Name); if (game.GamePlayers.Count >= game.NumPlayers) { GameService.Start(db, game); db.SaveChanges(); // TODO: send notification email? } else db.SaveChanges(); return RedirectToAction("Play", new { id = game.ID }); }
public ActionResult Create(CreateViewModel model) { if (!ModelState.IsValid) return View("Index", model); var game = GameService.CreateGame(model, User.Identity.GetUserId()); var db = new Entities(); db.Games.Add(game); db.SaveChanges(); return RedirectToAction("Play", "Game", new { id = game.ID }); }