Example #1
0
 public static IDeck Load(this IDeck deck, Game game, string path)
 {
     var ret = new Deck();
     ret.Sections = new List<ISection>();
     try
     {
         var cards = game.Sets().SelectMany(x => x.Cards).ToArray();
         using (var fs = File.Open(path, FileMode.Open, FileAccess.ReadWrite, FileShare.None))
         {
             var doc = XDocument.Load(fs);
             var gameId = Guid.Parse(doc.Descendants("deck").First().Attribute("game").Value);
             var shared = doc.Descendants("deck").First().Attr<bool>("shared");
             foreach (var sectionelem in doc.Descendants("section"))
             {
                 var section = new Section();
                 section.Cards = new List<IMultiCard>();
                 section.Name = sectionelem.Attribute("name").Value;
                 foreach (var cardelem in sectionelem.Descendants("card"))
                 {
                     var cardId = Guid.Parse(cardelem.Attribute("id").Value);
                     var cardq = Int32.Parse(cardelem.Attribute("qty").Value);
                     var card = cards.FirstOrDefault(x => x.Id == cardId);
                     if (card == null)
                     {
                         var cardN = cardelem.Value;
                         card = cards.FirstOrDefault(x => x.Name.Equals(cardN, StringComparison.CurrentCultureIgnoreCase));
                         if(card == null)
                             throw new UserMessageException(
                                 "Problem loading deck {0}. The card with id: {1} and name: {2} is not installed.", path, cardId, cardN);
                     }
                     (section.Cards as IList<IMultiCard>).Add(card.ToMultiCard(cardq));
                 }
                 (ret.Sections as List<ISection>).Add(section);
             }
             foreach (var section in shared ? game.SharedDeckSections : game.DeckSections)
             {
                 if (ret.Sections.Any(x => x.Name == section.Value.Name) == false)
                     (ret.Sections as List<ISection>).Add(
                         new Section { Name = section.Value.Name, Cards = new List<IMultiCard>() });
             }
             ret.GameId = gameId;
             ret.IsShared = shared;
         }
         deck = ret;
         return deck;
     }
     catch (UserMessageException)
     {
         throw;
     }
     catch (FormatException e)
     {
         Log.Error(String.Format("Problem loading deck from path {0}", path), e);
         throw new UserMessageException("The deck {0} is corrupt.",path);
     }
     catch (NullReferenceException e)
     {
         Log.Error(String.Format("Problem loading deck from path {0}", path), e);
         throw new UserMessageException("The deck {0} is corrupt.",path);
     }
     catch (XmlException e)
     {
         Log.Error(String.Format("Problem loading deck from path {0}", path), e);
         throw new UserMessageException("The deck {0} is corrupt.",path);
     }
     catch (FileNotFoundException)
     {
         throw new UserMessageException("Could not save deck to {0}, could not file the file.", path);
     }
     catch (IOException e)
     {
         Log.Error(String.Format("Problem loading deck from path {0}", path), e);
         throw new UserMessageException("Could not load deck from {0}, {1}", path, e.Message);
     }
     catch (Exception e)
     {
         Log.Error(String.Format("Problem loading deck from path {0}", path), e);
         throw new UserMessageException("Could not load deck from {0}, there was an unspecified problem.", path);
     }
     return ret;
 }
