From e545598c1cf2698906f1c4d78b46c4379ba68842 Mon Sep 17 00:00:00 2001 From: ptmaster Date: Wed, 11 Sep 2024 09:52:16 +0800 Subject: [PATCH] ChallengeCompete SaveScore --- .../Handlers/UpdatePlayResultCommand.cs | 7 ++- .../Services/ChallengeCompeteService.cs | 54 +++++++++++++++++++ .../Services/Extentions/ServiceExtensions.cs | 1 + .../Interfaces/IChallengeCompeteService.cs | 2 + 4 files changed, 63 insertions(+), 1 deletion(-) diff --git a/TaikoLocalServer/Handlers/UpdatePlayResultCommand.cs b/TaikoLocalServer/Handlers/UpdatePlayResultCommand.cs index 1f89d18..68b5af7 100644 --- a/TaikoLocalServer/Handlers/UpdatePlayResultCommand.cs +++ b/TaikoLocalServer/Handlers/UpdatePlayResultCommand.cs @@ -5,7 +5,7 @@ namespace TaikoLocalServer.Handlers; public record UpdatePlayResultCommand(uint Baid, CommonPlayResultData PlayResultData) : IRequest; -public class UpdatePlayResultCommandHandler(TaikoDbContext context, ILogger logger) +public class UpdatePlayResultCommandHandler(TaikoDbContext context, IChallengeCompeteService challengeCompeteService, ILogger logger) : IRequestHandler { public async Task Handle(UpdatePlayResultCommand request, CancellationToken cancellationToken) @@ -116,6 +116,11 @@ public class UpdatePlayResultCommandHandler(TaikoDbContext context, ILogger challengeCompetes = context.ChallengeCompeteData + .Include(e => e.Songs) + .Include(e => e.Participants) + .Where(e => e.CreateTime < DateTime.Now && DateTime.Now < e.ExpireTime) + .Where(e => e.Participants.Any(d => d.Baid == baid && d.IsActive)) + .Where(e => e.Songs.Any(d => d.SongId == playData.SongId && d.SongOpt == option)) + .ToList(); + foreach (var challengeCompete in challengeCompetes) + { + ChallengeCompeteSongDatum? song = challengeCompete.Songs.Find(e => e.SongId == playData.SongId); + if (song == null || song.Difficulty != playData.Difficulty) continue; + + ChallengeCompeteBestDatum? bestScore = song.BestScores.Find(e => e.Baid == baid); + if (bestScore == null) + { + await context.AddAsync(new ChallengeCompeteBestDatum + { + CompId = song.CompId, + Baid = baid, + SongId = song.SongId, + Difficulty = song.Difficulty, + Crown = playData.Crown, + Score = playData.Score, + ScoreRate = playData.ScoreRate, + ScoreRank = playData.ScoreRank, + GoodCount = playData.GoodCount, + OkCount = playData.OkCount, + MissCount = playData.MissCount, + ComboCount = playData.ComboCount, + HitCount = playData.HitCount, + DrumrollCount = playData.DrumrollCount, + Skipped = playData.Skipped + }); + } + else if (bestScore.Score < playData.Score) + { + bestScore.Crown = playData.Crown; + bestScore.Score = playData.Score; + bestScore.ScoreRate = playData.ScoreRate; + bestScore.ScoreRank = playData.ScoreRank; + bestScore.GoodCount = playData.GoodCount; + bestScore.OkCount = playData.OkCount; + bestScore.MissCount = playData.MissCount; + bestScore.ComboCount = playData.ComboCount; + bestScore.HitCount = playData.HitCount; + bestScore.DrumrollCount = playData.DrumrollCount; + bestScore.Skipped = playData.Skipped; + context.Update(bestScore); + } + } + } } diff --git a/TaikoLocalServer/Services/Extentions/ServiceExtensions.cs b/TaikoLocalServer/Services/Extentions/ServiceExtensions.cs index 578fbca..f6f2813 100644 --- a/TaikoLocalServer/Services/Extentions/ServiceExtensions.cs +++ b/TaikoLocalServer/Services/Extentions/ServiceExtensions.cs @@ -9,6 +9,7 @@ public static class ServiceExtensions services.AddScoped(); services.AddScoped(); services.AddScoped(); + services.AddScoped(); return services; } diff --git a/TaikoLocalServer/Services/Interfaces/IChallengeCompeteService.cs b/TaikoLocalServer/Services/Interfaces/IChallengeCompeteService.cs index 7e7d866..92d4a82 100644 --- a/TaikoLocalServer/Services/Interfaces/IChallengeCompeteService.cs +++ b/TaikoLocalServer/Services/Interfaces/IChallengeCompeteService.cs @@ -15,4 +15,6 @@ public interface IChallengeCompeteService public Task CreateChallenge(uint baid, uint targetBaid, ChallengeCompeteInfo challengeCompeteInfo); public Task AnswerChallenge(uint compId, uint baid, bool accept); + + public Task UpdateBestScore(uint baid, SongPlayDatum playData, short option); }