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); }
private void ComputeDay(DateTime day) { var rows = _context.Players .Include(x => x.Matches) .ThenInclude(x => x.Match) .Where(x => x.Division == Division.Master) .Select(x => new { LastMatch = x.Matches.Where(x => x.Match.StartDate < day).OrderByDescending(x => x.Match.StartDate).First(), Player = x }) .Select(x => new LeaderboardLog() { CreatedAt = day, ConservativeRating = _ratingService.GetConservative(x.LastMatch.NewRatingMean, x.LastMatch.NewRatingDeviation), TotalDecay = _decayService.GetDecayValues(x.LastMatch.DecayDays, x.LastMatch.Match.StartDate, day), PreviousRank = x.Player.Level, PlayerId = x.Player.Id }) .ToList() .OrderByDescending(x => x.ConservativeRating - x.TotalDecay) .ToList(); for (int i = 0; i < rows.Count; i++) { rows[i].Rank = i + 1; } var players = _context.Players.Where(x => x.Division == Division.Master).ToList(); foreach (var player in players) { player.Level = rows.Where(x => x.PlayerId == player.Id).Select(x => x.Rank).FirstOrDefault(); } _context.LeaderboardLogs.AddRange(rows); _context.SaveChanges(); }