Глобальный рефакторинг пространств имён
This commit is contained in:
@@ -0,0 +1,68 @@
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using Newtonsoft.Json.Linq;
|
||||
|
||||
namespace Pf2e.Scrapper.Importers.Implementations.Aonprd.Compendium;
|
||||
|
||||
/// <summary>
|
||||
/// Класс хранения данных компендиума.
|
||||
/// </summary>
|
||||
internal sealed class AonprdCompendium(AonprdRepository repository) : AbstractCompendium
|
||||
{
|
||||
/// <inheritdoc/>
|
||||
public override AonprdRepository Repository { get; } = repository;
|
||||
|
||||
/// <summary>
|
||||
/// Коллекция всех записей компендиума.
|
||||
/// </summary>
|
||||
public ConcurrentBag<AonprdCompendiumItem> Items { get; } = [];
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override int Count(params ICollection<string> types) =>
|
||||
types.Count == 0 ? Items.Count : Items.Count(i => types.Contains(i.Type));
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override IEnumerable<Tuple<AbstractCompendiumItem, JObject>> Iterate(
|
||||
params ICollection<string> types
|
||||
)
|
||||
{
|
||||
IEnumerable<string> files =
|
||||
types.Count == 0
|
||||
? Items.Select(item => item.File).Distinct()
|
||||
: Items
|
||||
.Where(item => types.Contains(item.Type))
|
||||
.Select(item => item.File)
|
||||
.Distinct();
|
||||
|
||||
foreach (string file in files)
|
||||
{
|
||||
Dictionary<string, AonprdCompendiumItem> fileReferences = Items
|
||||
.Where(i => i.File == file)
|
||||
.ToDictionary(i => i.Id);
|
||||
|
||||
using StreamReader fileReader = new(
|
||||
Path.Combine(Repository.RepositoryDirPath, $"{file}.json")
|
||||
);
|
||||
JArray array = JArray.Parse(fileReader.ReadToEnd());
|
||||
|
||||
foreach (JToken item in array)
|
||||
{
|
||||
string? id = item.SelectToken("id")?.ToString();
|
||||
|
||||
if (
|
||||
id is null
|
||||
|| !fileReferences.TryGetValue(id, out AonprdCompendiumItem? reference)
|
||||
|| (types.Count > 0 && !types.Contains(reference.Type))
|
||||
)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
yield return new(reference, (JObject)item);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
namespace Pf2e.Scrapper.Importers.Implementations.Aonprd.Compendium;
|
||||
|
||||
/// <summary>
|
||||
/// Ссылка на сущность с указанием файла, в котором она хранится.
|
||||
/// </summary>
|
||||
/// <param name="id">Идентификатор сущности.</param>
|
||||
/// <param name="file">Имя файла.</param>
|
||||
internal class AonprdCompendiumItem(string id, string file) : AbstractCompendiumItem
|
||||
{
|
||||
/// <summary>
|
||||
/// Идентификатор сущности.
|
||||
/// </summary>
|
||||
public override string Id { get; } = id;
|
||||
|
||||
/// <summary>
|
||||
/// Тип сущности.
|
||||
/// </summary>
|
||||
public override string Type { get; } = ResolveType(id);
|
||||
|
||||
/// <summary>
|
||||
/// Имя файла, в котором хранится сущность.
|
||||
/// </summary>
|
||||
public string File { get; } = file;
|
||||
|
||||
/// <summary>
|
||||
/// Определение типа сущности как префикса идентификатора.
|
||||
/// </summary>
|
||||
public static string ResolveType(string id)
|
||||
{
|
||||
string type = string.Join('-', id.Split('-')[..^1]);
|
||||
|
||||
return SubType(type, "equipment") ?? SubType(type, "weapon") ?? type;
|
||||
|
||||
static string? SubType(string type, string cat)
|
||||
{
|
||||
return type.StartsWith($"{cat}-") && type != cat
|
||||
? type.EndsWith("-bonus")
|
||||
? $"{cat}-bonus"
|
||||
: $"{cat}-sub"
|
||||
: null;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user