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 }); }
// GET: Browse public ActionResult Index() { var db = new Entities(); var games = db.Games.Where(g => g.HasStarted == false && g.Password == null).ToList(); ViewBag.Message = "Browse all open games here"; return View(games); }
// GET: Browse/Mine public ActionResult Mine() { var db = new Entities(); var userID = User.Identity.GetUserId(); var games = db.Games.Where(g => g.HasFinished == false && (g.HostedByUserID == userID || g.GamePlayers.Where(gp => gp.UserID == userID).Any())).ToList(); ViewBag.Message = "Browse all of your games here"; return View("Index", games); }
public static void Start(Entities db, Game game) { game.HasStarted = true; game.GamePlayers.PickRandom().PrivateTeamID = 2; // secretly change one player onto the aliens team game.LoadCards(db); var firstPlayer = game.SetupPlayers(); game.StartTurn(firstPlayer, 1); }
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 }); }
// GET: Game/Play/1 public ActionResult Play(int id, int? day) { var db = new Entities(); var game = db.Games.Single(g => g.ID == id); if (!game.HasStarted) return View("Lobby", new LobbyViewModel(game, User.Identity.GetUserId())); GameTurn turn; if (day.HasValue) turn = game.GameTurns.Single(t => t.Number == day); else turn = game.GameTurns.OrderByDescending(t => t.Number).First(); return View("Play", new PlayViewModel(game, turn, User.Identity.GetUserId())); }
private static void LoadCards(this Game game, Entities db) { var cards = new List<GameCard>(); foreach (var card in db.Cards) for (var i = 0; i < card.NumberPerGame; i++) { var gc = new GameCard(); gc.Game = game; gc.Card = card; gc.Discarded = false; cards.Add(gc); } cards.Shuffle(); for (var i = 0; i < cards.Count; i++) { var card = cards[i]; card.Position = i + 1; card.ID = -1 - i; // temp ID, to keep FK happy game.GameCards.Add(card); } }