Работа с порядком правил
This commit is contained in:
+92
-6
@@ -1,11 +1,10 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Data;
|
||||
using Microsoft.EntityFrameworkCore.Migrations.Operations;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using Pf2eModel.Model.Common.I18n;
|
||||
using Pf2eModel.Model.Enum;
|
||||
using Pf2eModel.Model.Structure;
|
||||
using Rule = Pf2eModel.Model.Structure.Rule;
|
||||
|
||||
namespace Pf2eDataScrapper.Importers.Implementations.Aonprd.DataBuilder.Processors.Types;
|
||||
|
||||
@@ -22,8 +21,95 @@ internal class RuleProcessor(AonprdProcessorInput input) : AbstractEntityProcess
|
||||
{
|
||||
base.Process();
|
||||
|
||||
Object.Breadcrumbs = (Input.GetOptional<JArray>("breadcrumbs") ?? [])
|
||||
string[] breadcrumbs = (Input.GetOptional<JArray>("breadcrumbs") ?? [])
|
||||
.Select(token => token.ToString())
|
||||
.ToArray();
|
||||
|
||||
RuleStructureItem item = GetStructureItem(
|
||||
[.. breadcrumbs, Entity.Name[LocaleType.English]]
|
||||
);
|
||||
|
||||
item.IsHandled = true;
|
||||
item.RuleId = Object.Id;
|
||||
item.Id = Input.GetRequired<string>("id");
|
||||
|
||||
if (item.Parent?.IsHandled ?? false)
|
||||
{
|
||||
Object.ParentId = item.Parent.RuleId!;
|
||||
// Object.Order = item.Parent.ChildrenOrder?.IndexOf(item.Id) ?? 0;
|
||||
Object.Order = 0;
|
||||
}
|
||||
|
||||
item.ChildrenOrder = [];
|
||||
|
||||
List<int> childrenIds = item
|
||||
.Children.Select(i => i.RuleId)
|
||||
.Where(id => id != null)
|
||||
.OfType<int>()
|
||||
.ToList();
|
||||
|
||||
List<Rule> rules = Context.Rules.Where(i => childrenIds.Contains(i.Id)).ToList();
|
||||
|
||||
foreach (Rule rule in rules)
|
||||
{
|
||||
rule.Order = 0;
|
||||
}
|
||||
/*
|
||||
* TODO
|
||||
* Обработка порядка из полей
|
||||
*
|
||||
"next_link": {
|
||||
"label": "Chapter 3: Classes",
|
||||
"url": "/Rules.aspx?ID=134"
|
||||
},
|
||||
"previous_link": {
|
||||
"label": "Chapter 1: Introduction",
|
||||
"url": "/Rules.aspx?ID=1"
|
||||
},
|
||||
*/
|
||||
}
|
||||
|
||||
private static readonly List<RuleStructureItem> Roots = [];
|
||||
|
||||
private RuleStructureItem GetStructureItem(string[] breadcrumbs, RuleStructureItem? from = null)
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
List<RuleStructureItem> children = from?.Children ?? Roots;
|
||||
|
||||
if (breadcrumbs.Length == 0)
|
||||
{
|
||||
return from!;
|
||||
}
|
||||
|
||||
string root = breadcrumbs[0];
|
||||
RuleStructureItem? item = children.FirstOrDefault(i => i.Name == root);
|
||||
|
||||
if (item is null)
|
||||
{
|
||||
item = new(root, from);
|
||||
children.Add(item);
|
||||
}
|
||||
|
||||
breadcrumbs = breadcrumbs[1..];
|
||||
from = item;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal class RuleStructureItem(string name, RuleStructureItem? parent)
|
||||
{
|
||||
public string? Id { get; set; } = null;
|
||||
|
||||
public string Name { get; set; } = name;
|
||||
|
||||
public int? RuleId { get; set; } = null;
|
||||
|
||||
public bool IsHandled { get; set; } = false;
|
||||
|
||||
public RuleStructureItem? Parent { get; set; } = parent;
|
||||
|
||||
public List<string>? ChildrenOrder { get; set; } = null;
|
||||
|
||||
public List<RuleStructureItem> Children { get; } = [];
|
||||
}
|
||||
|
||||
+26
-1
@@ -13,7 +13,7 @@ using Pf2eModel.Model.Enum;
|
||||
namespace Pf2eModel.Migrations
|
||||
{
|
||||
[DbContext(typeof(Pf2eDbContext))]
|
||||
[Migration("20250507075826_1")]
|
||||
[Migration("20250507112626_1")]
|
||||
partial class _1
|
||||
{
|
||||
/// <inheritdoc />
|
||||
@@ -1407,9 +1407,22 @@ namespace Pf2eModel.Migrations
|
||||
.HasColumnType("text[]")
|
||||
.HasColumnName("breadcrumbs");
|
||||
|
||||
b.Property<int>("Order")
|
||||
.HasColumnType("integer")
|
||||
.HasColumnName("order")
|
||||
.HasColumnOrder(2);
|
||||
|
||||
b.Property<int?>("ParentId")
|
||||
.HasColumnType("integer")
|
||||
.HasColumnName("parent_id")
|
||||
.HasColumnOrder(3);
|
||||
|
||||
b.HasKey("Id")
|
||||
.HasName("pk_rules");
|
||||
|
||||
b.HasIndex("ParentId")
|
||||
.HasDatabaseName("ix_rules_parent_id");
|
||||
|
||||
b.ToTable("rules", (string)null);
|
||||
});
|
||||
|
||||
@@ -2276,7 +2289,14 @@ namespace Pf2eModel.Migrations
|
||||
.IsRequired()
|
||||
.HasConstraintName("fk_rules_entities_id");
|
||||
|
||||
b.HasOne("Pf2eModel.Model.Structure.Rule", "Parent")
|
||||
.WithMany("Children")
|
||||
.HasForeignKey("ParentId")
|
||||
.HasConstraintName("fk_rules_rules_parent_id");
|
||||
|
||||
b.Navigation("Entity");
|
||||
|
||||
b.Navigation("Parent");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("Pf2eModel.Model.Structure.Spell", b =>
|
||||
@@ -2456,6 +2476,11 @@ namespace Pf2eModel.Migrations
|
||||
b.Navigation("Checks");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("Pf2eModel.Model.Structure.Rule", b =>
|
||||
{
|
||||
b.Navigation("Children");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("Pf2eModel.Model.Structure.Spell", b =>
|
||||
{
|
||||
b.Navigation("SpellAreas");
|
||||
@@ -329,6 +329,8 @@ namespace Pf2eModel.Migrations
|
||||
columns: table => new
|
||||
{
|
||||
id = table.Column<int>(type: "integer", nullable: false),
|
||||
order = table.Column<int>(type: "integer", nullable: false),
|
||||
parent_id = table.Column<int>(type: "integer", nullable: true),
|
||||
breadcrumbs = table.Column<string[]>(type: "text[]", nullable: false)
|
||||
},
|
||||
constraints: table =>
|
||||
@@ -340,6 +342,11 @@ namespace Pf2eModel.Migrations
|
||||
principalTable: "entities",
|
||||
principalColumn: "id",
|
||||
onDelete: ReferentialAction.Cascade);
|
||||
table.ForeignKey(
|
||||
name: "fk_rules_rules_parent_id",
|
||||
column: x => x.parent_id,
|
||||
principalTable: "rules",
|
||||
principalColumn: "id");
|
||||
});
|
||||
|
||||
migrationBuilder.CreateTable(
|
||||
@@ -1293,6 +1300,11 @@ namespace Pf2eModel.Migrations
|
||||
table: "ritual_checks",
|
||||
column: "training_id");
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "ix_rules_parent_id",
|
||||
table: "rules",
|
||||
column: "parent_id");
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "ix_spell_areas_spell_id",
|
||||
table: "spell_areas",
|
||||
@@ -1404,9 +1404,22 @@ namespace Pf2eModel.Migrations
|
||||
.HasColumnType("text[]")
|
||||
.HasColumnName("breadcrumbs");
|
||||
|
||||
b.Property<int>("Order")
|
||||
.HasColumnType("integer")
|
||||
.HasColumnName("order")
|
||||
.HasColumnOrder(2);
|
||||
|
||||
b.Property<int?>("ParentId")
|
||||
.HasColumnType("integer")
|
||||
.HasColumnName("parent_id")
|
||||
.HasColumnOrder(3);
|
||||
|
||||
b.HasKey("Id")
|
||||
.HasName("pk_rules");
|
||||
|
||||
b.HasIndex("ParentId")
|
||||
.HasDatabaseName("ix_rules_parent_id");
|
||||
|
||||
b.ToTable("rules", (string)null);
|
||||
});
|
||||
|
||||
@@ -2273,7 +2286,14 @@ namespace Pf2eModel.Migrations
|
||||
.IsRequired()
|
||||
.HasConstraintName("fk_rules_entities_id");
|
||||
|
||||
b.HasOne("Pf2eModel.Model.Structure.Rule", "Parent")
|
||||
.WithMany("Children")
|
||||
.HasForeignKey("ParentId")
|
||||
.HasConstraintName("fk_rules_rules_parent_id");
|
||||
|
||||
b.Navigation("Entity");
|
||||
|
||||
b.Navigation("Parent");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("Pf2eModel.Model.Structure.Spell", b =>
|
||||
@@ -2453,6 +2473,11 @@ namespace Pf2eModel.Migrations
|
||||
b.Navigation("Checks");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("Pf2eModel.Model.Structure.Rule", b =>
|
||||
{
|
||||
b.Navigation("Children");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("Pf2eModel.Model.Structure.Spell", b =>
|
||||
{
|
||||
b.Navigation("SpellAreas");
|
||||
|
||||
@@ -15,5 +15,15 @@ public class Rule : IEntity
|
||||
|
||||
public Entity Entity { get; set; } = null!;
|
||||
|
||||
[Column(Order = 2)]
|
||||
public int Order { get; set; }
|
||||
|
||||
[Column(Order = 3)]
|
||||
public int? ParentId { get; set; } = null;
|
||||
|
||||
public Rule? Parent { get; set; } = null;
|
||||
|
||||
public ICollection<Rule> Children { get; set; } = [];
|
||||
|
||||
public string[] Breadcrumbs { get; set; } = [];
|
||||
}
|
||||
|
||||
@@ -508,6 +508,12 @@ public partial class Pf2eDbContext
|
||||
modelBuilder.Entity<Rule>(entity =>
|
||||
{
|
||||
BindEntity(entity);
|
||||
|
||||
entity
|
||||
.HasOne(r => r.Parent)
|
||||
.WithMany(r => r.Children)
|
||||
.HasForeignKey(r => r.ParentId)
|
||||
.IsRequired(false);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user