1
0
mirror of synced 2024-11-23 22:41:01 +01:00

Fix saving the same new song by removing unnecessary updates

This commit is contained in:
asesidaa 2024-03-17 15:17:34 +08:00
parent 1514fd16fd
commit 3d8a59bc90
6 changed files with 39 additions and 42 deletions

View File

@ -1,4 +1,5 @@
using GameDatabase.Entities;
using EntityFramework.Exceptions.Sqlite;
using GameDatabase.Entities;
using Microsoft.EntityFrameworkCore;
using SharedProject.Utils;
@ -40,6 +41,7 @@ namespace GameDatabase.Context
path = dbFilePath;
}
optionsBuilder.UseSqlite($"Data Source={path}");
optionsBuilder.UseExceptionProcessor().EnableSensitiveDataLogging();
}
protected override void OnModelCreating(ModelBuilder modelBuilder)

View File

@ -12,7 +12,8 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.0-rc.2.23480.1" />
<PackageReference Include="EntityFrameworkCore.Exceptions.Sqlite" Version="8.1.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="8.0.0-rc.2.23480.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.0-rc.2.23480.1">
<PrivateAssets>all</PrivateAssets>

View File

@ -11,7 +11,7 @@
<ItemGroup>
<PackageReference Include="JorgeSerrano.Json.JsonSnakeCaseNamingPolicy" Version="0.9.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.0-rc.2.23480.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="8.0.0-rc.2.23480.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.0-rc.2.23480.1">
<PrivateAssets>all</PrivateAssets>

View File

@ -40,6 +40,7 @@ public class AddTokenCountCommandHandler : IRequestHandler<AddTokenCountCommand>
{
user.Tokens.Add(new Token
{
Baid = user.Baid,
Id = (int)tokenId,
Count = addTokenCount
});

View File

@ -18,7 +18,7 @@ public class UpdatePlayResultCommandHandler(TaikoDbContext context, ILogger<Upda
return 1;
}
var user = await context.UserData.FindAsync(request.Baid, cancellationToken);
var user = await context.UserData.FindAsync([request.Baid], cancellationToken);
if (user is null)
{
logger.LogWarning("Game uploading a non exisiting user with baid {Baid}", request.Baid);
@ -52,7 +52,6 @@ public class UpdatePlayResultCommandHandler(TaikoDbContext context, ILogger<Upda
else
{
UpdateDanPlayData(danPlayData, playResultData);
context.DanScoreData.Update(danPlayData);
}
await context.SaveChangesAsync(cancellationToken);
@ -68,7 +67,32 @@ public class UpdatePlayResultCommandHandler(TaikoDbContext context, ILogger<Upda
await UpdateAiBattleData(playResultData, stageData);
}
await UpdateBestData(playResultData, stageData);
var difficulty = (Difficulty)stageData.Level;
difficulty.Throw().IfOutOfRange();
var existing = await context.SongBestData.FindAsync([playResultData.Baid, stageData.SongNo, difficulty], cancellationToken);
// Determine whether it is dondaful crown as this is not reflected by play result
var crown = PlayResultToCrown(stageData.PlayResult, stageData.OkCnt);
if (existing is null)
{
var datum = new SongBestDatum
{
Baid = playResultData.Baid,
SongId = stageData.SongNo,
Difficulty = difficulty,
BestScore = stageData.PlayScore,
BestRate = stageData.ScoreRate,
BestCrown = crown,
BestScoreRank = (ScoreRank)stageData.ScoreRank
};
context.SongBestData.Add(datum);
}
else
{
existing.UpdateBestData(crown, stageData.ScoreRank, stageData.PlayScore, stageData.ScoreRate);
}
var songPlayDatum = new SongPlayDatum
{
@ -96,37 +120,6 @@ public class UpdatePlayResultCommandHandler(TaikoDbContext context, ILogger<Upda
return 1;
}
private async Task UpdateBestData(CommonPlayResultData playResultData, CommonPlayResultData.StageData stageData)
{
var difficulty = (Difficulty)stageData.Level;
difficulty.Throw().IfOutOfRange();
var existing = await context.SongBestData.FindAsync(playResultData.Baid, stageData.SongNo, difficulty);
// Determine whether it is dondaful crown as this is not reflected by play result
var crown = PlayResultToCrown(stageData.PlayResult, stageData.OkCnt);
if (existing is null)
{
existing = new SongBestDatum
{
Baid = playResultData.Baid,
SongId = stageData.SongNo,
Difficulty = difficulty,
BestScore = stageData.PlayScore,
BestRate = stageData.ScoreRate,
BestCrown = crown,
BestScoreRank = (ScoreRank)stageData.ScoreRank
};
context.SongBestData.Add(existing);
return;
}
existing.UpdateBestData(crown, stageData.ScoreRank, stageData.PlayScore, stageData.ScoreRate);
context.SongBestData.Update(existing);
}
private async Task UpdateAiBattleData(CommonPlayResultData playResultData, CommonPlayResultData.StageData stageData)
{
var difficulty = (Difficulty)stageData.Level;
@ -135,7 +128,10 @@ public class UpdatePlayResultCommandHandler(TaikoDbContext context, ILogger<Upda
.Include(datum => datum.AiSectionScoreData)
.FirstOrDefaultAsync(datum => datum.Baid == playResultData.Baid &&
datum.SongId == stageData.SongNo &&
datum.Difficulty == difficulty);
datum.Difficulty == difficulty)
?? context.AiScoreData.Local.FirstOrDefault(datum => datum.Baid == playResultData.Baid &&
datum.SongId == stageData.SongNo &&
datum.Difficulty == difficulty);
if (existing is null)
{
existing = new AiScoreDatum
@ -185,10 +181,8 @@ public class UpdatePlayResultCommandHandler(TaikoDbContext context, ILogger<Upda
};
aiSectionScoreDatum.UpdateBest(sectionData);
existing.AiSectionScoreData.Add(aiSectionScoreDatum);
context.AiScoreData.Update(existing);
}
}
}
private void UpdateDanPlayData(DanScoreDatum danPlayData, CommonPlayResultData playResultData)
@ -269,7 +263,6 @@ public class UpdatePlayResultCommandHandler(TaikoDbContext context, ILogger<Upda
user.DifficultyPlayedArray = JsonSerializer.Serialize(difficultyPlayedArray);
user.AiWinCount += playResultData.AryStageInfoes.Count(data => data.IsWin);
context.UserData.Update(user);
}
private static CrownType PlayResultToCrown(uint playResult, uint okCount)

View File

@ -17,7 +17,7 @@
<ItemGroup>
<PackageReference Include="MediatR" Version="12.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Server" Version="8.0.0-rc.1.23421.29" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.0-rc.2.23480.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="8.0.0-rc.2.23480.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.0-rc.2.23480.1">
<PrivateAssets>all</PrivateAssets>