/// <summary>
        /// Détermine le nombre de points récoltés par un joueur pour un match donné.
        /// </summary>
        /// <remarks>Cette fonction ne lève pas d'exceptions, toute anomalie entrainera un retour à 0.</remarks>
        /// <param name="match">Le match concerné.</param>
        /// <param name="player">Le joueur concerné.</param>
        /// <param name="wasPreviousExempt">Détermine si le joueur était exempt au tour précédent.</param>
        /// <returns>Le nombre de points pour ce match.</returns>
        public static uint GetPoints(Match match, Player player, bool wasPreviousExempt)
        {
            if (match == null || player == null)
            {
                return(0);
            }

            PointsAtpScale scale = GetLevelScale(match.Edition.TournamentLevel, match.Round)[0];

            if (match.Winner == player)
            {
                return(scale.WPoints);
            }
            else if (match.Loser == player)
            {
                return(wasPreviousExempt ? scale.LPointsEx : scale.LPoints);
            }

            return(0);
        }
Esempio n. 2
0
        /// <summary>
        /// Calcule les statistiques d'un joueur spécifié pour une édition de tournoi.
        /// </summary>
        /// <param name="playerId">L'identifiant du joueur.</param>
        /// <param name="editionId">Identifiant de l'édition.</param>
        /// <param name="stats">Statistique à calculer.</param>
        /// <returns>La statistique.</returns>
        /// <exception cref="ArgumentException">Le joueur spécifié n'a pas été trouvé.</exception>
        public static uint ComputePlayerStatsForEdition(ulong playerId, ulong editionId, StatType stats)
        {
            Player p = GetByID <Player>(playerId);

            if (p == null)
            {
                throw new ArgumentException("Le joueur spécifié n'a pas été trouvé.", nameof(playerId));
            }

            List <Match> baseMatchesList =
                Match.GetPlayerMatches(p.ID)
                .Where(item => item.Edition.ID == editionId)
                .ToList();

            if (baseMatchesList.Count == 0)
            {
                return(0);
            }

            switch (stats)
            {
            case StatType.round:
                return((uint)baseMatchesList.OrderBy(m => m.Round.GetSortOrder()).First().Round);

            case StatType.is_winner:
                return((uint)(baseMatchesList.OrderBy(m => m.Round.GetSortOrder()).First().Winner == p ? 1 : 0));

                #region Calcul des points ATP
            case StatType.points:
                // matchs avec points cumulés
                List <Match> cumuledTypeMatches =
                    baseMatchesList
                    .Where(item =>
                           PointsAtpScale.GetLevelScale(item.Edition.TournamentLevel, item.Round)[0].IsCumuled)
                    .ToList();
                long p1 = cumuledTypeMatches.Sum(item => PointsAtpScale.GetPoints(item, p, item.PlayerWasExempt(p)));

                // matchs perdus dés l'entrée en lice
                List <Match> nonCumuledFirstTurnLose =
                    baseMatchesList
                    .Where(item =>
                           !PointsAtpScale.GetLevelScale(item.Edition.TournamentLevel, item.Round)[0].IsCumuled &&
                           item.Loser == p &&
                           !baseMatchesList.Any(subItem => subItem.Edition == item.Edition && subItem.Round.RoundIsBefore(item.Round)))
                    .ToList();
                long p2 = nonCumuledFirstTurnLose.Sum(item => PointsAtpScale.GetPoints(item, p, item.PlayerWasExempt(p)));

                // matchs gagnés
                List <Match> nonCumuledBestWin =
                    baseMatchesList
                    .Where(item =>
                           !PointsAtpScale.GetLevelScale(item.Edition.TournamentLevel, item.Round)[0].IsCumuled &&
                           item.Winner == p &&
                           !baseMatchesList.Any(subItem => subItem.Edition == item.Edition && item.Round.RoundIsBefore(subItem.Round) && subItem.Winner == p))
                    .ToList();
                long p3 = nonCumuledBestWin.Sum(item => PointsAtpScale.GetPoints(item, p, item.PlayerWasExempt(p)));

                return((uint)(p1 + p2 + p3));

                #endregion
            case StatType.match_win:
                return((uint)baseMatchesList.Count(m => m.Winner == p && !m.Walkover));

            case StatType.match_lost:
                return((uint)baseMatchesList.Count(m => m.Loser == p && !m.Walkover));

            case StatType.set_win:
                return((uint)baseMatchesList.Sum(m => m.Sets.Count(s => s.HasValue && (m.Winner == p ? s.Value.Key == p : s.Value.Key != p))));

            case StatType.set_lost:
                return((uint)baseMatchesList.Sum(m => m.Sets.Count(s => s.HasValue && (m.Winner == p ? s.Value.Key != p : s.Value.Key == p))));

            case StatType.game_win:
                return((uint)baseMatchesList.Sum(m => m.Sets.Sum(s => !s.HasValue ? 0 : ((m.Winner == p ? s.Value.Key == p : s.Value.Key != p) ? s.Value.Value.WScore : s.Value.Value.LScore))));

            case StatType.game_lost:
                return((uint)baseMatchesList.Sum(m => m.Sets.Sum(s => !s.HasValue ? 0 : ((m.Winner == p ? s.Value.Key != p : s.Value.Key == p) ? s.Value.Value.WScore : s.Value.Value.LScore))));

            case StatType.tb_win:
                return((uint)baseMatchesList.Sum(m => m.Sets.Count(s => s.HasValue && (m.Winner == p ? s.Value.Key == p : s.Value.Key != p) && s.Value.Value.IsTieBreak)));

            case StatType.tb_lost:
                return((uint)baseMatchesList.Sum(m => m.Sets.Count(s => s.HasValue && (m.Winner == p ? s.Value.Key != p : s.Value.Key == p) && s.Value.Value.IsTieBreak)));

            case StatType.ace:
                return((uint)(baseMatchesList.Sum(m => m.Winner == p ? m.WinnerCountAce ?? 0 : m.LoserCountAce ?? 0)));

            case StatType.d_f:
                return((uint)(baseMatchesList.Sum(m => m.Winner == p ? m.WinnerCountDbFault ?? 0 : m.LoserCountDbFault ?? 0)));

            case StatType.sv_pt:
                return((uint)(baseMatchesList.Sum(m => m.Winner == p ? m.WinnerCountServePt ?? 0 : m.LoserCountServePt ?? 0)));

            case StatType.first_in:
                return((uint)(baseMatchesList.Sum(m => m.Winner == p ? m.WinnerCount1stIn ?? 0 : m.LoserCount1stIn ?? 0)));

            case StatType.first_won:
                return((uint)(baseMatchesList.Sum(m => m.Winner == p ? m.WinnerCount1stWon ?? 0 : m.LoserCount1stWon ?? 0)));

            case StatType.second_won:
                return((uint)(baseMatchesList.Sum(m => m.Winner == p ? m.WinnerCount2ndWon ?? 0 : m.LoserCount2ndWon ?? 0)));

            case StatType.sv_gms:
                return((uint)(baseMatchesList.Sum(m => m.Winner == p ? m.WinnerCountServeGames ?? 0 : m.LoserCountServeGames ?? 0)));

            case StatType.bp_saved:
                return((uint)(baseMatchesList.Sum(m => m.Winner == p ? m.WinnerCountBreakPtSaved ?? 0 : m.LoserCountBreakPtSaved ?? 0)));

            case StatType.bp_faced:
                return((uint)(baseMatchesList.Sum(m => m.Winner == p ? m.WinnerCountBreakPtFaced ?? 0 : m.LoserCountBreakPtFaced ?? 0)));
            }

            return(0);
        }