コード例 #1
0
ファイル: NewParser.cs プロジェクト: churchillyik/TravianTool
        private TTInfo ParseTroopDetail(string troopDetail, bool postInVillageTroops, TTroopType troop_type)
        {
            string header = HtmlUtility.GetElement(troopDetail, "thead");
            if (header == null)
            {
                return null;
            }

            string[] headerColumns = HtmlUtility.GetElements(header, "td");
            if (headerColumns.Length != 2)
            {
                return null;
            }

            //	��ò������ԵĴ�ׯ��Ϣ
            Match ownerMatch = Regex.Match(headerColumns[0], @"<a href=""(.+?)"">(.+?)</a>");
            if (!ownerMatch.Success)
            {
                return ParseNatureTroops(troopDetail, headerColumns, postInVillageTroops, troop_type);
            }

            string owner = ownerMatch.Groups[2].Value;
            owner = UnicodeToString(owner);
            int ownerVillageZ = 0;
            string ownerVillageUrl = "";
            Match ownerVillageZMatch = Regex.Match(ownerMatch.Groups[1].Value, @"karte.php\?d=(\d+)");
            if (ownerVillageZMatch.Success)
            {
                ownerVillageZ = Convert.ToInt32(ownerVillageZMatch.Groups[1].Value);
                ownerVillageUrl = ownerVillageZMatch.Groups[0].Value;
            }

            Match nameMatch = Regex.Match(headerColumns[1], @"<a href=""(.+?)"">(.+?)</a>");
            if (!nameMatch.Success)
            {
                return null;
            }

            string name = nameMatch.Groups[2].Value;
            if (name.Contains("<span class=\"coordinates coordinatesWithText\">"))
            {
                string raw_name = name;
                Match nameMch = Regex.Match(
                    raw_name, "([^<]*?)<span class=\"coordinates coordinatesWithText\">" +
                    "<span class=\"coordinatesWrapper\">" +
                    "<span class=\"coordinateX\">\\((.+?)</span>.*?" +
                    "<span class=\"coordinateY\">(.+?)\\)</span>");
                if (!nameMch.Success)
                {
                    return null;
                }

                name = UnicodeToString(nameMch.Groups[1].Value);
                name = name + "(" + nameMch.Groups[2].Value + "|" + nameMch.Groups[3].Value + ")";
            }
            else
            {
                name = UnicodeToString(name);
            }

            //	��ò��ӵ�λ����
            string unitsBody = HtmlUtility.GetElementWithClass(troopDetail, "tbody", "units");
            if (unitsBody == null)
            {
                return null;
            }

            string unitTypeRow = HtmlUtility.GetElement(unitsBody, "tr");
            if (unitTypeRow == null)
            {
                return null;
            }

            Match unitTypeMatch = Regex.Match(unitTypeRow, @"class=""unit u(\d+)""");
            if (!unitTypeMatch.Success)
            {
                return null;
            }

            int unitType = Convert.ToInt32(unitTypeMatch.Groups[1].Value);
            int tribe = unitType / 10 + 1;

            //	��ò��ӵ�λ��
            string unitsdataBody = HtmlUtility.GetElementWithClass(troopDetail, "tbody", "units last");
            if (unitsdataBody == null)
            {
                return null;
            }

            string unitdataRow = HtmlUtility.GetElement(unitsdataBody, "tr");
            if (unitdataRow == null)
            {
                return null;
            }

            string[] unitColumns = HtmlUtility.GetElements(unitdataRow, "td");
            if (unitColumns.Length < 10)
            {
                return null;
            }

            int[] units = new int[11];
            for (int i = 0; i < unitColumns.Length; i++)
            {
                Match match = Regex.Match(unitColumns[i], @"(\d+)");
                if (match.Success)
                {
                    units[i] = Convert.ToInt32(match.Groups[1].Value);
                }
                else if (unitColumns[i].Contains("?"))
                {
                    units[i] = -1;
                }
                else
                {
                    return null;
                }
            }

            string[] infoBodies = HtmlUtility.GetElementsWithClass(troopDetail, "tbody", "infos");
            if (infoBodies.Length == 0)
            {
                return null;
            }

            DateTime arrival = DateTime.MinValue;
            foreach (string infosBody in infoBodies)
            {
                string inDiv = HtmlUtility.GetElementWithClass(infosBody, "div", "in");
                if (inDiv == null)
                {
                    inDiv = HtmlUtility.GetElementWithClass(infosBody, "div", "in small");
                }

                if (inDiv != null)
                {
                    Match match = Regex.Match(inDiv, @"\d+:\d+:\d+");
                    if (match.Success)
                    {
                        arrival = DateTime.Now + TimeSpanParse(match.Groups[0].Value);
                        arrival.AddSeconds(20);
                    }
                }
            }

            TTroopType type = postInVillageTroops ? TTroopType.InOtherVillages : TTroopType.InVillage;
            if (arrival != DateTime.MinValue)
            {
                type = troop_type;
            }

            return new TTInfo()
            {
                Tribe = tribe,
                Owner = owner,
                OwnerVillageZ = ownerVillageZ,
                OwnerVillageUrl = ownerVillageUrl,
                VillageName = name,
                Troops = units,
                FinishTime = arrival,
                TroopType = type,
            };
        }
