예제 #1
0
        public async Task <byte[]> GetStatsBarChart(IList <Player> players)
        {
            var chart = new PlotlyChart
            {
                Figure = new Figure
                {
                    Data = new ArrayList()
                },
                Height = 500,
                Width  = 1000
            };
            var chartLayout = GetDefaultLayout();
            var x           = new ArrayList
            {
                "Shots", "Shots on Target", "Key Passes", "Tackles", "Blocks", "Interceptions", "Duels Won",
                "Dribbles Attempted", "Dribbles Won", "Fouls Drawn", "Fouls Committed"
            };

            foreach (var player in players)
            {
                var stats     = player.Statistics;
                var converter = new ToPer90Converter(stats.Games.MinutesPlayed);
                var y         = new ArrayList
                {
                    converter.ToPer90(stats.Shots.Total),
                    converter.ToPer90(stats.Shots.OnTarget),
                    converter.ToPer90(stats.Passes.Key),
                    converter.ToPer90(stats.Tackles.Total),
                    converter.ToPer90(stats.Tackles.Blocks),
                    converter.ToPer90(stats.Tackles.Interceptions),
                    converter.ToPer90(stats.Duels.Won),
                    converter.ToPer90(stats.Dribbles.Attempts),
                    converter.ToPer90(stats.Dribbles.Success),
                    converter.ToPer90(stats.Fouls.Drawn),
                    converter.ToPer90(stats.Fouls.Committed)
                };
                var trace = new BarTrace
                {
                    X    = x,
                    Y    = y,
                    Name = player.Name
                };

                chart.Figure.Data.Add(trace);
            }

            if (players.Count == 1)
            {
                chartLayout.Title.Text = $"{players[0].Name} per 90 statistics";
                chartLayout.ShowLegend = false;
            }
            else
            {
                chartLayout.Title.Text = "Per 90 statistics comparison";
                chartLayout.ShowLegend = true;
            }

            chart.Figure.Layout = chartLayout;

            return(await _plotlyClient.GetChartAsByteArray(chart));
        }
예제 #2
0
        public EmbedBuilder CreatePlayerEmbed(Player player)
        {
            var stats   = player.Statistics;
            var flag    = _emojiService.GetEmojiString(player.Nationality);
            var builder = new EmbedBuilder()
                          .WithTitle($"{player.Name} {flag}")
                          .WithColor(Color.Purple)
                          .WithDescription(
                $"Position: {player.Position.ToString()}, Age: {player.Age}, Height: {player.Height}, Weight: {player.Weight}"
                )
                          .WithFooter($"Stats last updated at {player.UpdatedAt:dd/MM/yyyy HH:mm} UTC")
                          .AddField("Games played", stats.Games.Appearances)
                          .AddField("Goals", stats.Goals.Total)
                          .AddField("Assists", stats.Goals.Assists)
                          .AddField("Rating", stats.Rating is null ? "0" : $"{stats.Rating:0.##}");

            var converter = new ToPer90Converter(stats.Games.MinutesPlayed);

            switch (player.Position)
            {
            case Position.Attacker:
                AddShots();
                AddPasses();
                AddDribbles();
                break;

            case Position.Midfielder:
                AddPasses();
                AddDribbles();
                AddTackles();
                break;

            case Position.Defender:
                AddGoalsConceded();
                AddTackles();
                AddPasses();
                break;

            case Position.Goalkeeper:
                AddGoalsConceded();
                AddPasses();
                break;

            case Position.None:
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }

            return(builder);

            void AddShots()
            {
                var shotsTotal              = converter.ToPer90(stats.Shots.Total);
                var shotsOnTarget           = converter.ToPer90(stats.Shots.OnTarget);
                var percentageShotsOnTarget = shotsOnTarget / shotsTotal;

                builder.AddField("Shots per 90 minutes",
                                 $"Total: {shotsTotal:0.##}, On Target: {shotsOnTarget:0.##} ({percentageShotsOnTarget:0.##%})");
            }

            void AddPasses()
            {
                builder.AddField("Passes per 90 minutes",
                                 $"Total: {converter.ToPer90(stats.Passes.Total):0.##}, Key passes: {converter.ToPer90(stats.Passes.Key):0.##}, Accuracy: {stats.Passes.Accuracy:0.##}%"
                                 );
            }

            void AddDribbles()
            {
                var dribblesAttempted            = converter.ToPer90(stats.Dribbles.Attempts);
                var dribblesSuccessful           = converter.ToPer90(stats.Dribbles.Success);
                var percentageDribblesSuccessful = dribblesSuccessful / dribblesAttempted;

                builder.AddField("Dribbles per 90 minutes",
                                 $"Attempted: {dribblesAttempted:0.##}, Successful: {converter.ToPer90(stats.Dribbles.Success):0.##} ({percentageDribblesSuccessful:0.##%})"
                                 );
            }

            void AddTackles()
            {
                builder.AddField("Tackles per 90 minutes",
                                 $"Tackles: {converter.ToPer90(stats.Tackles.Total):0.##}, Interceptions: {converter.ToPer90(stats.Tackles.Interceptions):0.##}, Blocks: {converter.ToPer90(stats.Tackles.Blocks):0.##}"
                                 );
            }

            void AddGoalsConceded()
            {
                builder.AddField("Goals conceded:", stats.Goals.Conceded);
            }
        }