Beispiel #1
0
        public void Load()
        {
            if (!IsFileLoadingComplete.Signaled)
            {
                throw new InvalidOperationException();
            }

            foreach (var set in deserializeAllSets())
            {
                preProcessSet(set);

                for (int i = set.ActualCards.Count - 1; i >= 0; i--)
                {
                    var card = set.ActualCards[i];
                    card.Set = set;

                    if (!string.IsNullOrEmpty(card.FaceName))
                    {
                        card.NameEn = card.FaceName;
                    }

                    card.Id = string.Intern(CardId.Generate(card));

                    card.Formatter = _formatter;

                    preProcessCard(card);
                    preProcessCardOrToken(card);

                    if (set.ActualCards[i].Remove)
                    {
                        set.ActualCards.RemoveAt(i);
                    }
                }

                var tokenLegalityByFormat = set.ActualCards.Aggregate(
                    new HashSet <string>(Legality.Formats, Str.Comparer),
                    (formats, card) =>
                {
                    formats.IntersectWith(card.LegalityByFormat.Keys);
                    return(formats);
                }).ToDictionary(_ => _, _ => Legality.Legal, Str.Comparer);

                for (int i = 0; i < set.Tokens.Count; i++)
                {
                    var token = set.Tokens[i];
                    token.IsToken = true;
                    token.Set     = set;

                    if (!string.IsNullOrEmpty(token.FaceName))
                    {
                        token.NameEn = token.FaceName;
                    }

                    token.Id = string.Intern(CardId.Generate(token));
                    token.LegalityByFormat = tokenLegalityByFormat;
                    token.Formatter        = _formatter;
                    preProcessToken(token);
                    preProcessCardOrToken(token);
                }

                var cards = new List <Card>(set.ActualCards.Count + set.Tokens.Count);
                cards.AddRange(set.ActualCards);
                cards.AddRange(set.Tokens);
                set.Cards = cards;

                // after preProcessCard, to have NameNormalized field set non empty
                set.ActualCardsByName = set.ActualCards.GroupBy(_ => _.NameNormalized)
                                        .ToDictionary(
                    gr => gr.Key,
                    gr => gr.ToList(),
                    Str.Comparer);

                set.TokensByName = set.Tokens.GroupBy(_ => _.NameNormalized)
                                   .ToDictionary(
                    gr => gr.Key,
                    gr => gr.ToList(),
                    Str.Comparer);

                set.ActualCardsById = set.ActualCards
                                      .ToDictionary(_ => _.MtgjsonId);

                set.TokensById = set.Tokens
                                 .ToDictionary(_ => _.MtgjsonId);

                foreach (var card in set.Cards)
                {
                    CardsById[card.Id] = card;
                }

                ImageNameCalculator.CalculateCardImageNames(set, Patch);

                lock (SetsByCode)
                    SetsByCode.Add(set.Code, set);

                lock (Cards)
                    foreach (var card in set.Cards)
                    {
                        Cards.Add(card);
                    }

                SetAdded?.Invoke();
            }

            CardsByName  = toNamesakesMap(Cards.Where(c => !c.IsToken));
            TokensByName = toNamesakesMap(Cards.Where(c => c.IsToken));

            CardIdsByName  = CardsByName.ToDictionary(_ => _.Key, _ => _.Value.ToHashSet(c => c.Id), Str.Comparer);
            TokenIdsByName = TokensByName.ToDictionary(_ => _.Key, _ => _.Value.ToHashSet(c => c.Id), Str.Comparer);

            CardPrintingsByName  = CardsByName.ToDictionary(_ => _.Key, toPrintings, Str.Comparer);
            TokenPrintingsByName = TokensByName.ToDictionary(_ => _.Key, toPrintings, Str.Comparer);

            for (int i = 0; i < Cards.Count; i++)
            {
                var card = Cards[i];
                card.IndexInFile = i;
                card.Namesakes   = MapByName(card.IsToken)[card.NameNormalized];
                card.NamesakeIds = MapIdByName(card.IsToken)[card.NameNormalized];
                card.Printings   = MapPrintingsByName(card.IsToken)[card.NameNormalized];
            }

            patchLegality();

            IsLoadingComplete.Signal();

            // release RAM
            _defaultSetsContent = null;
            Patch          = null;
            Cards.Capacity = Cards.Count;

            foreach (var namesakeList in CardsByName.Values)
            {
                namesakeList.Capacity = namesakeList.Count;
            }

            foreach (var namesakeList in TokensByName.Values)
            {
                namesakeList.Capacity = namesakeList.Count;
            }

            IReadOnlyList <string> toPrintings(KeyValuePair <string, List <Card> > _) =>
            _.Value.Select(c => c.Set).Distinct().OrderBy(s => s.ReleaseDate).Select(s => s.Code).ToList();

            Dictionary <string, List <Card> > toNamesakesMap(IEnumerable <Card> cards) =>
            cards.GroupBy(_ => _.NameNormalized)
            .ToDictionary(
                gr => gr.Key,
                // card_by_name_sorting
                gr => gr.OrderByDescending(_ => _.ReleaseDate).ToList(),
                Str.Comparer);
        }
