示例#1
0
        protected void CollectOneMatch(string matchIndex)
        {
            Uri        uri        = new Uri(baseuri, string.Format("Matches/{0}/Betting", matchIndex));
            WebRequest getRequest = WebRequest.Create(uri);
            string     matchAlias = gameType.ToString() + "-" + matchIndex;
            AveragedNonWeightedBetItemManager mgr = new AveragedNonWeightedBetItemManager();
            Guid   matchGuid = Guid.NewGuid();
            string team1 = "", team2 = "", dateStr = "";

            WebResponse response = GBCommon.GetResponseWithRetries(getRequest, GBCommon.DefaultRetries);

            using (Stream stream = response.GetResponseStream())
            {
                using (StreamReader reader = new StreamReader(stream))
                {
                    HtmlDocument doc = new HtmlDocument();
                    doc.Load(reader);

                    if (null == doc.DocumentNode)
                    {
                        throw new WebException(matchAlias + ": Error when parsing the root node");
                    }

                    HtmlNode bodyNode = doc.DocumentNode.SelectSingleNode("//body");
                    if (null == bodyNode)
                    {
                        throw new WebException(matchAlias + ": Error when parsing the body node");
                    }

                    var    scripts = bodyNode.SelectNodes("//script[@type='text/javascript']");
                    string pattern = @"matchHeader.load.*\d+,\d+,\'([A-Za-z0-9\. ]+)\',\'([A-Za-z0-9\. ]+)\',\'([0-9:/ ]+)\'";
                    foreach (var script in scripts)
                    {
                        Match match = Regex.Match(script.InnerText, pattern);
                        if (match.Success)
                        {
                            team1   = match.Groups[1].Value.Replace(' ', '_');
                            team2   = match.Groups[2].Value.Replace(' ', '_');
                            dateStr = match.Groups[3].Value;
                        }
                    }
                    if (string.IsNullOrEmpty(dateStr) || string.IsNullOrEmpty(team1) || string.IsNullOrEmpty(team2))
                    {
                        throw new ApplicationException(matchAlias + ": No match time or team name is found");
                    }


                    var bookMakerNameNodes = bodyNode.SelectNodes("//div[@id='ThreeWay-OrdinaryTime']//a[@class='bm-name']");
                    if (null == bookMakerNameNodes)
                    {
                        throw new ApplicationException(matchAlias + ": No target odds found");
                    }
                    foreach (var bookMakerNameNode in bookMakerNameNodes)
                    {
                        var     node      = bookMakerNameNode.ParentNode.ParentNode;
                        string  bookMaker = node.ChildNodes[1].SelectSingleNode(".//a[@class='bm-name']").InnerText;
                        string  win       = node.ChildNodes[3].SelectSingleNode(".//a/span").InnerText.Trim();
                        string  draw      = node.ChildNodes[5].SelectSingleNode(".//a/span").InnerText.Trim();
                        string  lose      = node.ChildNodes[7].SelectSingleNode(".//a/span").InnerText.Trim();
                        BetItem bet       = new BetItem(
                            matchGuid.ToString(),
                            matchIndex,
                            gameType,
                            new System.Collections.Generic.List <Team>()
                        {
                            new Team(team1, teamType), new Team(team2, teamType)
                        },
                            new ThreeWayOdds(Convert.ToDouble(win), Convert.ToDouble(lose), Convert.ToDouble(draw)),
                            false,
                            DateTime.UtcNow,
                            Convert.ToDateTime(dateStr),
                            bookMaker
                            );
                        mgr.CurrentBets.Add(bet);
                    }

                    string fileName = GBCommon.ConstructRecordFileName(gameType, team1, team2, dateStr);
                    if (File.Exists(fileName))
                    {
                        File.Delete(fileName);
                    }

                    GBCommon.LogInfo("{0}: Collected {1}", DateTime.Now, fileName);
                    mgr.Serialize(fileName);
                }
            }
        }
示例#2
0
        public static void Report()
        {
            foreach (string folder in Directory.GetDirectories(GBCommon.DataFolder))
            {
                GameType gameType = (GameType)Enum.Parse(typeof(GameType), folder.Remove(0, GBCommon.DataFolder.Length).TrimStart('\\'));
                DateTime continuationStartDate = GBCommon.ReadContinuationDate(GBCommon.ConstructReportContinuationFileName(gameType));
                DateTime latest = continuationStartDate;

                var files = new DirectoryInfo(folder).GetFiles("*.json", SearchOption.TopDirectoryOnly).Where(file => file.LastWriteTime > continuationStartDate + TimeSpan.FromSeconds(1));
                foreach (var file in files)
                {
                    AveragedNonWeightedBetItemManager mgr = new AveragedNonWeightedBetItemManager();
                    mgr.Deserialize(file.FullName, OddsType.ThreeWay);
                    if (mgr.CurrentBets.Count < 1)
                    {
                        throw new FileLoadException(string.Format("Record file {0} not valid", file.Name));
                    }
                    BetItem firstBet  = mgr.CurrentBets[0];
                    Stake   bestStake = mgr.BestStake(firstBet.GameName, firstBet.GameType, firstBet.Odds.Type);
                    string  output    = bestStake.ROI.ToString("p2")
                                        + " "
                                        + bestStake.BetItem.MatchDate.ToShortDateString()
                                        + " "
                                        + bestStake.BetItem.Teams[0].Name
                                        + " "
                                        + bestStake.BetItem.Teams[1].Name
                                        + " "
                                        + bestStake.BetItem.BookMaker.Replace(' ', '_')
                                        + " "
                                        + bestStake.BetItem.Odds.Type.ToString()
                                        + " "
                                        + bestStake.Decision;
                    GBCommon.Report(output, gameType);
                    if (file.LastWriteTime > latest)
                    {
                        latest = file.LastWriteTime;
                    }

                    // If the game stake exists, delete the old one
                    var deleteStakeResponse = GBCommon.SendRequest(
                        "{\"BetItem.GameName\": \"" + bestStake.BetItem.GameName + "\"}",
                        ConfigurationManager.AppSettings["datastore"],
                        ConfigurationManager.AppSettings["apikey"],
                        ConfigurationManager.AppSettings["passwd"],
                        "application/json",
                        "DELETE"
                        );
                    using (StreamReader streamReader = new StreamReader(deleteStakeResponse.GetResponseStream()))
                    {
                        var text = streamReader.ReadToEnd();
                        Console.WriteLine(text);
                    }

                    // Add the new stake
                    var addStakeResponse = GBCommon.SendRequest(
                        JsonConvert.SerializeObject(bestStake),
                        ConfigurationManager.AppSettings["datastore"],
                        ConfigurationManager.AppSettings["apikey"],
                        ConfigurationManager.AppSettings["passwd"],
                        "application/json",
                        "PUT"
                        );
                    using (StreamReader streamReader = new StreamReader(addStakeResponse.GetResponseStream()))
                    {
                        var text = streamReader.ReadToEnd();
                        Console.WriteLine(text);
                    }
                }

                if (latest > continuationStartDate)
                {
                    GBCommon.WriteContinuationDate(GBCommon.ConstructReportContinuationFileName(gameType), latest);
                }
            }

            GBCommon.LogInfo("Reporting Completed.");
        }