public Roster FindOptimalRoster(LeagueData leagueData, List <Player> team)
        {
            //create list of starters to return
            Roster roster = new Roster();

            //get position players
            List <Player> quarterbacks  = team.Where(p => p.Position == "QB").OrderByDescending(p => p.FantasyPoints).ToList();
            List <Player> runningBacks  = team.Where(p => p.Position == "RB").OrderByDescending(p => p.FantasyPoints).ToList();
            List <Player> wideReceivers = team.Where(p => p.Position == "WR").OrderByDescending(p => p.FantasyPoints).ToList();
            List <Player> tightEnds     = team.Where(p => p.Position == "TE").OrderByDescending(p => p.FantasyPoints).ToList();

            //get starting players
            foreach (Player player in quarterbacks.Take(1).ToList())
            {
                roster.Quarterbacks.Add(new RosterPlayer(player));
            }
            ;
            foreach (Player player in runningBacks.Take(2).ToList())
            {
                roster.RunningBacks.Add(new RosterPlayer(player));
            }
            ;
            foreach (Player player in wideReceivers.Take(2).ToList())
            {
                roster.WideReceivers.Add(new RosterPlayer(player));
            }
            ;
            foreach (Player player in tightEnds.Take(1).ToList())
            {
                roster.TightEnds.Add(new RosterPlayer(player));
            }
            ;

            //add best waivers to team if missing starters
            int neededQuarterbacks  = leagueData.League.Quarterbacks - roster.Quarterbacks.Count;
            int neededRunningBacks  = leagueData.League.RunningBacks - roster.RunningBacks.Count;
            int neededWideReceivers = leagueData.League.WideReceivers - roster.WideReceivers.Count;
            int neededTightEnds     = leagueData.League.TightEnds - roster.TightEnds.Count;

            for (int i = 0; i < neededQuarterbacks; i++)
            {
                roster.Quarterbacks.Add(new RosterPlayer(leagueData.GetWaiver("QB", i), true));
            }
            for (int i = 0; i < neededRunningBacks; i++)
            {
                roster.RunningBacks.Add(new RosterPlayer(leagueData.GetWaiver("RB", i), true));
            }
            for (int i = 0; i < neededWideReceivers; i++)
            {
                roster.WideReceivers.Add(new RosterPlayer(leagueData.GetWaiver("WR", i), true));
            }
            for (int i = 0; i < neededTightEnds; i++)
            {
                roster.TightEnds.Add(new RosterPlayer(leagueData.GetWaiver("TE", i), true));
            }

            //get possible flex players
            List <Player> possibleFlexes = new List <Player>();

            possibleFlexes.AddRange(runningBacks.Skip(leagueData.League.RunningBacks));
            possibleFlexes.AddRange(wideReceivers.Skip(leagueData.League.WideReceivers));
            possibleFlexes.AddRange(tightEnds.Skip(leagueData.League.TightEnds));
            possibleFlexes = possibleFlexes.OrderByDescending(p => p.FantasyPoints).ToList <Player>();

            //add remaining players on roster as flex

            for (int i = 0; i < leagueData.League.Flexes; i++)
            {
                if (possibleFlexes.Count > i)
                {
                    roster.Flexes.Add(new RosterPlayer(possibleFlexes[i]));
                }
            }

            //find out if you need waivers for flex
            int neededFlexes         = leagueData.League.Flexes - roster.Flexes.Count;
            int skippedQuarterbacks  = neededQuarterbacks;
            int skippedRunningBacks  = neededRunningBacks;
            int skippedWideReceivers = neededWideReceivers;
            int skippedTightEnds     = neededTightEnds;

            //add waivers to flex
            for (int i = 0; i < neededFlexes; i++)
            {
                Player player = leagueData.GetWaiver(skippedQuarterbacks, skippedRunningBacks, skippedWideReceivers, skippedTightEnds);

                if (player.Position == "QB")
                {
                    skippedQuarterbacks++;
                }
                if (player.Position == "RB")
                {
                    skippedRunningBacks++;
                }
                if (player.Position == "WR")
                {
                    skippedWideReceivers++;
                }
                if (player.Position == "TE")
                {
                    skippedTightEnds++;
                }

                roster.Flexes.Add(new RosterPlayer(player, true));
            }

            //calculate roster point total
            roster.QbPoints   = roster.Quarterbacks.Sum(p => p.Player.FantasyPoints);
            roster.RbPoints   = roster.RunningBacks.Sum(p => p.Player.FantasyPoints);
            roster.WrPoints   = roster.WideReceivers.Sum(p => p.Player.FantasyPoints);
            roster.TePoints   = roster.TightEnds.Sum(p => p.Player.FantasyPoints);
            roster.FlexPoints = roster.Flexes.Sum(p => p.Player.FantasyPoints);

            roster.Points = roster.QbPoints + roster.RbPoints + roster.WrPoints + roster.TePoints + roster.FlexPoints;

            return(roster);
        }
        public LeagueData Post(LeagueScraperPackage package)
        {
            LeagueData leagueData = new LeagueData();

            leagueData.League = package.League;

            WebScraper  scraper = null;
            IHostParser parser  = null;

            foreach (Player player in package.Projections.Players)
            {
                player.FantasyPoints = new FantasyPointsCalculator(package.League, player).FantasyPoints;
            }

            //determine which parser to use based on url
            if (package.League.Url.Contains("fleaflicker.com"))
            {
                parser = new FleaflickerLeagueParser();
            }
            else if (package.League.Url.Contains("myfantasyleague.com"))
            {
                parser = new MFLParser();
            }
            else if (package.League.Url.Contains("games.espn.go.com"))
            {
                parser = new EspnParser();
            }
            else if (package.League.Url.Contains("football.fantasysports.yahoo.com"))
            {
                parser = new YahooParser();
            }
            else if (package.League.Url.Contains("fantasy.nfl.com"))
            {
                parser = new NflParser();
            }
            else if (package.League.Url.Contains("football.cbssports.com"))
            {
                parser = new CbsSportsParser();
            }
            else
            {
                //throw exceptions saying league host not supported
            }

            //load web scraper based on whether a login is required
            if (package.League.RequiresLogin)
            {
                string loginUrl = parser.GetLoginUrl();
                //parser.GetPostData(package.Username, package.Password);
                string postData = parser.GetPostData(null, null);
                scraper = new WebScraper(package.League.Url, loginUrl, postData);
            }
            else
            {
                scraper = new WebScraper();
            }

            //parse league page to create teams in league data
            parser.ParseLeague(scraper.Scrape(package.League.Url), ref leagueData);

            //scrape each team and parse into league data
            foreach (Team team in leagueData.Teams)
            {
                parser.ParseTeam(scraper.Scrape(team.Url), package.League, team, package.Projections);
            }

            leagueData.Waivers = package.Projections.Players;

            //Player waiverQuarterback = leagueData.GetWaiver("QB", 0);
            //Player waiverRunningBack = leagueData.GetWaiver("RB", 0);
            //Player waiverWideReceiver = leagueData.GetWaiver("WR", 0);
            //Player waiverTightEnd = leagueData.GetWaiver("TE", 0);

            Player waiverQuarterback  = leagueData.GetWaiver("QB", 2);
            Player waiverRunningBack  = leagueData.GetWaiver("RB", 7);
            Player waiverWideReceiver = leagueData.GetWaiver("WR", 7);
            Player waiverTightEnd     = leagueData.GetWaiver("TE", 4);

            foreach (Team team in leagueData.Teams)
            {
                foreach (Player player in team.Players)
                {
                    if (player.Position == "QB")
                    {
                        player.TradeValue = player.FantasyPoints - waiverQuarterback.FantasyPoints;
                    }
                    if (player.Position == "RB")
                    {
                        player.TradeValue = player.FantasyPoints - waiverRunningBack.FantasyPoints;
                    }
                    if (player.Position == "WR")
                    {
                        player.TradeValue = player.FantasyPoints - waiverWideReceiver.FantasyPoints;
                    }
                    if (player.Position == "TE")
                    {
                        player.TradeValue = player.FantasyPoints - waiverTightEnd.FantasyPoints;
                    }
                }
            }

            return(leagueData);
        }