Exemplo n.º 1
0
        private PlayerResponse GetPlayer(int?playerId = null, string username = null)
        {
            var row = _context.Players
                      .Include(x => x.Badges)
                      .Include(x => x.Matches)
                      .ThenInclude(x => x.Match)
                      .Select(x => new {
                LastMatch = x.Matches.OrderByDescending(x => x.Match.StartDate).FirstOrDefault(),
                Player    = x
            })
                      .FirstOrDefault(x => string.IsNullOrEmpty(username) ? x.Player.Id == playerId : x.Player.Username == username);

            if (row == null)
            {
                throw new NotFoundException("Player not found");
            }

            var response = _mapper.Map <PlayerResponse>(row.Player);

            response.ConservativeRating = _ratingService.GetConservative(row.Player.RatingMean, row.Player.RatingDeviation);

            if (row.LastMatch != null)
            {
                response.MatchId = row.LastMatch.MatchId.ToString();
                var decayStep = _decayService.GetDecaySteps(row.LastMatch.DecayDays, row.LastMatch.Match.StartDate);
                response.TotalDecay  = _decayService.GetDecayValues(decayStep);
                response.RegainDecay = _decayService.GetDecayValue(decayStep);
            }

            return(response);
        }
Exemplo n.º 2
0
        public void RateMatch(Match match)
        {
            var gameInfo = new Moserware.Skills.GameInfo(
                _ratingConfig.InitialMean,
                _ratingConfig.InitialDeviation,
                _ratingConfig.InitialMean / _ratingConfig.BetaDivisor,
                _ratingConfig.InitialMean / _ratingConfig.DynamicsFactorDivisor,
                _ratingConfig.DrawProbability
                );

            var teams   = new List <Dictionary <Moserware.Skills.Player, Moserware.Skills.Rating> >();
            var players = new Dictionary <MatchPlayer, Moserware.Skills.Player>();

            foreach (var matchPlayer in match.MatchPlayers.OrderByDescending(x => x.IsWin))
            {
                var trueSkillPlayer = new Moserware.Skills.Player(matchPlayer.PlayerId);
                var rating          = new Moserware.Skills.Rating(matchPlayer.OldRatingMean, matchPlayer.OldRatingDeviation);
                players.Add(matchPlayer, trueSkillPlayer);

                var team = new Dictionary <Moserware.Skills.Player, Moserware.Skills.Rating>();
                team.Add(trueSkillPlayer, rating);
                teams.Add(team);
            }

            // Calculate the new ratings for each player, listing the winner first
            var newRatings = Moserware.Skills.TrueSkillCalculator.CalculateNewRatings(gameInfo, teams, 1, 2);

            // Save the new ratings back to the player entity
            foreach (var matchPlayer in match.MatchPlayers)
            {
                var trueSkillPlayer = players[matchPlayer];
                var newRating       = newRatings[trueSkillPlayer];
                matchPlayer.NewRatingMean      = GetMean(matchPlayer.OldRatingMean, newRating.Mean);
                matchPlayer.NewRatingDeviation = GetDeviation(matchPlayer.OldRatingDeviation, newRating.StandardDeviation);
                if (matchPlayer.IsWin)
                {
                    matchPlayer.DecayValue = _decayService.GetDecayValue(matchPlayer.DecayDays);
                    matchPlayer.DecayDays  = Math.Max(0, matchPlayer.DecayDays - 1);
                }
                matchPlayer.RatingDelta = GetDelta(matchPlayer);
            }
        }