diff --git a/Pf2eDataScrapper/Importers/Implementations/Aonprd/DataBuilder/LegacyEntityHandler.cs b/Pf2eDataScrapper/Importers/Implementations/Aonprd/DataBuilder/LegacyEntityHandler.cs new file mode 100644 index 0000000..e6f00d7 --- /dev/null +++ b/Pf2eDataScrapper/Importers/Implementations/Aonprd/DataBuilder/LegacyEntityHandler.cs @@ -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 LegacySources { get; private set; } = []; + + public bool LegacyPrepared { get; private set; } = false; + + public List RemasterEntities = []; + + public LegacyEntityHandler(AbstractEntityProcessor processor) + { + LegacyId = processor.Input.GetRequired("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("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); + } + } +} diff --git a/Pf2eDataScrapper/Importers/Implementations/Aonprd/DataBuilder/Processors/AbstractEntityProcessor.cs b/Pf2eDataScrapper/Importers/Implementations/Aonprd/DataBuilder/Processors/AbstractEntityProcessor.cs index e742685..828cd3a 100644 --- a/Pf2eDataScrapper/Importers/Implementations/Aonprd/DataBuilder/Processors/AbstractEntityProcessor.cs +++ b/Pf2eDataScrapper/Importers/Implementations/Aonprd/DataBuilder/Processors/AbstractEntityProcessor.cs @@ -29,18 +29,17 @@ internal abstract class AbstractEntityProcessor(AonprdProcessorInput input) { if (Input.GetOptional("remaster_id") is not null) { - string id = Input.GetRequired("id"); - string name = Input.GetRequired("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("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("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("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("primary_source_category"), - ReleaseDate = DateOnly.Parse(Input.GetRequired("release_date")), - }; - - Context.Sources.Add(source); + Entity.Sources.Add(entitySource); } - - EntitySource entitySource = new() - { - Entity = Entity, - Source = source, - Suffix = StringHelper.ResolveSource(Input.GetRequired("source_markdown")), - IsRemaster = Input.GetOptional("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("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 GetSources() + { + string[] sourceNames = Input + .GetRequired("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("id")}\") is not found" + ); + } + + List 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("source_markdown")), + IsRemaster = Input.GetOptional("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 LegacyAliases = []; - - protected static readonly Dictionary RemasterWithoutLegacy = []; + protected static readonly List LegacyHandlers = []; } public class LegacyItemException(string name)