Exemplo n.º 1
0
        private async Task <List <CollectionCardDto> > CalculateCollectionCardsAsync(IDictionary <long, short> playerCards, IEnumerable <PlayerDeck> playerDecks)
        {
            var collectionCards = new List <CollectionCardDto>();

            var playerCardCcs = playerCards.Select(pc => new CollectionCardDto
            {
                MtgaId             = pc.Key,
                OwnedCount         = pc.Value,
                RequiredCount      = 0,
                MissingCount       = 0,
                WildcardWorthiness = 0,
            });

            collectionCards.AddRange(playerCardCcs);

            foreach (var deck in playerDecks)
            {
                var deckCardCcs = deck.Cards.Select(dc => new CollectionCardDto
                {
                    MtgaId             = dc.Key,
                    OwnedCount         = 0,
                    RequiredCount      = dc.Value,
                    MissingCount       = 0,
                    WildcardWorthiness = 0,
                });

                foreach (var deckCardCc in deckCardCcs)
                {
                    // if player has card, take ownedCount
                    var existingPlayerCard = playerCardCcs.SingleOrDefault(pc => pc.MtgaId == deckCardCc.MtgaId);
                    var ownedCount         = existingPlayerCard != null
                        ? existingPlayerCard.OwnedCount
                        : deckCardCc.OwnedCount;
                    var missingCountForDeck = (short)Math.Max(deckCardCc.RequiredCount - ownedCount, 0);

                    // if collection has card, take max missingCount
                    var existingCollectionCard = collectionCards.SingleOrDefault(cc => cc.MtgaId == deckCardCc.MtgaId);
                    var missingCount           = existingCollectionCard != null
                        ? Math.Max(existingCollectionCard.MissingCount, missingCountForDeck)
                        : missingCountForDeck;

                    // add/update card
                    var collectionCard = existingCollectionCard ?? deckCardCc;

                    if (existingCollectionCard == null)
                    {
                        collectionCards.Add(collectionCard);
                    }

                    collectionCard.OwnedCount   = ownedCount;
                    collectionCard.MissingCount = missingCount;
                    if (collectionCard.DeckRequirements == null)
                    {
                        collectionCard.DeckRequirements = new List <DeckRequirementDto>();
                    }
                    collectionCard.DeckRequirements.Add(new DeckRequirementDto
                    {
                        DeckId        = deck.Id,
                        DeckName      = deck.Name,
                        OwnedCount    = ownedCount,
                        RequiredCount = deckCardCc.RequiredCount,
                    });
                }
            }

            // game data integration
            await _gameDataRepository.AssertInitializedAsync();

            collectionCards.ForEach(cc => cc.Data = _gameDataRepository.GetGameCard(cc.MtgaId));

            // sorting
            collectionCards = collectionCards
                              .OrderByDescending(cc => cc.Data.Rarity)
                              .ThenBy(cc => cc.Data.Name)
                              .ToList();

            return(collectionCards);
        }