Beispiel #2
0
        public void Load()
        {
            var serializer = new JsonSerializer();

            Stream stream = new MemoryStream(_streamContent);

            using (stream)
                using (var stringReader = new StreamReader(stream))
                    using (var jsonReader = new JsonTextReader(stringReader))
                    {
                        jsonReader.Read();

                        while (true)
                        {
                            jsonReader.Read();

                            if (jsonReader.TokenType == JsonToken.EndObject)
                            {
                                // sets are over, all json was read
                                break;
                            }

                            var setCode = (string)jsonReader.Value;

                            // skip set name
                            jsonReader.Read();

                            if (!FilterSetCode(setCode))
                            {
                                jsonReader.Skip();
                                continue;
                            }

                            var set = serializer.Deserialize <Set>(jsonReader);

                            for (int i = 0; i < set.Cards.Count; i++)
                            {
                                var card = set.Cards[i];
                                card.Set = set;

                                preProcessCard(card);
                            }

                            for (int i = set.Cards.Count - 1; i >= 0; i--)
                            {
                                if (set.Cards[i].Remove)
                                {
                                    set.Cards.RemoveAt(i);
                                }
                            }

                            // after preProcessCard, to have NameNormalized field set non empty
                            set.CardsByName = set.Cards.GroupBy(_ => _.NameNormalized)
                                              .ToDictionary(
                                gr => gr.Key,
                                gr => gr.ToList(),
                                Str.Comparer);

                            lock (SetsByCode)
                                SetsByCode.Add(set.Code, set);

                            lock (Cards)
                                foreach (var card in set.Cards)
                                {
                                    Cards.Add(card);
                                }

                            foreach (var card in set.Cards)
                            {
                                CardsById[card.Id] = card;
                            }

                            SetAdded?.Invoke();
                        }
                    }

            CardsByName = Cards.GroupBy(_ => _.NameNormalized)
                          .ToDictionary(
                gr => gr.Key,
                // card_by_name_sorting
                gr => gr.OrderByDescending(_ => _.ReleaseDate).ToList(),
                Str.Comparer);

            for (int i = 0; i < Cards.Count; i++)
            {
                var card = Cards[i];

                card.IndexInFile = i;

                card.Namesakes = CardsByName[card.NameNormalized]
                                 .Where(c => c != card)
                                 .OrderByDescending(c => c.ReleaseDate)
                                 .ToArray();
            }

            patchLegality();

            IsLoadingComplete = true;
            LoadingComplete?.Invoke();

            // release RAM
            _streamContent = null;
            _patch         = null;
            Cards.Capacity = Cards.Count;
        }
