58 lines
9.0 KiB
C#
58 lines
9.0 KiB
C#
// Operational fixture only. Never registered in the website; all writes use existing repositories/SPs.
|
|
using System.Text.Json.Nodes;
|
|
using System.Diagnostics;
|
|
using Dapper;
|
|
using Microsoft.Data.SqlClient;
|
|
using Microsoft.Extensions.Configuration;
|
|
using PlotLine.Data;
|
|
using PlotLine.Models;
|
|
if(args.Length!=1||!args[0].StartsWith("/tmp/pd-dev-browser-"))throw new InvalidOperationException("Dedicated Development identity file required.");
|
|
var file=args[0];var id=JsonNode.Parse(await File.ReadAllTextAsync(file))!;
|
|
if(id["Database"]!.GetValue<string>()!="PlotDirector_Development"||!id["Email"]!.GetValue<string>().StartsWith("pwa-browser-")||!id["Email"]!.GetValue<string>().EndsWith("@example.invalid"))throw new InvalidOperationException("Fixture scope mismatch.");
|
|
var start=new ProcessStartInfo("sudo"){RedirectStandardOutput=true,RedirectStandardError=true};
|
|
foreach(var part in new[]{"-n","cat","/etc/plotdirector/plotdirector.env"})start.ArgumentList.Add(part);
|
|
using var process=Process.Start(start)!;var environment=await process.StandardOutput.ReadToEndAsync();await process.WaitForExitAsync();if(process.ExitCode!=0)throw new InvalidOperationException("Development configuration unavailable.");
|
|
var line=environment.Split('\n').Single(l=>l.StartsWith("ConnectionStrings__DefaultConnection="));
|
|
var raw=line[(line.IndexOf('=')+1)..].Trim().Trim('\'','"');var cs=new SqlConnectionStringBuilder(raw);
|
|
if(cs.InitialCatalog!="PlotDirector_Development")throw new InvalidOperationException("Wrong database.");
|
|
await using var connection=new SqlConnection(cs.ConnectionString);await connection.OpenAsync();if(await connection.ExecuteScalarAsync<string>("SELECT DB_NAME()")!="PlotDirector_Development")throw new InvalidOperationException("Wrong database.");
|
|
var factory=new SqlConnectionFactory(new ConfigurationBuilder().AddInMemoryCollection(new Dictionary<string,string?>{["ConnectionStrings:DefaultConnection"]=cs.ConnectionString}).Build());
|
|
var user=await new UserRepository(factory).GetByEmailAsync(id["Email"]!.GetValue<string>());if(user?.UserID!=id["UserID"]!.GetValue<int>())throw new InvalidOperationException("Identity mismatch.");
|
|
var project=await new ProjectRepository(factory).GetForUserAsync(id["ProjectID"]!.GetValue<int>(),user.UserID);if(project is null||!project.IsOwner||project.ProjectName!="The Glass Harbour")throw new InvalidOperationException("Not the owned fictional project.");
|
|
var chapter=await new ChapterRepository(factory).GetAsync(id["ChapterID"]!.GetValue<int>());var book=await new BookRepository(factory).GetAsync(id["BookID"]!.GetValue<int>());if(chapter?.BookID!=book?.BookID||book?.ProjectID!=project.ProjectID)throw new InvalidOperationException("Fixture ownership mismatch.");
|
|
var existing=await new SceneRepository(factory).ListByChapterAsync(chapter!.ChapterID);
|
|
if(id["SceneID"] is null&&existing.Count>0){var cast=await new CharacterRepository(factory).ListCharactersAsync(project.ProjectID);id["SceneID"]=existing[0].SceneID;id["SceneIDs"]=new JsonArray(existing.Select(x=>(JsonNode?)JsonValue.Create(x.SceneID)).ToArray());id["CharacterID"]=cast.Single(x=>x.CharacterName=="Mara Vale").CharacterID;await File.WriteAllTextAsync(file,id.ToJsonString());}
|
|
if(id["SceneID"] is null){
|
|
var lookup=await new LookupRepository(factory).GetAllAsync();var characters=new CharacterRepository(factory);
|
|
var mara=await characters.SaveCharacterAsync(new Character{ProjectID=project.ProjectID,CharacterName="Mara Vale",ShortName="Mara",DefaultDescription="An archivist carrying a letter with no named sender.",CharacterImportance=1,ShowInQuickAddBar=true});
|
|
var elias=await characters.SaveCharacterAsync(new Character{ProjectID=project.ProjectID,CharacterName="Elias Hart",ShortName="Elias",DefaultDescription="A harbour pilot who recognises the seal on the letter.",CharacterImportance=2});
|
|
var scenes=new SceneRepository(factory);var writer=new WriterWorkspaceRepository(factory);var noteType=(await writer.ListNoteTypesAsync()).First(x=>x.IsActive).SceneNoteTypeID;
|
|
var names=new[]{"Arrival at the quay","A concealed message","The witness returns","A promise made","The missing page","Across the water"};var sceneIds=new JsonArray();
|
|
var time=lookup.TimeModes.First(x=>x.TimeModeName.Contains("Exact"));var revision=lookup.RevisionStatuses.First(x=>x.StatusName=="Planned");
|
|
for(var i=0;i<names.Length;i++){
|
|
var scene=await scenes.SaveAsync(new Scene{ChapterID=chapter!.ChapterID,SceneNumber=i+1,SceneTitle=names[i],Summary=i==0?"Mara reaches the harbour with a sealed letter. Elias recognises its crest but will not explain why.":"The letter draws Mara and Elias closer to the harbour's unfinished story.",TimeModeID=time.TimeModeID,StartDateTime=new DateTime(1926,5,14).AddDays(i).AddHours(7+i),TimeConfidenceID=lookup.TimeConfidences.First().TimeConfidenceID,RevisionStatusID=revision.RevisionStatusID,POVCharacterID=i%2==0?mara:elias,ScenePurposeNotes="Connect the discovery to the next decision.",SceneOutcomeNotes="The search for the sender gains a new direction."});
|
|
sceneIds.Add(scene);await characters.SaveSceneCharacterAsync(new SceneCharacter{SceneID=scene,CharacterID=mara,ProjectID=project.ProjectID,AppearanceNotes="Carries the unopened letter."});await characters.SaveSceneCharacterAsync(new SceneCharacter{SceneID=scene,CharacterID=elias,ProjectID=project.ProjectID,AppearanceNotes="Recognises the crest on the seal."});
|
|
if(i==0)await writer.SaveNoteAsync(new SceneNote{SceneID=scene,SceneNoteTypeID=noteType,NoteTitle="At the harbour",NoteText="By the time Mara reached the quay, the last light in the customs house had gone out.\n\nShe stopped at the end of the pier. In her coat pocket, the letter felt heavier than it had any right to.\n\nAcross the water a door closed. Elias turned at the sound, and for a moment she thought he might tell her everything.\n\nThen he looked away. Someone had been here before her.",IsPinned=true});
|
|
}
|
|
id["SceneID"]=sceneIds[0]!.GetValue<int>();id["SceneIDs"]=sceneIds;id["CharacterID"]=mara;
|
|
await File.WriteAllTextAsync(file,id.ToJsonString());Console.WriteLine("Verified Development fixture ownership; seeded six fictional scenes, two characters and one writing note through existing repositories.");
|
|
|
|
}
|
|
if(id["LineAID"] is null){
|
|
var plots=new PlotRepository(factory);var plotLookup=await plots.GetLookupsAsync();var savedScenes=id["SceneIDs"]!.AsArray().Select(x=>x!.GetValue<int>()).ToArray();
|
|
for(var lineIndex=0;lineIndex<2;lineIndex++){
|
|
var title=lineIndex==0?"The missing letter":"An uneasy alliance";
|
|
var lineId=await plots.SavePlotLineAsync(new PlotLineItem{ProjectID=project.ProjectID,BookID=book!.BookID,PlotLineName=title,PlotLineTypeID=plotLookup.PlotLineTypes.First().PlotLineTypeID,PlotImportanceID=plotLookup.PlotImportance.First().PlotImportanceID,Colour=lineIndex==0?"#6c3154":"#557d69",SortOrder=lineIndex+1});
|
|
id[lineIndex==0?"LineAID":"LineBID"]=lineId;
|
|
var thread=await plots.SavePlotThreadAsync(new PlotThread{PlotLineID=lineId,ProjectID=project.ProjectID,ThreadTitle=title,ThreadTypeID=plotLookup.ThreadTypes.First().ThreadTypeID,ThreadStatusID=plotLookup.ThreadStatuses.First(x=>x.IsOpenStatus).ThreadStatusID,IntroducedSceneID=savedScenes[0],PlannedResolutionSceneID=savedScenes[^1]});
|
|
if(lineIndex==0)id["ThreadID"]=thread;
|
|
for(var i=0;i<savedScenes.Length;i++)if((i+lineIndex)%3!=2)await plots.SaveThreadEventAsync(new ThreadEvent{PlotThreadID=thread,SceneID=savedScenes[i],EventTypeID=plotLookup.ThreadEventTypes.First().ThreadEventTypeID,PlotEventTypeID=plotLookup.PlotEventTypes.First().PlotEventTypeID,EventTitle=i==0?"A letter at the quay":i==savedScenes.Length-1?"The threads reconnect":"A new connection",EventDescription="The discovery changes what Mara can do next."});
|
|
}
|
|
var chars=new CharacterRepository(factory);var known=(await chars.GetLookupsAsync(project.ProjectID)).KnowledgeStates.First(x=>x.StateName=="Knows");
|
|
await chars.SaveKnowledgeAsync(new CharacterKnowledgeItem{CharacterID=id["CharacterID"]!.GetValue<int>(),PlotThreadID=id["ThreadID"]!.GetValue<int>(),SceneID=savedScenes[2],KnowledgeStateID=known.KnowledgeStateID,Description="Mara learns that the letter was posted from the old archive."});
|
|
var metrics=new SceneMetricTypeRepository(factory);await metrics.AddMissingDefaultsAsync(project.ProjectID);foreach(var metric in (await metrics.ListForManagementAsync(project.ProjectID)).Where(m=>m.IsActive).Take(4))for(var i=0;i<savedScenes.Length;i++)await new SceneRepository(factory).SaveMetricValueAsync(savedScenes[i],metric.MetricTypeID,Math.Clamp(i%3==0?3:i%3==1?7:5,metric.MinValue,metric.MaxValue));
|
|
await File.WriteAllTextAsync(file,id.ToJsonString());Console.WriteLine("Added two fictional plot lines and recorded knowledge; only the owned fixture project changed.");
|
|
}
|
|
|
|
var profileRepository=new CharacterRepository(factory);var profile=await profileRepository.GetCharacterAsync(id["CharacterID"]!.GetValue<int>());if(profile is null||profile.ProjectID!=project.ProjectID)throw new InvalidOperationException("Character ownership mismatch.");profile.BirthDate=new DateTime(1898,3,12);profile.AgeAtSeriesStart=28;profile.Height="Five feet six inches";profile.EyeColour="Grey-green";profile.CharacterImportance=3;await profileRepository.SaveCharacterAsync(profile);
|