async public Task <Deck> PutCardsInPile(string deckId, string pileName, List <string> cardCodes)
        {
            using (var context = new DeckContext())
            {
                Deck deck = await context.Decks
                            .Include(x => x.Cards)
                            .Include(x => x.Piles)
                            .SingleAsync(x => x.DeckId == deckId);

                Pile pile = null;

                foreach (Pile p in deck.Piles)
                {
                    if (p.Name == pileName)
                    {
                        pile = p;
                    }
                }
                if (pile == null)
                {
                    //create new pile
                    pile = new Pile()
                    {
                        Name   = pileName,
                        DeckId = deck.Id,
                        Deck   = deck
                    };
                    context.Piles.Add(pile);
                    context.SaveChanges();
                    pile = context.Piles
                           .Include(p => p.Cards)
                           .First(p => p.DeckId == deck.Id && p.Name == pileName);
                }


                foreach (Card card in deck.Cards)
                {
                    foreach (string cardCode in cardCodes)
                    {
                        if (card.Code == cardCode)
                        {
                            card.PileId = pile.Id;
                        }
                    }
                }

                await context.SaveChangesAsync();

                return(deck);
            }
        }
示例#2
0
        async public Task <Pile> AddCardToPileAsync(string deckId, string pileName, string cards)
        {
            string[] cardArr = cards.Split(',');
            using (var context = new DeckContext())
            {
                Deck deck = await context.Decks
                            .Include(x => x.Cards)
                            .SingleAsync(x => x.DeckId.Equals(deckId));

                Deck deckPiles = await context.Decks
                                 .Include(x => x.Piles)
                                 .SingleAsync(x => x.DeckId.Equals(deckId));

                //Couldn't figure out how to do multiple includes so I made two

                Pile pile;
                try
                {
                    pile = deckPiles.Piles.Single(x => x.Name.Equals(pileName) && x.DeckId == deckPiles.Id);
                }
                catch (Exception)
                {
                    pile        = new Pile();
                    pile.Deck   = deckPiles;
                    pile.DeckId = deckPiles.Id;
                    pile.Name   = pileName;
                    pile.Cards  = new List <Card>();
                    deckPiles.Piles.Add(pile);
                }

                foreach (var card in cardArr)
                {
                    Card Card = deck.Cards.Single(x => x.Code.Equals(card));
                    Card.Drawn = true; // we took it out so it's out of the deck
                    if (pile.Cards.Contains(Card))
                    {
                        continue;                            //small check so we won't put multiple instances of the same card in the pile
                    }
                    pile.Cards.Add(Card);
                    Card.Pile = pile;
                }

                await context.SaveChangesAsync();

                return(pile);
            }
        }
示例#3
0
        async public Task <Pile> PileShuffleAsync(string deckId, string pileName)
        {
            using (var context = new DeckContext())
            {
                Pile pile = await context.Piles
                            .Include(x => x.Cards)
                            .SingleAsync(x => x.Name.Equals(pileName));

                //no try catch this time because if you try to shuffle a pile that doesn't exist you deserve to have the API scream at you
                /*Pile pile = deckPiles.Piles.Single(x => x.Name.Equals(pileName) && x.DeckId == deckPiles.Id);*/
                var rnd          = new Random();
                var shuffledPile = pile.Cards.OrderBy(x => rnd.Next()).ToList();

                pile.Cards = shuffledPile;

                await context.SaveChangesAsync();

                return(pile);
            }
        }