/// <summary> /// Loads all decks from the XML files found in the program directory, /// if these are valid. /// </summary> /// <returns>A list of decks that could not be loaded, /// so this can be presented to the user</returns> public void LoadDecks() { _decks.Clear(); string[] xmlFiles = Directory.GetFiles(_deckDirectory, "*.xml"); ValidationEventHandler schemaValidationHandler = new ValidationEventHandler(SchemaValidationEventHandler); ValidationEventHandler deckValidationHandler = new ValidationEventHandler(DeckValidationEventHandler); XmlSchema schema = XmlSchema.Read(new StringReader(Properties.Resources.deckSchema), schemaValidationHandler); XmlReaderSettings settings = new XmlReaderSettings(); settings.Schemas.Add(schema); settings.ValidationType = ValidationType.Schema; foreach (string xmlFile in xmlFiles) { // Assume the deck file is valid until proven wrong. _validDeckFile = true; Deck deck = new Deck(xmlFile); XmlReader xmlReader = XmlReader.Create(xmlFile, settings); try { deck.XmlDoc.Load(xmlReader); if (_validCardsSchema) deck.XmlDoc.Validate(deckValidationHandler); else _validDeckFile = false; } catch (Exception) { // If the default deck has been messed up, we can always // reset it. We know the embedded default.xml will always // be valid, so there's no need to validate it against the // schema. if (deck.FilePath.Contains("default.xml")) { // We'll need to close the XML reader early, to access // the default.xml file. xmlReader.Close(); deck.XmlDoc.LoadXml(Properties.Resources.defaultDeck); deck.XmlDoc.Save(deck.FilePath); _validDeckFile = true; } // If it's a custom deck, we simply mark it as invalid. else { _validDeckFile = false; } } finally { // Close the XML reader if it hasn't already been. if (xmlReader.ReadState != ReadState.Closed) xmlReader.Close(); } if (_validDeckFile) { deck.Name = deck.XmlDoc.DocumentElement.Attributes["name"].Value; foreach (XmlNode cardNode in deck.XmlDoc.DocumentElement.ChildNodes) { int id = Convert.ToInt32(cardNode.Attributes["id"].Value); // We only want to load cards that haven't been played. bool played = cardNode["played"].InnerText == "1" ? true : false; if (!played) { Card card = new Card(); card.ID = id; card.Value = cardNode["value"].InnerText; switch (cardNode.Name) { case "character": card.Type = CardType.CHARACTER; break; case "location": card.Type = CardType.LOCATION; break; case "task": card.Type = CardType.TASK; card.Value = "must " + card.Value; break; case "modifier": card.Type = CardType.MODIFIER; break; case "minorTwist": card.Type = CardType.MINOR_TWIST; break; case "majorTwist": card.Type = CardType.MAJOR_TWIST; break; } deck.GetCardsOfType(card.Type).Add(card); } } _decks.Add(deck); } } }
/// <summary> /// Marks the cards played during one OCS session as such in the XML file of the current deck. /// </summary> /// <param name="cards">The cards to be marked as played</param> public void MarkSessionCardsAsPlayed(Card[] cards) { foreach (Card card in cards) { if (card != null) { foreach (XmlNode cardNode in _currentDeck.XmlDoc.DocumentElement.ChildNodes) { if (cardNode.Attributes["id"].Value == card.ID.ToString()) cardNode["played"].InnerText = "1"; } } } _currentDeck.XmlDoc.Save(_currentDeck.FilePath); }
/// <summary> /// Loads all cards from the "deck", picks one of /// each type and "draws" the chosen cards. /// </summary> /// <returns>Boolean indicated whether drawing the cards succeeded</returns> private bool DrawCards() { try { _character = Dealer.GetInstance.PickCard(CardType.CHARACTER); _location = Dealer.GetInstance.PickCard(CardType.LOCATION); _task = Dealer.GetInstance.PickCard(CardType.TASK); _modifier = Dealer.GetInstance.PickCard(CardType.MODIFIER); _plotTwist1 = Dealer.GetInstance.PickCard(CardType.MINOR_TWIST); _plotTwist2 = Dealer.GetInstance.PickCard(CardType.MAJOR_TWIST); cardChar.BackgroundImage = Properties.Resources.card_front; cardLocation.BackgroundImage = Properties.Resources.card_front; cardTask.BackgroundImage = Properties.Resources.card_front; cardModifier.BackgroundImage = Properties.Resources.card_front; lblCharacter.Text = _character.ToString(); lblLocation.Text = _location.ToString(); lblTask.Text = _task.ToString(); lblModifier.Text = _modifier.ToString(); lblPlotTwist1.Text = _plotTwist1.ToString(); lblPlotTwist2.Text = _plotTwist2.ToString(); _soundPlayer = new SoundPlayer(Properties.Resources.card); _soundPlayer.Play(); return true; } catch (Exception e) { MessageBox.Show(this, e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return false; } }