105 lines
4.7 KiB
C#
105 lines
4.7 KiB
C#
using System.Collections.ObjectModel;
|
|
using System.Text.Json;
|
|
|
|
namespace View_by_Distance.Instance.Models;
|
|
|
|
/// <summary>
|
|
// List<string>
|
|
/// </summary>
|
|
internal class F_Random
|
|
{
|
|
|
|
private readonly Serilog.ILogger? _Log;
|
|
private readonly Configuration _Configuration;
|
|
private readonly JsonSerializerOptions _WriteIndentedJsonSerializerOptions;
|
|
|
|
internal F_Random(Configuration configuration)
|
|
{
|
|
_Configuration = configuration;
|
|
_Log = Serilog.Log.ForContext<F_Random>();
|
|
_WriteIndentedJsonSerializerOptions = new JsonSerializerOptions { WriteIndented = false };
|
|
}
|
|
|
|
public override string ToString()
|
|
{
|
|
string result = JsonSerializer.Serialize(this, new JsonSerializerOptions() { WriteIndented = true });
|
|
return result;
|
|
}
|
|
|
|
private static Dictionary<string, List<string>> Get(Dictionary<long, List<int>> personKeyToIds, ReadOnlyCollection<Shared.Models.Mapping> mappingCollection, string dateFormat)
|
|
{
|
|
Dictionary<string, List<string>> results = new();
|
|
string key;
|
|
long personKey;
|
|
DateTime dateTime;
|
|
List<long>? personKeys;
|
|
List<string>? relativePaths;
|
|
Dictionary<int, List<long>> idToPersonKeys = Map.Models.Stateless.Methods.IMapLogic.GetIdToPersonKeys(personKeyToIds);
|
|
foreach (Shared.Models.Mapping mapping in mappingCollection)
|
|
{
|
|
if (mapping.MappingFromItem.ImageFileHolder.DirectoryName is null || mapping.MappingFromPerson is null)
|
|
continue;
|
|
if (!idToPersonKeys.TryGetValue(mapping.MappingFromItem.Id, out personKeys))
|
|
continue;
|
|
if (Shared.Models.Stateless.Methods.IPersonBirthday.IsCounterPersonBirthday(mapping.MappingFromPerson.PersonBirthday))
|
|
continue;
|
|
if (!personKeys.Contains(mapping.MappingFromPerson.PersonBirthday.Value.Ticks))
|
|
continue;
|
|
personKey = mapping.MappingFromPerson.PersonBirthday.Value.Ticks;
|
|
dateTime = new(personKey);
|
|
key = dateTime.ToString(dateFormat);
|
|
if (!results.TryGetValue(key, out relativePaths))
|
|
{
|
|
results.Add(key, new());
|
|
if (!results.TryGetValue(key, out relativePaths))
|
|
throw new Exception();
|
|
}
|
|
relativePaths.Add(mapping.MappingFromItem.RelativePath);
|
|
}
|
|
return results;
|
|
}
|
|
|
|
internal void Random(Property.Models.Configuration configuration, string outputResolution, Dictionary<long, List<int>> personKeyToIds, ReadOnlyCollection<Shared.Models.Mapping> mappingCollection)
|
|
{
|
|
string key;
|
|
string json;
|
|
string jsonFile;
|
|
Random random = new();
|
|
List<string>? collection;
|
|
string dateFormat = "MM-dd";
|
|
List<string> relativePaths = new();
|
|
List<int> distinctCollection = new();
|
|
DateTime dateTime = new(2024, 1, 1); //Leap year
|
|
Dictionary<string, List<string>> dayToRelativePaths = Get(personKeyToIds, mappingCollection, dateFormat);
|
|
string fRandomCollectionDirectory = Property.Models.Stateless.IResult.GetResultsDateGroupDirectory(configuration, nameof(F_Random), "[]");
|
|
string[] files = Directory.GetFiles(fRandomCollectionDirectory, "*", SearchOption.TopDirectoryOnly);
|
|
foreach (string file in files)
|
|
File.Delete(file);
|
|
foreach (Shared.Models.Mapping mapping in mappingCollection)
|
|
{
|
|
if (distinctCollection.Contains(mapping.MappingFromItem.Id))
|
|
continue;
|
|
if (mapping.MappingFromItem.ImageFileHolder.DirectoryName is null)
|
|
continue;
|
|
relativePaths.Add(mapping.MappingFromItem.RelativePath);
|
|
distinctCollection.Add(mapping.MappingFromItem.Id);
|
|
}
|
|
if (relativePaths.Count > 0)
|
|
{
|
|
for (int i = 0; i < 366; i++)
|
|
{
|
|
key = dateTime.AddDays(i).ToString(dateFormat);
|
|
if (dayToRelativePaths.TryGetValue(key, out collection) && collection.Count > 10)
|
|
collection = (from l in collection orderby random.NextDouble() select l).ToList();
|
|
else
|
|
collection = (from l in relativePaths orderby random.NextDouble() select l).ToList();
|
|
jsonFile = Path.Combine(fRandomCollectionDirectory, $"{key}.json");
|
|
json = JsonSerializer.Serialize(collection, _WriteIndentedJsonSerializerOptions);
|
|
_ = Shared.Models.Stateless.Methods.IPath.WriteAllText(jsonFile, json, updateDateWhenMatches: false, compareBeforeWrite: false);
|
|
if (!_Configuration.SaveFullYearOfRandomFiles)
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
} |