private void MoveCardToOtherCollection(ICardCollection collection, int idGatherer, int idLanguage, int countToMove, ICardCountKey cardCountKey, ICardCollection collectionDestination)
        {
            if (countToMove <= 0 || cardCountKey == null)
            {
                return;
            }

            using (new WriterLock(_lock))
            {
                ICardInCollectionCount cardInCollectionCount = GetCardCollection(collection, idGatherer, idLanguage);
                if (cardInCollectionCount == null)
                {
                    return;
                }

                if (cardInCollectionCount.GetCount(cardCountKey) < countToMove)
                {
                    return;
                }

                CardCount cardCountSource = new CardCount
                {
                    { cardCountKey, -countToMove }
                };

                CardCount cardCountDestination = new CardCount
                {
                    { cardCountKey, countToMove }
                };

                InsertOrUpdateCardInCollection(collection.Id, idGatherer, idLanguage, cardCountSource);
                InsertOrUpdateCardInCollection(collectionDestination.Id, idGatherer, idLanguage, cardCountDestination);
            }
        }
        private void RemoveFromReferential(ICardInCollectionCount cardInCollectionCount)
        {
            //No call to private ICardEdition GetCardEdition(int idGatherer) because call in CheckReferentialLoaded()
            ICardEdition cardEdition = _cardEditions.GetOrDefault(cardInCollectionCount.IdGatherer);

            if (_allCardInCollectionCount.TryGetValue(cardEdition.IdCard, out IList <ICardInCollectionCount> list))
            {
                list.Remove(cardInCollectionCount);
            }
        }
Beispiel #3
0
        public StatisticViewModel(ICardInCollectionCount cardInCollectionCount)
        {
            IMagicDatabaseReadOnly magicDatabase = MagicDatabaseManager.ReadOnly;

            FoilAltArtNumber = cardInCollectionCount.FoilAltArtNumber;
            AltArtNumber     = cardInCollectionCount.AltArtNumber;
            FoilNumber       = cardInCollectionCount.FoilNumber;
            Number           = cardInCollectionCount.Number;
            Collection       = magicDatabase.GetCollection(cardInCollectionCount.IdCollection).Name;
            Edition          = magicDatabase.GetEdition(cardInCollectionCount.IdGatherer).Name;
            Language         = magicDatabase.GetLanguage(cardInCollectionCount.IdLanguage).Name;
        }
        private void InsertInReferential(ICardInCollectionCount cardInCollectionCount)
        {
            //No call to private ICardEdition GetCardEdition(int idGatherer) because call in CheckReferentialLoaded()
            ICardEdition cardEdition = _cardEditions.GetOrDefault(cardInCollectionCount.IdGatherer);

            if (!_allCardInCollectionCount.TryGetValue(cardEdition.IdCard, out IList <ICardInCollectionCount> list))
            {
                list = new List <ICardInCollectionCount>();
                _allCardInCollectionCount.Add(cardEdition.IdCard, list);
            }

            if (list.Contains(cardInCollectionCount))
            {
                throw new Exception("Invalid addition");
            }

            list.Add(cardInCollectionCount);
        }