Example #2
0
        public static IDeck Load(this IDeck deck, Game game, string path, bool cloneCards = true)
        {
            var ret = new Deck();
            ret.Sections = new List<ISection>();
            try
            {
                var cards = game.Sets().SelectMany(x => x.Cards).ToArray();
                using (var fs = File.Open(path, FileMode.Open, FileAccess.ReadWrite, FileShare.None))
                {
                    var doc = XDocument.Load(fs);
                    var gameId = Guid.Parse(doc.Descendants("deck").First().Attribute("game").Value);
                    var shared = doc.Descendants("deck").First().Attr<bool>("shared");
                    foreach (var sectionelem in doc.Descendants("section"))
                    {
                        var section = new Section();
                        section.Cards = new List<IMultiCard>();
                        section.Name = sectionelem.Attribute("name").Value;
                        section.Shared = sectionelem.Attr<bool>("shared");
                        // On old style decks, if it's shared, then all subsequent sections are shared
                        if (shared)
                            section.Shared = true;
                        foreach (var cardelem in sectionelem.Descendants("card"))
                        {
                            var cardId = Guid.Parse(cardelem.Attribute("id").Value);
                            var cardq = Int32.Parse(cardelem.Attribute("qty").Value);
                            var card = cards.FirstOrDefault(x => x.Id == cardId);
                            if (card == null)
                            {
                                var cardN = cardelem.Value;
                                card = cards.FirstOrDefault(x => x.Name.Equals(cardN, StringComparison.CurrentCultureIgnoreCase));
                                if (card == null)
                                    throw new UserMessageException(
                                        "Problem loading deck {0}. The card with id: {1} and name: {2} is not installed.", path, cardId, cardN);
                            }
                            (section.Cards as IList<IMultiCard>).Add(card.ToMultiCard(cardq, cloneCards));
                        }
                        if(section.Cards.Any())
                            (ret.Sections as List<ISection>).Add(section);
                    }
                    // Add deck notes
                    var notesElem = doc.Descendants("notes").FirstOrDefault();
                    if (notesElem != null)
                    {
                        var cd = (notesElem.FirstNode as XCData);
                        if (cd != null)
                        {
                            ret.Notes = cd.Value.Clone() as string;
                        }
                    }
                    if (ret.Notes == null) ret.Notes = "";

                    // Add all missing sections so that the game doesn't get pissed off
                    {
                        var combinedList =
                            game.DeckSections.Select(x => x.Value).Concat(game.SharedDeckSections.Select(y => y.Value));
                        foreach (var section in combinedList)
                        {
                            if (ret.Sections.Any(x => x.Name.Equals(section.Name, StringComparison.InvariantCultureIgnoreCase) && x.Shared == section.Shared) == false)
                            {
                                // Section not defined in the deck, so add an empty version of it.
                                (ret.Sections as List<ISection>).Add(
                                    new Section
                                    {
                                        Name = section.Name.Clone() as string,
                                        Cards = new List<IMultiCard>(),
                                        Shared = section.Shared
                                    });
                            }
                        }
                    }
                    ret.GameId = gameId;
                    ret.IsShared = shared;
                }
                // This is an old style shared deck file, we need to convert it now, for posterity sake.
                if (ret.IsShared)
                {
                    ret.IsShared = false;
                    ret.Save(game, path);
                }
                deck = ret;
                return deck;
            }
            catch (UserMessageException)
            {
                throw;
            }
            catch (FormatException e)
            {
                Log.Error(String.Format("Problem loading deck from path {0}", path), e);
                throw new UserMessageException("The deck {0} is corrupt.", path);
            }
            catch (NullReferenceException e)
            {
                Log.Error(String.Format("Problem loading deck from path {0}", path), e);
                throw new UserMessageException("The deck {0} is corrupt.", path);
            }
            catch (XmlException e)
            {
                Log.Error(String.Format("Problem loading deck from path {0}", path), e);
                throw new UserMessageException("The deck {0} is corrupt.", path);
            }
            catch (FileNotFoundException)
            {
                throw new UserMessageException("Could not save deck to {0}, could not file the file.", path);
            }
            catch (IOException e)
            {
                Log.Error(String.Format("Problem loading deck from path {0}", path), e);
                throw new UserMessageException("Could not load deck from {0}, {1}", path, e.Message);
            }
            catch (Exception e)
            {
                Log.Error(String.Format("Problem loading deck from path {0}", path), e);
                throw new UserMessageException("Could not load deck from {0}, there was an unspecified problem.", path);
            }
            return ret;
        }
Example #3
0
        public static Deck LoadDeck(this Game game, string filename)
        {
            if (game == null) throw new ArgumentNullException("game");

            XDocument doc;
            Guid gameId = new Guid();
            try
            {
                doc = XDocument.Load(filename);
            }
            catch (Exception e)
            {
                throw new FileNotReadableException(e);
            }

            if (doc.Root != null)
            {
                XAttribute gameAttribute = doc.Root.Attribute("game");
                if (gameAttribute == null)
                    throw new InvalidFileFormatException("The <deck> tag is missing the 'game' attribute");

                try
                {
                    gameId = new Guid(gameAttribute.Value);
                }
                catch
                {
                    throw new InvalidFileFormatException("The game attribute is not a valid GUID");
                }
            }

            if (gameId != game.Id) throw new WrongGameException(gameId, game.Id.ToString());

            Deck deck;
            try
            {
                var isShared = doc.Root.Attr<bool>("shared");
                IEnumerable<string> defSections = isShared ? game.SharedDeckSections : game.DeckSections;

                deck = new Deck { GameId = game.Id, IsShared = isShared };
                if (doc.Root != null)
                {
                    IEnumerable<Section> sections = from section in doc.Root.Elements("section")
                                                    let xAttribute = section.Attribute("name")
                                                    where xAttribute != null
                                                    select new Section()
                                                    {
                                                        Name = xAttribute.Value,
                                                        Cards = new ObservableCollection<MultiCard>
                                                            (from card in section.Elements("card")
                                                             select new MultiCard
                                                             {
                                                                 Id = new Guid(card.Attr<string>("id")),
                                                                 Name = card.Value,
                                                                 Quantity =card.Attr<byte>("qty", 1)
                                                             })
                                                    };
                    var allSections = new Section[defSections.Count()];
                    int i = 0;
                    foreach (string sectionName in defSections)
                    {
                        allSections[i] = sections.FirstOrDefault(x => x.Name == sectionName);
                        if (allSections[i] == null) allSections[i] = new Section { Name = sectionName };
                        ++i;
                    }
                    deck.Sections = allSections;
                }
            }
            catch
            {
                throw new InvalidFileFormatException();
            }
            // Matches with actual cards in database

            foreach (var sec in deck.Sections)
            {
                var newList = (from e in sec.Cards let card = game.GetCardById(e.Id) select card.ToMultiCard(e.Quantity)).ToList();
                sec.Cards = newList;
            }

            return deck;
        }