Пример #1
0
 public static void Open(Definitions.GameDef game, bool readOnly)
 {
     openedGame = Program.GamesRepository.Games.First(g => g.Id == game.Id);
     openedGame.OpenDatabase(readOnly);
 }
Пример #2
0
        private static Deck LoadCore(XDocument doc, Game game)
        {
            Deck deck;
            try
            {
            var isShared = doc.Root.Attr<bool>("shared");
            var defSections = isShared ? game.SharedDeckSections : game.DeckSections;

            deck = new Deck { GameId = game.Id, IsShared = isShared };
                var sections = from section in doc.Root.Elements("section")
                                             select new Section(false)
                                             {
                                                 Name = section.Attribute("name").Value,
                                                 Cards = new ObservableCollection<Element>
                                                                 (from card in section.Elements("card")
                                                                    select new Element
                                                                    {
                                                                        loadedId = card.Attr<string>("id"),
                                                                        loadedName = card.Value,
                                                                        Quantity = card.Attr<byte>("qty", 1)
                                                                    })
                                             };
                Section[] 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(); }

            bool isDbClosed = !game.IsDatabaseOpen;
            try
            {
                if (isDbClosed)
                    game.OpenDatabase(true);
                // Matches with actual cards in database
                foreach (Section s in deck.Sections)
                    foreach (Element e in s.Cards)
                        try
                        {
                            // First try by id, if one is provided
                            if (e.loadedId != null) e.Card = game.GetCardById(new Guid(e.loadedId));
                            // If there's no id, or if it doesn't match a card in the database, try to fallback on the name
                            if (e.Card == null) e.Card = game.GetCardByName(e.loadedName);
                            // If we still can't find the card, report an error
                            if (e.Card == null)
                                throw new UnknownCardException(e.loadedId, e.loadedName);
                        }
                        catch (FormatException)
                        { throw new InvalidFileFormatException(string.Format("Could not parse card id {0}.", e.loadedId)); }
            }
            finally
            {
                if (isDbClosed)
                    game.CloseDatabase();
            }

            return deck;
        }