private async Task UpdateLeaderboard(Challenge.Challenge challenge, IAsyncEnumerable <RankedSolution> solutions) { // Update trueskill await _skillUpdate.ApplyChallengeResults(challenge.Id); const uint maxScore = 10; var score = maxScore; // Enumerate through each group of equally ranked users var ranks = solutions.GroupBy(a => a.Rank).OrderBy(a => a.Key); await foreach (var rank in ranks) { // Award all users at the same rank some points var count = 0; await foreach (var solution in rank) { count++; await _leaderboard.AddScore(solution.Solution.UserId, score *(uint)challenge.Difficulty); } // If there was only one user in the top rank, award them a bonus if (count == 1 && score == maxScore) { await _leaderboard.AddScore((await rank.SingleAsync()).Solution.UserId, (uint)challenge.Difficulty); } // Award at least one point to every entrant if (score > 1) { score--; } } // Find the smallest solution, if there's only one of them (i.e. no tie for smallest) award a bonus point var smallestGroup = await solutions.GroupBy(a => a.Solution.Yolol.Length).AggregateAsync((a, b) => a.Key < b.Key ? a : b); if (await smallestGroup.CountAsync() == 1) { await _leaderboard.AddScore((await smallestGroup.FirstAsync()).Solution.UserId, (uint)challenge.Difficulty); } }
public async Task GivePoints(IUser user, uint score) { await _leaderboard.AddScore(user.Id, score); }