Implement phase 6 critical effect normalization
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System.Text.Json;
|
||||
|
||||
using RolemasterDb.App.Data;
|
||||
using RolemasterDb.App.Domain;
|
||||
@@ -8,6 +9,11 @@ namespace RolemasterDb.ImportTool;
|
||||
|
||||
public sealed class CriticalImportLoader(string databasePath)
|
||||
{
|
||||
private static readonly JsonSerializerOptions JsonOptions = new()
|
||||
{
|
||||
WriteIndented = true
|
||||
};
|
||||
|
||||
public async Task<int> ResetCriticalsAsync(CancellationToken cancellationToken = default)
|
||||
{
|
||||
await using var dbContext = CreateDbContext();
|
||||
@@ -17,6 +23,7 @@ public sealed class CriticalImportLoader(string databasePath)
|
||||
var removedTableCount = await dbContext.CriticalTables.CountAsync(cancellationToken);
|
||||
await using var transaction = await dbContext.Database.BeginTransactionAsync(cancellationToken);
|
||||
|
||||
await dbContext.CriticalEffects.ExecuteDeleteAsync(cancellationToken);
|
||||
await dbContext.CriticalBranches.ExecuteDeleteAsync(cancellationToken);
|
||||
await dbContext.CriticalResults.ExecuteDeleteAsync(cancellationToken);
|
||||
await dbContext.CriticalGroups.ExecuteDeleteAsync(cancellationToken);
|
||||
@@ -88,8 +95,11 @@ public sealed class CriticalImportLoader(string databasePath)
|
||||
RawCellText = item.RawCellText,
|
||||
DescriptionText = item.DescriptionText,
|
||||
RawAffixText = item.RawAffixText,
|
||||
ParsedJson = "{}",
|
||||
ParseStatus = "raw",
|
||||
ParsedJson = SerializeParsedEffects(item.Effects),
|
||||
ParseStatus = ResolveParseStatus(item.Effects, item.Branches),
|
||||
Effects = item.Effects
|
||||
.Select(CreateEffectEntity)
|
||||
.ToList(),
|
||||
Branches = item.Branches
|
||||
.Select(branch => new CriticalBranch
|
||||
{
|
||||
@@ -100,8 +110,11 @@ public sealed class CriticalImportLoader(string databasePath)
|
||||
RawText = branch.RawText,
|
||||
DescriptionText = branch.DescriptionText,
|
||||
RawAffixText = branch.RawAffixText,
|
||||
ParsedJson = "{}",
|
||||
SortOrder = branch.SortOrder
|
||||
ParsedJson = SerializeParsedEffects(branch.Effects),
|
||||
SortOrder = branch.SortOrder,
|
||||
Effects = branch.Effects
|
||||
.Select(CreateEffectEntity)
|
||||
.ToList()
|
||||
})
|
||||
.ToList()
|
||||
})
|
||||
@@ -138,6 +151,14 @@ public sealed class CriticalImportLoader(string databasePath)
|
||||
return;
|
||||
}
|
||||
|
||||
await dbContext.CriticalEffects
|
||||
.Where(item => item.CriticalBranch != null && item.CriticalBranch.CriticalResult.CriticalTableId == tableId.Value)
|
||||
.ExecuteDeleteAsync(cancellationToken);
|
||||
|
||||
await dbContext.CriticalEffects
|
||||
.Where(item => item.CriticalResult != null && item.CriticalResult.CriticalTableId == tableId.Value)
|
||||
.ExecuteDeleteAsync(cancellationToken);
|
||||
|
||||
await dbContext.CriticalBranches
|
||||
.Where(item => item.CriticalResult.CriticalTableId == tableId.Value)
|
||||
.ExecuteDeleteAsync(cancellationToken);
|
||||
@@ -162,4 +183,32 @@ public sealed class CriticalImportLoader(string databasePath)
|
||||
.Where(item => item.Id == tableId.Value)
|
||||
.ExecuteDeleteAsync(cancellationToken);
|
||||
}
|
||||
|
||||
private static CriticalEffect CreateEffectEntity(ParsedCriticalEffect effect) =>
|
||||
new()
|
||||
{
|
||||
EffectCode = effect.EffectCode,
|
||||
Target = effect.Target,
|
||||
ValueInteger = effect.ValueInteger,
|
||||
ValueExpression = effect.ValueExpression,
|
||||
DurationRounds = effect.DurationRounds,
|
||||
PerRound = effect.PerRound,
|
||||
Modifier = effect.Modifier,
|
||||
BodyPart = effect.BodyPart,
|
||||
IsPermanent = effect.IsPermanent,
|
||||
SourceType = effect.SourceType,
|
||||
SourceText = effect.SourceText
|
||||
};
|
||||
|
||||
private static string SerializeParsedEffects(IReadOnlyList<ParsedCriticalEffect> effects) =>
|
||||
effects.Count == 0
|
||||
? "{}"
|
||||
: JsonSerializer.Serialize(new { effects }, JsonOptions);
|
||||
|
||||
private static string ResolveParseStatus(
|
||||
IReadOnlyList<ParsedCriticalEffect> effects,
|
||||
IReadOnlyList<ParsedCriticalBranch> branches) =>
|
||||
effects.Count > 0 || branches.Any(branch => branch.Effects.Count > 0)
|
||||
? "partial"
|
||||
: "raw";
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user