Example #1
0
        public void CalculateHoleStatistics(int month, int year)
        {
            Check.Argument.IsNotZeroOrNegative(month, "month");
            Check.Argument.IsNotZeroOrNegative(year, "year");

            DeleteHoleStatistics(month, year);

            var holes = _holeRepository.All().ToList();
            var shots = _shotRepository.GetByMonthAndYear(month, year);

            foreach (var hole in holes)
            {
                var holeShots = shots.Where(s => s.Hole.Id == hole.Id);

                if (holeShots.Any())
                {
                    var holeStatistics = new HoleStatistics()
                    {
                        Hole = hole, Month = month, Year = year
                    };

                    holeStatistics.ShotsMade          = holeShots.Count(s => s.ShotMade);
                    holeStatistics.Attempts           = holeShots.Sum(s => s.Attempts);
                    holeStatistics.ShootingPercentage = Decimal.Round((decimal)holeStatistics.ShotsMade / (decimal)holeStatistics.Attempts, 3, MidpointRounding.AwayFromZero);
                    holeStatistics.PointsScored       = holeShots.Sum(s => s.Points);
                    holeStatistics.Pushes             = holeShots.Count(s => s.ShotType.Id == 3);
                    holeStatistics.Steals             = holeShots.Count(s => s.ShotType.Id == 4);
                    holeStatistics.SugarFreeSteals    = holeShots.Count(s => s.ShotType.Id == 5);

                    _holeStatisticsRepository.Add(holeStatistics);
                }
            }
        }