From ff668069dba8aed35eebdc537a0a8caea4e1b1de Mon Sep 17 00:00:00 2001 From: Egor Dvachevskiy Date: Thu, 24 Mar 2022 00:57:32 +0200 Subject: [PATCH] modified recalculator --- .../Beatmaps/BeatmapsController.cs | 5 +-- OsuLazerServer/Services/Commands/Commands.cs | 39 +++++++++++++------ OsuLazerServer/Services/Users/UserStorage.cs | 7 +++- OsuLazerServer/Utils/BeatmapUtils.cs | 8 +++- 4 files changed, 42 insertions(+), 17 deletions(-) diff --git a/OsuLazerServer/Controllers/Beatmaps/BeatmapsController.cs b/OsuLazerServer/Controllers/Beatmaps/BeatmapsController.cs index 1b5ad20..035eb04 100644 --- a/OsuLazerServer/Controllers/Beatmaps/BeatmapsController.cs +++ b/OsuLazerServer/Controllers/Beatmaps/BeatmapsController.cs @@ -212,7 +212,7 @@ public class BeatmapsController : Controller { stats.RankedScore += score.TotalScore; - stats.PerfomancePoints += (int) Math.Floor(score.PerfomancePoints); + await _storage.UpdatePerformance(ruleset.ShortName, user.Id, score.PerfomancePoints); } else { @@ -235,7 +235,7 @@ public class BeatmapsController : Controller stats.Accuracy = (float)(_context.Scores.Where(s => s.Passed && s.UserId == user.Id).Select(a => a.Accuracy * 100) .ToList().Average() / 100F); } - await _storage.UpdatePerformance(ruleset.ShortName, user.Id, score.PerfomancePoints); + } @@ -253,7 +253,6 @@ public class BeatmapsController : Controller _storage.GlobalLeaderboardCache.Remove($"{ruleset.ShortName}:score"); ModeUtils.StatsCache.Remove($"{ruleset.ShortName}:{user.Id}"); - await _storage.UpdateRankings(ruleset.ShortName); return Json(new APIScore diff --git a/OsuLazerServer/Services/Commands/Commands.cs b/OsuLazerServer/Services/Commands/Commands.cs index d3e033b..08ca4a5 100644 --- a/OsuLazerServer/Services/Commands/Commands.cs +++ b/OsuLazerServer/Services/Commands/Commands.cs @@ -54,7 +54,7 @@ public class Commands Console.WriteLine("Recalculating scores..."); var scores = ctx.Scores; - foreach (var score in scores.Where(c => c.Status == DbScoreStatus.BEST && c.Passed)) + foreach (var score in scores.Where(c => c.Passed)) { try @@ -77,10 +77,7 @@ public class Commands await user.FetchUserStats(); - if (score.Mods.Contains("RX")) - { - continue; - } + var ruleset = (RulesetId) score.RuleSetId switch { @@ -123,6 +120,8 @@ public class Commands score.PerfomancePoints = perfomance; + + var userStats = user.GetStats(score.RuleSetId switch { 0 => "osu", @@ -131,21 +130,37 @@ public class Commands 3 => "mania", _ => "osu" }); - userStats.PerfomancePoints += (int) score.PerfomancePoints; - userStats.TotalScore += score.TotalScore; + + var unrankedMods = new []{"AT", "AA", "CN", "DA", "RX"}; + + + if (score.Mods.Any(mod => unrankedMods.Contains(mod))) + { + score.Status = DbScoreStatus.OUTDATED; + score.PerfomancePoints = 0; + Console.WriteLine($"Score {score.Id} by {score.UserId} using unranked mods ({String.Join("", score.Mods)}), Unranking this score."); + } + + + if (score.Status == DbScoreStatus.BEST) + { + userStats.TotalScore = score.TotalScore; + } - if (isRanked) + if (isRanked && score.Status == DbScoreStatus.BEST) + { + userStats.PerfomancePoints += (int) score.PerfomancePoints; userStats.RankedScore += score.TotalScore; - + } + - Console.WriteLine( - $"{score.UserId} {beatmap.BeatmapInfo.GetDisplayTitle()} ({beatmap.BeatmapInfo.GetDisplayTitleRomanisable()}) => {perfomance}"); + Console.WriteLine($"{score.UserId} {beatmap.BeatmapInfo.GetDisplayTitle()} ({beatmap.BeatmapInfo.GetDisplayTitleRomanisable()}) => {perfomance}"); await currentContext.SaveChangesAsync(); } catch (Exception e) { - Console.WriteLine($"Cannot recalculate score {score.Id}: {e}"); + Console.WriteLine($"Cannot recalculate score {score.Id}"); } } diff --git a/OsuLazerServer/Services/Users/UserStorage.cs b/OsuLazerServer/Services/Users/UserStorage.cs index 2ee25d6..58f1208 100644 --- a/OsuLazerServer/Services/Users/UserStorage.cs +++ b/OsuLazerServer/Services/Users/UserStorage.cs @@ -335,6 +335,10 @@ public class UserStorage : IUserStorage, IServiceScope public async Task UpdatePerformance(string mode, int userId, double peromance) { var cache = ServiceProvider.GetService()!; + var context = new LazerContext(); + var user = context.Users.FirstOrDefault(c => c.Id == userId); + + var stats = user.FetchStats(mode); var currentPerfomance = await GetUserPerfomancePoints(userId, mode switch { @@ -345,7 +349,8 @@ public class UserStorage : IUserStorage, IServiceScope _ => 0 }) + peromance; await cache.SetAsync($"leaderboard:{mode}:{userId}:perfomance", BitConverter.GetBytes(currentPerfomance)); - + stats.PerfomancePoints = (int)peromance; + return currentPerfomance; } diff --git a/OsuLazerServer/Utils/BeatmapUtils.cs b/OsuLazerServer/Utils/BeatmapUtils.cs index a07b8e7..54a1bf8 100644 --- a/OsuLazerServer/Utils/BeatmapUtils.cs +++ b/OsuLazerServer/Utils/BeatmapUtils.cs @@ -21,6 +21,8 @@ namespace OsuLazerServer.Utils; */ public class BeatmapUtils { + + public static Dictionary BeatmapStreamCache { get; set; } = new(); public static BeatmapOnlineStatus Status(string status) { var result = BeatmapOnlineStatus.None; @@ -54,9 +56,13 @@ public class BeatmapUtils public static async Task GetBeatmapStream(int beatmapId) { + /*if (BeatmapStreamCache.TryGetValue(beatmapId, out var stream)) + return stream;*/ var response = await (await new HttpClient().GetAsync($"https://osu.ppy.sh/osu/{beatmapId}")).Content.ReadAsStreamAsync(); response.Seek(0, SeekOrigin.Begin); - + + /* + BeatmapStreamCache.Add(beatmapId, response);*/ return response; } -- GitLab