Exemple #1
0
        public ShootingGameStats GetAverageGameStats(int id, int numberOfRecentGames)
        {
            List <GameStats> allStats = new List <GameStats>();

            RestClient client =
                new RestClient(
                    $"https://www.balldontlie.io/api/v1/stats?seasons[]=2021&player_ids[]={id}&per_page=100")
            {
                Timeout = -1
            };

            client.UseNewtonsoftJson();
            RestRequest   request  = new RestRequest(Method.GET);
            IRestResponse response = client.Execute(request);

            BallDontLieGameStatsResponse result =
                JsonConvert.DeserializeObject <BallDontLieGameStatsResponse>(response.Content);

            allStats.AddRange(result.data);

            List <GameStats> stats           = new List <GameStats>();
            List <GameStats> statsEnumerable = allStats.OrderByDescending(stat => stat.id).ToList();

            //TODO: verify the game is not active
            for (int i = 0; i < numberOfRecentGames; i++)
            {
                stats.Add(statsEnumerable[i]);
            }

            ShootingGameStats finalStats = CalculateAverageShootingOverRange(stats);

            return(finalStats);
        }
Exemple #2
0
        public ActionResult <ShootingGameStats> GetAverageGameStatsMultiplePlayers([FromQuery] int[] playerIds, int numberOfRecentGames)
        {
            ShootingGameStats stats = _manager.GetAverageGameStats(playerIds, numberOfRecentGames);

            if (stats.fg3a == 0 && stats.fg2a == 0)
            {
                return(NotFound());
            }

            return(stats != null?Ok(stats) : NotFound());
        }
Exemple #3
0
        public ActionResult <ShootingGameStats> GetAverageGameStats(int playerId, int numberOfRecentGames)
        {
            ShootingGameStats stats = _manager.GetAverageGameStats(playerId, numberOfRecentGames);

            if (stats.fg3a == 0 && stats.fg2a == 0)
            {
                return(NotFound());
            }

            return(stats != null?Ok(stats) : NotFound());
        }
Exemple #4
0
        protected ShootingGameStats CalculateAverageShootingOverRoster(List <ShootingGameStats> shootingGameStatsList)
        {
            double threePointAttemptSum = shootingGameStatsList.Select(x => x.fg3a).DefaultIfEmpty(0).Sum();
            double twoPointAttemptSum   = shootingGameStatsList.Select(x => x.fg2a).DefaultIfEmpty(0).Sum();
            double threePointMadeSum    = shootingGameStatsList.Select(x => x.fg3m).DefaultIfEmpty(0).Sum();
            double twoPointMadeSum      = shootingGameStatsList.Select(x => x.fg2m).DefaultIfEmpty(0).Sum();

            double twoPointAttempts = twoPointAttemptSum * shootingGameStatsList.Count;
            double twoPointMakes    = twoPointMadeSum * shootingGameStatsList.Count;

            double threePointAttempts = threePointAttemptSum * shootingGameStatsList.Count;
            double threePointMakes    = threePointMadeSum * shootingGameStatsList.Count;

            ShootingGameStats gameStats = new ShootingGameStats();

            gameStats.fg3m    = threePointMadeSum;
            gameStats.fg3a    = threePointAttemptSum;
            gameStats.fg3_pct = threePointMakes / threePointAttempts;
            gameStats.fg2a    = twoPointAttemptSum;
            gameStats.fg2m    = twoPointMadeSum;
            gameStats.fg2_pct = twoPointMakes / twoPointAttempts;

            return(gameStats);
        }
Exemple #5
0
        protected ShootingGameStats CalculateAverageShootingOverRange(List <GameStats> gameStatsList)
        {
            double threePointAttemptAverage = gameStatsList.Select(x => x.fg3a).DefaultIfEmpty(0).Average();
            double twoPointAttemptAverage   = gameStatsList.Select(x => x.fga - x.fg3a).DefaultIfEmpty(0).Average();
            double threePointMadeAverage    = gameStatsList.Select(x => x.fg3m).DefaultIfEmpty(0).Average();
            double twoPointMadeAverage      = gameStatsList.Select(x => x.fgm - x.fg3m).DefaultIfEmpty(0).Average();

            double twoPointAttempts = twoPointAttemptAverage * gameStatsList.Count;
            double twoPointMakes    = twoPointMadeAverage * gameStatsList.Count;

            double threePointAttempts = threePointAttemptAverage * gameStatsList.Count;
            double threePointMakes    = threePointMadeAverage * gameStatsList.Count;

            ShootingGameStats gameStats = new ShootingGameStats();

            gameStats.fg3m    = threePointMadeAverage;
            gameStats.fg3a    = threePointAttemptAverage;
            gameStats.fg3_pct = threePointMakes / threePointAttempts;
            gameStats.fg2a    = twoPointAttemptAverage;
            gameStats.fg2m    = twoPointMadeAverage;
            gameStats.fg2_pct = twoPointMakes / twoPointAttempts;

            return(gameStats);
        }