Exemplo n.º 1
0
        public void WriteFile(string fileName, MagicBinder collection)
        {
            if (File.Exists(fileName))
            {
                var dest = fileName + ".backup";
                File.Copy(fileName, dest, true);
            }

            using (var textWriter = new StreamWriter(fileName))
            {
                // Write header first:
                var writer = new CsvWriter(textWriter, _config);
                {
                    writer.Configuration.CultureInfo = CultureInfo.InvariantCulture;

                    writer.WriteField <string>("Name");
                    writer.WriteField <string>("Version");
                    writer.NextRecord();

                    writer.WriteField <string>(collection.Name);
                    writer.WriteField(collection.Version);
                    writer.NextRecord();

                    if (collection.Cards != null && collection.Cards.Any())
                    {
                        textWriter.WriteLine(_delimiter);

                        // Now write the cards
                        writer.WriteHeader <MagicBinderCard>();
                        writer.WriteRecords(collection.Cards);
                    }
                }
            }
        }
Exemplo n.º 2
0
        public void TestMyMagicCollectionCsvWrite()
        {
            var cards = new[] {
                new MagicBinderCard()
                {
                    RowId  = "row1",
                    CardId = "card id 1",
                    Grade  = MagicGrade.LightlyPlayed,
                },
                new MagicBinderCard()
                {
                    RowId         = "row2",
                    CardId        = "card id 2",
                    Grade         = MagicGrade.NearMint,
                    Language      = MagicLanguage.German,
                    QuantityTrade = 1,
                }
            }.ToList();

            var collection = new MagicBinder(cards)
            {
                Name = "My collection",
            };

            var targetFileName = Path.Combine(TestContext.DeploymentDirectory, "CsvTest " + Guid.NewGuid().ToString() + ".csv");
            var target         = new MyMagicCollectionCsv();

            target.WriteFile(targetFileName, collection);

            var referenceFile = GetType().Assembly.LoadEmbeddedResourceTextFile("TestMyMagicCollectionCsvWrite.csv");
            var writtenFile   = File.ReadAllText(targetFileName);

            Assert.AreEqual(referenceFile, writtenFile);
        }
Exemplo n.º 3
0
        public MagicBinder ReadFile(string fileName)
        {
            MagicBinder result = null;

            try
            {
                var    content      = File.ReadAllText(fileName);
                var    delimiterPos = content.IndexOf(_delimiter, StringComparison.InvariantCultureIgnoreCase);
                string header       = "";
                string cards        = "";

                if (delimiterPos >= 0)
                {
                    // delimiter found -> split text
                    header = content.Substring(0, delimiterPos).Trim('\r', '\n');
                    cards  = content.Substring(delimiterPos + _delimiter.Length, content.Length - _delimiter.Length - delimiterPos).Trim('\r', '\n');
                }
                else
                {
                    // no delimiter -> take all
                    header = content;
                }

                IList <MagicBinderCard> collectionCards = new List <MagicBinderCard>();
                if (!string.IsNullOrWhiteSpace(cards))
                {
                    using (var inputCsv = new CsvReader(new StringReader(cards), _config))
                    {
                        inputCsv.Configuration.CultureInfo = CultureInfo.InvariantCulture;
                        collectionCards = inputCsv.GetRecords <MagicBinderCard>().ToList();
                    }
                }

                result = new MagicBinder(Consolidate(collectionCards.Where(c => c.Quantity > 0 || c.QuantityTrade > 0 || c.QuantityWanted > 0)));

                using (var inputCsv = new CsvReader(new StringReader(header), _config))
                {
                    inputCsv.Configuration.CultureInfo = CultureInfo.InvariantCulture;

                    inputCsv.Read();
                    result.Name    = inputCsv.GetField <string>("Name");
                    result.Version = inputCsv.GetField <int>("Version");
                }
            }
            catch (Exception error)
            {
                NotificationCenter.Instance.FireNotification(
                    LogLevel.Error,
                    nameof(MyMagicCollectionCsv) + "ReadFile() failed: " + error.Message);

                result = new MagicBinder();
            }

            return(result);
        }
Exemplo n.º 4
0
 public MagicBinderViewModel(string name, INotificationCenter notificationCenter)
 {
     _notificationCenter = notificationCenter;
     _cards       = new ObservableCollection <MagicBinderCardViewModel>();
     _sortedCards = new Dictionary <string, MagicBinderCardViewModel>();
     if (!string.IsNullOrEmpty(name))
     {
         _magicCollection = new MagicBinder
         {
             Name = name,
         };
     }
 }
Exemplo n.º 5
0
        public void ReadFile(string fileName)
        {
            var loader = new MyMagicCollectionCsv();

            _magicCollection = loader.ReadFile(fileName);

            if (_magicCollection != null)
            {
                _cards = new ObservableCollection <MagicBinderCardViewModel>(_magicCollection.Cards
                                                                             .Select(c =>
                {
                    MagicCardDefinition definition;
                    if (StaticMagicData.CardDefinitionsByCardId.TryGetValue(c.CardId, out definition))
                    {
                        return(new MagicBinderCardViewModel(definition, c));
                    }
                    else
                    {
                        return(null);
                    }
                }).Where(c => c != null));
            }
            else
            {
                _cards = new ObservableCollection <MagicBinderCardViewModel>();
            }

            // Now wrap every card with a view model:

            foreach (var card in _cards)
            {
                card.PriceChanged += Card_PriceChanged;
            }

            _sortedCards = Cards.ToDictionary(c => c.RowId);
            _fileName    = fileName;

            CalculateTotals();
        }