public void Delete(Player model)
 {
     using (var context = new BolfTrackerContext())
     {
         context.Players.Remove(model);
         context.SaveChanges();
     }
 }
 public void Update(Player player)
 {
     using (var context = new BolfTrackerContext())
     {
         context.Entry<Player>(player).State = EntityState.Modified;
         context.SaveChanges();
     }
 }
 public void Add(Player model)
 {
     using (var context = new BolfTrackerContext())
     {
         context.Players.Attach(model);
         context.Entry(model).State = EntityState.Added;
         context.SaveChanges();
     }
 }
 public PlayerViewModel(int month, int year, Player player, IEnumerable<PlayerStatistics> playerStatistics, PlayerCareerStatistics playerCareerStatistics, IEnumerable<PlayerHoleStatistics> playerHoleStatistics)
 {
     Month = month;
     Year = year;
     Player = player;
     _playerStatistics = playerStatistics;
     PlayerCareerStatistics = playerCareerStatistics;
     PlayerHoleStatistics = playerHoleStatistics;
 }
 public static PlayerRivalryStatistics CreatePlayerRivalryStatistics(Game game, Player player, Player affectedPlayer, Hole hole, ShotType shotType)
 {
     return new PlayerRivalryStatistics
     {
         Game = game,
         Player = player,
         AffectedPlayer = affectedPlayer,
         Hole = hole,
         ShotType = shotType,
         Attempts = 10,
         Points = 5
     };
 }
 public static PlayerHoleStatistics CreatePlayerHoleStatistics(Player player, Hole hole)
 {
     return new PlayerHoleStatistics
     {
         Player = player,
         Hole = hole,
         Month = DateTime.Today.Month,
         Year = DateTime.Today.Year,
         Attempts = 10,
         ShotsMade = 5,
         ShootingPercentage = .500M,
         PointsScored = 12,
         Pushes = 3,
         Steals = 2,
         SugarFreeSteals = 1
     };
 }
 public static PlayerGameStatistics CreatePlayerGameStatistics(Game game, Player player)
 {
     return new PlayerGameStatistics
     {
         Game = game,
         Player = player,
         Points = 8,
         Winner = true,
         OvertimeWin = false,
         ShotsMade = 7,
         Attempts = 5,
         ShootingPercentage = 0.234M,
         Pushes = 4,
         Steals = 3,
         SugarFreeSteals = 2,
         StainlessSteals = 1,
         GameWinningSteal = false,
         Shutout = true,
         PerfectGame = false
     };
 }
        public Player Create(string name)
        {
            Check.Argument.IsNotNullOrEmpty(name, "name", "Player name must contain a value");

            var player = new Player() { Name = name };

            _playerRepository.Add(player);
            _unitOfWork.Commit();

            return player;
        }
 public LeaderboardViewModel(Player player, IEnumerable<Shot> playerShots, Game game, IEnumerable<Shot> shots)
 {
     Player = player;
     Points = playerShots.Where(s => s.ShotType.Id != ShotTypePush).Sum(s => s.Points);
     ShotsMade = playerShots.Count(s => s.ShotMade);
     Attempts = playerShots.Sum(s => s.Attempts);
     ShootingPercentage = Decimal.Round(Convert.ToDecimal(ShotsMade) / Convert.ToDecimal(Attempts), 2, MidpointRounding.AwayFromZero);
     Steals = shots.Count(s => s.Player.Id == player.Id && (s.ShotType.Id == 4 || s.ShotType.Id == 5));
     Pushes = shots.Count(s => s.Player.Id == player.Id && s.ShotType.Id == 3);
 }
        private Player GetCurrentPlayer()
        {
            var playerResult = new Player();
            int currentHole = GetCurrentHole();

            if (Shots.Any())
            {
                var activePlayers = ActivePlayers;
                var playersDescending = GetCurrentActivePlayers(activePlayers, includeOvertime: false);

                var duplicatePlayers = Shots.GroupBy(s => s.Player.Id).Where(p => p.Count() > 1);

                // Check to see if we've had any duplicate players yet (if so, that means we can determine the order)
                if (duplicatePlayers.Any())
                {
                    // If we are on the last hole or in overtime, the order could change because not everyone can win
                    if (currentHole >= 10)
                    {
                        var playersWhoCanWin = GetPlayersWhoCanWin(currentHole);

                        if (!playersWhoCanWin.Any() || playersWhoCanWin.Count() >= playersDescending.Count())
                        {
                            // If all of the players can win, we will go in normal order
                            var lastPlayerToShoot = Shots.Where(s => s.Game.Id == Game.Id).OrderByDescending(s => s.Id).Select(s => s.Player).First();

                            var playerList = playersDescending.Reverse().ToList();

                            int index = playerList.IndexOf(lastPlayerToShoot);

                            return playerList[index == -1 ? 0 : (index + 1) % (playerList.Count)];
                        }
                        else
                        {
                            // If only some of the players can win, we will go in descending order by points
                            var playersWhoCanStillWin = new List<LeaderboardViewModel>();

                            foreach (var player in playersWhoCanWin)
                            {
                                var playerCurrentHoleShots = Shots.Where(s => s.Player.Id == player.Player.Id && s.Game.Id == Game.Id && s.Hole.Id == currentHole);

                                if (!playerCurrentHoleShots.Any())
                                {
                                    playersWhoCanStillWin.Add(player);
                                }
                            }

                            var playersWhoCannotWin = new List<Player>();

                            if (playersWhoCanStillWin.Count == 0)
                            {
                                // This means that one of the players who could win made a shot, so all of the people
                                // that cannot win need to take a shot to push them
                                foreach (var player in playersDescending)
                                {
                                    if (!Shots.Any(s => s.Player.Id == player.Id && s.Hole.Id == currentHole))
                                    {
                                        if (!playersWhoCanWin.Any(l => l.Player.Id == player.Id))
                                        {
                                            playersWhoCannotWin.Add(player);
                                        }
                                    }
                                }

                                // TODO: Here we need to figure out the order that these players who can push
                                // the hole need to go in.  My thought is that the players with the least amount of
                                // pushes for the current period (in our case month) get to go first.
                                if (playersWhoCannotWin.Any())
                                {
                                    return playersWhoCannotWin.Last();
                                }
                                else
                                {
                                    return playersDescending.Last();
                                }
                            }
                            else
                            {
                                return playersWhoCanStillWin.OrderByDescending(l => l.Points).First().Player;
                            }
                        }
                    }

                    return playersDescending.Last();
                }
                else
                {
                    // TODO: My thought for initial player order is that the people with the lowest shooting percentage
                    // get to go first, and once one of them makes it, the players with the lowest pushes get to go second

                    // If we can't determine the order, just get the next player who has not gone already
                    foreach (var player in _allPlayers)
                    {
                        if (!activePlayers.Any(p => p.Id == player.Id))
                        {
                            return player;
                        }
                    }
                }
            }

            return playerResult;
        }
 public static Ranking CreateRanking(Player player)
 {
     return new Ranking
     {
         Month = DateTime.Today.Month,
         Year = DateTime.Today.Year,
         Player = player,
         Wins = 5,
         Losses = 7,
         TotalPoints = 54,
         GamesBack = 2,
         LastTenWins = 5,
         LastTenLosses = 5,
         LastTenWinningPercentage = .111M,
         WinningPercentage = .222M,
         PointsPerGame = 4
     };
 }
 public static Shot CreateShot(Game game, Player player, ShotType shotType, Hole hole)
 {
     return new Shot
     {
         Game = game,
         Player = player,
         ShotType = shotType,
         Hole = hole,
         ShotMade = true,
         Attempts = 1,
         Points = 10
     };
 }
 public LeaderboardViewModel(Player player, int gameId)
 {
     Player = player;
     Points = player.Shots.Where(s => s.Game.Id == gameId).Sum(s => s.Points);
     ShotsMade = player.Shots.Count(s => s.Game.Id == gameId && s.ShotMade);
     Attempts = player.Shots.Where(s => s.Game.Id == gameId).Sum(s => s.Attempts);
     ShootingPercentage = Decimal.Round(Convert.ToDecimal(ShotsMade) / Convert.ToDecimal(Attempts), 2, MidpointRounding.AwayFromZero);
     Steals = player.Shots.Count(s => s.Game.Id == gameId && (s.ShotType.Id == 4 || s.ShotType.Id == 5));
     Pushes = player.Shots.Count(s => s.Game.Id == gameId && s.ShotType.Id == 3);
 }
 public static PlayerStatistics CreatePlayerStatistics(Player player)
 {
     return new PlayerStatistics
     {
         Player = player,
         Month = DateTime.Today.Month,
         Year = DateTime.Today.Year,
         Attempts = 10,
         ShotsMade = 5,
         Points = 12,
         Pushes = 3,
         Steals = 2,
         SugarFreeSteals = 1,
         Wins = 6,
         Losses = 5
     };
 }
        private PlayerStatistics CalculatePlayerStatistics(Player player, int month, int year)
        {
            var playerStatistics = new PlayerStatistics() { Player = player, Month = month, Year = year };

            var playerGameStatistics = _gameStatisticsRepository.GetByPlayerMonthAndYear(player.Id, month, year);

            playerStatistics.Wins = playerGameStatistics.Count(gs => gs.Winner);
            playerStatistics.Losses = playerGameStatistics.Count(gs => !gs.Winner);
            playerStatistics.WinningPercentage = Decimal.Round(Convert.ToDecimal(playerStatistics.Wins) / Convert.ToDecimal(playerStatistics.TotalGames), 3, MidpointRounding.AwayFromZero);
            playerStatistics.ShotsMade = playerGameStatistics.Sum(gs => gs.ShotsMade);
            playerStatistics.Attempts = playerGameStatistics.Sum(gs => gs.Attempts);
            playerStatistics.ShootingPercentage = Decimal.Round(Convert.ToDecimal(playerStatistics.ShotsMade) / Convert.ToDecimal(playerStatistics.Attempts), 3, MidpointRounding.AwayFromZero);
            playerStatistics.Points = playerGameStatistics.Sum(gs => gs.Points);
            playerStatistics.PointsPerGame = Decimal.Round(Convert.ToDecimal(playerStatistics.Points) / Convert.ToDecimal(playerStatistics.TotalGames), 1, MidpointRounding.AwayFromZero);
            playerStatistics.Pushes = playerGameStatistics.Sum(gs => gs.Pushes);
            playerStatistics.PushesPerGame = Decimal.Round(Convert.ToDecimal(playerStatistics.Pushes) / Convert.ToDecimal(playerStatistics.TotalGames), 1, MidpointRounding.AwayFromZero);
            playerStatistics.Steals = playerGameStatistics.Sum(gs => gs.Steals);
            playerStatistics.StealsPerGame = Decimal.Round(Convert.ToDecimal(playerStatistics.Steals) / Convert.ToDecimal(playerStatistics.TotalGames), 1, MidpointRounding.AwayFromZero); ;
            playerStatistics.SugarFreeSteals = playerGameStatistics.Sum(gs => gs.SugarFreeSteals);
            playerStatistics.SugarFreeStealsPerGame = Decimal.Round(Convert.ToDecimal(playerStatistics.SugarFreeSteals) / Convert.ToDecimal(playerStatistics.TotalGames), 1, MidpointRounding.AwayFromZero);

            return playerStatistics;
        }
        private PlayerStatistics CalculatePlayerStatistics(Player player, IEnumerable<PlayerGameStatistics> playerGameStatistics, int month, int year)
        {
            var playerStatistics = new PlayerStatistics() { Player = player, Month = month, Year = year };

            playerStatistics.Wins = playerGameStatistics.Count(gs => gs.Winner);
            playerStatistics.Losses = playerGameStatistics.Count(gs => !gs.Winner);
            playerStatistics.WinningPercentage = Decimal.Round((decimal)playerStatistics.Wins / (decimal)playerStatistics.TotalGames, 3, MidpointRounding.AwayFromZero);
            playerStatistics.ShotsMade = playerGameStatistics.Sum(gs => gs.ShotsMade);
            playerStatistics.Attempts = playerGameStatistics.Sum(gs => gs.Attempts);
            playerStatistics.ShootingPercentage = Decimal.Round((decimal)playerStatistics.ShotsMade / (decimal)playerStatistics.Attempts, 3, MidpointRounding.AwayFromZero);
            playerStatistics.Points = playerGameStatistics.Sum(gs => gs.Points);
            playerStatistics.PointsPerGame = Decimal.Round((decimal)playerStatistics.Points / (decimal)playerStatistics.TotalGames, 1, MidpointRounding.AwayFromZero);
            playerStatistics.Pushes = playerGameStatistics.Sum(gs => gs.Pushes);
            playerStatistics.PushesPerGame = Decimal.Round((decimal)playerStatistics.Pushes / (decimal)playerStatistics.TotalGames, 1, MidpointRounding.AwayFromZero);
            playerStatistics.Steals = playerGameStatistics.Sum(gs => gs.Steals);
            playerStatistics.StealsPerGame = Decimal.Round((decimal)playerStatistics.Steals / (decimal)playerStatistics.TotalGames, 1, MidpointRounding.AwayFromZero); ;
            playerStatistics.SugarFreeSteals = playerGameStatistics.Sum(gs => gs.SugarFreeSteals);
            playerStatistics.SugarFreeStealsPerGame = Decimal.Round((decimal)playerStatistics.SugarFreeSteals / (decimal)playerStatistics.TotalGames, 1, MidpointRounding.AwayFromZero);

            return playerStatistics;
        }
        private PlayerCareerStatistics CalculatePlayerCareerStatistics(Player player, IEnumerable<PlayerGameStatistics> playerGameStatistics)
        {
            var playerCareerStatistics = new PlayerCareerStatistics() { Player = player };

            playerCareerStatistics.Wins = playerGameStatistics.Count(pgs => pgs.Winner);
            playerCareerStatistics.Losses = playerGameStatistics.Count(pgs => !pgs.Winner);
            playerCareerStatistics.WinningPercentage = Decimal.Round((decimal)playerCareerStatistics.Wins / (decimal)playerCareerStatistics.TotalGames, 3, MidpointRounding.AwayFromZero);
            playerCareerStatistics.ShotsMade = playerGameStatistics.Sum(pgs => pgs.ShotsMade);
            playerCareerStatistics.Attempts = playerGameStatistics.Sum(pgs => pgs.Attempts);
            playerCareerStatistics.ShotsMissed = playerCareerStatistics.Attempts - playerCareerStatistics.ShotsMade;
            playerCareerStatistics.ShootingPercentage = Decimal.Round((decimal)playerCareerStatistics.ShotsMade / (decimal)playerCareerStatistics.Attempts, 3, MidpointRounding.AwayFromZero);
            playerCareerStatistics.Points = playerGameStatistics.Sum(pgs => pgs.Points);
            playerCareerStatistics.PointsPerGame = Decimal.Round((decimal)playerCareerStatistics.Points / (decimal)playerCareerStatistics.TotalGames, 1, MidpointRounding.AwayFromZero);
            playerCareerStatistics.Pushes = playerGameStatistics.Sum(pgs => pgs.Pushes);
            playerCareerStatistics.PushesPerGame = Decimal.Round((decimal)playerCareerStatistics.Pushes / (decimal)playerCareerStatistics.TotalGames, 1, MidpointRounding.AwayFromZero);
            playerCareerStatistics.Steals = playerGameStatistics.Sum(pgs => pgs.Steals);
            playerCareerStatistics.StealsPerGame = Decimal.Round((decimal)playerCareerStatistics.Steals / (decimal)playerCareerStatistics.TotalGames, 1, MidpointRounding.AwayFromZero); ;
            playerCareerStatistics.SugarFreeSteals = playerGameStatistics.Sum(pgs => pgs.SugarFreeSteals);
            playerCareerStatistics.SugarFreeStealsPerGame = Decimal.Round((decimal)playerCareerStatistics.SugarFreeSteals / (decimal)playerCareerStatistics.TotalGames, 1, MidpointRounding.AwayFromZero);
            playerCareerStatistics.StainlessSteals = playerGameStatistics.Sum(pgs => pgs.StainlessSteals);
            playerCareerStatistics.StainlessStealsPerGame = Decimal.Round((decimal)playerCareerStatistics.StainlessSteals / (decimal)playerCareerStatistics.TotalGames, 1, MidpointRounding.AwayFromZero);
            playerCareerStatistics.GameWinningSteals = playerGameStatistics.Count(pgs => pgs.GameWinningSteal);
            playerCareerStatistics.OvertimeWins = playerGameStatistics.Count(pgs => pgs.OvertimeWin);
            playerCareerStatistics.RegulationWins = playerGameStatistics.Count(pgs => !pgs.OvertimeWin && pgs.Winner);
            playerCareerStatistics.Shutouts = playerGameStatistics.Count(pgs => pgs.Shutout);
            playerCareerStatistics.PerfectGames = playerGameStatistics.Count(pgs => pgs.PerfectGame);

            return playerCareerStatistics;
        }
        private void CalculatePlayerCareerStatistics(Player player)
        {
            var playerCareerGameStatistics = _playerGameStatisticsRepository.GetByPlayer(player.Id);

            var playerCareerStatistics = CalculatePlayerCareerStatistics(player, playerCareerGameStatistics);
            _playerCareerStatisticsRepository.Add(playerCareerStatistics);
        }
 public void PlayerTestsInitialize()
 {
     _player = new Player();
 }
        private Player GetCurrentPlayer()
        {
            var playerResult = new Player();

            if (Game.Shots.Any())
            {
                var activePlayers = ActivePlayers;

                // TODO: This logic is flawed because it is using the player count, which could change if a player drops
                // out midgame.  This logic keeps on counting the player that drops out, which messes up the order the rest
                // of the game.
                var playersDescending = Game.Shots.OrderByDescending(s => s.Id).Take(activePlayers.Count()).Select(s => s.Player);

                var duplicatePlayers = Game.Shots.GroupBy(s => s.Player.Id).Where(p => p.Count() > 1);

                // Check to see if we've had any duplicate players yet (if so, that means we can determine the order)
                if (duplicatePlayers.Any())
                {
                    // If we are on the last hole or in overtime, the order could change because not
                    // everyone can win
                    int currentHole = GetCurrentHole();

                    if (currentHole >= 10)
                    {
                        int currentPointsAvailable = PointsAvailable;
                        //int maxPointsAtCurrentHole = _allHoles.Where(h => h.Id <= currentHole).Sum(h => h.Par);
                        //int totalPointsTaken = maxPointsAtCurrentHole - currentPointsAvailable;
                        var leaderboard = Leaderboard.OrderByDescending(l => l.Points);

                        var leader = leaderboard.First();

                        // This is the leader's points not counting any temporary points scored on the current hole
                        var leaderPoints = Game.Shots.Where(s => s.Game.Id == Game.Id && s.Player.Id == leader.Player.Id && s.Hole.Id < currentHole).Sum(s => s.Points);

                        var playersWhoCanWin = new List<LeaderboardViewModel>();

                        foreach (var player in leaderboard)
                        {
                            // If the player has already gone on this hole and made the shot then we need to
                            // subtract those points for the next calculation
                            var playerCurrentHoleShot = player.Player.Shots.Where(s => s.Game.Id == Game.Id && s.Player.Id == player.Player.Id && s.Hole.Id == currentHole && s.Points > 0);

                            int playerCurrentHolePoints = playerCurrentHoleShot.Any() ? playerCurrentHoleShot.First().Points : 0;

                            // If the player can at least tie the leader, then he gets to take all shots
                            if (((player.Points - playerCurrentHolePoints) + currentPointsAvailable) >= leaderPoints)
                            {
                                playersWhoCanWin.Add(player);
                            }
                        }

                        if (!playersWhoCanWin.Any() || playersWhoCanWin.Count() == playersDescending.Count())
                        {
                            // If all of the players can win, we will go in normal order
                            return playersDescending.Last();
                        }
                        else
                        {
                            // If only some of the players can win, we will go in descending order by points

                        }

                        return playersWhoCanWin.OrderByDescending(l => l.Points).First().Player;
                    }

                    return playersDescending.Last();
                }
                else
                {
                    // If we can't determine the order, just get the next player who has not gone already
                    foreach (var player in _allPlayers)
                    {
                        if (!activePlayers.Contains(player))
                        {
                            return player;
                        }
                    }
                }
            }

            return playerResult;
        }