77 lines
5.7 KiB
C#

using PlotLine.Data;
using PlotLine.Models;
namespace PlotLine.Services.Context;
public sealed record ContextAssetEvidence(string Value, int? SceneID, string? SceneLabel);
public sealed record ContextAssetRow(StoryAsset Asset, string Presence, ContextAssetEvidence OpeningHolder,
ContextAssetEvidence OpeningLocation, IReadOnlyList<string> Changes);
public sealed record ContextAssetsFrame(SpatialScope Scope, IReadOnlyList<ContextAssetRow> Assets);
// A projection of canonical events, never a persisted second continuity model.
public sealed class ContextAssets(ContextSpatial spatial, ITimelineRepository timeline,
IAssetRepository assets, IStoryPointRepository history)
{
public async Task<ContextAssetsFrame> Read(int bookId, int sceneId)
{
var scope = await spatial.Scope(bookId, sceneId);
var order = StoryStateService.OrderScenes(await timeline.GetByProjectAsync(scope.ProjectID, null, false, [], false)).ToArray();
var catalog = await assets.ListAssetsAsync(scope.ProjectID);
var events = await assets.ListEventsBySceneAsync(sceneId);
var point = await history.GetHistoryAsync(scope.ProjectID, sceneId);
return Build(scope, order, catalog, events, point);
}
public static bool IsMention(string type) => type is "Mentioned" or "First Mentioned";
public static bool IsReferenceOnly(string type) => IsMention(type) || type == "Present";
public static ContextAssetsFrame Build(SpatialScope scope, IReadOnlyList<Scene> order,
IReadOnlyList<StoryAsset> assets, IReadOnlyList<AssetEvent> events, StoryPointHistory history)
{
var indexes = order.Select((s, i) => (s.SceneID, i)).ToDictionary(x => x.SceneID, x => x.i);
if (!indexes.TryGetValue(scope.SceneID, out var boundary)) throw new ArgumentException("Scene order unavailable.");
var labels = order.ToDictionary(s => s.SceneID, s => $"Scene {s.SceneNumber:0.##}: {s.SceneTitle}");
bool Before(int scene) => indexes.TryGetValue(scene, out var index) && index < boundary;
ContextAssetEvidence Evidence(string? value, int? scene) => new(string.IsNullOrWhiteSpace(value) ? "Unknown" : value,
scene, scene.HasValue ? labels.GetValueOrDefault(scene.Value) : null);
var linked = events.Where(e => e.SceneID == scope.SceneID).Select(e => e.StoryAssetID)
.Concat(history.AssetLocations.Where(e => e.SceneID == scope.SceneID).Select(e => e.StoryAssetID))
.Concat(history.Custody.Where(e => e.SceneID == scope.SceneID).Select(e => e.StoryAssetID)).ToHashSet();
var rows = new List<ContextAssetRow>();
foreach (var asset in assets.Where(a => a.ProjectID == scope.ProjectID && !a.IsArchived && linked.Contains(a.StoryAssetID)).OrderByDescending(a => a.Importance).ThenBy(a => a.AssetName))
{
var current = events.Where(e => e.StoryAssetID == asset.StoryAssetID && e.SceneID == scope.SceneID).OrderBy(e => e.UpdatedDate).ThenBy(e => e.AssetEventID).ToArray();
var presence = current.Any(e => e.AssetEventTypeName == "Present") ? "Present"
: current.Any(e => IsMention(e.AssetEventTypeName)) ? "Mentioned" : "Unrecorded";
var location = history.AssetLocations.Where(e => e.StoryAssetID == asset.StoryAssetID && Before(e.SceneID))
.OrderBy(e => indexes[e.SceneID]).ThenBy(e => e.UpdatedDate).ThenBy(e => e.SceneAssetLocationID).LastOrDefault();
var holder = Evidence(null, null);
foreach (var custody in history.Custody.Where(e => e.StoryAssetID == asset.StoryAssetID && Before(e.SceneID))
.OrderBy(e => indexes[e.SceneID]).ThenBy(e => e.UpdatedDate).ThenBy(e => e.AssetCustodyEventID))
{
var value = Holder(custody, history.Custodians);
if (value != null) holder = Evidence(value, custody.SceneID);
}
var changes = current.Select(e => $"{e.AssetEventTypeName}: {e.EventTitle}" +
(string.IsNullOrWhiteSpace(e.EventDescription) ? "" : $" · {e.EventDescription}") +
(!IsReferenceOnly(e.AssetEventTypeName) && !string.IsNullOrWhiteSpace(e.ToStateName) ? $" · state: {e.ToStateName}" : "")).ToList();
changes.AddRange(history.AssetLocations.Where(e => e.StoryAssetID == asset.StoryAssetID && e.SceneID == scope.SceneID)
.OrderBy(e => e.UpdatedDate).ThenBy(e => e.SceneAssetLocationID).Select(e => $"Location recorded: {e.LocationPath ?? e.LocationName ?? "Unknown"}"));
changes.AddRange(history.Custody.Where(e => e.StoryAssetID == asset.StoryAssetID && e.SceneID == scope.SceneID)
.OrderBy(e => e.UpdatedDate).ThenBy(e => e.AssetCustodyEventID).Select(e => $"{e.CustodyEventTypeName}: " + (Holder(e, history.Custodians) ?? "No possession change recorded")));
rows.Add(new(asset, presence, holder, Evidence(location?.LocationPath ?? location?.LocationName, location?.SceneID), changes));
}
return new(scope, rows);
}
// null means no transition; Unknown means an explicit clearing of possession.
public static string? Holder(AssetCustodyEvent custody, IReadOnlyList<AssetCustodyCharacter> participants)
{
var rows = participants.Where(p => p.AssetCustodyEventID == custody.AssetCustodyEventID).ToArray();
if (custody.CustodyEventTypeName == "Lost By" || rows.Length == 0) return "Unknown";
var holders = rows.Where(p => p.RoleName is "Has Custody" or "Shared Custody" or "Transfers To")
.Select(p => string.IsNullOrWhiteSpace(p.CharacterName) ? p.CharacterNameText : p.CharacterName)
.Select(n => string.IsNullOrWhiteSpace(n) ? "Unknown" : n).Distinct().ToArray();
return holders.Length == 0 ? null : string.Join(", ", holders);
}
}