コード例 #2
0
ファイル: NewParser.cs プロジェクト: churchillyik/TravianTool
        private TTInfo ParseNatureTroops(string troopDetail, string[] headerColumns, bool postInVillageTroops, TTroopType troop_type)
        {
            Match ownerMatch = Regex.Match(headerColumns[0], "<strong>(\\w+)</strong>");
            if (!ownerMatch.Success)
            {
                return null;
            }
            string owner = ownerMatch.Groups[1].Value;
            owner = UnicodeToString(owner);

            Match nameMatch = Regex.Match(headerColumns[1], "<a>([^<]*?)</a>");
            if (!nameMatch.Success)
            {
                return null;
            }
            string name = nameMatch.Groups[1].Value;
            name = UnicodeToString(name);

            //	��ò��ӵ�λ����
            string unitsBody = HtmlUtility.GetElementWithClass(troopDetail, "tbody", "units");
            if (unitsBody == null)
            {
                return null;
            }

            string unitTypeRow = HtmlUtility.GetElement(unitsBody, "tr");
            if (unitTypeRow == null)
            {
                return null;
            }

            Match unitTypeMatch = Regex.Match(unitTypeRow, @"class=""unit u(\d+)""");
            if (!unitTypeMatch.Success)
            {
                return null;
            }

            int unitType = Convert.ToInt32(unitTypeMatch.Groups[1].Value);
            int tribe = unitType / 10 + 1;

            //	��ò��ӵ�λ��
            string unitsdataBody = HtmlUtility.GetElementWithClass(troopDetail, "tbody", "units last");
            if (unitsdataBody == null)
            {
                return null;
            }

            string unitdataRow = HtmlUtility.GetElement(unitsdataBody, "tr");
            if (unitdataRow == null)
            {
                return null;
            }

            string[] unitColumns = HtmlUtility.GetElements(unitdataRow, "td");
            if (unitColumns.Length < 10)
            {
                return null;
            }

            int[] units = new int[11];
            for (int i = 0; i < unitColumns.Length; i++)
            {
                Match match = Regex.Match(unitColumns[i], @"(\d+)");
                if (match.Success)
                {
                    units[i] = Convert.ToInt32(match.Groups[1].Value);
                }
                else if (unitColumns[i].Contains("?"))
                {
                    units[i] = -1;
                }
                else
                {
                    return null;
                }
            }

            string[] infoBodies = HtmlUtility.GetElementsWithClass(troopDetail, "tbody", "infos");
            if (infoBodies.Length == 0)
            {
                return null;
            }

            DateTime arrival = DateTime.MinValue;
            foreach (string infosBody in infoBodies)
            {
                string inDiv = HtmlUtility.GetElementWithClass(infosBody, "div", "in");
                if (inDiv == null)
                {
                    inDiv = HtmlUtility.GetElementWithClass(infosBody, "div", "in small");
                }

                if (inDiv != null)
                {
                    Match match = Regex.Match(inDiv, @"\d+:\d+:\d+");
                    if (match.Success)
                    {
                        arrival = DateTime.Now + TimeSpanParse(match.Groups[0].Value);
                        arrival.AddSeconds(20);
                    }
                }
            }

            TTroopType type = postInVillageTroops ? TTroopType.InOtherVillages : TTroopType.InVillage;
            if (arrival != DateTime.MinValue)
            {
                type = troop_type;
            }

            return new TTInfo()
            {
                Tribe = tribe,
                Owner = owner,
                OwnerVillageZ = 0,
                OwnerVillageUrl = "",
                VillageName = name,
                Troops = units,
                FinishTime = arrival,
                TroopType = type,
            };
        }