Больше работы над парсингом заклинаний

This commit is contained in:
2025-05-06 01:23:34 +04:00
parent c1b70a12eb
commit 5693fa8ce8
13 changed files with 279 additions and 239 deletions
@@ -90,6 +90,8 @@ internal class AonprdDataBuilder(AonprdLoader loader)
continue;
}
SubProcessProgressBar.Refresh(0, "");
SubProcessProgressBar.Max = Compendium.Count([item.Attribute.Type]);
foreach (AonprdLoader.LoaderEntry entry in Loader.Entries(item.Attribute.Type))
@@ -9,7 +9,7 @@ using Pf2eModel.Model.Structure;
namespace Pf2eDataScrapper.Importers.Implementations.Aonprd.DataBuilder.Processors.Subclasses;
[AonprdProcessor("apparition", ["class", "spell", "trainings"])]
[AonprdProcessor("apparition", ["class", "spell", "skill"])]
internal class ApparitionProcessor(AonprdProcessorInput input) : AbstractSubclassProcessor(input)
{
public override Subclass Object { get; } = new();
@@ -1,4 +1,5 @@
using Pf2eModel.Model.Common.I18n;
using Newtonsoft.Json.Linq;
using Pf2eModel.Model.Common.I18n;
using Pf2eModel.Model.Enum;
using Pf2eModel.Model.Structure;
using Pf2eModel.Model.Structure.SpellRelation;
@@ -21,6 +22,15 @@ internal class SpellProcessor(AonprdProcessorInput input) : AbstractEntityProces
Object.Level = Input.GetRequired<int>("level");
Object.Actions = Input.EnumOptional("actions", ActionEnum.Unknown);
Object.IsFocus = Input.GetRequired<string>("spell_type") == "Focus";
Object.Duration = Input.GetOptional<int>("duration", -1);
Object.Range = Input.GetOptional<int>("range", -1);
Object.Trigger = Input.GetOptionalLocale("trigger_markdown", LocaleType.English);
Object.Target = Input.GetOptionalLocale("target_markdown", LocaleType.English);
Object.Traditions = (Input.GetOptional<JArray>("tradition") ?? [])
.Select(token => Input.Enum<SpellTradition>(token.ToString()))
.ToArray();
switch (Input.GetRequired<string>("spell_type"))
{
@@ -32,25 +42,78 @@ internal class SpellProcessor(AonprdProcessorInput input) : AbstractEntityProces
break;
}
foreach (string areaType in Input.GetOptional<string[]>("area_type") ?? [])
ProcessArea();
}
private void ProcessArea()
{
List<string> areaTypes = (Input.GetOptional<string[]>("area_type") ?? []).ToList();
switch (areaTypes.Count)
{
Object.SpellAreas.Add(
case 0:
return;
case 1:
A(
new()
{
Spell = Object,
Type = Input.Enum<SpellAreaType>(areaTypes[0]),
Details = Input.GetOptionalLocale("area_raw", LocaleType.English),
}
);
return;
}
List<string> areaDetails = (Input.GetOptional<string>("area_raw") ?? "")
.Replace(", or", ",")
.Replace(" or ", ", ")
.Split(", ")
.ToList();
if (areaTypes.Remove("square"))
{
A(new() { Spell = Object, Type = SpellAreaType.Square });
}
if (areaTypes.Count != areaDetails.Count)
{
throw new(
$"Area details count does not match area types count for spell {Entity.Name[LocaleType.English]}"
);
}
for (int i = 0; i < areaTypes.Count; i++)
{
A(
new()
{
Spell = Object,
Type = Input.EnumRequired<SpellAreaType>(areaType),
Value = 0,
Type = Input.Enum<SpellAreaType>(areaTypes[i]),
Details = new() { [LocaleType.English] = areaDetails[i] },
}
);
}
return;
void A(SpellArea spellArea)
{
Object.SpellAreas.Add(spellArea);
Context.SpellAreas.Add(spellArea);
}
}
protected override string GetDescription() =>
base.GetDescription()
+ AddedDescription(
[
"area_raw",
"range",
"range_raw",
"bloodline_markdown",
"cost_markdown",
"deity_markdown",
"domain_markdown",
"lesson_markdown",
@@ -59,9 +122,7 @@ internal class SpellProcessor(AonprdProcessorInput input) : AbstractEntityProces
"summary_markdown",
"tradition_markdown",
"patron_theme_markdown",
"trigger_markdown",
"saving_throw_markdown",
"target_markdown",
]
);
}
@@ -1,5 +1,6 @@
using Newtonsoft.Json.Linq;
using NpgsqlTypes;
using Pf2eModel.Model.Common.I18n;
namespace Pf2eDataScrapper.Importers;
@@ -24,6 +25,16 @@ internal class ProcessorInput(JObject input)
: token.ToObject<T>() ?? default;
}
public virtual T GetOptional<T>(string path, T defaultValue)
{
JToken? token = Input.SelectToken(path) ?? null;
if (token is null || token.Type == JTokenType.Null)
{
return defaultValue;
}
return token.ToObject<T>() ?? defaultValue;
}
public virtual T EnumRequired<T>(string path)
where T : Enum
{
@@ -54,6 +65,21 @@ internal class ProcessorInput(JObject input)
}
}
public virtual LocaleString GetRequiredLocale(string path, LocaleType type)
{
return new() { [type] = GetRequired<string>(path) };
}
public virtual LocaleString? GetOptionalLocale(string path, LocaleType type)
{
string? value = GetOptional<string>(path);
if (value is null)
{
return null;
}
return new() { [type] = value };
}
public virtual T Enum<T>(string value)
where T : Enum
{