132 lines
6.2 KiB
C#
132 lines
6.2 KiB
C#
using PlotLine.Models;
|
|
using PlotLine.ViewModels;
|
|
|
|
namespace PlotLine.Services.Writer;
|
|
|
|
public sealed record WriterCapacityPreview(int Available, int Reserved, int Required,
|
|
int Shortfall, DateTime? Forecast, IReadOnlyList<WriterPlanActivity> Allocations)
|
|
{
|
|
public int AlreadyAllocated { get; init; }
|
|
public int Usable => Available - Reserved - AlreadyAllocated;
|
|
}
|
|
|
|
public static class WriterPlanning
|
|
{
|
|
private sealed record DayCapacity(DateTime Date, int Available, int Reserved, int AlreadyAllocated)
|
|
{
|
|
public int Usable => Available - Reserved - AlreadyAllocated;
|
|
}
|
|
|
|
public static WriterCapacityPreview Review(WritingScheduleSetupViewModel setup,
|
|
IReadOnlyList<WriterPlanActivity> selected, int minimumBudget = 5,
|
|
IReadOnlyList<WriterPlanActivity>? completed = null)
|
|
{
|
|
if (setup.ContingencyPercent is < 1 or > 99)
|
|
throw new ArgumentException("Choose contingency between 1 and 99 percent.");
|
|
|
|
var dates = Capacity(setup.StartDate, setup.DeadlineDate,
|
|
setup.WritingDayAvailability, setup.ContingencyPercent, completed ?? []);
|
|
ValidateBudgets(selected, minimumBudget);
|
|
|
|
var required = selected.Sum(item => item.Minutes);
|
|
var shortfall = Math.Max(0, required - dates.Sum(date => date.Usable));
|
|
var allocations = selected.Count > 0 && shortfall == 0 ? Fit(dates, selected) : [];
|
|
return new(dates.Sum(date => date.Available), dates.Sum(date => date.Reserved),
|
|
required, shortfall, allocations.LastOrDefault()?.ScheduledDate, allocations)
|
|
{
|
|
AlreadyAllocated = dates.Sum(date => date.AlreadyAllocated)
|
|
};
|
|
}
|
|
|
|
public static IReadOnlyList<WriterPlanActivity> Allocate(WritingPlanSettingsEditViewModel plan,
|
|
IReadOnlyList<WriterPlanActivity> selected)
|
|
{
|
|
if (selected.Count == 0)
|
|
throw new ArgumentException("Select at least one chapter or scene.");
|
|
ValidateBudgets(selected, minimumBudget: 5);
|
|
var dates = Capacity(plan.StartDate, plan.DeadlineDate, plan.WritingDayAvailability, 0, []);
|
|
if (dates.Sum(date => date.Usable) < selected.Sum(item => item.Minutes))
|
|
throw new ArgumentException("The selected budgets exceed available time before the target. Reduce budgets or edit availability. The saved schedule is unchanged.");
|
|
return Fit(dates, selected);
|
|
}
|
|
|
|
public static int DraftingBudget(int words, int wordsPerHour)
|
|
=> (int)Math.Clamp(Math.Ceiling(words * 60m / wordsPerHour), 5, int.MaxValue);
|
|
|
|
public static int StartingBudget(int? words)
|
|
=> words is > 0 ? Math.Clamp((int)Math.Ceiling(words.Value / 200d / 5) * 5, 5, 120) : 30;
|
|
|
|
private static IReadOnlyList<DayCapacity> Capacity(DateTime start, DateTime target,
|
|
IReadOnlyList<WritingDayAvailabilityViewModel> days, int contingency, IReadOnlyList<WriterPlanActivity> completed)
|
|
{
|
|
var length = (target.Date - start.Date).Days + 1;
|
|
if (length < 1) throw new ArgumentException("The target date is before the proposed start. Choose another start or explicitly edit the target date.");
|
|
if (length > 3651) throw new ArgumentException("Choose a planning window of at most ten years.");
|
|
|
|
var enabled = days.Where(day => day.IsAvailable).ToArray();
|
|
if (enabled.Length == 0 || enabled.Any(day =>
|
|
!Enum.TryParse<DayOfWeek>(day.Day, out var weekday) || weekday.ToString() != day.Day || day.Minutes is < 1 or > 720)
|
|
|| enabled.Select(day => day.Day).Distinct().Count() != enabled.Length)
|
|
throw new ArgumentException("Choose valid weekly writing availability.");
|
|
|
|
var availability = enabled.ToDictionary(day => Enum.Parse<DayOfWeek>(day.Day), day => day.Minutes);
|
|
var completedByDay = completed.GroupBy(item => item.ScheduledDate.Date)
|
|
.ToDictionary(group => group.Key, group => group.Sum(item => item.Minutes));
|
|
|
|
var dates = new List<DayCapacity>(length);
|
|
var totalAvailable = 0;
|
|
var totalReserved = 0;
|
|
for (var offset = 0; offset < length; offset++)
|
|
{
|
|
var date = start.Date.AddDays(offset);
|
|
var available = availability.GetValueOrDefault(date.DayOfWeek);
|
|
totalAvailable += available;
|
|
var targetReserve = (int)Math.Ceiling(totalAvailable * contingency / 100m);
|
|
var reserved = targetReserve - totalReserved;
|
|
totalReserved = targetReserve;
|
|
var occupied = Math.Min(available - reserved, completedByDay.GetValueOrDefault(date));
|
|
dates.Add(new(date, available, reserved, occupied));
|
|
}
|
|
return dates;
|
|
}
|
|
|
|
private static void ValidateBudgets(IReadOnlyList<WriterPlanActivity> work, int minimumBudget)
|
|
{
|
|
if (work.Any(item => item.Minutes < minimumBudget || item.Minutes > 1440))
|
|
throw new ArgumentException($"Enter a budget between {minimumBudget} and 1,440 minutes for each selected activity.");
|
|
if (work.Any(item => item.Kind is not ("Draft" or "Edit" or "Plan" or "Review" or "AudioReview")))
|
|
throw new ArgumentException("Choose a supported activity.");
|
|
}
|
|
|
|
private static List<WriterPlanActivity> Fit(IReadOnlyList<DayCapacity> dates, IReadOnlyList<WriterPlanActivity> work)
|
|
{
|
|
var usable = dates.Where(date => date.Usable > 0).ToArray();
|
|
var result = new List<WriterPlanActivity>();
|
|
var day = 0;
|
|
var used = 0;
|
|
foreach (var candidate in work)
|
|
{
|
|
var remaining = candidate.Minutes;
|
|
while (remaining > 0)
|
|
{
|
|
var capacity = usable[day];
|
|
var minutes = Math.Min(remaining, capacity.Usable - used);
|
|
result.Add(new()
|
|
{
|
|
BookID = candidate.BookID, ChapterID = candidate.ChapterID, SceneID = candidate.SceneID,
|
|
GroupID = candidate.GroupID, Title = candidate.Title, BookTitle = candidate.BookTitle,
|
|
Minutes = minutes, ScheduledDate = capacity.Date, Kind = candidate.Kind
|
|
});
|
|
remaining -= minutes;
|
|
used += minutes;
|
|
if (used == capacity.Usable)
|
|
{
|
|
day++;
|
|
used = 0;
|
|
}
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
}
|