1
0
mirror of synced 2024-11-28 00:20:53 +01:00

Merge branch 'Refactor' of https://github.com/asesidaa/TaikoLocalServer into LoginRefactor

This commit is contained in:
S-Sebb 2024-05-25 17:53:47 +01:00
commit 75e90d9aa2

View File

@ -5,7 +5,7 @@ namespace TaikoLocalServer.Handlers;
public record GetSelfBestQuery(uint Baid, uint Difficulty, uint[] SongIdList) : IRequest<CommonSelfBestResponse>; public record GetSelfBestQuery(uint Baid, uint Difficulty, uint[] SongIdList) : IRequest<CommonSelfBestResponse>;
public class GetSelfBestQueryHandler(IGameDataService gameDataService, TaikoDbContext context, ILogger<GetSelfBestQueryHandler> logger) public class GetSelfBestQueryHandler(IGameDataService gameDataService, TaikoDbContext context, ILogger<GetSelfBestQueryHandler> logger)
: IRequestHandler<GetSelfBestQuery, CommonSelfBestResponse> : IRequestHandler<GetSelfBestQuery, CommonSelfBestResponse>
{ {
public async Task<CommonSelfBestResponse> Handle(GetSelfBestQuery request, CancellationToken cancellationToken) public async Task<CommonSelfBestResponse> Handle(GetSelfBestQuery request, CancellationToken cancellationToken)
@ -21,28 +21,38 @@ public class GetSelfBestQueryHandler(IGameDataService gameDataService, TaikoDbCo
logger.LogWarning("Invalid song IDs: {InvalidSongIds}", invalidSongIds.Stringify()); logger.LogWarning("Invalid song IDs: {InvalidSongIds}", invalidSongIds.Stringify());
requestSet.ExceptWith(invalidSongIds); requestSet.ExceptWith(invalidSongIds);
} }
var selfbestScores = await context.SongBestData var selfBestScores = await context.SongBestData
.Where(datum => datum.Baid == request.Baid && .Where(datum => datum.Baid == request.Baid &&
requestSet.Contains(datum.SongId) && requestSet.Contains(datum.SongId) &&
(datum.Difficulty == requestDifficulty || (datum.Difficulty == requestDifficulty ||
(datum.Difficulty == Difficulty.UraOni && requestDifficulty == Difficulty.Oni))) (datum.Difficulty == Difficulty.UraOni && requestDifficulty == Difficulty.Oni)))
.OrderBy(datum => datum.SongId)
.ToListAsync(cancellationToken); .ToListAsync(cancellationToken);
var selfBestList = selfbestScores.ConvertAll(datum => new CommonSelfBestResponse.SelfBestData var selfBestList = new List<CommonSelfBestResponse.SelfBestData>();
foreach (var songId in request.SongIdList)
{ {
SongNo = datum.SongId, var selfBest = new CommonSelfBestResponse.SelfBestData();
SelfBestScore = datum.BestScore, var selfBestScore = selfBestScores
UraBestScore = datum.Difficulty == Difficulty.UraOni ? datum.BestScore : 0, .FirstOrDefault(datum => datum.SongId == songId &&
SelfBestScoreRate = datum.BestRate, datum.Difficulty == requestDifficulty);
UraBestScoreRate = datum.Difficulty == Difficulty.UraOni ? datum.BestRate : 0 var uraSelfBestScore = selfBestScores
}); .FirstOrDefault(datum => datum.SongId == songId &&
// For songs that don't have a score, add them to the response with 0s datum.Difficulty == Difficulty.UraOni && requestDifficulty == Difficulty.Oni);
var missingSongs = requestSet.Except(selfBestList.Select(datum => datum.SongNo));
selfBestList.AddRange(missingSongs.Select(songNo => new CommonSelfBestResponse.SelfBestData selfBest.SongNo = songId;
{ if (selfBestScore is not null)
SongNo = songNo {
})); selfBest.SelfBestScore = selfBestScore.BestScore;
selfBest.SelfBestScoreRate = selfBestScore.BestRate;
}
if (uraSelfBestScore is not null)
{
selfBest.UraBestScore = uraSelfBestScore.BestScore;
selfBest.UraBestScoreRate = uraSelfBestScore.BestRate;
}
selfBestList.Add(selfBest);
}
var response = new CommonSelfBestResponse var response = new CommonSelfBestResponse
{ {