From 7c2f2349c059291c7af36eb30222cadf1982a0e0 Mon Sep 17 00:00:00 2001 From: kozhilya Date: Wed, 7 May 2025 14:07:28 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9D=D0=BE=D0=B2=D1=8B=D0=B9=20=D0=BA=D0=BB?= =?UTF-8?q?=D0=B0=D1=81=D1=81=20=D0=BE=D0=B1=D1=80=D0=B0=D0=B1=D0=BE=D1=82?= =?UTF-8?q?=D0=BA=D0=B8=20=D0=BB=D0=B5=D0=B3=D0=B0=D1=81=D0=B8-=D1=81?= =?UTF-8?q?=D1=83=D1=89=D0=BD=D0=BE=D1=81=D1=82=D0=B5=D0=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Aonprd/DataBuilder/LegacyEntityHandler.cs | 71 +++++++++ .../Processors/AbstractEntityProcessor.cs | 142 ++++++++++-------- 2 files changed, 148 insertions(+), 65 deletions(-) create mode 100644 Pf2eDataScrapper/Importers/Implementations/Aonprd/DataBuilder/LegacyEntityHandler.cs 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)