public static int GetRank(Guid leagueId, string userId) { var ratings = db.Ratings .OrderByDescending(q => q.Created) .Where(q => q.LeagueId == leagueId) .ToList(); if (ratings.Count == 0) return 0; var league = db.Leagues .Include(i => i.Users) .FirstOrDefault(q => q.Id == leagueId); if (league == null) return 0; var rankings = new List<RatingsViewModel>(); foreach (var item in league.Users.Distinct()) { var userRatings = ratings.Where(q => q.UserId == item.Id); var latestRating = userRatings.FirstOrDefault(); var ranking = new RatingsViewModel() { Draw = userRatings.Count(q => q.Draw == true), Lost = userRatings.Count(q => q.Lost == true), Won = userRatings.Count(q => q.Won == true), Matches = userRatings.Count(), User = item, Rank = latestRating == null ? 0 : latestRating.Rank, Rate = latestRating == null ? 1000 : latestRating.Rate, RatingChange = latestRating == null ? 0 : latestRating.RatingChange, Team = null }; rankings.Add(ranking); } rankings = rankings .OrderByDescending(q => q.Rate) .ThenByDescending(q => q.Won) .ThenByDescending(q => q.Draw) .ThenBy(q => q.Matches) .ThenBy(q => q.User.UserName) .ToList(); var rank = 1; foreach (var item in rankings) { if (item.User.Id == userId) return rank; rank++; } return league.Users.Distinct().Count(); }
public async Task<ActionResult> Details(string slug) { var league = await db.Leagues.FirstOrDefaultAsync(q => q.Slug.Equals(slug, StringComparison.InvariantCultureIgnoreCase)); if (league == null) return HttpNotFound(); var ratings = await db.Ratings .Where(q => q.LeagueId == league.Id) .OrderByDescending(q => q.Created) .ToListAsync(); var latestMatches = await db.Matches .Take(20) .Include(i => i.Sets) .Include(i => i.AwayUsers) .Include(i => i.HomeUsers) .Include(i => i.AwayTeam) .Include(i => i.HomeTeam) .OrderByDescending(q => q.Created).ToListAsync(); var ratingViewModels = new List<RatingsViewModel>(); foreach (var user in ratings.Select(q => q.User).Distinct()) { var userRatings = ratings.Where(q => q.UserId == user.Id); var userLatestRating = userRatings.FirstOrDefault(); var ratingsViewModel = new RatingsViewModel() { Matches = userRatings.Count(), Draw = userRatings.Count(q => q.Draw), Lost = userRatings.Count(q => q.Lost), Won = userRatings.Count(q => q.Won), Rank = userLatestRating == null ? 0 : userLatestRating.Rank, RankingChange = userLatestRating == null ? 0 : userLatestRating.RankingChange, Rate = userLatestRating == null ? 1000 : userLatestRating.Rate, RatingChange = userLatestRating == null ? 0 : userLatestRating.RatingChange, Score = userRatings.Sum(q => q.Score), Team = null, User = user }; ratingViewModels.Add(ratingsViewModel); } var viewmodel = new LeagueDetailsViewModel() { LatestMatches = latestMatches, League = league, Ratings = ratingViewModels .OrderByDescending(q => q.Rate) .ThenByDescending(q => q.Won) .ThenByDescending(q => q.Draw) .ThenBy(q => q.Matches) .ThenBy(q => q.User.UserName) .ToList() }; var rank = 0; foreach (var item in viewmodel.Ratings) item.Rank = ++rank; return View(viewmodel); }