1
0
mirror of synced 2025-01-09 11:51:36 +01:00

ChallengeCompete SaveScore

This commit is contained in:
ptmaster 2024-09-11 09:52:16 +08:00
parent 583c95071c
commit e545598c1c
4 changed files with 63 additions and 1 deletions

View File

@ -5,7 +5,7 @@ namespace TaikoLocalServer.Handlers;
public record UpdatePlayResultCommand(uint Baid, CommonPlayResultData PlayResultData) : IRequest<uint>;
public class UpdatePlayResultCommandHandler(TaikoDbContext context, ILogger<UpdatePlayResultCommandHandler> logger)
public class UpdatePlayResultCommandHandler(TaikoDbContext context, IChallengeCompeteService challengeCompeteService, ILogger<UpdatePlayResultCommandHandler> logger)
: IRequestHandler<UpdatePlayResultCommand, uint>
{
public async Task<uint> Handle(UpdatePlayResultCommand request, CancellationToken cancellationToken)
@ -116,6 +116,11 @@ public class UpdatePlayResultCommandHandler(TaikoDbContext context, ILogger<Upda
Difficulty = (Difficulty)stageData.Level
};
context.SongPlayData.Add(songPlayDatum);
byte[] option = stageData.OptionFlg;
short optionVal = (short)(option[0] + (option[1]) << 8);
await challengeCompeteService.UpdateBestScore(songPlayDatum.Baid, songPlayDatum, optionVal);
}
await context.SaveChangesAsync(cancellationToken);

View File

@ -171,4 +171,58 @@ public class ChallengeCompeteService : IChallengeCompeteService
return true;
}
public async Task UpdateBestScore(uint baid, SongPlayDatum playData, short option)
{
List<ChallengeCompeteDatum> 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);
}
}
}
}

View File

@ -9,6 +9,7 @@ public static class ServiceExtensions
services.AddScoped<ISongPlayDatumService, SongPlayDatumService>();
services.AddScoped<ISongBestDatumService, SongBestDatumService>();
services.AddScoped<IDanScoreDatumService, DanScoreDatumService>();
services.AddScoped<IChallengeCompeteService, ChallengeCompeteService>();
return services;
}

View File

@ -15,4 +15,6 @@ public interface IChallengeCompeteService
public Task CreateChallenge(uint baid, uint targetBaid, ChallengeCompeteInfo challengeCompeteInfo);
public Task<bool> AnswerChallenge(uint compId, uint baid, bool accept);
public Task UpdateBestScore(uint baid, SongPlayDatum playData, short option);
}