Beispiel #5
0
        private void UpdateMaxCount()
        {
            int idGatherer = _magicDatabase.GetIdGatherer(Card, EditionSelected);

            if (LanguageSelected == null)
            {
                MaxCount = 0;
                return;
            }

            ICardInCollectionCount cardInCollectionCount = _cardInCollectionCounts.FirstOrDefault(cicc => cicc.IdGatherer == idGatherer && cicc.IdLanguage == LanguageSelected.Id);

            if (cardInCollectionCount == null)
            {
                MaxCount = 0;
                return;
            }
            MaxCount = cardInCollectionCount.GetCount(new CardCountKey(IsFoil, IsAltArt));
        }
        public void ChangeCardEditionFoilAltArtLanguage(ICardCollection collection, ICard card, int countToChange, IEdition editionSource, ICardCountKey cardCountKeySource, ILanguage languageSource,
                                                        IEdition editionDestination, ICardCountKey cardCountKeyDestination, ILanguage languageDestination)
        {
            if (countToChange <= 0)
            {
                return;
            }

            using (new WriterLock(_lock))
            {
                if (languageSource == null || languageDestination == null || cardCountKeySource == null || cardCountKeyDestination == null)
                {
                    return;
                }

                int idGathererSource      = GetIdGatherer(card, editionSource);
                int idGathererDestination = GetIdGatherer(card, editionDestination);
                ICardInCollectionCount cardInCollectionCount = GetCardCollection(collection, idGathererSource, languageSource.Id);

                if (cardInCollectionCount == null || idGathererDestination == 0)
                {
                    return;
                }

                if (cardInCollectionCount.GetCount(cardCountKeySource) < countToChange)
                {
                    return;
                }

                CardCount cardCountSource = new CardCount
                {
                    { cardCountKeySource, -countToChange }
                };

                CardCount cardCountDestination = new CardCount
                {
                    { cardCountKeyDestination, countToChange }
                };

                InsertOrUpdateCardInCollection(collection.Id, idGathererSource, languageSource.Id, cardCountSource);
                InsertOrUpdateCardInCollection(collection.Id, idGathererDestination, languageDestination.Id, cardCountDestination);
            }
        }
        public void InsertOrUpdateCardInCollection(int idCollection, int idGatherer, int idLanguage, ICardCount cardCount)
        {
            if (cardCount == null)
            {
                return;
            }

            using (new WriterLock(_lock))
            {
                using (BatchMode())
                {
                    int countToAdd           = cardCount.GetCount(CardCountKeys.Standard);
                    int foilCountToAdd       = cardCount.GetCount(CardCountKeys.Foil);
                    int altArtCountToAdd     = cardCount.GetCount(CardCountKeys.AltArt);
                    int foilAltArtCountToAdd = cardCount.GetCount(CardCountKeys.FoilAltArt);

                    ICardInCollectionCount cardInCollection = GetCardCollection(idCollection, idGatherer, idLanguage);
                    if (cardInCollection == null)
                    {
                        //Insert new
                        if (cardCount.Any(kv => kv.Value < 0) || cardCount.GetTotalCount() == 0)
                        {
                            return;
                        }

                        CardInCollectionCount newCardInCollectionCount = new CardInCollectionCount
                        {
                            IdCollection     = idCollection,
                            IdGatherer       = idGatherer,
                            Number           = countToAdd,
                            FoilNumber       = foilCountToAdd,
                            AltArtNumber     = altArtCountToAdd,
                            FoilAltArtNumber = foilAltArtCountToAdd,
                            IdLanguage       = idLanguage
                        };


                        AddToDbAndUpdateReferential(newCardInCollectionCount, InsertInReferential);

                        AuditAddCard(idCollection, idGatherer, idLanguage, cardCount);
                        return;
                    }

                    //Update
                    int newCount            = countToAdd + cardInCollection.Number;
                    int newFoilCount        = foilCountToAdd + cardInCollection.FoilNumber;
                    int newAltArtCountToAdd = altArtCountToAdd + cardInCollection.AltArtNumber;
                    int newFoilAltArtCount  = foilAltArtCountToAdd + cardInCollection.FoilAltArtNumber;

                    if (newCount < 0 || newFoilCount < 0 || newAltArtCountToAdd < 0 || newFoilAltArtCount < 0)
                    {
                        return;
                    }

                    if (cardInCollection is not CardInCollectionCount updateCardInCollectionCount)
                    {
                        return;
                    }

                    if (newCount + newFoilCount + newAltArtCountToAdd + newFoilAltArtCount == 0)
                    {
                        RemoveFromDbAndUpdateReferential(updateCardInCollectionCount, RemoveFromReferential);

                        AuditAddCard(idCollection, idGatherer, idLanguage, cardCount);

                        return;
                    }

                    updateCardInCollectionCount.Number           = newCount;
                    updateCardInCollectionCount.FoilNumber       = newFoilCount;
                    updateCardInCollectionCount.AltArtNumber     = newAltArtCountToAdd;
                    updateCardInCollectionCount.FoilAltArtNumber = newFoilAltArtCount;

                    using (IDbConnection cnx = _databaseConnection.GetMagicConnection())
                    {
                        Mapper <CardInCollectionCount> .UpdateOne(cnx, updateCardInCollectionCount);
                    }

                    AuditAddCard(idCollection, idGatherer, idLanguage, cardCount);
                }
            }
        }