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

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) if (Input.GetOptional<JArray>("remaster_id") is not null)
{ {
string id = Input.GetRequired<string>("id"); LegacyEntityHandler handler = new(this);
string name = Input.GetRequired<string>("name"); LegacyHandlers.Add(handler);
throw new LegacyItemException(handler.LegacyName);
ProcessLegacy(id, name);
throw new LegacyItemException(name);
} }
AddEntity(); AddEntity();
AddAlias();
AddSource(); AddSource();
AddExternal(); AddExternal();
AddTraits(); AddTraits();
ProcessLegacy();
} }
public override void Save() public override void Save()
@@ -49,6 +48,8 @@ internal abstract class AbstractEntityProcessor(AonprdProcessorInput input)
Context.SaveChanges(); Context.SaveChanges();
} }
// Description generation
protected virtual string[] AddedDescriptionPaths() => []; protected virtual string[] AddedDescriptionPaths() => [];
protected string AddedDescription(string path) => protected string AddedDescription(string path) =>
@@ -60,6 +61,8 @@ internal abstract class AbstractEntityProcessor(AonprdProcessorInput input)
protected virtual string GetDescription() => protected virtual string GetDescription() =>
Input.GetRequired<string>("markdown") + AddedDescription(AddedDescriptionPaths()); Input.GetRequired<string>("markdown") + AddedDescription(AddedDescriptionPaths());
// Entity generation
protected virtual void AddEntity() protected virtual void AddEntity()
{ {
Entity.Id = Helper.NextEntityId; Entity.Id = Helper.NextEntityId;
@@ -78,57 +81,12 @@ internal abstract class AbstractEntityProcessor(AonprdProcessorInput input)
Context.Entities.Add(Entity); 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() protected virtual void AddSource()
{ {
string name = Input.GetRequired<string>("source[0]"); foreach (EntitySource entitySource in GetSources())
Source? source = Context.Sources.FirstOrDefault(s => s.Name == name);
if (source == null)
{ {
source = new() Entity.Sources.Add(entitySource);
{
Name = name,
Category = Input.EnumRequired<SourceCategory>("primary_source_category"),
ReleaseDate = DateOnly.Parse(Input.GetRequired<string>("release_date")),
};
Context.Sources.Add(source);
} }
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() protected virtual void AddExternal()
@@ -179,20 +137,76 @@ internal abstract class AbstractEntityProcessor(AonprdProcessorInput input)
} }
} }
protected void ProcessLegacy(string id, string name) protected virtual void ProcessLegacy()
{ {
if (RemasterWithoutLegacy.TryGetValue(id, out Entity? remastered)) JArray? legacyIdArray = Input.GetOptional<JArray>("legacy_id");
{
// if (remastered.ToString() != name)
remastered.Name[LocaleType.LegacyEnglish] = name;
RemasterWithoutLegacy.Remove(id); if (legacyIdArray is null)
{
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 else
{ {
LegacyAliases.Add(id, name); 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) public string GenerateExtra(params string[] args)
{ {
@@ -214,9 +228,7 @@ internal abstract class AbstractEntityProcessor(AonprdProcessorInput input)
return isLowercase ? hash.ToLower() : hash; return isLowercase ? hash.ToLower() : hash;
} }
protected static readonly Dictionary<string, string> LegacyAliases = []; protected static readonly List<LegacyEntityHandler> LegacyHandlers = [];
protected static readonly Dictionary<string, Entity> RemasterWithoutLegacy = [];
} }
public class LegacyItemException(string name) public class LegacyItemException(string name)