Новый класс обработки легаси-сущностей

This commit is contained in:
2025-05-07 14:07:28 +04:00
parent 9708bf31e2
commit 7c2f2349c0
2 changed files with 148 additions and 65 deletions
@@ -0,0 +1,71 @@
using Pf2eDataScrapper.Importers.Implementations.Aonprd.DataBuilder.Helpers;
using Pf2eDataScrapper.Importers.Implementations.Aonprd.DataBuilder.Processors;
using Pf2eModel.Model.Base;
using Pf2eModel.Model.Base.Source;
using Pf2eModel.Model.Common.I18n;
namespace Pf2eDataScrapper.Importers.Implementations.Aonprd.DataBuilder;
internal class LegacyEntityHandler
{
public string LegacyId { get; private set; }
public string LegacyName { get; private set; } = "";
public List<EntitySource> LegacySources { get; private set; } = [];
public bool LegacyPrepared { get; private set; } = false;
public List<Entity> RemasterEntities = [];
public LegacyEntityHandler(AbstractEntityProcessor processor)
{
LegacyId = processor.Input.GetRequired<string>("id");
Prepare(processor);
}
public LegacyEntityHandler(string legacyId, Entity remasterEntity)
{
LegacyId = legacyId;
LegacyPrepared = false;
RemasterEntities.Add(remasterEntity);
}
public void Prepare(AbstractEntityProcessor processor)
{
if (LegacyPrepared)
{
return;
}
LegacyName = processor.Input.GetRequired<string>("name");
LegacySources = processor.GetSources();
LegacyPrepared = true;
foreach (Entity remasterEntity in RemasterEntities)
{
Apply(remasterEntity);
}
}
public void Apply(Entity remasterEntity)
{
if (!remasterEntity.Name.Contains(LegacyName))
{
remasterEntity.Name[LocaleType.LegacyEnglish] = LegacyName;
}
foreach (EntitySource legacySource in LegacySources)
{
EntitySource entitySource = new()
{
Entity = remasterEntity,
Source = legacySource.Source,
Suffix = legacySource.Suffix,
IsRemaster = false,
};
remasterEntity.Sources.Add(entitySource);
}
}
}
@@ -29,18 +29,17 @@ internal abstract class AbstractEntityProcessor(AonprdProcessorInput input)
{
if (Input.GetOptional<JArray>("remaster_id") is not null)
{
string id = Input.GetRequired<string>("id");
string name = Input.GetRequired<string>("name");
ProcessLegacy(id, name);
throw new LegacyItemException(name);
LegacyEntityHandler handler = new(this);
LegacyHandlers.Add(handler);
throw new LegacyItemException(handler.LegacyName);
}
AddEntity();
AddAlias();
AddSource();
AddExternal();
AddTraits();
ProcessLegacy();
}
public override void Save()
@@ -49,6 +48,8 @@ internal abstract class AbstractEntityProcessor(AonprdProcessorInput input)
Context.SaveChanges();
}
// Description generation
protected virtual string[] AddedDescriptionPaths() => [];
protected string AddedDescription(string path) =>
@@ -60,6 +61,8 @@ internal abstract class AbstractEntityProcessor(AonprdProcessorInput input)
protected virtual string GetDescription() =>
Input.GetRequired<string>("markdown") + AddedDescription(AddedDescriptionPaths());
// Entity generation
protected virtual void AddEntity()
{
Entity.Id = Helper.NextEntityId;
@@ -78,57 +81,12 @@ internal abstract class AbstractEntityProcessor(AonprdProcessorInput input)
Context.Entities.Add(Entity);
}
protected virtual void AddAlias()
{
JArray? legacyIdArray = Input.GetOptional<JArray>("legacy_id");
if (legacyIdArray is null)
{
return;
}
string legacyId = legacyIdArray[0].ToString();
if (LegacyAliases.TryGetValue(legacyId, out string? legacyName))
{
// if (legacyName != Entity.Name[LocaleType.English])
Entity.Name[LocaleType.LegacyEnglish] = legacyName;
LegacyAliases.Remove(legacyId);
}
else
{
RemasterWithoutLegacy.Add(legacyId, Entity);
}
}
protected virtual void AddSource()
{
string name = Input.GetRequired<string>("source[0]");
Source? source = Context.Sources.FirstOrDefault(s => s.Name == name);
if (source == null)
foreach (EntitySource entitySource in GetSources())
{
source = new()
{
Name = name,
Category = Input.EnumRequired<SourceCategory>("primary_source_category"),
ReleaseDate = DateOnly.Parse(Input.GetRequired<string>("release_date")),
};
Context.Sources.Add(source);
Entity.Sources.Add(entitySource);
}
EntitySource entitySource = new()
{
Entity = Entity,
Source = source,
Suffix = StringHelper.ResolveSource(Input.GetRequired<string>("source_markdown")),
IsRemaster = Input.GetOptional<JArray>("legacy_id") is not null,
};
Context.EntitySources.Add(entitySource);
}
protected virtual void AddExternal()
@@ -179,19 +137,75 @@ internal abstract class AbstractEntityProcessor(AonprdProcessorInput input)
}
}
protected void ProcessLegacy(string id, string name)
protected virtual void ProcessLegacy()
{
if (RemasterWithoutLegacy.TryGetValue(id, out Entity? remastered))
{
// if (remastered.ToString() != name)
remastered.Name[LocaleType.LegacyEnglish] = name;
JArray? legacyIdArray = Input.GetOptional<JArray>("legacy_id");
RemasterWithoutLegacy.Remove(id);
}
else
if (legacyIdArray is null)
{
LegacyAliases.Add(id, name);
return;
}
string[] legacyIds = legacyIdArray.ToArray().Select(t => t.ToString()).ToArray();
if (legacyIds.Length == 0)
{
return;
}
foreach (string legacyId in legacyIds)
{
LegacyEntityHandler? handler = LegacyHandlers.FirstOrDefault(h =>
h.LegacyId == legacyId
);
if (handler is not null)
{
handler.Apply(Entity);
}
else
{
handler = new(legacyId, Entity);
LegacyHandlers.Add(handler);
}
}
}
public List<EntitySource> GetSources()
{
string[] sourceNames = Input
.GetRequired<JArray>("source")
.Select(token => token.ToString())
.ToArray();
if (sourceNames.Length == 0)
{
throw new(
$"Source for entity with id \"{Entity.Name[LocaleType.English]}\" "
+ $"(Aonprd id \"{Input.GetRequired<string>("id")}\") is not found"
);
}
List<EntitySource> entitySources = new();
foreach (string sourceName in sourceNames)
{
Source source =
Context.Sources.FirstOrDefault(s => s.Name == sourceName)
?? throw new($"Source \"{sourceName}\" did not found");
EntitySource entitySource = new()
{
Entity = Entity,
Source = source,
Suffix = StringHelper.ResolveSource(Input.GetRequired<string>("source_markdown")),
IsRemaster = Input.GetOptional<JArray>("legacy_id") is not null,
};
entitySources.Add(entitySource);
}
return entitySources;
}
public string GenerateExtra(params string[] args)
@@ -214,9 +228,7 @@ internal abstract class AbstractEntityProcessor(AonprdProcessorInput input)
return isLowercase ? hash.ToLower() : hash;
}
protected static readonly Dictionary<string, string> LegacyAliases = [];
protected static readonly Dictionary<string, Entity> RemasterWithoutLegacy = [];
protected static readonly List<LegacyEntityHandler> LegacyHandlers = [];
}
public class LegacyItemException(string name)