25 lines
3.2 KiB
C#
25 lines
3.2 KiB
C#
using System.Data;using System.Text.Json;using Dapper;using PlotLine.Models;using PlotLine.Services;
|
|
namespace PlotLine.Data;
|
|
public sealed record ContextLensRead(RelationshipMapData Data,int? FocusCharacterID,bool Truncated);
|
|
public interface IContextRelationshipReader
|
|
{
|
|
Task<ContextLensRead> Read(int project,int user,int scene,int? focus,string lens,int depth,IReadOnlyList<int> physical,CancellationToken cancellationToken=default);
|
|
Task<IReadOnlyList<Character>> Lookup(int project,int user,string search,CancellationToken cancellationToken=default);
|
|
}
|
|
public sealed class ContextRelationshipReader(ISqlConnectionFactory connectionFactory,IConfiguration configuration):IContextRelationshipReader
|
|
{
|
|
private sealed record Boundary(int? FocusCharacterID,bool Truncated);
|
|
public async Task<ContextLensRead> Read(int project,int user,int scene,int? focus,string lens,int depth,IReadOnlyList<int> physical,CancellationToken cancellationToken=default)
|
|
{
|
|
using var db=connectionFactory.CreateConnection();IReadOnlyList<Book> books;IReadOnlyList<Chapter> chapters;IReadOnlyList<Scene> scenes;IReadOnlyList<Character> people;IReadOnlyList<CharacterRelationship> links;Boundary boundary;
|
|
using(var grid=await db.QueryMultipleAsync(new CommandDefinition("dbo.ContextRelationshipLens_Read",new{ProjectID=project,UserID=user,FocusCharacterID=focus,Lens=lens,Depth=depth,PhysicalIDs=JsonSerializer.Serialize(physical)},commandType:CommandType.StoredProcedure,cancellationToken:cancellationToken))){books=(await grid.ReadAsync<Book>()).ToList();chapters=(await grid.ReadAsync<Chapter>()).ToList();scenes=(await grid.ReadAsync<Scene>()).ToList();people=(await grid.ReadAsync<Character>()).ToList();links=(await grid.ReadAsync<CharacterRelationship>()).ToList();boundary=await grid.ReadSingleAsync<Boundary>();}
|
|
if(books.Count>20000||chapters.Count>20000||scenes.Count>20000)throw new ArgumentException("This project exceeds the Context narrative-order limit. Use the full Relationships Editor.");
|
|
var order=StoryStateService.OrderScenes(new(){Books=books,Chapters=chapters,Scenes=scenes}).Select((s,i)=>new{s.SceneID,Ordinal=i}).ToList();var at=lens=="family"||scene==0?order.Count-1:order.FirstOrDefault(s=>s.SceneID==scene)?.Ordinal??-1;
|
|
var changes=(await db.QueryAsync<RelationshipEvent>(new CommandDefinition("dbo.ContextRelationshipLens_Events",new{ProjectID=project,UserID=user,RelationshipIDs=JsonSerializer.Serialize(links.Select(r=>r.CharacterRelationshipID)),SceneOrder=JsonSerializer.Serialize(order),AtOrdinal=at},commandType:CommandType.StoredProcedure,cancellationToken:cancellationToken))).ToList();
|
|
await ParagraphEvidenceProjection.Apply(db,configuration,changes);
|
|
return new(new(){Books=books,Chapters=chapters,Scenes=scenes,Characters=people,Relationships=links,RelationshipEvents=changes},boundary.FocusCharacterID,boundary.Truncated);
|
|
}
|
|
public async Task<IReadOnlyList<Character>> Lookup(int project,int user,string search,CancellationToken cancellationToken=default)
|
|
{using var db=connectionFactory.CreateConnection();return (await db.QueryAsync<Character>(new CommandDefinition("dbo.ContextCharacter_Lookup",new{ProjectID=project,UserID=user,Search=search},commandType:CommandType.StoredProcedure,cancellationToken:cancellationToken))).ToList();}
|
|
}
|