/// <summary>
        /// 由搜索结果获得某人某场比赛的具体成绩
        /// </summary>
        /// <param name="web_result"></param>
        /// <returns></returns>
        private static CfContestDetail CreateContestDetail(string web_result)
        {
            var contestDetail = new CfContestDetail();

            string regexPattern = "<td([\\s\\S]*?)</td>";
            Regex reg = new Regex(regexPattern, RegexOptions.Compiled);
            var matchs = reg.Matches(web_result);
            if (matchs.Count <= 0)
                return null;

            contestDetail.ProblemNum = matchs.Count - 4;

            string sRank = ">\\s*?(\\d+)";
            Regex rRank = new Regex(sRank, RegexOptions.Compiled);
            var match = rRank.Match(matchs[0].Result("$1"));
            contestDetail.Rank = Convert.ToInt32(match.Result("$1"));

            string sName = "<a href=.*? title=.*? class=.*?>(.*?)</a>";
            Regex rName = new Regex(sName, RegexOptions.Compiled);
            match = rName.Match(matchs[1].Result("$1"));
            contestDetail.UserName = match.Result("$1");

            string sPoint = "<span style=\"font-weight:bold;\">(\\d*?)</span>";
            Regex rPoint = new Regex(sPoint, RegexOptions.Compiled);
            match = rPoint.Match(matchs[2].Result("$1"));
            if (match.Success)
                contestDetail.Point = Convert.ToInt32(match.Result("$1"));
            else
                contestDetail.Point = 0;

            string sChallengeSucceed = "<span class=\"successfullChallengeCount\" title=\"Successful hacking attempts\">\\+(\\d*?)</span>";
            Regex rChallengeSucceed = new Regex(sChallengeSucceed, RegexOptions.Compiled);
            match = rChallengeSucceed.Match(matchs[3].Result("$1"));
            if (match.Success)
                contestDetail.ChallengeSucceed = Convert.ToInt32(match.Result("$1"));
            else
                contestDetail.ChallengeSucceed = 0;

            string sChallengeFailed = "<span class=\"unsuccessfullChallengeCount\" title=\"Unsuccessful hacking attempts\">-(\\d*?)</span>";
            Regex rChallengeFailed = new Regex(sChallengeFailed, RegexOptions.Compiled);
            match = rChallengeFailed.Match(matchs[3].Result("$1"));
            if (match.Success)
                contestDetail.ChallengeFailed = Convert.ToInt32(match.Result("$1"));
            else
                contestDetail.ChallengeFailed = 0;

            //5/6 problems to be fetched

            //抓取每道题目的信息
            contestDetail.PointTime = new List<Tuple<int, string>>();
            for (int i = 1; i <= contestDetail.ProblemNum; ++i)
            {
                string td = matchs[i + 3].Result("$1");//从0开始

                string sPassed = "<span class=\"cell-passed-system-test\">(\\d*?)</span>";
                Regex rPassed = new Regex(sPassed, RegexOptions.Compiled);
                match = rPassed.Match(td);
                if (match.Success)
                {//ac
                    int point = Convert.ToInt32(match.Result("$1"));

                    string sTime = "<span class=\"cell-time\">(.*?)</span>";
                    Regex rTime = new Regex(sTime, RegexOptions.Compiled);
                    match = rTime.Match(td);

                    string time = match.Result("$1");
                    contestDetail.PointTime.Add(Tuple.Create<int, string>(point, time));
                }
                else
                {
                    string sFailed = "<span class=\"cell-failed-system-test\">\\s+([-\\d]+)\\s+</span>";
                    Regex rFailed = new Regex(sFailed, RegexOptions.Compiled);
                    match = rFailed.Match(td);
                    if (match.Success)
                    {//failed
                        int point = Convert.ToInt32(match.Result("$1"));
                        contestDetail.PointTime.Add(Tuple.Create<int, string>(point, ""));
                    }
                    else
                    {
                        contestDetail.PointTime.Add(Tuple.Create<int, string>(0, ""));
                    }
                }
            }

            //1:rank
            //2:name
            //3:总分  有分数   否则直接0
            //4:challenge
            //成功:
            //失败,后面-是否要转义还不明确:

            return contestDetail;
        }
        public void GetContestDetailsTest57()
        {
            CodeforcesProvider target = new CodeforcesProvider();
            int contestId = 57;

            List<CfContestDetail> actual;
            actual = target.GetContestDetails(contestId);
            List<Tuple<int, string>> list = new List<Tuple<int, string>>();
            list.Add(Tuple.Create<int, string>(488, "00:06"));
            list.Add(Tuple.Create<int, string>(908, "00:23"));
            list.Add(Tuple.Create<int, string>(1104, "01:06"));
            list.Add(Tuple.Create<int, string>(0, ""));
            list.Add(Tuple.Create<int, string>(0, ""));
            CfContestDetail gassa = new CfContestDetail()
            {
                UserName = "******",
                Point = 5762,
                ChallengeFailed = 0,
                ChallengeSucceed = 12,
                PointTime = list,
                Rank = 1
            };

            compareCfContestDetail(gassa, actual[0]);
        }
        public void GetContestDetailsTest1()
        {
            CodeforcesProvider target = new CodeforcesProvider();
            int contestId = 128;

            List<CfContestDetail> actual;
            actual = target.GetContestDetails(contestId);

            CfContestDetail gassa = new CfContestDetail()
            {
                UserName = "******",
                Point = 5338,
                ChallengeFailed = 1,
                ChallengeSucceed = 4,
                Rank = 2
            };

            compareCfContestDetail(gassa, actual[1]);
        }
        public void GetContestDetailsTest2()
        {
            CodeforcesProvider target = new CodeforcesProvider();
            int contestId = 99;

            List<CfContestDetail> actual;
            actual = target.GetContestDetails(contestId);

            List<Tuple<int, string>> list = new List<Tuple<int, string>>();
            list.Add(Tuple.Create<int, string>(488, "00:06"));
            list.Add(Tuple.Create<int, string>(908, "00:23"));
            list.Add(Tuple.Create<int, string>(1104, "01:06"));
            list.Add(Tuple.Create<int, string>(0, ""));
            list.Add(Tuple.Create<int, string>(0, ""));

            CfContestDetail sazzad = new CfContestDetail()
            {
                UserName = "******",
                Point = 2750,
                ChallengeFailed = 1,
                ChallengeSucceed = 3,
                Rank = 4,
                PointTime = list,
                ProblemNum = 5
            };

            compareCfContestDetail(sazzad, actual[3]);
        }
 private static void compareCfContestDetail(CfContestDetail a, CfContestDetail b)
 {
     Assert.AreEqual(a.Rank, b.Rank);
     Assert.AreEqual(a.UserName, b.UserName);
     Assert.AreEqual(a.Point, b.Point);
     Assert.AreEqual(a.ChallengeSucceed, b.ChallengeSucceed);
     Assert.AreEqual(a.ChallengeFailed, b.ChallengeFailed);
     //Assert.AreEqual(a.PointTime, b.PointTime);
 }
Beispiel #6
0
        public void GetContestDetailsTest3()
        {
            CodeforcesProvider target = new CodeforcesProvider();
            int contestId = 131;

            List<CfContestDetail> actual;
            actual = target.GetContestDetails(contestId);

            CfContestDetail user = new CfContestDetail()
            {
                UserName = "******",
                Point = 1096,
                ChallengeFailed = 1,
                ChallengeSucceed = 0,
                Rank = 850
            };

            compareCfContestDetail(user, actual[849]);
        }