181 lines
4.9 KiB
C#
181 lines
4.9 KiB
C#
using System.Security.Cryptography;
|
|
using System.Text;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Newtonsoft.Json.Linq;
|
|
using Pf2eDataScrapper.Helpers;
|
|
using Pf2eDataScrapper.Processors;
|
|
using Pf2eModel;
|
|
using Pf2eModel.Model;
|
|
using Pf2eModel.Model.Base;
|
|
using Pf2eModel.Model.Base.Source;
|
|
using Pf2eModel.Model.Base.Targets;
|
|
using Pf2eModel.Model.Common.I18n;
|
|
using Pf2eModel.Model.Enum;
|
|
using Pf2eModel.Model.Structure;
|
|
|
|
namespace Pf2eDataScrapper.Importers.Implementations.FoundryEn.Processors;
|
|
|
|
internal abstract class FoundryEnAbstractProcessor(
|
|
DatabaseHelper databaseHelper,
|
|
Pf2eDbContext context,
|
|
ProcessorInput input
|
|
) : AbstractInputProcessor(input)
|
|
{
|
|
protected DatabaseHelper Helper { get; } = databaseHelper;
|
|
|
|
public Pf2eDbContext Context { get; } = context;
|
|
|
|
public Entity Entity { get; set; } = new();
|
|
|
|
public abstract EntityType Type { get; }
|
|
|
|
public abstract IEntity Object { get; }
|
|
|
|
public override void Process()
|
|
{
|
|
Entity.Id = Helper.NextEntityId;
|
|
Entity.Type = Type;
|
|
|
|
Entity.Name[LocaleType.English] = Input.GetRequired<string>("name");
|
|
Entity.Img = Input.GetRequired<string>("img");
|
|
Entity.Description = Input.GetRequired<string>("system.description.value");
|
|
|
|
Entity.IsTranslated = false;
|
|
Entity.IsChecked = false;
|
|
|
|
Entity.Slug = Helper.GenerateSlug(Entity.Name[LocaleType.English]);
|
|
Entity.Rarity = Input.EnumOptional("system.traits.rarity", EntityRarity.Common);
|
|
|
|
Object.Id = Entity.Id;
|
|
|
|
Context.Entities.Add(Entity);
|
|
|
|
// Alias
|
|
/*
|
|
EntityAlias alias = new()
|
|
{
|
|
Type = LocaleType.English,
|
|
Alias = Entity.Group,
|
|
EntityId = Entity.Id,
|
|
Entity = Entity
|
|
};
|
|
Context.EntityAliases.Add(alias);
|
|
*/
|
|
// Source
|
|
EntitySource source = new()
|
|
{
|
|
Entity = Entity,
|
|
|
|
Source = GetSource(Input.GetRequired<string>("system.publication.title")),
|
|
|
|
Suffix = "",
|
|
IsRemaster = Input.GetRequired<bool>("system.publication.remaster"),
|
|
};
|
|
Context.EntitySources.Add(source);
|
|
|
|
// External
|
|
EntityExternal external = new()
|
|
{
|
|
Entity = Entity,
|
|
Target = GetTarget(),
|
|
Suffix = Input.GetRequired<string>("_id"),
|
|
Md5 = Md5(Input.Input.ToString()),
|
|
};
|
|
Context.EntityExternals.Add(external);
|
|
|
|
// Traits
|
|
JToken? traitsJson = Input.Input.SelectToken("system.traits.value");
|
|
|
|
if (traitsJson?.Type != JTokenType.Array)
|
|
{
|
|
throw new Exception("system.traits.value is expected to be an array");
|
|
}
|
|
|
|
foreach (Trait trait in GetTraits(traitsJson.ToArray().Select(jToken => jToken.ToString())))
|
|
{
|
|
Entity.Traits.Add(trait);
|
|
}
|
|
}
|
|
|
|
public override void Save()
|
|
{
|
|
Context.SaveChanges();
|
|
}
|
|
|
|
protected Source GetSource(string name)
|
|
{
|
|
Source? source = Context.Sources.FirstOrDefault(s => s.Name == name);
|
|
|
|
if (source != null)
|
|
{
|
|
return source;
|
|
}
|
|
|
|
source = new() { Name = name };
|
|
|
|
Context.Sources.Add(source);
|
|
return source;
|
|
}
|
|
|
|
protected ExternalTarget GetTarget()
|
|
{
|
|
const string name = "pf2e - Foundry module";
|
|
|
|
ExternalTarget? target = Context.ExternalTargets.FirstOrDefault(s => s.Name == name);
|
|
|
|
if (target != null)
|
|
{
|
|
return target;
|
|
}
|
|
|
|
target = new()
|
|
{
|
|
Name = "pf2e - Foundry module",
|
|
Icon = "",
|
|
Prefix = "https://github.com/foundryvtt/pf2e",
|
|
};
|
|
|
|
Context.ExternalTargets.Add(target);
|
|
return target;
|
|
}
|
|
|
|
protected IEnumerable<Trait> GetTraits(IEnumerable<string> names)
|
|
{
|
|
List<Trait> result = Context
|
|
.Traits.Include(t => t.Entity)
|
|
.ThenInclude(e => e.Name)
|
|
// ReSharper disable once EntityFramework.UnsupportedServerSideFunctionCall
|
|
.Where(t => Pf2eDbContext.LocaleSearchArray(t.Entity.Name, names))
|
|
.ToList();
|
|
|
|
/*
|
|
foreach (string name in names)
|
|
{
|
|
if (result.Any(t => t.Entity.Group.Contains(name)))
|
|
{
|
|
continue;
|
|
}
|
|
Trait trait = new()
|
|
{
|
|
Group = name,
|
|
Slug = name,
|
|
Type = TraitType.Regular,
|
|
Description = "",
|
|
};
|
|
Context.Traits.Add(trait);
|
|
result.Add(trait);
|
|
}
|
|
*/
|
|
|
|
return result;
|
|
}
|
|
|
|
public static string Md5(string input, bool isLowercase = false)
|
|
{
|
|
using MD5 md5 = MD5.Create();
|
|
byte[] byteHash = md5.ComputeHash(Encoding.UTF8.GetBytes(input));
|
|
string hash = BitConverter.ToString(byteHash).Replace("-", "");
|
|
return isLowercase ? hash.ToLower() : hash;
|
|
}
|
|
}
|