PlotDirector/PlotLine/Services/Writer/WriterPlanning.cs

30 lines
2.1 KiB
C#

using PlotLine.Models;
using PlotLine.ViewModels;
namespace PlotLine.Services.Writer;
public static class WriterPlanning
{
public static IReadOnlyList<WriterPlanActivity> Allocate(WritingPlanSettingsEditViewModel plan,IReadOnlyList<WriterPlanActivity> selected)
{
if(selected.Count==0)throw new ArgumentException("Select at least one chapter or scene. Add a chapter to the selected book if none exists.");
if(plan.DeadlineDate.Date<plan.StartDate.Date||(plan.DeadlineDate-plan.StartDate).TotalDays>3650)throw new ArgumentException("Choose a planning window of at most ten years.");
var availability=plan.WritingDayAvailability.Where(x=>x.IsAvailable).ToDictionary(x=>Enum.Parse<DayOfWeek>(x.Day),x=>x.Minutes);
if(availability.Count==0||availability.Values.Any(x=>x is <1 or >720))throw new ArgumentException("Choose valid writing availability.");
var result=new List<WriterPlanActivity>();var date=plan.StartDate.Date;var used=0;
foreach(var candidate in selected)
{
if(candidate.Minutes is <5 or >1440)throw new ArgumentException("Choose a working budget between 5 and 1,440 minutes for each activity.");
if(candidate.Kind is not("Draft" or "Edit" or "Plan" or "Review" or "AudioReview"))throw new ArgumentException("Choose a supported activity.");
var remaining=candidate.Minutes;
while(remaining>0)
{
while(date<=plan.DeadlineDate.Date&&(!availability.TryGetValue(date.DayOfWeek,out var capacity)||used>=capacity)){date=date.AddDays(1);used=0;}
if(date>plan.DeadlineDate.Date)throw new ArgumentException("Selected working budgets exceed the available time before your deadline. Reduce budgets, deselect activities, extend the deadline or edit availability. No schedule was changed.");
var minutes=Math.Min(remaining,availability[date.DayOfWeek]-used);remaining-=minutes;used+=minutes;
result.Add(new(){BookID=candidate.BookID,ChapterID=candidate.ChapterID,SceneID=candidate.SceneID,Title=candidate.Title,BookTitle=candidate.BookTitle,Minutes=minutes,ScheduledDate=date,Kind=candidate.Kind});
}
}
return result;
}
public static int StartingBudget(int? words)=>words is >0?Math.Clamp((int)Math.Ceiling(words.Value/200d/5)*5,5,120):30;
}