public ProbablePitcherViewModel GetProbablePitchers(DateTime queryDate)
        {
            var result  = new ProbablePitcherViewModel();
            var request = new ProbablePitcherRequest(null);

            result = request.Submit(queryDate);
            return(result);
        }
        public ProbablePitcherViewModel Submit(DateTime queryDate)
        {
            var result = new ProbablePitcherViewModel
            {
                GameDate = queryDate,
                HomeOnly = HomeOnly
            };

            GameDate = queryDate;

            var strDate = Utility.UniversalDate(queryDate);
            //url: "https://api.stattleship.com/baseball/mlb/probable_pitchers?season_id=mlb-2018&on=2018-04-26
            var pitchers = new List <Pitcher>();

            TeamList = new Dictionary <string, Pitcher>();
            var httpWebRequest = CreateRequest(
                sport: "baseball",
                league: "mlb",
                apiRequest: "probable_pitchers",
                queryParms: $"season_id=mlb-{queryDate.Year}&on={strDate}");

            var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();

            using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
            {
                var json = streamReader.ReadToEnd();

                var dto = JsonConvert.DeserializeObject <ProbablePitchersDto>(
                    json);

                Players = dto.Players;
                Teams   = dto.Teams;
                Games   = dto.Games;

                foreach (var item in dto.Pitchers)
                {
                    // take the last pitcher
                    TryAddPitcher(item);
                }
                ;
            }
            foreach (KeyValuePair <string, Pitcher> pair in TeamList)
            {
                var p = pair.Value;
                if (HomeOnly && p.Away)
                {
                    continue;
                }

                pitchers.Add(p);
            }
            result.ProbablePitchers = pitchers.OrderByDescending(
                o => o.OpponentsBattingAverage).ToList();
            return(result);
        }
        public Pitcher PitcherFacing(string teamSlug, DateTime gameDate)
        {
            var pitcherFacing = new Pitcher();

            Pitchers = GetProbablePitchers(gameDate);
            foreach (var pitcher in Pitchers.ProbablePitchers)
            {
                if (pitcher.OpponentSlug.Equals(teamSlug))
                {
                    pitcherFacing = pitcher;
                    break;
                }
            }
            return(pitcherFacing);
        }
        private List <Selection> SelectBatters(
            DateTime gameDate,
            int numberRequired)
        {
            var batters = new List <Selection>();
            ProbablePitcherViewModel pitchers = GetProbablePitchers(
                gameDate);
            var lines = pitchers.Dump();

            LogLines(lines);
            var i = 0;

            foreach (var pitcher in pitchers.ProbablePitchers)
            {
                if (pitcher.OpponentsBattingAverage < PickerOptions.DecimalOption(
                        Constants.Options.PitchersMendozaLine))
                {
#if DEBUG
                    Log($"Pitcher {pitcher} has too good a Opp B Avg");
#endif
                    continue;
                }
                if (PickerOptions.OptionOn(Constants.Options.TeamClip))
                {
                    var pitchersTeamClip = TeamClip(
                        statsDate: gameDate.AddDays(-1),
                        teamName: pitcher.TeamId);
                    if (pitchersTeamClip > PickerOptions.DecimalOption(
                            Constants.Options.PitchersTeamMendozaLine))
                    {
#if DEBUG
                        Log($"Pitchers Team {pitcher.TeamName} has too good a Clip {pitchersTeamClip}");
#endif
                        continue;
                    }
                    var battersTeamClip = TeamClip(
                        statsDate: gameDate.AddDays(-1),
                        teamName: pitcher.OpponentSlug);
                    if (battersTeamClip < PickerOptions.DecimalOption(
                            Constants.Options.BattersTeamMendozaLine))
                    {
#if DEBUG
                        Log($@"Batters Team {
							pitcher.OpponentSlug
							} has not got a good enough Clip {
							battersTeamClip
							}"                            );
#endif
                        continue;
                    }
                }
                ++i;
                var printLine = $"{i.ToString(),2} {pitcher}";
#if DEBUG
                Log($"Looking for a {pitcher.OpponentSlug} batter facing {pitcher}");
#endif
                var lineupQueryDate          = gameDate.AddDays(-1);
                var lineupPositionsToExamine = PickerOptions.IntegerOption(
                    Constants.Options.LineupPositions);

                var opponents = _lineupProjector.ProjectLineup(
                    pitcher,
                    lineupQueryDate,
                    lineupPositionsToExamine);

                if (opponents.Lineup.Count.Equals(0))
                {
#if DEBUG
                    Log("  cold team - skip this pitcher");
#endif
                    continue;                      //  cold team
                }
#if DEBUG
                opponents.DumpLineup();
#endif
                var batter1 = opponents.BattingAt("1");
                var batter2 = new Batter();
                var batter3 = new Batter();
                var batter4 = new Batter();

                if (lineupPositionsToExamine > 1)
                {
                    batter2 = opponents.BattingAt("2");
                    if (lineupPositionsToExamine > 2)
                    {
                        batter3 = opponents.BattingAt("3");
                    }
                    if (lineupPositionsToExamine > 3)
                    {
                        batter4 = opponents.BattingAt("4");
                    }
                }
                if (batter1 != null)
                {
                    var selection = new Selection
                    {
                        GameDate = gameDate,
                        Batter1  = batter1,
                        Batter2  = batter2,
                        Batter3  = batter3,
                        Batter4  = batter4,
                        Pitcher  = pitcher,
                        Game     = new Game
                        {
                            Title = pitcher.NextOpponent
                        }
                    };
                    //  additive
                    var    like   = true;
                    string reason = string.Empty;
                    foreach (var test in Tests)
                    {
                        if (!test.Likes(selection, out reason))
                        {
                            printLine += reason;
                            like       = false;
#if DEBUG
                            Log($"{selection} no good :- {reason}");
#endif
                            break;                              // one strike and ur out
                        }
                    }
                    if (like)
                    {
                        batters.Add(selection);
#if DEBUG
                        Log($"{selection} good");
#endif
                        printLine += "  " + selection.Batter.ToString();
                    }
                }
                else
                {
                    printLine +=
                        $@"  skipped No {
                            pitcher.OpponentSlug
                            } lineups available for {
                            lineupQueryDate.ToShortDateString()
                            } team COLD";
                }

                if (batters.Count == numberRequired)
                {
                    break;
                }
            }
            return(batters);
        }