diff --git a/Pf2eDataScrapper/Importers/Implementations/Pf2eFoundry/DataBuilder/PF2eFoundryDataBuilder.cs b/Pf2eDataScrapper/Importers/Implementations/Pf2eFoundry/DataBuilder/PF2eFoundryDataBuilder.cs index d9cc945..4306edb 100644 --- a/Pf2eDataScrapper/Importers/Implementations/Pf2eFoundry/DataBuilder/PF2eFoundryDataBuilder.cs +++ b/Pf2eDataScrapper/Importers/Implementations/Pf2eFoundry/DataBuilder/PF2eFoundryDataBuilder.cs @@ -29,13 +29,14 @@ namespace Pf2eDataScrapper.Importers.Implementations.Pf2eFoundry.DataBuilder public void PrepareProcessors() { - foreach (Type type in Assembly.GetExecutingAssembly().GetTypes()) + foreach (var type in Assembly.GetExecutingAssembly().GetTypes()) { - if (type.GetCustomAttributes(typeof(Pf2eTypeProcessor), true).Length > 0) + if (type.GetCustomAttributes(typeof(Pf2eTypeProcessor), true).Length <= 0) { - var attribute = type.GetCustomAttribute(); - Processors.Add(attribute!.Type, type.GetConstructor(new[] { typeof(JObject) })!); + continue; } + var attribute = type.GetCustomAttribute(); + Processors.Add(attribute!.Type, type.GetConstructor(new[] { typeof(JObject) })!); } } @@ -53,7 +54,7 @@ namespace Pf2eDataScrapper.Importers.Implementations.Pf2eFoundry.DataBuilder { Console.WriteLine("Truncating all old data..."); - Database.ExecuteCommand($"SELECT f_truncate_tables();"); + Database.ExecuteCommand("SELECT f_truncate_tables();"); Console.WriteLine("Truncation completed!"); } diff --git a/Pf2eDataScrapper/Importers/Implementations/Pf2eFoundry/DataBuilder/Processors/ConditionProcessor.cs b/Pf2eDataScrapper/Importers/Implementations/Pf2eFoundry/DataBuilder/Processors/ConditionProcessor.cs index f027bb8..14940da 100644 --- a/Pf2eDataScrapper/Importers/Implementations/Pf2eFoundry/DataBuilder/Processors/ConditionProcessor.cs +++ b/Pf2eDataScrapper/Importers/Implementations/Pf2eFoundry/DataBuilder/Processors/ConditionProcessor.cs @@ -1,5 +1,6 @@ using Newtonsoft.Json.Linq; using Pf2eDataScrapper.Model; +using Pf2eDataScrapper.Model.Enum; using Slugify; using static LinqToDB.Common.Configuration; @@ -24,7 +25,7 @@ namespace Pf2eDataScrapper.Importers.Implementations.Pf2eFoundry.DataBuilder.Pro Img = GetRequired("img"), Description = GetRequired("system.description.value"), DescriptionGm = GetOptional("system.description.gm"), - // Type = + Type = EntityType.Condition, IsTranslated = false, IsChecked = false, diff --git a/Pf2eDataScrapper/Model/Action.cs b/Pf2eDataScrapper/Model/Action.cs index 757099f..b7daa2e 100644 --- a/Pf2eDataScrapper/Model/Action.cs +++ b/Pf2eDataScrapper/Model/Action.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using Pf2eDataScrapper.Model.Enum; namespace Pf2eDataScrapper.Model; @@ -11,6 +12,8 @@ public partial class Action public string Img { get; set; } = null!; + public EntityType Type { get; set; } = EntityType.Action; + public string Description { get; set; } = null!; public string? DescriptionGm { get; set; } diff --git a/Pf2eDataScrapper/Model/Ancestry.cs b/Pf2eDataScrapper/Model/Ancestry.cs index b4d7f25..7af5ce0 100644 --- a/Pf2eDataScrapper/Model/Ancestry.cs +++ b/Pf2eDataScrapper/Model/Ancestry.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using Pf2eDataScrapper.Model.Enum; namespace Pf2eDataScrapper.Model; @@ -11,6 +12,8 @@ public partial class Ancestry public string Img { get; set; } = null!; + public EntityType Type { get; set; } = EntityType.Ancestry; + public string Description { get; set; } = null!; public string? DescriptionGm { get; set; } diff --git a/Pf2eDataScrapper/Model/Background.cs b/Pf2eDataScrapper/Model/Background.cs index f44cda8..25e5c0b 100644 --- a/Pf2eDataScrapper/Model/Background.cs +++ b/Pf2eDataScrapper/Model/Background.cs @@ -1,4 +1,5 @@ -using System; +using Pf2eDataScrapper.Model.Enum; +using System; using System.Collections.Generic; namespace Pf2eDataScrapper.Model; @@ -11,6 +12,8 @@ public partial class Background public string Img { get; set; } = null!; + public EntityType Type { get; set; } = EntityType.Background; + public string Description { get; set; } = null!; public string? DescriptionGm { get; set; } diff --git a/Pf2eDataScrapper/Model/CampaignFeature.cs b/Pf2eDataScrapper/Model/CampaignFeature.cs index 7711592..916cecf 100644 --- a/Pf2eDataScrapper/Model/CampaignFeature.cs +++ b/Pf2eDataScrapper/Model/CampaignFeature.cs @@ -1,4 +1,5 @@ -using System; +using Pf2eDataScrapper.Model.Enum; +using System; using System.Collections.Generic; namespace Pf2eDataScrapper.Model; @@ -11,6 +12,8 @@ public partial class CampaignFeature public string Img { get; set; } = null!; + public EntityType Type { get; set; } = EntityType.CampaignFeature; + public string Description { get; set; } = null!; public string? DescriptionGm { get; set; } diff --git a/Pf2eDataScrapper/Model/Class.cs b/Pf2eDataScrapper/Model/Class.cs index f3ad051..b008c7a 100644 --- a/Pf2eDataScrapper/Model/Class.cs +++ b/Pf2eDataScrapper/Model/Class.cs @@ -1,4 +1,5 @@ -using System; +using Pf2eDataScrapper.Model.Enum; +using System; using System.Collections.Generic; namespace Pf2eDataScrapper.Model; @@ -11,6 +12,8 @@ public partial class Class public string Img { get; set; } = null!; + public EntityType Type { get; set; } = EntityType.Class; + public string Description { get; set; } = null!; public string? DescriptionGm { get; set; } diff --git a/Pf2eDataScrapper/Model/Condition.cs b/Pf2eDataScrapper/Model/Condition.cs index 4f1fb73..e20a952 100644 --- a/Pf2eDataScrapper/Model/Condition.cs +++ b/Pf2eDataScrapper/Model/Condition.cs @@ -1,16 +1,19 @@ -using System; +using Pf2eDataScrapper.Model.Enum; +using System; using System.Collections.Generic; namespace Pf2eDataScrapper.Model; public partial class Condition { - public int Id { get; set; } + public int Id { get; set; } = -1; public string Name { get; set; } = null!; public string Img { get; set; } = null!; + public EntityType Type { get; set; } = EntityType.Condition; + public string Description { get; set; } = null!; public string? DescriptionGm { get; set; } @@ -21,5 +24,7 @@ public partial class Condition public bool? IsChecked { get; set; } + public ConditionGroup Group { get; set; } + public string? Extra { get; set; } } diff --git a/Pf2eDataScrapper/Model/Deity.cs b/Pf2eDataScrapper/Model/Deity.cs index fafb915..92f2451 100644 --- a/Pf2eDataScrapper/Model/Deity.cs +++ b/Pf2eDataScrapper/Model/Deity.cs @@ -1,4 +1,5 @@ -using System; +using Pf2eDataScrapper.Model.Enum; +using System; using System.Collections.Generic; namespace Pf2eDataScrapper.Model; @@ -11,6 +12,8 @@ public partial class Deity public string Img { get; set; } = null!; + public EntityType Type { get; set; } = EntityType.Deity; + public string Description { get; set; } = null!; public string? DescriptionGm { get; set; } diff --git a/Pf2eDataScrapper/Model/Entity.cs b/Pf2eDataScrapper/Model/Entity.cs index b29c06a..96293e8 100644 --- a/Pf2eDataScrapper/Model/Entity.cs +++ b/Pf2eDataScrapper/Model/Entity.cs @@ -5,7 +5,7 @@ namespace Pf2eDataScrapper.Model; public partial class Entity { - public int Id { get; set; } + public int Id { get; set; } = -1; public string Name { get; set; } = null!; diff --git a/Pf2eDataScrapper/Model/Enum/ConditionGroup.cs b/Pf2eDataScrapper/Model/Enum/ConditionGroup.cs new file mode 100644 index 0000000..cc710cc --- /dev/null +++ b/Pf2eDataScrapper/Model/Enum/ConditionGroup.cs @@ -0,0 +1,22 @@ +using NpgsqlTypes; + +namespace Pf2eDataScrapper.Model.Enum; + + +public enum ConditionGroup +{ + [PgName("senses")] + Senses, + + [PgName("abilities")] + Abilities, + + [PgName("death")] + Death, + + [PgName("attitudes")] + Attitudes, + + [PgName("detection")] + Detection +} \ No newline at end of file diff --git a/Pf2eDataScrapper/Model/Enum/EntityType.cs b/Pf2eDataScrapper/Model/Enum/EntityType.cs new file mode 100644 index 0000000..6eea1e9 --- /dev/null +++ b/Pf2eDataScrapper/Model/Enum/EntityType.cs @@ -0,0 +1,42 @@ +using NpgsqlTypes; + +namespace Pf2eDataScrapper.Model.Enum; + +public enum EntityType +{ + [PgName("unknown")] + Unknown, + + [PgName("action")] + Action, + + [PgName("ancestry")] + Ancestry, + + [PgName("background")] + Background, + + [PgName("backpack")] + Backpack, + + [PgName("campaign_feature")] + CampaignFeature, + + [PgName("class")] + Class, + + [PgName("condition")] + Condition, + + [PgName("deity")] + Deity, + + [PgName("feat")] + Feat, + + [PgName("heritage")] + Heritage, + + [PgName("spell")] + Spell +} \ No newline at end of file diff --git a/Pf2eDataScrapper/Model/ExternalTarget.cs b/Pf2eDataScrapper/Model/ExternalTarget.cs index 99f8627..efe8150 100644 --- a/Pf2eDataScrapper/Model/ExternalTarget.cs +++ b/Pf2eDataScrapper/Model/ExternalTarget.cs @@ -2,7 +2,7 @@ using System; using System.Collections.Generic; -namespace Pf2eDataScrapper; +namespace Pf2eDataScrapper.Model; public partial class ExternalTarget { diff --git a/Pf2eDataScrapper/Model/Feat.cs b/Pf2eDataScrapper/Model/Feat.cs index 899f0a9..478916e 100644 --- a/Pf2eDataScrapper/Model/Feat.cs +++ b/Pf2eDataScrapper/Model/Feat.cs @@ -1,7 +1,8 @@ -using System; +using Pf2eDataScrapper.Model.Enum; +using System; using System.Collections.Generic; -namespace Pf2eDataScrapper; +namespace Pf2eDataScrapper.Model; public partial class Feat { @@ -11,6 +12,8 @@ public partial class Feat public string Img { get; set; } = null!; + public EntityType Type { get; set; } = EntityType.Feat; + public string Description { get; set; } = null!; public string? DescriptionGm { get; set; } diff --git a/Pf2eDataScrapper/Model/FeatPrerequisite.cs b/Pf2eDataScrapper/Model/FeatPrerequisite.cs index 0dc9ecb..9efe9b5 100644 --- a/Pf2eDataScrapper/Model/FeatPrerequisite.cs +++ b/Pf2eDataScrapper/Model/FeatPrerequisite.cs @@ -1,7 +1,7 @@ using System; using System.Collections.Generic; -namespace Pf2eDataScrapper; +namespace Pf2eDataScrapper.Model; public partial class FeatPrerequisite { diff --git a/Pf2eDataScrapper/Model/Frequency.cs b/Pf2eDataScrapper/Model/Frequency.cs index 8199a40..23ee80b 100644 --- a/Pf2eDataScrapper/Model/Frequency.cs +++ b/Pf2eDataScrapper/Model/Frequency.cs @@ -2,7 +2,7 @@ using System; using System.Collections.Generic; -namespace Pf2eDataScrapper; +namespace Pf2eDataScrapper.Model; public partial class Frequency { diff --git a/Pf2eDataScrapper/Model/Heritage.cs b/Pf2eDataScrapper/Model/Heritage.cs index e902136..0901c02 100644 --- a/Pf2eDataScrapper/Model/Heritage.cs +++ b/Pf2eDataScrapper/Model/Heritage.cs @@ -1,8 +1,9 @@ using Pf2eDataScrapper.Model; using System; using System.Collections.Generic; +using Pf2eDataScrapper.Model.Enum; -namespace Pf2eDataScrapper; +namespace Pf2eDataScrapper.Model; public partial class Heritage { @@ -12,6 +13,8 @@ public partial class Heritage public string Img { get; set; } = null!; + public EntityType Type { get; set; } = EntityType.Heritage; + public string Description { get; set; } = null!; public string? DescriptionGm { get; set; } diff --git a/Pf2eDataScrapper/Model/Keyword.cs b/Pf2eDataScrapper/Model/Keyword.cs index 04500f1..6eec91d 100644 --- a/Pf2eDataScrapper/Model/Keyword.cs +++ b/Pf2eDataScrapper/Model/Keyword.cs @@ -1,7 +1,7 @@ using System; using System.Collections.Generic; -namespace Pf2eDataScrapper; +namespace Pf2eDataScrapper.Model; public partial class Keyword { diff --git a/Pf2eDataScrapper/Model/KeywordAlias.cs b/Pf2eDataScrapper/Model/KeywordAlias.cs index 141c4a1..4e8b920 100644 --- a/Pf2eDataScrapper/Model/KeywordAlias.cs +++ b/Pf2eDataScrapper/Model/KeywordAlias.cs @@ -1,7 +1,7 @@ using System; using System.Collections.Generic; -namespace Pf2eDataScrapper; +namespace Pf2eDataScrapper.Model; public partial class KeywordAlias { diff --git a/Pf2eDataScrapper/Model/Language.cs b/Pf2eDataScrapper/Model/Language.cs index 3d331cb..8eb5304 100644 --- a/Pf2eDataScrapper/Model/Language.cs +++ b/Pf2eDataScrapper/Model/Language.cs @@ -2,7 +2,7 @@ using System; using System.Collections.Generic; -namespace Pf2eDataScrapper; +namespace Pf2eDataScrapper.Model; public partial class Language { diff --git a/Pf2eDataScrapper/Model/Spell.cs b/Pf2eDataScrapper/Model/Spell.cs index 9867327..6b7fd4b 100644 --- a/Pf2eDataScrapper/Model/Spell.cs +++ b/Pf2eDataScrapper/Model/Spell.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using Pf2eDataScrapper.Model.Enum; namespace Pf2eDataScrapper.Model; @@ -11,6 +12,8 @@ public partial class Spell public string Img { get; set; } = null!; + public EntityType Type { get; set; } = EntityType.Spell; + public string Description { get; set; } = null!; public string? DescriptionGm { get; set; } diff --git a/Pf2eDataScrapper/Pf2eDatabase.cs b/Pf2eDataScrapper/Pf2eDatabase.cs index d0d3d03..b2187a2 100644 --- a/Pf2eDataScrapper/Pf2eDatabase.cs +++ b/Pf2eDataScrapper/Pf2eDatabase.cs @@ -62,6 +62,8 @@ namespace Pf2eDataScrapper } + + /* public void RebuildDatabase() { diff --git a/Pf2eDataScrapper/Pf2eTestContext.cs b/Pf2eDataScrapper/Pf2eTestContext.cs index c92a146..be285ce 100644 --- a/Pf2eDataScrapper/Pf2eTestContext.cs +++ b/Pf2eDataScrapper/Pf2eTestContext.cs @@ -1,7 +1,12 @@ using System; using System.Collections.Generic; +using System.ComponentModel; +using System.Reflection; +using LinqToDB.SqlQuery; using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; using Pf2eDataScrapper.Model; +using Pf2eDataScrapper.Model.Enum; namespace Pf2eDataScrapper; @@ -98,10 +103,74 @@ public partial class Pf2eTestContext : DbContext public virtual DbSet Traits { get; set; } - protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) => optionsBuilder.UseNpgsql(EnvSettings.DatabaseConnection); + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { + optionsBuilder.UseNpgsql(EnvSettings.DatabaseConnection, + builder => + { + builder + .MapEnum("entity_type") + .MapEnum("condition_group"); + } + ); + } + + protected string FromEnum(T enumValue) where T : Enum + { + var enumFieldName = enumValue.ToString(); + var field = typeof(T).GetField(enumFieldName); + if (field is null) + { + return enumFieldName; + } + + var attribute = field.GetCustomAttribute(); + return attribute is null ? enumFieldName : attribute.Description; + } + + protected T ToEnum(string enumValue) where T : Enum + { + var enumType = typeof(T); + foreach (var field in enumType.GetFields()) + { + var attribute = field.GetCustomAttribute(); + if (attribute is null) + { + if (field.Name == enumValue) + { + return (T)field.GetValue(null)!; + } + } + else + { + if (attribute.Description == enumValue) + { + return (T)field.GetValue(null)!; + } + } + } + throw new ArgumentException($"No matching value found for {enumValue} in {enumType.Name}"); + } + + protected ValueConverter GetEnumConverter() where T : Enum + { + return new ValueConverter( + v => FromEnum(v), + v => ToEnum(v)); + } protected override void OnModelCreating(ModelBuilder modelBuilder) { + modelBuilder.UseIdentityByDefaultColumns(); + + modelBuilder.HasSequence("entity_sequence"); + + modelBuilder + .HasPostgresEnum("entity_type") + .HasPostgresEnum("condition_group") + ; + + + modelBuilder .HasPostgresEnum("action_category", new[] { "offensive", "defensive", "interaction", "familiar" }) .HasPostgresEnum("action_type", new[] { "action", "free", "reaction", "passive" }) @@ -109,12 +178,12 @@ public partial class Pf2eTestContext : DbContext .HasPostgresEnum("ancestry_vision", new[] { "normal", "low_light_vision", "darkvision" }) .HasPostgresEnum("campaign_feature_category", new[] { "army_tactic", "army_war_action", "kingdom_activity", "kingdom_feat", "kingdom_feature" }) .HasPostgresEnum("class_progression_type", new[] { "entity", "ancestry_feat", "class_feat", "general_feat", "skill_feat", "skill_increase" }) - .HasPostgresEnum("condition_group", new[] { "senses", "abilities", "death", "attitudes", "detection" }) + // .HasPostgresEnum("condition_group", new[] { "senses", "abilities", "death", "attitudes", "detection" }) .HasPostgresEnum("deity_category", new[] { "deity", "pantheon", "covenant", "philosophy" }) .HasPostgresEnum("deity_fonts", new[] { "none", "heal", "harm", "any" }) .HasPostgresEnum("deity_sanctification", new[] { "holy", "unholy", "any" }) .HasPostgresEnum("entity_rarity", new[] { "common", "uncommon", "rare", "unique" }) - .HasPostgresEnum("entity_type", new[] { "action", "ancestry", "background", "backpack", "campaign_feature", "class", "condition", "deity", "feat", "heritage", "spell" }) + // .HasPostgresEnum("entity_type", new[] { "action", "ancestry", "background", "backpack", "campaign_feature", "class", "condition", "deity", "feat", "heritage", "spell" }) .HasPostgresEnum("enum_size", new[] { "med", "lanuages", "system", "tiny" }) .HasPostgresEnum("feat_category", new[] { "ancestryfeature", "classfeature", "calling", "class", "skill", "general", "ancestry", "bonus", "deityboon", "curse", "pfsboon" }) .HasPostgresEnum("spell_area_type", new[] { "burst", "emanation", "cone", "line", "square", "cube", "cylinder" }) @@ -131,6 +200,9 @@ public partial class Pf2eTestContext : DbContext entity.Property(e => e.Id) .ValueGeneratedNever() .HasColumnName("id"); + entity.Property(e => e.Type) + .HasConversion(GetEnumConverter()) + .HasColumnName("type"); entity.Property(e => e.Actions).HasColumnName("actions"); entity.Property(e => e.DeathNote).HasColumnName("death_note"); entity.Property(e => e.Description).HasColumnName("description"); @@ -162,6 +234,9 @@ public partial class Pf2eTestContext : DbContext entity.Property(e => e.Id) .ValueGeneratedNever() .HasColumnName("id"); + entity.Property(e => e.Type) + .HasConversion(GetEnumConverter()) + .HasColumnName("type"); entity.Property(e => e.Description).HasColumnName("description"); entity.Property(e => e.DescriptionGm).HasColumnName("description_gm"); entity.Property(e => e.Hp).HasColumnName("hp"); @@ -256,6 +331,9 @@ public partial class Pf2eTestContext : DbContext entity.Property(e => e.Id) .ValueGeneratedNever() .HasColumnName("id"); + entity.Property(e => e.Type) + .HasConversion(GetEnumConverter()) + .HasColumnName("type"); entity.Property(e => e.Description).HasColumnName("description"); entity.Property(e => e.DescriptionGm).HasColumnName("description_gm"); entity.Property(e => e.Img) @@ -376,6 +454,9 @@ public partial class Pf2eTestContext : DbContext entity.Property(e => e.Id) .ValueGeneratedNever() .HasColumnName("id"); + entity.Property(e => e.Type) + .HasConversion(GetEnumConverter()) + .HasColumnName("type"); entity.Property(e => e.AdditionalTrainedSkills).HasColumnName("additionalTrainedSkills"); entity.Property(e => e.Description).HasColumnName("description"); entity.Property(e => e.DescriptionGm).HasColumnName("description_gm"); @@ -469,8 +550,13 @@ public partial class Pf2eTestContext : DbContext entity.ToTable("condition"); entity.Property(e => e.Id) - .ValueGeneratedNever() + //.ValueGeneratedNever() + .HasDefaultValueSql("nextval('entity_sequence')") .HasColumnName("id"); + entity.Property(e => e.Type) + //.HasConversion(GetEnumConverter()) + //.HasConversion() + .HasColumnName("type"); entity.Property(e => e.Description).HasColumnName("description"); entity.Property(e => e.DescriptionGm).HasColumnName("description_gm"); entity.Property(e => e.Extra).HasColumnName("extra"); @@ -489,6 +575,10 @@ public partial class Pf2eTestContext : DbContext entity.Property(e => e.Slug) .HasMaxLength(150) .HasColumnName("slug"); + entity.Property(e => e.Group) + //.HasConversion(GetEnumConverter()) + //.HasConversion() + .HasColumnName("group"); }); modelBuilder.Entity(entity => @@ -500,6 +590,9 @@ public partial class Pf2eTestContext : DbContext entity.Property(e => e.Id) .ValueGeneratedNever() .HasColumnName("id"); + entity.Property(e => e.Type) + .HasConversion(GetEnumConverter()) + .HasColumnName("type"); entity.Property(e => e.Description).HasColumnName("description"); entity.Property(e => e.DescriptionGm).HasColumnName("description_gm"); entity.Property(e => e.Img) @@ -777,6 +870,9 @@ public partial class Pf2eTestContext : DbContext entity.Property(e => e.Id) .ValueGeneratedNever() .HasColumnName("id"); + entity.Property(e => e.Type) + .HasConversion(GetEnumConverter()) + .HasColumnName("type"); entity.Property(e => e.Actions).HasColumnName("actions"); entity.Property(e => e.Description).HasColumnName("description"); entity.Property(e => e.DescriptionGm).HasColumnName("description_gm"); @@ -851,6 +947,9 @@ public partial class Pf2eTestContext : DbContext entity.Property(e => e.Id) .ValueGeneratedNever() .HasColumnName("id"); + entity.Property(e => e.Type) + .HasConversion(GetEnumConverter()) + .HasColumnName("type"); entity.Property(e => e.AncestryId).HasColumnName("ancestry_id"); entity.Property(e => e.Description).HasColumnName("description"); entity.Property(e => e.DescriptionGm).HasColumnName("description_gm"); @@ -940,6 +1039,9 @@ public partial class Pf2eTestContext : DbContext entity.Property(e => e.Id) .ValueGeneratedNever() .HasColumnName("id"); + entity.Property(e => e.Type) + .HasConversion(GetEnumConverter()) + .HasColumnName("type"); entity.Property(e => e.CanHeightening).HasColumnName("can_heightening"); entity.Property(e => e.Cost).HasColumnName("cost"); entity.Property(e => e.Counteraction).HasColumnName("counteraction"); diff --git a/Pf2eDataScrapper/Program.cs b/Pf2eDataScrapper/Program.cs index 3fa9c8a..3ca6c6a 100644 --- a/Pf2eDataScrapper/Program.cs +++ b/Pf2eDataScrapper/Program.cs @@ -8,9 +8,10 @@ var importer = new Pf2EFoundryImporter(); importer.Prepare(); +importer.Import(); +/* try { - importer.Import(); } catch (Exception ex) { @@ -22,3 +23,4 @@ catch (Exception ex) // importer.BuildSchemas(); +*/ \ No newline at end of file diff --git a/Pf2eDataScrapper/SchemaBuilder/SchemaNode.cs b/Pf2eDataScrapper/SchemaBuilder/SchemaNode.cs index f69460a..78a59f8 100644 --- a/Pf2eDataScrapper/SchemaBuilder/SchemaNode.cs +++ b/Pf2eDataScrapper/SchemaBuilder/SchemaNode.cs @@ -204,7 +204,7 @@ namespace Pf2eDataScrapper.SchemaBuilder /// /// Словарь данных. /// Строка форматированных данных - public static string FormatYaml(params List> input) + public static string FormatYaml(params Dictionary[] input) { var result = ""; @@ -218,7 +218,7 @@ namespace Pf2eDataScrapper.SchemaBuilder return result.Trim(); } - public static Dictionary MergeDictionaries(params List> input) + public static Dictionary MergeDictionaries(params Dictionary[] input) { var result = new Dictionary(); diff --git a/Pf2eDataScrapper/schema.pgsql b/Pf2eDataScrapper/schema.pgsql index fb0f38f..a2e0ec1 100644 --- a/Pf2eDataScrapper/schema.pgsql +++ b/Pf2eDataScrapper/schema.pgsql @@ -1,4 +1,6 @@ -- Common Enums + +DROP TYPE IF EXISTS "enum_size"; CREATE TYPE "enum_size" AS ENUM ( 'med', 'lanuages', @@ -9,6 +11,9 @@ CREATE TYPE "enum_size" AS ENUM ( -- Entity +CREATE SEQUENCE "entity_sequence"; + +DROP TYPE IF EXISTS "entity_type"; CREATE TYPE "entity_type" AS ENUM ( 'action', 'ancestry', @@ -23,6 +28,7 @@ CREATE TYPE "entity_type" AS ENUM ( 'spell' ); +DROP TYPE IF EXISTS "entity_rarity"; CREATE TYPE "entity_rarity" AS ENUM ( 'common', 'uncommon', @@ -31,7 +37,7 @@ CREATE TYPE "entity_rarity" AS ENUM ( ); CREATE TABLE "entity" ( - "id" int GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, + "id" int DEFAULT nextval('entity_sequence') PRIMARY KEY, "name" varchar(150) NOT NULL, "img" varchar(150) NOT NULL, "description" text NOT NULL, @@ -47,6 +53,7 @@ CREATE TABLE "entity" ( -- Alias +DROP TYPE IF EXISTS "alias_type"; CREATE TYPE "alias_type" AS ENUM ( 'old', 'english', @@ -126,6 +133,7 @@ ALTER TABLE "entity_external" ADD FOREIGN KEY ("target_id") REFERENCES "external -- Trait +DROP TYPE IF EXISTS "trait_type"; CREATE TYPE "trait_type" AS ENUM ( 'regular', 'tradition' @@ -179,6 +187,7 @@ CREATE TABLE "trainings" ( -- Spell +DROP TYPE IF EXISTS "spell_area_type"; CREATE TYPE "spell_area_type" AS ENUM ( 'burst', 'emanation', @@ -188,10 +197,12 @@ CREATE TYPE "spell_area_type" AS ENUM ( 'cube', 'cylinder' ); +DROP TYPE IF EXISTS "spell_damage_category"; CREATE TYPE "spell_damage_category" AS ENUM ( 'persistent', 'splash' ); +DROP TYPE IF EXISTS "spell_damage_kinds"; CREATE TYPE "spell_damage_kinds" AS ENUM ( 'damage', 'healing', @@ -199,7 +210,7 @@ CREATE TYPE "spell_damage_kinds" AS ENUM ( ); CREATE TABLE "spell" ( - -- "id" int PRIMARY KEY, + "id" int DEFAULT nextval('entity_sequence') PRIMARY KEY, "level" int NOT NULL, "cost" int NOT NULL, "counteraction" bool NOT NULL, @@ -209,8 +220,7 @@ CREATE TABLE "spell" ( "requirements" varchar NOT NULL, "target" varchar NOT NULL, "time" varchar NOT NULL, - "can_heightening" bool NOT NULL, - PRIMARY KEY ("id") + "can_heightening" bool NOT NULL ) INHERITS ("entity"); -- ALTER TABLE "spell" ADD FOREIGN KEY ("id") REFERENCES "entity" ("id"); @@ -255,18 +265,21 @@ ALTER TABLE "spell_defense" ADD FOREIGN KEY ("spell_id") REFERENCES "spell" ("id -- Action +DROP TYPE IF EXISTS "action_type"; CREATE TYPE "action_type" AS ENUM ( 'action', 'free', 'reaction', 'passive' ); +DROP TYPE IF EXISTS "action_category"; CREATE TYPE "action_category" AS ENUM ( 'offensive', 'defensive', 'interaction', 'familiar' ); +DROP TYPE IF EXISTS "ancestry_vision"; CREATE TYPE "ancestry_vision" AS ENUM ( 'normal', 'low_light_vision', @@ -274,13 +287,12 @@ CREATE TYPE "ancestry_vision" AS ENUM ( ); CREATE TABLE "action" ( - -- "id" int PRIMARY KEY, + "id" int DEFAULT nextval('entity_sequence') PRIMARY KEY, "action_type" action_type NOT NULL, "actions" int, "category" action_category, "death_note" bool, - "extra" text, - PRIMARY KEY ("id") + "extra" text ) INHERITS ("entity"); -- ALTER TABLE "action" ADD FOREIGN KEY ("id") REFERENCES "entity" ("id"); @@ -288,13 +300,12 @@ CREATE TABLE "action" ( -- Ancestry CREATE TABLE "ancestry" ( - -- "id" int PRIMARY KEY, + "id" int DEFAULT nextval('entity_sequence') PRIMARY KEY, "hp" int NOT NULL, "reach" int NOT NULL, "size" enum_size NOT NULL, "speed" int NOT NULL, - "vision" ancestry_vision NOT NULL, - PRIMARY KEY ("id") + "vision" ancestry_vision NOT NULL ) INHERITS ("entity"); -- ALTER TABLE "ancestry" ADD FOREIGN KEY ("id") REFERENCES "entity" ("id"); @@ -328,8 +339,7 @@ ALTER TABLE "ancestry_items" ADD FOREIGN KEY ("item_id") REFERENCES "entity" ("i -- Background CREATE TABLE "background" ( - -- "id" int PRIMARY KEY, - PRIMARY KEY ("id") + "id" int DEFAULT nextval('entity_sequence') PRIMARY KEY ) INHERITS ("entity"); -- ALTER TABLE "background" ADD FOREIGN KEY ("id") REFERENCES "entity" ("id"); @@ -360,6 +370,7 @@ ALTER TABLE "background_boosts" ADD FOREIGN KEY ("background_id") REFERENCES "ba -- Campaign Feature +DROP TYPE IF EXISTS "campaign_feature_category"; CREATE TYPE "campaign_feature_category" AS ENUM ( 'army_tactic', 'army_war_action', @@ -369,20 +380,20 @@ CREATE TYPE "campaign_feature_category" AS ENUM ( ); CREATE TABLE "campaign_feature" ( - -- "id" int PRIMARY KEY, + "id" int DEFAULT nextval('entity_sequence') PRIMARY KEY, "level" int, "action_type" action_type NOT NULL, "actions" int NOT NULL, "category" campaign_feature_category NOT NULL, "prerequisites" varchar, - "extra" text, - PRIMARY KEY ("id") + "extra" text ) INHERITS ("entity"); -- ALTER TABLE "campaign_feature" ADD FOREIGN KEY ("id") REFERENCES "entity" ("id"); -- Class +DROP TYPE IF EXISTS "class_progression_type"; CREATE TYPE "class_progression_type" AS ENUM ( 'entity', 'ancestry_feat', @@ -393,12 +404,11 @@ CREATE TYPE "class_progression_type" AS ENUM ( ); CREATE TABLE "class" ( - -- "id" int PRIMARY KEY, + "id" int DEFAULT nextval('entity_sequence') PRIMARY KEY, "hp" int NOT NULL, "perception" int NOT NULL, "spellcasting" bool NOT NULL, - "additionalTrainedSkills" int NOT NULL, - PRIMARY KEY ("id") + "additionalTrainedSkills" int NOT NULL ) INHERITS ("entity"); -- ALTER TABLE "class" ADD FOREIGN KEY ("id") REFERENCES "entity" ("id"); @@ -432,6 +442,7 @@ ALTER TABLE "class_trainings" ADD FOREIGN KEY ("item") REFERENCES "trainings" (" -- Condition +DROP TYPE IF EXISTS "condition_group"; CREATE TYPE "condition_group" AS ENUM ( 'senses', 'abilities', @@ -441,28 +452,30 @@ CREATE TYPE "condition_group" AS ENUM ( ); CREATE TABLE "condition" ( - -- "id" int PRIMARY KEY, + "id" int DEFAULT nextval('entity_sequence') PRIMARY KEY, "group" condition_group, - "extra" text, - PRIMARY KEY ("id") + "extra" text ) INHERITS ("entity"); -- ALTER TABLE "condition" ADD FOREIGN KEY ("id") REFERENCES "entity" ("id"); -- Deity +DROP TYPE IF EXISTS "deity_category"; CREATE TYPE "deity_category" AS ENUM ( 'deity', 'pantheon', 'covenant', 'philosophy' ); +DROP TYPE IF EXISTS "deity_fonts"; CREATE TYPE "deity_fonts" AS ENUM ( 'none', 'heal', 'harm', 'any' ); +DROP TYPE IF EXISTS "deity_sanctification"; CREATE TYPE "deity_sanctification" AS ENUM ( 'holy', 'unholy', @@ -470,12 +483,11 @@ CREATE TYPE "deity_sanctification" AS ENUM ( ); CREATE TABLE "deity" ( - -- "id" int PRIMARY KEY, + "id" int DEFAULT nextval('entity_sequence') PRIMARY KEY, "category" deity_category NOT NULL, "font" deity_fonts NOT NULL, "sanctification_must" bool NOT NULL, - "sanctification_type" deity_sanctification NOT NULL, - PRIMARY KEY ("id") + "sanctification_type" deity_sanctification NOT NULL ) INHERITS ("entity"); -- ALTER TABLE "deity" ADD FOREIGN KEY ("id") REFERENCES "entity" ("id"); @@ -521,6 +533,7 @@ ALTER TABLE "deity_domains" ADD FOREIGN KEY ("deity_id") REFERENCES "deity" ("id -- Feat +DROP TYPE IF EXISTS "feat_category"; CREATE TYPE "feat_category" AS ENUM ( 'ancestryfeature', 'classfeature', @@ -536,7 +549,7 @@ CREATE TYPE "feat_category" AS ENUM ( ); CREATE TABLE "feat" ( - -- "id" int PRIMARY KEY, + "id" int DEFAULT nextval('entity_sequence') PRIMARY KEY, "action_type" action_type NOT NULL, "actions" int, "category" feat_category NOT NULL, @@ -545,8 +558,7 @@ CREATE TABLE "feat" ( "max_takable" int, "location" varchar(12), "only_level_1" bool, - "extra" text, - PRIMARY KEY ("id") + "extra" text ) INHERITS ("entity"); -- ALTER TABLE "feat" ADD FOREIGN KEY ("id") REFERENCES "entity" ("id"); @@ -561,9 +573,63 @@ ALTER TABLE "feat_prerequisites" ADD FOREIGN KEY ("feat_id") REFERENCES "feat" ( -- Heritage CREATE TABLE "heritage" ( - -- "id" int PRIMARY KEY, - "ancestry_id" int, - PRIMARY KEY ("id") + "id" int DEFAULT nextval('entity_sequence') PRIMARY KEY, + "ancestry_id" int ) INHERITS ("entity"); -- ALTER TABLE "heritage" ADD FOREIGN KEY ("id") REFERENCES "entity" ("id"); ALTER TABLE "heritage" ADD FOREIGN KEY ("ancestry_id") REFERENCES "ancestry" ("id"); + + +---------------------------------------------------------------------------------------------- + +CREATE OR REPLACE FUNCTION f_truncate_tables() + RETURNS void + LANGUAGE plpgsql AS +$func$ +BEGIN + EXECUTE + (SELECT 'TRUNCATE TABLE ' + || string_agg(format('%I.%I', schemaname, tablename), ', ') + || ' CASCADE;' + FROM pg_tables + WHERE schemaname = 'public' + ); +END +$func$; + +---------------------------------------------------------------------------------------------- + +CREATE OR REPLACE FUNCTION f_drop_tables() + RETURNS void + LANGUAGE plpgsql AS +$func$ +BEGIN + EXECUTE + (SELECT 'DROP TABLE ' + || string_agg(format('%I.%I', schemaname, tablename), ', ') + || ' CASCADE;' + FROM pg_tables + WHERE schemaname = 'public' + ); +END +$func$; + +---------------------------------------------------------------------------------------------- + +CREATE OR REPLACE FUNCTION f_drop_tables(_username text) + RETURNS void + LANGUAGE plpgsql AS +$func$ +DECLARE + _tbl text; + _sch text; +BEGIN + FOR _sch, _tbl IN + SELECT schemaname, tablename + FROM pg_tables + WHERE schemaname = 'public' + LOOP + EXECUTE format('ALTER TABLE %I.%I OWNER TO %I', _sch, _tbl, _username); + END LOOP; +END +$func$; \ No newline at end of file