Глобальный рефакторинг пространств имён
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user