예제 #1
0
 public ActionResult Create(DecksForUsersModel deckForUser, string SelectedCardList)
 {
     using (var context = new HearthstoneContext())
     {
         var user = (from u in context.UserProfiles
                     where u.UserName == User.Identity.Name
                     select u).FirstOrDefault();
         var exists = (from dfu in context.DecksForUsers where dfu.UserID == user.UserId && dfu.DeckName.ToLower() == deckForUser.DeckName.ToLower() && dfu.IsActive select dfu).Any();
         var cardList = SelectedCardList.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries).ToList();
         var cardCount = 0;
         for (var i = 0; i < cardList.Count; i++)
         {
             cardCount += int.Parse(cardList[i].Split(new char[] { '-' })[1]);
         }
         if (cardCount < 30)
         {
             ModelState.AddModelError("CardCount", new Exception("Not enough cards."));
             ViewBag.SelectedCardList = SelectedCardList;
             ViewBag.Error = "Not enough cards.";
             return View(deckForUser);
         }
         if (cardList.Count > 30)
         {
             ModelState.AddModelError("CardCount", new Exception("Too Many cards."));
             ViewBag.SelectedCardList = SelectedCardList;
             ViewBag.Error = "Too Many cards.";
             return View(deckForUser);
         }
         if (exists)
         {
             ModelState.AddModelError("DuplicateDeck", new Exception("You already have a deck with this name."));
             ViewBag.SelectedCardList = SelectedCardList;
             ViewBag.Error = "You already have a deck with this name.";
             return View(deckForUser);
         }
         DeckModel newDeck = DeckModel.GetUniqueDeck(deckForUser.Deck, SelectedCardList, context);
         deckForUser.Deck = newDeck;
         deckForUser.IsActive = true;
         deckForUser.UserID = user.UserId;
         if (newDeck.DeckID == 0)
         {
             foreach (var cardID in cardList)
             {
                 var id = int.Parse(cardID.Split(new char[] { '-' })[0]);
                 var count = int.Parse(cardID.Split(new char[] { '-' })[1]);
                 if (id > 0 && context.Cards.Find(id) != null)
                 {
                     var card = new CardsInDeckModel() { CardID = id, Deck = newDeck, CardCount = count };
                     context.CardsInDecks.Add(card);
                 }
             }
         }
         context.DecksForUsers.Add(deckForUser);
         context.SaveChanges();
         return RedirectToAction("Index");
     }
 }
예제 #2
0
 public ActionResult Edit(DecksForUsersModel deckForUser, string SelectedCardList)
 {
     using (var context = new HearthstoneContext())
     {
         var user = (from u in context.UserProfiles
                     where u.UserName == User.Identity.Name
                     select u).FirstOrDefault();
         var cardList = SelectedCardList.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries).ToList();
         var cardCount = 0;
         for (var i = 0; i < cardList.Count; i++)
         {
             cardCount += int.Parse(cardList[i].Split(new char[] { '-' })[1]);
         }
         if (cardCount < 30)
         {
             ModelState.AddModelError("CardCount", new Exception("Not enough cards."));
             ViewBag.SelectedCardList = SelectedCardList;
             ViewBag.Error = "Not enough cards.";
             return View(deckForUser);
         }
         if (cardList.Count > 30)
         {
             ModelState.AddModelError("CardCount", new Exception("Too Many cards."));
             ViewBag.SelectedCardList = SelectedCardList;
             ViewBag.Error = "Too Many cards.";
             return View(deckForUser);
         }
         DeckModel newDeck = DeckModel.GetUniqueDeck(deckForUser.Deck, SelectedCardList, context);
         deckForUser.Deck = newDeck;
         deckForUser.IsActive = true;
         deckForUser.UserID = user.UserId;
         var lastDeck = (from dfu in context.DecksForUsers
                                    where dfu.DecksForUsersID == deckForUser.DecksForUsersID
                                    orderby dfu.DeckVersion descending
                                    select dfu).FirstOrDefault();
         if (newDeck.DeckID == lastDeck.DeckID) deckForUser.DeckVersion = lastDeck.DeckVersion;
         else deckForUser.DeckVersion = lastDeck.DeckVersion + 1;
         if (lastDeck.DeckName != deckForUser.DeckName)
         {
             //find a way to ensure unique deckname, but still allow deck name to be changeable
         }
         foreach (var d in context.DecksForUsers.Where(dfu => dfu.DeckName == deckForUser.DeckName))
         {
             d.IsActive = false;
         }
         if (newDeck.DeckID == 0)
         {
             foreach (var cardID in cardList)
             {
                 var id = int.Parse(cardID.Split(new char[] { '-' })[0]);
                 var count = int.Parse(cardID.Split(new char[] { '-' })[1]);
                 if (id > 0 && context.Cards.Find(id) != null)
                 {
                     var card = new CardsInDeckModel() { CardID = id, Deck = newDeck, CardCount = count };
                     context.CardsInDecks.Add(card);
                 }
             }
         }
         context.DecksForUsers.Add(deckForUser);
         context.SaveChanges();
         return RedirectToAction("Index");
     }
 }