public void CalculatePlayerHoleStatistics(int month, int year) { Check.Argument.IsNotZeroOrNegative(month, "month"); Check.Argument.IsNotZeroOrNegative(year, "year"); DeletePlayerHoleStatistics(month, year); var players = _playerRepository.All().ToList(); var shots = _shotRepository.GetByMonthAndYear(month, year); var holes = _holeRepository.All().ToList(); foreach (var player in players) { var playerShots = shots.Where(s => s.Player.Id == player.Id); if (playerShots.Any()) { foreach (var hole in holes) { var playerHoleShots = playerShots.Where(s => s.Hole.Id == hole.Id); if (playerHoleShots.Any()) { var playerHoleStatistics = new PlayerHoleStatistics() { Player = player, Hole = hole, Month = month, Year = year }; playerHoleStatistics.ShotsMade = playerHoleShots.Count(s => s.ShotMade); playerHoleStatistics.Attempts = playerHoleShots.Sum(s => s.Attempts); playerHoleStatistics.ShootingPercentage = Decimal.Round((decimal)playerHoleStatistics.ShotsMade / (decimal)playerHoleStatistics.Attempts, 3, MidpointRounding.AwayFromZero); playerHoleStatistics.PointsScored = playerHoleShots.Where(s => s.ShotType.Id != ShotTypePush).Sum(s => s.Points); playerHoleStatistics.Pushes = playerHoleShots.Count(s => s.ShotType.Id == ShotTypePush); playerHoleStatistics.Steals = playerHoleShots.Count(s => s.ShotType.Id == ShotTypeSteal); playerHoleStatistics.SugarFreeSteals = playerHoleShots.Count(s => s.ShotType.Id == ShotTypeSugarFreeSteal); _playerHoleStatisticsRepository.Add(playerHoleStatistics); } } } } }
public void Create(int gameId, int playerId, int holeId, int attempts, bool shotMade) { var game = _gameRepository.GetById(gameId); var shots = _shotRepository.GetByGame(gameId); var player = _playerRepository.GetById(playerId); var hole = _holeRepository.GetById(holeId); var allHoles = _holeRepository.All(); var allShotTypes = _shotTypeRepository.All(); int playerCurrentPoints = shots.Where(s => s.Player.Id == player.Id).Sum(s => s.Points); int totalPoints = allHoles.Where(h => h.Id <= hole.Id).Sum(h => h.Par); int totalPointsTaken = shots.Sum(s => s.Points); int pointsAvailable = totalPoints - totalPointsTaken; var currentShot = new Shot { Game = game, Player = player, Hole = hole, Attempts = attempts, ShotMade = shotMade }; // If first shot of the game, logic is easy if (!shots.Any()) { if (currentShot.ShotMade) { currentShot.ShotType = allShotTypes.Single(st => st.Id == ShotTypeMake); currentShot.Points = hole.Par; } else { currentShot.ShotType = allShotTypes.Single(st => st.Id == ShotTypeMiss); } } else { // If not the first shot, stuff starts getting a little complicated // If the shot was made, start the logic, else just put in a missed shot if (currentShot.ShotMade) { // First lets check if it is the first shot of the current hole if (shots.Any(s => s.Hole.Id == currentShot.Hole.Id && s.ShotMade == true)) { // If it's not the first shot, figure out if it is a push var currentLowestShotMade = shots.Where(s => s.Hole.Id == currentShot.Hole.Id && s.ShotMade == true).OrderBy(s => s.Attempts).First(); if (currentLowestShotMade.Attempts == currentShot.Attempts) { // This is a push currentShot.ShotType = allShotTypes.Single(st => st.Id == ShotTypePush); currentShot.Points = pointsAvailable; // Zero out other player's points that this player pushed. Update(currentLowestShotMade.Id, 0, allShotTypes.Single(st => st.Id == ShotTypeMake)); } else { // Then it is a steal (because someone has already made it if we got to this point) // TODO: Need to account for StainlessSteals here // First figure out if the hole had been pushed, because then it will be a "Sugar-Free Steal" if (shots.Any(s => s.Hole.Id == currentShot.Hole.Id && s.ShotType.Id == ShotTypePush)) { // This is a sugar-free steal currentShot.ShotType = allShotTypes.Single(st => st.Id == ShotTypeSugarFreeSteal); currentShot.Points = pointsAvailable; // Reset the person who had a push to just be a "Make" now var playerWithPush = shots.Single(s => s.Hole.Id == currentShot.Hole.Id && s.ShotType.Id == ShotTypePush); Update(playerWithPush.Id, 0, allShotTypes.Single(st => st.Id == ShotTypeMake)); } else { var playerWithPoints = shots.Single(s => s.Hole.Id == currentShot.Hole.Id && s.Points > 0); // This is a regular steal currentShot.ShotType = allShotTypes.Single(st => st.Id == ShotTypeSteal); currentShot.Points = playerWithPoints.Points; // Reset the score to zero for the player who previously had the points Update(playerWithPoints.Id, 0, allShotTypes.Single(st => st.Id == ShotTypeMake)); } } } else { // If it is the first shot on the hole, set type and sum points if (currentShot.ShotMade) { currentShot.ShotType = allShotTypes.Single(st => st.Id == ShotTypeMake); currentShot.Points = pointsAvailable; } else { currentShot.ShotType = allShotTypes.Single(st => st.Id == ShotTypeMiss); } } } else { currentShot.ShotType = allShotTypes.Single(st => st.Id == ShotTypeMiss); } } _shotRepository.Add(currentShot); }
public IEnumerable <Hole> GetHoles() { return(_holeRepository.All().ToList()); }