public async Task AddAsync(Ranking ranking)
        {
            var rankings = await GetAllAsync();

            if (rankings.Any(x => x.Description == ranking.Description))
            {
                AddNotification("Já existe outro ranking cadastrado com essa descrição.");
            }

            if (ValidateEntity(ranking))
            {
                _rankingRepository.Add(ranking);

                if (!await CommitAsync())
                {
                    AddNotification("Não foi possível cadastrar o ranking.");
                }
            }
        }
예제 #2
0
 public ActionResult <Ranking> PostRanking(RankingDTO rankingDTO)
 {
     try
     {
         Ranking ranking = new Ranking(rankingDTO.Naam);
         if (rankingDTO.Gebieden.Count() != 0)
         {
             rankingDTO.Gebieden.ToList().ForEach(gebiedRankingDTO =>
             {
                 Gebied gebied = _gebiedRepository.GetById(gebiedRankingDTO.GebiedId);
                 ranking.AddGebied(gebied, gebiedRankingDTO.Positie);
             });
         }
         _rankingRepository.Add(ranking);
         _rankingRepository.SaveChanges();
         return(CreatedAtAction(nameof(GetRanking), new { id = ranking.Id }, ranking));
     }
     catch (Exception e)
     {
         return(BadRequest(e.Message));
     }
 }
예제 #3
0
        public async Task FinishPeriod()
        {
            List <Team> teams = await _teamRepository.Get();

            foreach (Team team in teams)
            {
                int     year      = team.Ranking3.Year + 4;
                decimal newPoints = (team.Ranking2.Points * Convert.ToDecimal(0.25)) + (team.Ranking3.Points * Convert.ToDecimal(0.5));

                Domain.Ranking ranking = new Domain.Ranking()
                {
                    TeamID = team.Id,
                    Year   = year,
                    Points = 0
                };

                await _rankingRepository.Add(ranking);

                team.TotalPoints = newPoints;
                _teamRepository.Update(team);
            }

            await _rankingRepository.SaveChanges();
        }
예제 #4
0
        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();
        }