public void Delete(Ranking model) { using (var context = new BolfTrackerContext()) { context.Rankings.Remove(model); context.SaveChanges(); } }
public void Add(Ranking model) { using (var context = new BolfTrackerContext()) { context.Rankings.Attach(model); context.Entry(model).State = EntityState.Added; context.SaveChanges(); } }
public void CalculateRankings(int month, int year) { // The easiest thing to do now is to just delete all of the rankings and insert the updated stats DeleteRankings(month, year); var games = _gameRepository.GetFinalizedByMonthAndYear(month, year); var playerGameStatistics = _playerGameStatisticsRepository.GetByMonthAndYear(month, year); var shots = _shotRepository.GetByMonthAndYear(month, year); var players = _playerRepository.GetActiveByMonthAndYear(month, year); // The eligibility line will only sample the top half of players sorted by number of games played int eligibilityLine = DetermineEligibilityLine(games, players, playerGameStatistics); // The second thing we want to do is find the top ranked player because we will need their stats // to be able to calculate the "games back" for all other players decimal topPlayerWinningPercentage = 0.00M; int topPlayerWins = 0, topPlayerLosses = 0, topPlayerTotalPoints = 0, topPlayerPointsPerGame = 0; Action<decimal, int, int, int, int> setTopPlayerStats = (winningPercentage, wins, losses, totalPoints, pointsPerGame) => { topPlayerWinningPercentage = winningPercentage; topPlayerWins = wins; topPlayerLosses = losses; topPlayerTotalPoints = totalPoints; topPlayerPointsPerGame = pointsPerGame; }; foreach (var player in players) { int wins = playerGameStatistics.Count(gs => gs.Player.Id == player.Id && gs.Winner); int losses = playerGameStatistics.Count(gs => gs.Player.Id == player.Id && !gs.Winner); int totalPoints = playerGameStatistics.Where(gs => gs.Player.Id == player.Id).Sum(gs => gs.Points); int pointsPerGame = totalPoints / (wins + losses); decimal winningPercentage = (losses > 0) ? Decimal.Round(Convert.ToDecimal(wins) / Convert.ToDecimal(wins + losses), 3, MidpointRounding.AwayFromZero) : 1.00M; // Player must first be eligible to be determined as the top player if ((wins + losses) >= eligibilityLine) { // From here on down, use our top player algorithm to determine result (winning percentage --> ppg --> total points) if (winningPercentage > topPlayerWinningPercentage) { setTopPlayerStats(winningPercentage, wins, losses, totalPoints, pointsPerGame); } else if (winningPercentage == topPlayerWinningPercentage) { if (pointsPerGame > topPlayerPointsPerGame) { setTopPlayerStats(winningPercentage, wins, losses, totalPoints, pointsPerGame); } else if (pointsPerGame == topPlayerPointsPerGame) { if (totalPoints > topPlayerTotalPoints) { setTopPlayerStats(winningPercentage, wins, losses, totalPoints, pointsPerGame); } } } } } // Next go through each player and calculate ranking stats to be put in the database foreach (var player in players) { var ranking = new Ranking() { Player = player, Month = month, Year = year }; ranking.Wins = playerGameStatistics.Count(gs => gs.Player.Id == player.Id && gs.Winner); ranking.Losses = playerGameStatistics.Count(gs => gs.Player.Id == player.Id && !gs.Winner); ranking.WinningPercentage = Decimal.Round((decimal)ranking.Wins / (decimal)ranking.TotalGames, 3, MidpointRounding.AwayFromZero); ranking.TotalPoints = playerGameStatistics.Where(gs => gs.Player.Id == player.Id).Sum(gs => gs.Points); ranking.PointsPerGame = Convert.ToInt32(Decimal.Round((decimal)ranking.TotalPoints / (decimal)ranking.TotalGames, 1, MidpointRounding.AwayFromZero)); //ranking.TotalPoints / ranking.TotalGames; ranking.Eligible = ranking.TotalGames >= eligibilityLine; var lastTenGames = playerGameStatistics.Where(gs => gs.Player.Id == player.Id).OrderByDescending(gs => gs.Game.Date).Take(10); ranking.LastTenWins = lastTenGames.Count(gs => gs.Player.Id == player.Id && gs.Winner); ranking.LastTenLosses = lastTenGames.Count(gs => gs.Player.Id == player.Id && !gs.Winner); ranking.LastTenWinningPercentage = Decimal.Round((decimal)ranking.LastTenWins / (decimal)ranking.LastTenTotalGames, 3, MidpointRounding.AwayFromZero); if (ranking.WinningPercentage != topPlayerWinningPercentage) { ranking.GamesBack = Decimal.Round((Convert.ToDecimal((topPlayerWins - topPlayerLosses) - (ranking.Wins - ranking.Losses))) / 2M, 1, MidpointRounding.AwayFromZero); } _rankingRepository.Add(ranking); } //_unitOfWork.Commit(); }