Exemplo n.º 1
0
        public static void SaveRunner(Race race)
        {
            using (RacingPostRacesEntities db = new RacingPostRacesEntities())
            {
                int?prevPos = 0;
                foreach (var runner in race.Runners)
                {
                    RPRunner rpRunner = new RPRunner();
                    rpRunner.HorseId = runner.HorseId;
                    rpRunner.RaceId  = race.Id;
                    if (DidNotFinsh.Any(df => df.Equals(runner.PosTemp)))
                    {
                        rpRunner.DidNotFinish = runner.PosTemp;
                        rpRunner.Position     = prevPos + 1;
                        prevPos = rpRunner.Position;
                    }
                    else
                    {
                        rpRunner.Position = Convert.ToInt32(runner.PosTemp);
                        prevPos           = rpRunner.Position;
                    }
                    rpRunner.Status   = "Runner";
                    rpRunner.Draw     = string.IsNullOrEmpty(runner.Draw) ? 0 : Convert.ToInt32(runner.Draw);
                    rpRunner.Distance = runner.Distance;
                    // rpRunner.DistBeaten = Convert.ToDouble(runner.DistBeaten);
                    rpRunner.Price     = runner.SP;
                    rpRunner.WeightRaw = runner.WeightRaw;
                    rpRunner.Age       = Convert.ToInt32(runner.Age);

                    if (db.Jockeys.FirstOrDefault(j => j.Id == runner.JockeyId) == null)
                    {
                        db.Jockeys.Add(new Jockey {
                            Id = runner.JockeyId, Name = runner.Jockey
                        });
                    }
                    if (db.Trainers.FirstOrDefault(j => j.Id == runner.TrainerId) == null)
                    {
                        db.Trainers.Add(new Trainer {
                            Id = runner.TrainerId, Name = runner.Trainer
                        });
                    }

                    rpRunner.JockeyId     = runner.JockeyId;
                    rpRunner.TrainerId    = runner.TrainerId;
                    rpRunner.PostTemplate = true;
                    SaveHorse(new Horse {
                        Id = (int)rpRunner.HorseId
                    });
                    db.RPRunners.Add(rpRunner);
                    db.SaveChanges();
                }
            }
        }
Exemplo n.º 2
0
        public static void ProcessRunner(RPRace race, HtmlDocument doc, RacingPostRacesEntities db)
        {
            RPRunner runner;
            var      rows = doc.QuerySelectorAll("tbody").FirstOrDefault().ChildNodes.Where(c => c.Name == "tr").ToList().Where(row => row.Attributes[0].Value == "rp-horseTable__mainRow").ToList();

            foreach (var item in rows)
            {
                runner              = new RPRunner();
                runner.Status       = "Runner";
                runner.PostTemplate = true;
                runner.RaceId       = race.Id;

                var posTemp = item.QuerySelectorAll("div .rp-horseTable__pos").FirstOrDefault().ChildNodes[3].ChildNodes[1].ChildNodes[0].InnerHtml.Replace("\n", "").Trim();
                if (DidNotFinsh.Any(df => df.Equals(posTemp)))
                {
                    runner.DidNotFinish = posTemp;
                }
                else
                {
                    runner.Position = Convert.ToInt32(item.QuerySelectorAll("div .rp-horseTable__pos").FirstOrDefault().ChildNodes[3].ChildNodes[1].ChildNodes[0].InnerHtml.Replace("\n", "").Trim());
                }

                string draw = item.QuerySelectorAll("div .rp-horseTable__pos").FirstOrDefault().ChildNodes[3].ChildNodes[1].ChildNodes[2].InnerHtml.Replace(" (", "").Replace(")", "").Trim();
                int    DrawPos;
                int.TryParse(draw, out DrawPos);
                runner.Draw = DrawPos > 0 ? Convert.ToInt32(DrawPos) : (int?)null;

                var lendthAttr = item.QuerySelectorAll("span .rp-horseTable__pos__length").FirstOrDefault();
                if (lendthAttr.ChildNodes.Count >= 2)
                {
                    runner.Distance = lendthAttr.ChildNodes[1].InnerHtml;
                }
                if (lendthAttr.ChildNodes.Count >= 4)
                {
                    // var dis = FractionToDouble(lendthAttr.ChildNodes[3].InnerHtml.Replace("[", "").Replace("]", ""));
                }
                var horseUrl = item.QuerySelectorAll("a .rp-horseTable__horse__name").FirstOrDefault().Attributes[0].Value;
                runner.HorseId = Helper.GetIdfromUrl(horseUrl, "/profile/horse/");
                runner.Price   = item.QuerySelectorAll("span .rp-horseTable__horse__price").FirstOrDefault().InnerHtml.Replace("\n", "").Trim();
                var persons = item.QuerySelectorAll("span .rp-horseTable__human__wrapper");

                //jockey info
                var jockeyUrl = persons.FirstOrDefault().ChildNodes[1];
                runner.JockeyId = Helper.GetIdfromUrl(jockeyUrl.Attributes[0].Value, "/profile/jockey/");
                Jockey jockey = new Jockey();
                jockey.Id   = Convert.ToInt32(runner.JockeyId);
                jockey.Name = jockeyUrl.InnerHtml.Replace("\n", "").Trim();
                jockey.Name = jockey.Name.Substring(0, jockey.Name.IndexOf("<"));

                if (!db.Jockeys.Where(j => j.Id == jockey.Id).Any())
                {
                    db.Jockeys.Add(jockey);
                    db.SaveChanges();
                }

                //trainer info
                var trainerUrl = persons[1].ChildNodes[1];
                runner.TrainerId = Helper.GetIdfromUrl(trainerUrl.Attributes[0].Value, "/profile/trainer/");
                Trainer trainer = new Trainer();
                trainer.Id   = Convert.ToInt32(runner.TrainerId);
                trainer.Name = trainerUrl.InnerHtml.Replace("\n", "").Trim();
                if (trainer.Name.IndexOf("<") > 0)
                {
                    trainer.Name = trainer.Name.Substring(0, trainer.Name.IndexOf("<"));
                }
                if (!db.Trainers.Where(j => j.Id == trainer.Id).Any())
                {
                    db.Trainers.Add(trainer);
                    db.SaveChanges();
                }
                var age = item.ChildNodes[7].InnerHtml.Replace("\n", "").Trim();
                if (age.IndexOf("<") > 0)
                {
                    age = age.Substring(0, age.IndexOf("<"));
                }
                runner.Age = Convert.ToInt32(age);

                var wt = item.ChildNodes[9].InnerHtml.Replace("\n", "").Trim();
                runner.WeightRaw = ProcessWt(wt);
                db.RPRunners.Add(runner);
            }
        }