Fix SongBestDatum tracking conflict issue
This commit is contained in:
parent
568cb5cd5a
commit
7351424e71
@ -77,8 +77,6 @@ public class PlayResultController : BaseController<PlayResultController>
|
||||
await UpdateDanPlayData(request, playResultData, danType);
|
||||
return Ok(response);
|
||||
}
|
||||
|
||||
var bestData = await songBestDatumService.GetAllSongBestData(request.BaidConf);
|
||||
|
||||
gameDataService.GetFolderDictionary().TryGetValue(9, out var folder9Data);
|
||||
|
||||
@ -109,7 +107,7 @@ public class PlayResultController : BaseController<PlayResultController>
|
||||
await UpdateAiBattleData(request, stageData);
|
||||
}
|
||||
|
||||
await UpdateBestData(request, stageData, bestData);
|
||||
await UpdateBestData(request, stageData);
|
||||
|
||||
await UpdatePlayData(request, songNumber, stageData, lastPlayDatetime);
|
||||
}
|
||||
@ -325,25 +323,35 @@ public class PlayResultController : BaseController<PlayResultController>
|
||||
return JsonSerializer.Serialize(flgData);
|
||||
}
|
||||
|
||||
private async Task UpdateBestData(PlayResultRequest request, StageData stageData,
|
||||
IEnumerable<SongBestDatum> bestData)
|
||||
private async Task UpdateBestData(PlayResultRequest request, StageData stageData)
|
||||
{
|
||||
var bestDatum = bestData
|
||||
.FirstOrDefault(datum => datum.SongId == stageData.SongNo &&
|
||||
datum.Difficulty == (Difficulty)stageData.Level,
|
||||
new SongBestDatum
|
||||
{
|
||||
Baid = request.BaidConf,
|
||||
SongId = stageData.SongNo,
|
||||
Difficulty = (Difficulty)stageData.Level
|
||||
});
|
||||
var difficulty = (Difficulty)stageData.Level;
|
||||
difficulty.Throw().IfOutOfRange();
|
||||
var existing = await songBestDatumService.GetSongBestData(request.BaidConf, stageData.SongNo, difficulty);
|
||||
|
||||
// Determine whether it is dondaful crown as this is not reflected by play result
|
||||
var crown = PlayResultToCrown(stageData.PlayResult, stageData.OkCnt);
|
||||
|
||||
bestDatum.UpdateBestData(crown, stageData.ScoreRank, stageData.PlayScore, stageData.ScoreRate);
|
||||
if (existing is null)
|
||||
{
|
||||
var songBestDatum = new SongBestDatum
|
||||
{
|
||||
Baid = request.BaidConf,
|
||||
SongId = stageData.SongNo,
|
||||
Difficulty = difficulty,
|
||||
BestScore = stageData.PlayScore,
|
||||
BestRate = stageData.ScoreRate,
|
||||
BestCrown = crown,
|
||||
BestScoreRank = (ScoreRank)stageData.ScoreRank
|
||||
};
|
||||
|
||||
await songBestDatumService.UpdateOrInsertSongBestDatum(bestDatum);
|
||||
await songBestDatumService.InsertSongBestData(songBestDatum);
|
||||
return;
|
||||
}
|
||||
|
||||
existing.UpdateBestData(crown, stageData.ScoreRank, stageData.PlayScore, stageData.ScoreRate);
|
||||
|
||||
await songBestDatumService.UpdateSongBestData(existing);
|
||||
}
|
||||
|
||||
private async Task UpdateAiBattleData(PlayResultRequest request, StageData stageData)
|
||||
|
@ -7,7 +7,11 @@ public interface ISongBestDatumService
|
||||
{
|
||||
public Task<List<SongBestDatum>> GetAllSongBestData(ulong baid);
|
||||
|
||||
public Task UpdateOrInsertSongBestDatum(SongBestDatum datum);
|
||||
public Task<SongBestDatum?> GetSongBestData(ulong baid, uint songId, Difficulty difficulty);
|
||||
|
||||
public Task UpdateSongBestData(SongBestDatum datum);
|
||||
|
||||
public Task InsertSongBestData(SongBestDatum datum);
|
||||
|
||||
public Task<List<SongBestData>> GetAllSongBestAsModel(ulong baid);
|
||||
}
|
@ -20,19 +20,30 @@ public class SongBestDatumService : ISongBestDatumService
|
||||
return await context.SongBestData.Where(datum => datum.Baid == baid).ToListAsync();
|
||||
}
|
||||
|
||||
public async Task UpdateOrInsertSongBestDatum(SongBestDatum datum)
|
||||
public async Task<SongBestDatum?> GetSongBestData(ulong baid, uint songId, Difficulty difficulty)
|
||||
{
|
||||
var existing = await context.SongBestData.AnyAsync(
|
||||
bestDatum => bestDatum.Baid == datum.Baid &&
|
||||
bestDatum.Difficulty == datum.Difficulty &&
|
||||
bestDatum.SongId == datum.SongId);
|
||||
if (existing)
|
||||
{
|
||||
context.SongBestData.Update(datum);
|
||||
await context.SaveChangesAsync();
|
||||
return;
|
||||
}
|
||||
return await context.SongBestData.Where(datum => datum.Baid == baid &&
|
||||
datum.SongId == songId &&
|
||||
datum.Difficulty == difficulty)
|
||||
.FirstOrDefaultAsync();
|
||||
}
|
||||
|
||||
public async Task UpdateSongBestData(SongBestDatum datum)
|
||||
{
|
||||
var existing = await context.SongBestData.FindAsync(datum.Baid, datum.SongId, datum.Difficulty);
|
||||
existing.ThrowIfNull("Cannot update a non-existing best data!");
|
||||
|
||||
context.SongBestData.Update(datum);
|
||||
await context.SaveChangesAsync();
|
||||
}
|
||||
|
||||
public async Task InsertSongBestData(SongBestDatum datum)
|
||||
{
|
||||
var existing = await context.SongBestData.FindAsync(datum.Baid, datum.SongId, datum.Difficulty);
|
||||
if (existing is not null)
|
||||
{
|
||||
throw new ArgumentException("Best data already exists!", nameof(datum));
|
||||
}
|
||||
context.SongBestData.Add(datum);
|
||||
await context.SaveChangesAsync();
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user