Beispiel #1
0
        public CollectionModel GetCollection()
        {
            var uId    = HttpContext.User.Id();
            var result = _collectionSource.GetCollection(uId);

            if (result == null || result.OwnedCards == null || result.BorrowedCards == null)
            {
                return(null);
            }
            var uniqueCardNames = GetUniqueCardNames(result.OwnedCards.Keys, result.BorrowedCards.Keys);
            var cardInfo        = GetCardInfo(uniqueCardNames);

            var newBorrowedCards = new Dictionary <string, IDictionary <string, IBorrowedCard> >();
            //var userids = result.BorrowedCards.SelectMany(card => card.Value.Select(user => user.Key)).ToArray();
            var userids = result.OwnedCards.Where(card => card.Value.LentTo.Count > 0)
                          .SelectMany(card => card.Value.LentTo.Keys)
                          .Concat(
                result.BorrowedCards.SelectMany(card => card.Value.Select(user => user.Key))
                ).ToArray();
            var userNames = _userSource.GetUserNamesByUserIds(userids);

            foreach (var borrowedCards in result.BorrowedCards)
            {
                var cardName = borrowedCards.Key;
                var borrowed = borrowedCards.Value;
                foreach (var user in borrowed)
                {
                    var bCard = new BorrowedCardModel
                    {
                        AmountBorrowed = user.Value.AmountBorrowed,
                        CardId         = user.Value.CardId,
                        Lender         = user.Value.Lender,
                        LenderUserName = userNames[user.Value.Lender]
                    };
                    if (!newBorrowedCards.ContainsKey(cardName))
                    {
                        newBorrowedCards[cardName] = new Dictionary <string, IBorrowedCard>();
                    }

                    newBorrowedCards[cardName].Add(user.Value.Lender, bCard);
                }
            }
            var model = new CollectionModel
            {
                CardInfo       = cardInfo,
                UserCollection = new Collection
                {
                    BorrowedCards = newBorrowedCards,
                    CollectionId  = result.CollectionId,
                    OwnedCards    = result.OwnedCards,
                    UserId        = result.UserId
                },
                UserIdToUserName = userNames
            };

            return(model);
        }
Beispiel #2
0
        public IActionResult Search([FromBody] List <string> decks)
        {
            var             uId       = UserInfo.Id(HttpContext.User);
            var             userEmail = UserInfo.Email(HttpContext.User);
            List <IMtgDeck> deckLists;


            deckLists = GetMultipleByName(decks);

            var collection = _collectionSource.GetCollection(uId);
            Dictionary <string, int> usableCards = new Dictionary <string, int>();

            foreach (var card in collection.OwnedCards)
            {
                usableCards.Add(card.Value.CardId, card.Value.Available);
            }


            foreach (var card in collection.BorrowedCards)
            {
                if (usableCards.ContainsKey(card.Key))
                {
                    usableCards[card.Key] += card.Value.Sum(borowedCard => borowedCard.Value.AmountBorrowed);
                }
                else
                {
                    usableCards.Add(card.Key, card.Value.Sum(borowedCard => borowedCard.Value.AmountBorrowed));
                }
            }



            List <string> buildable = new List <string>();
            bool          status;

            foreach (var deck in deckLists)
            {
                status = true;
                var decklist = deck.Cards;
                foreach (var card in decklist)
                {
                    if (usableCards.ContainsKey((card.Value.Name)))
                    {
                        if (usableCards[card.Value.Name] < card.Value.Count)
                        {
                            status = false;
                            break;
                        }
                    }
                    else
                    {
                        status = false;
                        break;
                    }
                }

                if (status)
                {
                    buildable.Add(deck.id);
                }
            }
            //do some comparisons here
            return(Json(buildable));
        }