BusinessLogic.Logic.Players.PlayerRetriever.GetPlayerStatistics C# (CSharp) Метод

GetPlayerStatistics() публичный Метод

public GetPlayerStatistics ( int playerId ) : PlayerStatistics
playerId int
Результат BusinessLogic.Models.Players.PlayerStatistics
        public virtual PlayerStatistics GetPlayerStatistics(int playerId)
        {
            var playerStatistics = new PlayerStatistics();
            var gameDefinitionTotals = GetGameDefinitionTotals(playerId);
            playerStatistics.GameDefinitionTotals = gameDefinitionTotals;

            var topLevelTotals = GetTopLevelTotals(gameDefinitionTotals);

            playerStatistics.TotalGames = topLevelTotals.TotalGames;
            playerStatistics.TotalGamesLost = topLevelTotals.TotalGamesLost;
            playerStatistics.TotalGamesWon = topLevelTotals.TotalGamesWon;

            if (playerStatistics.TotalGames > 0)
            {
                playerStatistics.WinPercentage = (int)((decimal)playerStatistics.TotalGamesWon / (playerStatistics.TotalGames) * 100);
            }

            playerStatistics.NemePointsSummary = GetNemePointsSummary(playerId);

            //had to cast to handle the case where there is no data:
            //http://stackoverflow.com/questions/6864311/the-cast-to-value-type-int32-failed-because-the-materialized-value-is-null
            playerStatistics.AveragePlayersPerGame = (float?)dataContext.GetQueryable<PlayedGame>()
                .Where(playedGame => playedGame.PlayerGameResults.Any(result => result.PlayerId == playerId))
                    .Average(game => (int?)game.NumberOfPlayers) ?? 0F;

            return playerStatistics;
        }

Usage Example

        public void ItGetsTheTotalPoints()
        {
            using (IDataContext dataContext = new NemeStatsDataContext())
            {
                IPlayerRepository playerRepository = new EntityFrameworkPlayerRepository(dataContext);
                IPlayedGameRetriever playedGameRetriever = new PlayedGameRetriever(dataContext);
                IPlayerRetriever playerRetriever = new PlayerRetriever(dataContext, playerRepository, playedGameRetriever); 
                PlayerStatistics playerStatistics = playerRetriever.GetPlayerStatistics(testPlayer1.Id);

                int totalBasePoints = 0;
                int totalGameDurationPoints = 0;
                int totalWeightBonusPoints = 0;

                foreach(PlayedGame playedGame in testPlayedGames)
                {
                    if(playedGame.PlayerGameResults.Any(result => result.PlayerId == testPlayer1.Id))
                    {
                        var playerGameResult = playedGame.PlayerGameResults.First(result => result.PlayerId == testPlayer1.Id);
                        totalBasePoints += playerGameResult.NemeStatsPointsAwarded;
                        totalGameDurationPoints += playerGameResult.GameDurationBonusPoints;
                        totalWeightBonusPoints += playerGameResult.GameWeightBonusPoints;
                    }
                }

                Assert.AreEqual(totalBasePoints, playerStatistics.NemePointsSummary.BaseNemePoints);
                Assert.AreEqual(totalGameDurationPoints, playerStatistics.NemePointsSummary.GameDurationBonusNemePoints);
                Assert.AreEqual(totalWeightBonusPoints, playerStatistics.NemePointsSummary.WeightBonusNemePoints);
            }
        }
All Usage Examples Of BusinessLogic.Logic.Players.PlayerRetriever::GetPlayerStatistics