Beispiel #3
0
        public void Load()
        {
            foreach (var set in DeserializeSets())
            {
                for (int i = 0; i < set.Cards.Count; i++)
                {
                    var card = set.Cards[i];

                    card.Set = set;
                    card.Id  = CardId.Generate(card);
                    preProcessCard(card);
                }

                for (int i = set.Cards.Count - 1; i >= 0; i--)
                {
                    if (set.Cards[i].Remove)
                    {
                        set.Cards.RemoveAt(i);
                    }
                }

                // after preProcessCard, to have NameNormalized field set non empty
                set.CardsByName = set.Cards.GroupBy(_ => _.NameNormalized)
                                  .ToDictionary(
                    gr => gr.Key,
                    gr => gr.ToList(),
                    Str.Comparer);

                foreach (var card in set.Cards)
                {
                    CardsById[card.Id] = card;
                    CardsByScryfallId[card.ScryfallId] = card;
                }

                for (int i = 0; i < set.Cards.Count; i++)
                {
                    postProcessCard(set.Cards[i]);
                }

                ImageNameCalculator.CalculateImageNames(set, Patch);

                lock (SetsByCode)
                    SetsByCode.Add(set.Code, set);

                lock (Cards)
                    foreach (var card in set.Cards)
                    {
                        Cards.Add(card);
                    }

                SetAdded?.Invoke();
            }

            CardsByName = Cards.GroupBy(_ => _.NameNormalized)
                          .ToDictionary(
                gr => gr.Key,
                // card_by_name_sorting
                gr => gr.OrderByDescending(_ => _.ReleaseDate).ToList(),
                Str.Comparer);

            CardsByMultiverseId = Cards
                                  .Where(c => c.MultiverseId.HasValue)
                                  .GroupBy(c => c.MultiverseId.Value)
                                  .ToDictionary(gr => gr.Key, gr => (IList <Card>)gr.ToList());

            for (int i = 0; i < Cards.Count; i++)
            {
                var card = Cards[i];

                card.IndexInFile = i;

                card.Namesakes = CardsByName[card.NameNormalized]
                                 .Where(c => c != card)
                                 .OrderByDescending(c => c.ReleaseDate)
                                 .ToArray();
            }

            patchLegality();

            IsLoadingComplete = true;
            LoadingComplete?.Invoke();

            // release RAM
            _defaultSetsContent = null;
            Patch          = null;
            Cards.Capacity = Cards.Count;

            foreach (var namesakeList in CardsByName.Values)
            {
                namesakeList.Capacity = namesakeList.Count;
            }
        }
Beispiel #4
0
        public void Load()
        {
            var serializer = new JsonSerializer();

            Stream stream = new MemoryStream(_streamContent);

            using (stream)
                using (var stringReader = new StreamReader(stream))
                    using (var jsonReader = new JsonTextReader(stringReader))
                    {
                        jsonReader.Read();

                        while (true)
                        {
                            // пропустить имя сета
                            jsonReader.Read();

                            if (jsonReader.TokenType == JsonToken.EndObject)
                            {
                                // сеты кончились, весь json прочитан
                                break;
                            }

                            // пропустить имя сета
                            jsonReader.Read();

                            var set = serializer.Deserialize <Set>(jsonReader);

                            for (int i = 0; i < set.Cards.Count; i++)
                            {
                                var card = set.Cards[i];
                                card.Set = set;

                                preProcessCard(card);
                            }

                            for (int i = set.Cards.Count - 1; i >= 0; i--)
                            {
                                if (set.Cards[i].Remove)
                                {
                                    set.Cards.RemoveAt(i);
                                }
                            }

                            // после preProcessCard, чтобы было заполено поле NameNormalized
                            set.CardsByName = set.Cards.GroupBy(_ => _.NameNormalized)
                                              .ToDictionary(
                                gr => gr.Key,
                                gr => gr.ToList(),
                                Str.Comparer);

                            lock (SetsByCode)
                                SetsByCode.Add(set.Code, set);

                            lock (Cards)
                                foreach (var card in set.Cards)
                                {
                                    Cards.Add(card);
                                }

                            foreach (var card in set.Cards)
                            {
                                CardsById[card.Id] = card;
                            }

                            SetAdded?.Invoke();
                        }
                    }

            CardsByName = Cards.GroupBy(_ => _.NameNormalized)
                          .ToDictionary(
                gr => gr.Key,
                // card_by_name_sorting
                gr => gr.OrderByDescending(_ => _.ReleaseDate).ToList(),
                Str.Comparer);

            for (int i = 0; i < Cards.Count; i++)
            {
                Cards[i].IndexInFile = i;
            }

            patchLegality();

            IsLoadingComplete = true;
            LoadingComplete?.Invoke();

            // освободить память
            _streamContent = null;
            _patch         = null;
            Cards.Capacity = Cards.Count;
        }