예제 #1
0
        public PlayerStats GetPlayerStats(string playerName)
        {
            var player = _playerRepository.GetByName(playerName);

            if (player == null)
            {
                return(null);
            }
            var allScores            = _scoreRepository.GetScoreBoardByPlayerId(player.PlayerId).ToList();
            var allPlayerMatchesDict = _matchRepository
                                       .GetAllMatchesByIds(allScores.Select(score => new BsonValue(score.MatchId)))
                                       .ToDictionary(match => match.MatchId, match => match);

            var serverDict   = new Dictionary <string, int>();
            var gameModeDict = new Dictionary <string, int>();
            var dayDict      = new Dictionary <DateTime, int>();

            var lastMatchDate   = DateTime.MinValue;
            var totalMatchesWon = 0;
            var scoreBoard      = 0.0;

            foreach (var score in allScores)
            {
                var currentMatch = allPlayerMatchesDict[score.MatchId];

                var serverName = currentMatch.Server.Name;
                serverDict.Increment(serverName);

                var gameModeName = currentMatch.GameMode.Name;
                gameModeDict.Increment(gameModeName);

                totalMatchesWon += score.Position == 1 ? 1 : 0;

                var totalPlayers = currentMatch.ScoreBoard.Count;
                scoreBoard += totalPlayers == 1
                                    ?  1
                                    : (double)(totalPlayers - score.Position) / (totalPlayers - 1);

                var dayKey = currentMatch.Date.Date;
                dayDict.Increment(dayKey);

                lastMatchDate = lastMatchDate.Max(currentMatch.Date);
            }

            var sumAndMax = dayDict.SumAndMax();

            return(new PlayerStats()
            {
                totalMatchesPlayed = allPlayerMatchesDict.Count,
                totalMatchesWon = totalMatchesWon,
                favoriteServer = serverDict.KeyOfMaxValue(int.MinValue),
                uniqueServers = serverDict.Count,
                favoriteGameMode = gameModeDict.KeyOfMaxValue(int.MinValue),
                averageScoreboardPercent = scoreBoard * 100 / allScores.Count,
                maximumMatchesPerDay = sumAndMax.Item2,
                averageMatchesPerDay = (double)sumAndMax.Item1 / dayDict.Count,
                lastMatchPlayed = lastMatchDate.ToIsoFormat(),
                killToDeathRatio = player.KillsToDeathRatio
            });
        }