示例#1
0
 public static Deck CreateDeck(this Game game)
 {
     var deck = new Deck { GameId = game.Id };
     deck.Sections = game.DeckSections.Select(x => new Section { Name = x.Value.Name.Clone() as string, Cards = new List<IMultiCard>(), Shared = x.Value.Shared }).ToList();
     deck.Sections = deck.Sections.Concat(game.SharedDeckSections.Select(x => new Section { Name = x.Value.Name.Clone() as string, Cards = new List<IMultiCard>(), Shared = x.Value.Shared })).ToList();
     return deck;
 }
示例#2
0
 public static IDeck Load(this IDeck deck, string path, bool cloneCards = true)
 {
     var ret = new Deck();
     try
     {
         Game game = null;
         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);
             game = Octgn.Core.DataManagers.GameManager.Get().GetById(gameId);
             if (game == null)
             {
                 throw new UserMessageException("Could not load deck from {0}, you do not have the associated game installed.", path);
             }
         }
         return deck.Load(game, path,cloneCards);
     }
     catch (UserMessageException)
     {
         throw;
     }
     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 null;
 }
示例#3
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;
        }
示例#4
0
 public static Deck CreateDeck(this Game game)
 {
     var deck = new Deck { GameId = game.Id };
     deck.Sections = game.DeckSections.Select(x=> new Section{Name=x.Value.Name,Cards = new List<IMultiCard>()}).ToList();
     return deck;
 }
示例#5
0
        /// <summary>
        /// This happens when the menu item is clicked.
        /// </summary>
        /// <param name="con"></param>
        public void OnClick(IDeckBuilderPluginController con)
        {
            var curDeck = con.GetLoadedDeck();

            if(curDeck != null)
                MessageBox.Show(String.Format("{0}",curDeck.CardCount()));

            // Find the first game with cards in it.
            var game = con.Games.Games.FirstOrDefault(x => x.AllCards().Count() > 0);
            if (game == null)
            {
                MessageBox.Show("No Games Installed?!?!?");
                return;
            }
            // Before we make a deck, we need to make sure we load the correct game for the deck.
            con.SetLoadedGame(game);

            // Select a random card from the games card list.
            var cm = game.AllCards().First();
            var d = new Deck();
            d.GameId = game.Id;

            // It's weird, but this is how you add a card.
            var multiCard = cm.ToMultiCard(1);
            var secArray = d.Sections.ToArray();
            secArray[0].Cards.AddCard(multiCard);
            d.Sections = secArray;
            // Load that mother.
            con.LoadDeck(d);
        }
示例#6
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;
 }
示例#7
0
        private void Open(object sender, RoutedEventArgs e)
        {
            e.Handled = true;

            var loadDirectory = Program.GameEngine.Definition.GetDefaultDeckPath();

            // Show the dialog to choose the file

            var ofd = new OpenFileDialog
                          {
                              Filter = "Octgn deck files (*.o8d) | *.o8d",
                              InitialDirectory = loadDirectory
                          };
            //ofd.InitialDirectory = Program.Game.Definition.DecksPath;
            if (ofd.ShowDialog() != true) return;
            // Try to load the file contents
            try
            {
                var game = GameManager.Get().GetById(Program.GameEngine.Definition.Id);
                var newDeck = new Deck().Load(game, ofd.FileName);
                //DataNew.Entities.Deck newDeck = Deck.Load(ofd.FileName,
                //                         Program.GamesRepository.Games.First(g => g.Id == Program.Game.Definition.Id));
                // Load the deck into the game
                Program.GameEngine.LoadDeck(newDeck);
                if (!String.IsNullOrWhiteSpace(newDeck.Notes))
                {
                    this.table.AddNote(100,0,newDeck.Notes);
                }
            }
            catch (DeckException ex)
            {
                TopMostMessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
            catch (Exception ex)
            {
                TopMostMessageBox.Show("Octgn couldn't load the deck.\r\nDetails:\r\n\r\n" + ex.Message, "Error",
                                MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
示例#8
0
 public static Deck CreateDeck(this Game game)
 {
     var deck = new Deck { GameId = game.Id };
     deck.Sections = game.DeckSections.Select(x=> new Section{Name=x}).ToList();
     return deck;
 }
示例#9
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;
        }