From 15c63c097ce3c9314eef2e92bf77bf5fc03b4040 Mon Sep 17 00:00:00 2001 From: esuo1198 <54134.es@gmail.com> Date: Thu, 18 Apr 2024 22:02:22 +0900 Subject: [PATCH 1/2] Fix Oni/UraOni best score handling --- TaikoLocalServer/Handlers/GetSelfBestQuery.cs | 41 +++++++++++++------ 1 file changed, 29 insertions(+), 12 deletions(-) diff --git a/TaikoLocalServer/Handlers/GetSelfBestQuery.cs b/TaikoLocalServer/Handlers/GetSelfBestQuery.cs index 66d575d..c21e27f 100644 --- a/TaikoLocalServer/Handlers/GetSelfBestQuery.cs +++ b/TaikoLocalServer/Handlers/GetSelfBestQuery.cs @@ -5,7 +5,7 @@ namespace TaikoLocalServer.Handlers; public record GetSelfBestQuery(uint Baid, uint Difficulty, uint[] SongIdList) : IRequest; -public class GetSelfBestQueryHandler(IGameDataService gameDataService, TaikoDbContext context, ILogger logger) +public class GetSelfBestQueryHandler(IGameDataService gameDataService, TaikoDbContext context, ILogger logger) : IRequestHandler { public async Task Handle(GetSelfBestQuery request, CancellationToken cancellationToken) @@ -21,22 +21,39 @@ public class GetSelfBestQueryHandler(IGameDataService gameDataService, TaikoDbCo logger.LogWarning("Invalid song IDs: {InvalidSongIds}", invalidSongIds.Stringify()); requestSet.ExceptWith(invalidSongIds); } - - var selfbestScores = await context.SongBestData - .Where(datum => datum.Baid == request.Baid && + + var selfBestScores = await context.SongBestData + .Where(datum => datum.Baid == request.Baid && requestSet.Contains(datum.SongId) && - (datum.Difficulty == requestDifficulty || + (datum.Difficulty == requestDifficulty || (datum.Difficulty == Difficulty.UraOni && requestDifficulty == Difficulty.Oni))) .OrderBy(datum => datum.SongId) .ToListAsync(cancellationToken); - var selfBestList = selfbestScores.ConvertAll(datum => new CommonSelfBestResponse.SelfBestData + var selfBestList = new List(); + foreach (var songId in selfBestScores.Select(datum => datum.SongId)) { - SongNo = datum.SongId, - SelfBestScore = datum.BestScore, - UraBestScore = datum.Difficulty == Difficulty.UraOni ? datum.BestScore : 0, - SelfBestScoreRate = datum.BestRate, - UraBestScoreRate = datum.Difficulty == Difficulty.UraOni ? datum.BestRate : 0 - }); + var selfBest = new CommonSelfBestResponse.SelfBestData(); + var selfBestScore = selfBestScores + .FirstOrDefault(datum => datum.SongId == songId && + datum.Difficulty == requestDifficulty); + var uraSelfBestScore = selfBestScores + .FirstOrDefault(datum => datum.SongId == songId && + (datum.Difficulty == Difficulty.UraOni && requestDifficulty == Difficulty.Oni)); + + selfBest.SongNo = songId; + if (selfBestScore is not null) + { + selfBest.SelfBestScore = selfBestScore.BestScore; + selfBest.SelfBestScoreRate = selfBestScore.BestRate; + } + if (uraSelfBestScore is not null) + { + selfBest.UraBestScore = uraSelfBestScore.BestScore; + selfBest.UraBestScoreRate = uraSelfBestScore.BestRate; + } + + selfBestList.Add(selfBest); + } // For songs that don't have a score, add them to the response with 0s var missingSongs = requestSet.Except(selfBestList.Select(datum => datum.SongNo)); selfBestList.AddRange(missingSongs.Select(songNo => new CommonSelfBestResponse.SelfBestData From 4803c0acb894b3bb2a990c29fb167414022ac5c3 Mon Sep 17 00:00:00 2001 From: esuo1198 <54134.es@gmail.com> Date: Sat, 4 May 2024 11:21:33 +0900 Subject: [PATCH 2/2] Refactor GetSelfBestQuery --- TaikoLocalServer/Handlers/GetSelfBestQuery.cs | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/TaikoLocalServer/Handlers/GetSelfBestQuery.cs b/TaikoLocalServer/Handlers/GetSelfBestQuery.cs index c21e27f..fbd08ac 100644 --- a/TaikoLocalServer/Handlers/GetSelfBestQuery.cs +++ b/TaikoLocalServer/Handlers/GetSelfBestQuery.cs @@ -27,10 +27,9 @@ public class GetSelfBestQueryHandler(IGameDataService gameDataService, TaikoDbCo requestSet.Contains(datum.SongId) && (datum.Difficulty == requestDifficulty || (datum.Difficulty == Difficulty.UraOni && requestDifficulty == Difficulty.Oni))) - .OrderBy(datum => datum.SongId) .ToListAsync(cancellationToken); var selfBestList = new List(); - foreach (var songId in selfBestScores.Select(datum => datum.SongId)) + foreach (var songId in request.SongIdList) { var selfBest = new CommonSelfBestResponse.SelfBestData(); var selfBestScore = selfBestScores @@ -38,7 +37,7 @@ public class GetSelfBestQueryHandler(IGameDataService gameDataService, TaikoDbCo datum.Difficulty == requestDifficulty); var uraSelfBestScore = selfBestScores .FirstOrDefault(datum => datum.SongId == songId && - (datum.Difficulty == Difficulty.UraOni && requestDifficulty == Difficulty.Oni)); + datum.Difficulty == Difficulty.UraOni && requestDifficulty == Difficulty.Oni); selfBest.SongNo = songId; if (selfBestScore is not null) @@ -54,12 +53,6 @@ public class GetSelfBestQueryHandler(IGameDataService gameDataService, TaikoDbCo selfBestList.Add(selfBest); } - // For songs that don't have a score, add them to the response with 0s - var missingSongs = requestSet.Except(selfBestList.Select(datum => datum.SongNo)); - selfBestList.AddRange(missingSongs.Select(songNo => new CommonSelfBestResponse.SelfBestData - { - SongNo = songNo - })); var response = new CommonSelfBestResponse {