示例#1
0
        public void InitThread(IGetFromWebNotify notifier)
        {
            m_thread = new Thread
                (
                    delegate()
                    {
                        if (!Singleton<CardRepository>.Instance.LoadCards())
                        {
                            Singleton<CardRepository>.Instance.GetFromWeb(notifier);
                        }

                        Singleton<ImageRepostiry>.Instance.LoadImages(Singleton<CardRepository>.Instance.Cards);
                    }
                );
            m_thread.Start();
        }
示例#2
0
        public void InitThread(IGetFromWebNotify notifier)
        {
            m_thread = new Thread
                       (
                delegate()
            {
                if (!Singleton <CardRepository> .Instance.LoadCards())
                {
                    Singleton <CardRepository> .Instance.GetFromWeb(notifier);
                }

                Singleton <ImageRepostiry> .Instance.LoadImages(Singleton <CardRepository> .Instance.Cards);
            }
                       );
            m_thread.Start();
        }
示例#3
0
        public void GetFromWeb(IGetFromWebNotify notifier)
        {
            Directory.CreateDirectory(Config.ImagePath);

            if (notifier != null)
                notifier.Notity(String.Format("���� {0}", Config.Uri), 0.0f);
            WebClient webClient = new WebClient();
            webClient.Encoding = Encoding.UTF8;
            String strHtml = webClient.DownloadString(Config.Uri);

            if (notifier != null)
                notifier.Notity("����html�ĵ�", 0.0f);
            Lexer lexer = new Lexer(strHtml);
            Parser parser = new Parser(lexer);
            AndFilter andFilter = new AndFilter(new NodeClassFilter(typeof(TableRow)), new OrFilter(new HasAttributeFilter("class", "even"), new HasAttributeFilter("class", "odd")));
            NodeList htmlNodes = parser.ExtractAllNodesThatMatch(andFilter);
            lock (this)
            {
                m_Cards = new List<Card>();
                foreach (INode node in htmlNodes.ToNodeArray())
                {
                    int iFiledIndex = 0;
                    Card card = new Card();
                    foreach (INode subNode in node.Children.ToNodeArray())
                    {
                        if (subNode is TextNode)
                        {
                            continue;
                        }

                        switch (iFiledIndex)
                        {
                            case 0:
                                card.ID = Convert.ToInt32(subNode.FirstChild.GetText());
                                card.ImagePath = Path.Combine(Config.ImagePath, card.ID.ToString() + ".jpg");
                                break;
                            case 1:
                                card.Name = subNode.FirstChild.FirstChild.GetText();
                                break;
                            case 2:
                                StringHelper.FillCardLongInfo(subNode.FirstChild.GetText(), card);
                                break;
                            case 3:
                                if (subNode.FirstChild != null)
                                {
                                    card.ManaCost = subNode.FirstChild.GetText();
                                }
                                else
                                {
                                    card.ManaCost = String.Empty;
                                }
                                break;
                            case 4:
                                card.Rare = subNode.FirstChild.GetText();
                                break;
                        }

                        iFiledIndex++;
                    }
                    m_Cards.Add(card);
                }
            }

            XmlSerializer s = new XmlSerializer(typeof(List<Card>));
            FileStream fstream = new FileStream(Config.CardsXml, FileMode.CreateNew);
            s.Serialize(fstream, m_Cards);
            fstream.Close();

            foreach (Card card in m_Cards)
            {
                if (notifier != null)
                    notifier.Notity(String.Format("��ȡ��Ƭ\"{0}\"��Ϣ", card.Name), 1.0f / m_Cards.Count);
                webClient.DownloadFile(Path.Combine(Config.BaseImageUri, card.ID.ToString() + ".jpg"), card.ImagePath);
            }
        }
示例#4
0
        public void GetFromWeb(IGetFromWebNotify notifier)
        {
            Directory.CreateDirectory(Config.ImagePath);

            if (notifier != null)
            {
                notifier.Notity(String.Format("下载 {0}", Config.Uri), 0.0f);
            }
            WebClient webClient = new WebClient();

            webClient.Encoding = Encoding.UTF8;
            String strHtml = webClient.DownloadString(Config.Uri);

            if (notifier != null)
            {
                notifier.Notity("解析html文档", 0.0f);
            }
            Lexer     lexer     = new Lexer(strHtml);
            Parser    parser    = new Parser(lexer);
            AndFilter andFilter = new AndFilter(new NodeClassFilter(typeof(TableRow)), new OrFilter(new HasAttributeFilter("class", "even"), new HasAttributeFilter("class", "odd")));
            NodeList  htmlNodes = parser.ExtractAllNodesThatMatch(andFilter);

            lock (this)
            {
                m_Cards = new List <Card>();
                foreach (INode node in htmlNodes.ToNodeArray())
                {
                    int  iFiledIndex = 0;
                    Card card        = new Card();
                    foreach (INode subNode in node.Children.ToNodeArray())
                    {
                        if (subNode is TextNode)
                        {
                            continue;
                        }

                        switch (iFiledIndex)
                        {
                        case 0:
                            card.ID        = Convert.ToInt32(subNode.FirstChild.GetText());
                            card.ImagePath = Path.Combine(Config.ImagePath, card.ID.ToString() + ".jpg");
                            break;

                        case 1:
                            card.Name = subNode.FirstChild.FirstChild.GetText();
                            break;

                        case 2:
                            StringHelper.FillCardLongInfo(subNode.FirstChild.GetText(), card);
                            break;

                        case 3:
                            if (subNode.FirstChild != null)
                            {
                                card.ManaCost = subNode.FirstChild.GetText();
                            }
                            else
                            {
                                card.ManaCost = String.Empty;
                            }
                            break;

                        case 4:
                            card.Rare = subNode.FirstChild.GetText();
                            break;
                        }

                        iFiledIndex++;
                    }
                    m_Cards.Add(card);
                }
            }

            XmlSerializer s       = new XmlSerializer(typeof(List <Card>));
            FileStream    fstream = new FileStream(Config.CardsXml, FileMode.CreateNew);

            s.Serialize(fstream, m_Cards);
            fstream.Close();

            foreach (Card card in m_Cards)
            {
                if (notifier != null)
                {
                    notifier.Notity(String.Format("获取卡片\"{0}\"信息", card.Name), 1.0f / m_Cards.Count);
                }
                webClient.DownloadFile(Path.Combine(Config.BaseImageUri, card.ID.ToString() + ".jpg"), card.ImagePath);
            }
        }