Ejemplo n.º 1
0
        public static IEnumerable <Card> ParseForCards(string xml_folder)
        {
            Directory.GetFiles(xml_folder);
            foreach (string file in Directory.GetFiles(xml_folder))
            {
                StringBuilder sb = new StringBuilder();
                using (StreamReader sr = new StreamReader(file))
                {
                    String line;
                    while ((line = sr.ReadLine()) != null)
                    {
                        sb.AppendLine(line);
                    }
                }
                string      xmlstring = sb.ToString();
                XmlDocument xml       = new XmlDocument();
                xml.LoadXml(xmlstring);
                XmlNodeList elemList = xml.FirstChild.ChildNodes;
                Card        card     = new Card();
                for (int i = 0; i < elemList.Count; i++)
                {
                    switch (elemList[i].Name)
                    {
                    case "id":
                        card.Id = Convert.ToInt32(elemList[i].InnerText);
                        break;

                    case "name":
                        card.Name = elemList[i].InnerText;
                        break;

                    case "age":
                        card.Age = (Age)Enum.Parse(typeof(Age), elemList[i].InnerText);
                        break;

                    case "color":
                        card.Color = (CardColor)Enum.Parse(typeof(CardColor), elemList[i].InnerText);
                        break;

                    case "military":
                        card.Military = Convert.ToInt32(elemList[i].InnerText);
                        break;

                    case "tech":
                        card.Tech = (Tech)Enum.Parse(typeof(Tech), elemList[i].InnerText);
                        break;

                    case "etb":
                    {
                        Card.Calculator.PerMode mode    = Card.Calculator.PerMode.Single;
                        Card.Calculator.Targets targets = Card.Calculator.Targets.Self;
                        int value = Convert.ToInt32(elemList[i].InnerText);
                        foreach (XmlAttribute attr in elemList[i].Attributes)
                        {
                            switch (attr.Name)
                            {
                            case "PerMode":
                            {
                                mode = (Card.Calculator.PerMode)Enum.Parse(typeof(Card.Calculator.PerMode), attr.Value);
                                break;
                            }

                            case "Targets":
                            {
                                targets = (Card.Calculator.Targets)Enum.Parse(typeof(Card.Calculator.Targets), attr.Value);
                                break;
                            }
                            }
                        }
                        card.ETB = new Card.Calculator()
                        {
                            mode = mode, targets = targets, value = value
                        };
                        break;
                    }

                    case "fame":
                    {
                        Card.Calculator.PerMode mode    = Card.Calculator.PerMode.Single;
                        Card.Calculator.Targets targets = Card.Calculator.Targets.Self;
                        int value = Convert.ToInt32(elemList[i].InnerText);
                        foreach (XmlAttribute attr in elemList[i].Attributes)
                        {
                            switch (attr.Name)
                            {
                            case "PerMode":
                            {
                                mode = (Card.Calculator.PerMode)Enum.Parse(typeof(Card.Calculator.PerMode), attr.Value);
                                break;
                            }

                            case "Targets":
                            {
                                targets = (Card.Calculator.Targets)Enum.Parse(typeof(Card.Calculator.Targets), attr.Value);
                                break;
                            }
                            }
                        }
                        card.Fame = new Card.Calculator()
                        {
                            mode = mode, targets = targets, value = value
                        };
                        break;
                    }

                    case "cost":
                        card.Cost = elemList[i].InnerText;
                        break;

                    case "players":
                        card.Players = Convert.ToInt32(elemList[i].InnerText);
                        break;
                    }
                }
                yield return(card);
            }
        }
Ejemplo n.º 2
0
        // костыль
        // второй параметр false - парсит все чудеса, иначе только рандомную сторону
        public static IEnumerable <Wonder> ParseForWonders(string xml_folder, bool random)
        {
            // !!!
            // говнокод
            foreach (string dir in Directory.GetDirectories(xml_folder))
            {
                List <string> tmp = new List <string>();
                if (random)
                {
                    tmp.Add((Guid.NewGuid().GetHashCode() % 2 == 0) ? Directory.GetDirectories(dir).First() : Directory.GetDirectories(dir).Last());
                }
                else
                {
                    tmp.Add(Directory.GetDirectories(dir).First());
                    tmp.Add(Directory.GetDirectories(dir).Last());
                }
                foreach (string wonder_path in tmp)
                {
                    string        filename = Directory.GetFiles(wonder_path, "*.xml").First();
                    StringBuilder sb       = new StringBuilder();
                    using (StreamReader sr = new StreamReader(filename))
                    {
                        String line;
                        while ((line = sr.ReadLine()) != null)
                        {
                            sb.AppendLine(line);
                        }
                    }
                    Wonder      wonder    = new Wonder();
                    string      xmlstring = sb.ToString();
                    XmlDocument xml       = new XmlDocument();
                    xml.LoadXml(xmlstring);
                    var wondernode = xml.FirstChild;
                    wonder.Id   = Convert.ToInt32(wondernode.Attributes["Id"].Value);
                    wonder.Name = wondernode.Attributes["Name"].Value;
                    foreach (XmlNode child in wondernode.ChildNodes)
                    {
                        Card card = new Card();
                        foreach (XmlNode card_node in child.ChildNodes)
                        {
                            switch (card_node.Name)
                            {
                            case "military":
                                card.Military = Convert.ToInt32(card_node.InnerText);
                                break;

                            case "tech":
                                card.Tech = (Tech)Enum.Parse(typeof(Tech), card_node.InnerText);
                                break;

                            case "etb":
                            {
                                Card.Calculator.PerMode mode    = Card.Calculator.PerMode.Single;
                                Card.Calculator.Targets targets = Card.Calculator.Targets.Self;
                                int value = Convert.ToInt32(card_node.InnerText);
                                foreach (XmlAttribute attr in card_node.Attributes)
                                {
                                    switch (attr.Name)
                                    {
                                    case "PerMode":
                                    {
                                        mode = (Card.Calculator.PerMode)Enum.Parse(typeof(Card.Calculator.PerMode), attr.Value);
                                        break;
                                    }

                                    case "Targets":
                                    {
                                        targets = (Card.Calculator.Targets)Enum.Parse(typeof(Card.Calculator.Targets), attr.Value);
                                        break;
                                    }
                                    }
                                }
                                card.ETB = new Card.Calculator()
                                {
                                    mode = mode, targets = targets, value = value
                                };
                                break;
                            }

                            case "fame":
                            {
                                Card.Calculator.PerMode mode    = Card.Calculator.PerMode.Single;
                                Card.Calculator.Targets targets = Card.Calculator.Targets.Self;
                                int value = Convert.ToInt32(card_node.InnerText);
                                foreach (XmlAttribute attr in card_node.Attributes)
                                {
                                    switch (attr.Name)
                                    {
                                    case "PerMode":
                                    {
                                        mode = (Card.Calculator.PerMode)Enum.Parse(typeof(Card.Calculator.PerMode), attr.Value);
                                        break;
                                    }

                                    case "Targets":
                                    {
                                        targets = (Card.Calculator.Targets)Enum.Parse(typeof(Card.Calculator.Targets), attr.Value);
                                        break;
                                    }
                                    }
                                }
                                card.Fame = new Card.Calculator()
                                {
                                    mode = mode, targets = targets, value = value
                                };
                                break;
                            }

                            case "cost":
                                card.Cost = card_node.InnerText;
                                break;
                            }
                        }
                        wonder.Tiers.Add(card);
                    }
                    yield return(wonder);
                }
            }
        }