32 lines
3.2 KiB
C#
32 lines
3.2 KiB
C#
using PlotLine.Services;
|
|
// Isolated browser-host repository. Production persistence/security is tested against SQL separately.
|
|
internal sealed class NarrativeFocusFixture : INarrativeFocus
|
|
{
|
|
private readonly object gate=new();
|
|
private readonly Dictionary<(int,int),NarrativeFocusState> states=new();
|
|
public Task<NarrativeFocusState> Read(int user,int project,int book)
|
|
{
|
|
lock(gate){if(user is not (4 or 5)||project!=7||book is not (12 or 13))throw new UnauthorizedAccessException();
|
|
if(!states.TryGetValue((user,book),out var state))states[(user,book)]=state=new(){ProjectID=project,BookID=book,ChapterID=book==12?21:null,SceneID=book==12?31:null,Version=1,Positions=book==12?[
|
|
new(){ChapterID=21,ChapterNumber=1,ChapterTitle="The crossing",SceneID=31,SceneNumber=1,SceneTitle="Arrival",BookSceneIndex=1},
|
|
new(){ChapterID=21,ChapterNumber=1,ChapterTitle="The crossing",SceneID=101,SceneNumber=2,SceneTitle="A second scene",BookSceneIndex=2},
|
|
new(){ChapterID=22,ChapterNumber=2,ChapterTitle="The harbour",SceneID=32,SceneNumber=1,SceneTitle="Departure",BookSceneIndex=3},
|
|
new(){ChapterID=23,ChapterNumber=3,ChapterTitle="Unwritten"}]:[]};return Task.FromResult(state);}
|
|
}
|
|
public async Task<NarrativeFocusState> Set(int user,int project,int book,int? chapter,int? scene,long version,int direction=0)
|
|
{
|
|
var state=await Read(user,project,book);
|
|
lock(gate){state=states[(user,book)];if(state.Version!=version)throw new NarrativeFocusConflictException();
|
|
if(direction!=0){var scenes=state.Positions.Where(p=>p.SceneID.HasValue).ToArray();var index=Array.FindIndex(scenes,p=>p.SceneID==state.SceneID);NarrativePosition? target;
|
|
if(index>=0)target=scenes.ElementAtOrDefault(index+direction);
|
|
else {var chapterScenes=scenes.Where(p=>p.ChapterID==state.ChapterID).ToArray();var chapterIndex=state.Positions.ToList().FindIndex(p=>p.ChapterID==state.ChapterID);var candidates=chapterScenes.Length>0?chapterScenes:chapterIndex<0?scenes:state.Positions.Where((p,i)=>p.SceneID.HasValue&&(direction<0?i<chapterIndex:i>chapterIndex)).ToArray();target=direction<0?candidates.LastOrDefault():candidates.FirstOrDefault();}
|
|
if(target is null)return state;scene=target.SceneID;chapter=target.ChapterID;}
|
|
var found=state.Positions.FirstOrDefault(p=>scene.HasValue?p.SceneID==scene:p.ChapterID==chapter);
|
|
if((scene.HasValue||chapter.HasValue)&&found is null)throw new UnauthorizedAccessException();
|
|
chapter=found?.ChapterID;if(chapter==state.ChapterID&&scene==state.SceneID)return state;var history=state.Recent.Where(p=>p.ChapterID!=chapter||p.SceneID!=scene).ToList();
|
|
if(chapter.HasValue)history.Insert(0,new(chapter.Value,scene,state.Version+1));
|
|
return states[(user,book)]=state with{ChapterID=chapter,SceneID=scene,Version=state.Version+1,Recent=history.Take(20).ToArray()};}
|
|
}
|
|
public async Task<NarrativeFocusState?> Open(int user,int? chapter,int? scene,int? expectedProject=null,int? expectedBook=null){if(expectedProject.HasValue&&expectedProject!=7||expectedBook.HasValue&&expectedBook!=12)return null;var state=await Read(user,7,12);return await Set(user,7,12,chapter,scene,state.Version);}
|
|
}
|