private string GetPlayerAnalysisTable(IPlayer player, IEnumerable <IPlayer> players, string tableTitle)
        {
            PercentilePlayerGroupAnalyzer analyzer = new PercentilePlayerGroupAnalyzer();
            LeagueConstants lc = LeagueConstants.For(this.league.FantasyLeague);
            IEnumerable <IStatExtractor> extractors = player is Batter?lc.BattingSupportingStatExtractors.Union(lc.BattingScoringStatExtractors) : lc.PitchingSupportingStatExtractors.Union(lc.PitchingScoringStatExtractors);

            StringBuilder sb = new StringBuilder();

            sb.Append("<TABLE BORDER=1><TR><TD>Stat Name</TD><TD>Player's Stat Value</TD><TD>Max Value</TD><TD>Min Value</TD><TD>Graph</TD><TD>Player Percentile</TD></TR>");
            int percentileSum = 0;
            int count         = 0;

            foreach (IStatExtractor extractor in extractors)
            {
                PlayerGroupAnalysis analysis = analyzer.Analyze(this.Text, extractor, players, player, p => p == player ? Brushes.Yellow : !string.IsNullOrEmpty(p.FantasyTeam) ? Brushes.Blue : Brushes.Violet);
                byte[] img;
                using (MemoryStream stm = new MemoryStream())
                {
                    analysis.Graph.Save(stm, System.Drawing.Imaging.ImageFormat.Jpeg);
                    img = stm.ToArray();
                }
                sb.AppendLine($"<TR><TD>{analysis.Stat}</TD><TD>{analysis.PlayerStatValue}</TD><TD>{analysis.MaxStatValue}</TD><TD>{analysis.MinStatValue}</TD><TD><img src=\"data:image/jpg;base64,{Convert.ToBase64String(img)}\"/></TD><TD>{analysis.PlayerPercentile}</TD></TR>");
                percentileSum += analysis.PlayerPercentile;
                count++;
            }
            sb.AppendLine("</TABLE>");

            return(string.Format("<H1>{0} (Average Percentile: {1})</H1>{2}", tableTitle, percentileSum / count, sb.ToString()));
        }
        private void UpdateWebView()
        {
            PercentilePlayerGroupAnalyzer analyzer = new PercentilePlayerGroupAnalyzer();
            string title = "Analysis of " + this.Text;
            IEnumerable <IStatExtractor> extractors = this.constants.ScoringStatExtractors;

            if (this.currentPlayer != null)
            {
                title += " on player " + this.currentPlayer.Name;
                if (this.currentPlayer is Batter)
                {
                    extractors = this.constants.BattingScoringStatExtractors;
                }
                else
                {
                    extractors = this.constants.PitchingScoringStatExtractors;
                }
            }

            StringBuilder sb = new StringBuilder();

            sb.Append("<HTML><BODY><H1>");
            sb.Append(title);
            sb.Append("</H1><TABLE BORDER=1><TR><TD>Stat Name</TD><TD>Player's Stat Value</TD><TD>Max Value</TD><TD>Min Value</TD><TD>Graph</TD><TD>Player Percentile</TD></TR>");
            foreach (IStatExtractor extractor in extractors)
            {
                PlayerGroupAnalysis analysis = analyzer.Analyze(this.Text, extractor, this.allPlayers, this.currentPlayer);
                byte[] img;
                using (MemoryStream stm = new MemoryStream())
                {
                    analysis.Graph.Save(stm, System.Drawing.Imaging.ImageFormat.Jpeg);
                    img = stm.ToArray();
                }
                sb.AppendLine($"<TR><TD>{analysis.Stat}</TD><TD>{analysis.PlayerStatValue}</TD><TD>{analysis.MaxStatValue}</TD><TD>{analysis.MinStatValue}</TD><TD><img src=\"data:image/jpg;base64,{Convert.ToBase64String(img)}\"/></TD><TD>{analysis.PlayerPercentile}</TD></TR>");
            }
            sb.AppendLine("</TABLE></BODY></HTML>");
            this.wbOut.DocumentText = sb.ToString();
        }
Example #3
0
        public AllPlayersStatCenter(League league)
        {
            InitializeComponent();

            LeagueConstants lc = LeagueConstants.For(league.FantasyLeague);

            this.lv.BeginUpdate();
            int columnWidth = lv.Width / (lc.ScoringStatExtractors.Count + lc.SupportingStatExtractors.Count + 4);

            lv.Columns.Add("Player Name", columnWidth);
            lv.Columns.Add("Fantasy Team", columnWidth);
            foreach (IStatExtractor extractor in lc.ScoringStatExtractors.Union(lc.SupportingStatExtractors))
            {
                ColumnHeader column = new ColumnHeader();
                column.Text  = extractor.StatName;
                column.Width = columnWidth;
                if (!extractor.MoreIsBetter)
                {
                    column.Tag = "asc";
                }
                lv.Columns.Add(column);
            }
            lv.Columns.Add("Average Percentile (Batter)", columnWidth);
            lv.Columns.Add("Average Percentile (Pitcher)", columnWidth);

            Dictionary <string, int>      statToPlayerCount = new Dictionary <string, int>();
            PercentilePlayerGroupAnalyzer analyzer          = new PercentilePlayerGroupAnalyzer();

            foreach (IPlayer player in league.AllPlayers)
            {
                ListViewItem item = new ListViewItem(player.Name);
                item.SubItems.Add(player.FantasyTeam);
                int percentile = 0;
                int statcount  = 0;
                foreach (IStatExtractor extractor in lc.ScoringStatExtractors.Union(lc.SupportingStatExtractors))
                {
                    IStatValue value = extractor.Extract(player);
                    if (value != null)
                    {
                        IEnumerable <IPlayer> players = league.Batters;
                        if (!player.IsBatter)
                        {
                            players = league.Pitchers;
                        }
                        percentile += analyzer.GetPercentile(extractor, players, player);
                        statcount++;
                        item.SubItems.Add(value.Value.ToString());
                        if (!statToPlayerCount.ContainsKey(extractor.StatName))
                        {
                            statToPlayerCount[extractor.StatName] = 1;
                        }
                        else
                        {
                            statToPlayerCount[extractor.StatName]++;
                        }
                    }
                    else
                    {
                        item.SubItems.Add(string.Empty);
                    }
                }

                if (player.IsBatter)
                {
                    item.SubItems.Add((percentile / statcount).ToString());
                    item.SubItems.Add(string.Empty);
                }
                else
                {
                    item.SubItems.Add(string.Empty);
                    item.SubItems.Add((percentile / statcount).ToString());
                }
                this.lv.Items.Add(item);
            }

            this.lv.EndUpdate();
        }