Enhance staffing management in school simulation by introducing API endpoints for hiring staff and assigning subjects. Implement payroll cap validation to ensure hiring and subject assignments do not exceed the allocated budget. Update the simulation options to include a monthly payroll cap and revise related classes to support new staffing functionalities. Enhance documentation to reflect these changes and update tests to validate the new features.
This commit is contained in:
@@ -296,6 +296,16 @@ internal static class PeopleDefValidator
|
||||
{
|
||||
throw new ContentLoadException($"StaffingDef '{staffing.DefName}' wage scale cannot be negative.");
|
||||
}
|
||||
|
||||
if (staffing.BaseWeeklyHours <= 0f || staffing.WeeksPerMonth <= 0f)
|
||||
{
|
||||
throw new ContentLoadException($"StaffingDef '{staffing.DefName}' monthly hours must be positive.");
|
||||
}
|
||||
|
||||
if (staffing.ExtraSubjectSurcharge < 0f)
|
||||
{
|
||||
throw new ContentLoadException($"StaffingDef '{staffing.DefName}' extraSubjectSurcharge cannot be negative.");
|
||||
}
|
||||
}
|
||||
|
||||
private static void ValidateNameSet(NameSetDef names)
|
||||
|
||||
@@ -175,6 +175,17 @@ public sealed class StaffingDef : Def
|
||||
public float HourlyWageBase { get; init; }
|
||||
|
||||
public float HourlyWagePerSkill { get; init; }
|
||||
|
||||
/// <summary>Hours assumed in the monthly base rate. Extra subjects add a surcharge on top.</summary>
|
||||
public float BaseWeeklyHours { get; init; }
|
||||
|
||||
public float WeeksPerMonth { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// Added to the monthly base for each subject after the first. <c>0.25</c> means a second
|
||||
/// subject costs 25% of the base rate.
|
||||
/// </summary>
|
||||
public float ExtraSubjectSurcharge { get; init; }
|
||||
}
|
||||
|
||||
public enum BodyAttributeKind
|
||||
|
||||
@@ -48,6 +48,14 @@ public sealed record ApplicantPool(int Week, int NextIndex, IReadOnlyList<Applic
|
||||
return pool;
|
||||
}
|
||||
|
||||
public ApplicantPool Without(string personId) =>
|
||||
this with
|
||||
{
|
||||
Applicants = Applicants
|
||||
.Where(applicant => !applicant.Person.Id.Equals(personId, StringComparison.Ordinal))
|
||||
.ToArray(),
|
||||
};
|
||||
|
||||
public static float HourlyAsk(DefCatalog catalog, Person person)
|
||||
{
|
||||
var rules = catalog.StaffingRules
|
||||
|
||||
@@ -40,6 +40,12 @@ public sealed record Person
|
||||
|
||||
public required IReadOnlyDictionary<string, float> Needs { get; init; }
|
||||
|
||||
/// <summary>SubjectDef names this staff member is assigned. Empty for everyone else.</summary>
|
||||
public IReadOnlyList<string> Subjects { get; init; } = [];
|
||||
|
||||
/// <summary>Hourly rate frozen at hire. Null until the person is staff.</summary>
|
||||
public float? HourlyWageAsk { get; init; }
|
||||
|
||||
public int AgeOn(DateTime asOf) => SchoolYears.AgeYears(BirthDate, asOf);
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,357 @@
|
||||
using HSchool.Content;
|
||||
|
||||
namespace HSchool.People;
|
||||
|
||||
public enum StaffingError
|
||||
{
|
||||
None,
|
||||
UnknownSchool,
|
||||
UnknownApplicant,
|
||||
AlreadyHired,
|
||||
UnknownPosition,
|
||||
UnknownSubject,
|
||||
NotStaff,
|
||||
NotTeacher,
|
||||
AlreadyAssigned,
|
||||
SubjectNotAssigned,
|
||||
PayrollExceeded,
|
||||
NoOpening,
|
||||
}
|
||||
|
||||
public sealed record StaffingOutcome(
|
||||
StaffingError Error,
|
||||
Roster Roster,
|
||||
ApplicantPool Pool,
|
||||
float Allocated,
|
||||
float Payroll,
|
||||
float Remaining,
|
||||
float Attempted);
|
||||
|
||||
/// <summary>
|
||||
/// Hire, subject assignment and the monthly payroll cap. Scale lives on <see cref="StaffingDef"/>;
|
||||
/// the cap is supplied by the caller because it is a simulation option, not catalog data.
|
||||
/// </summary>
|
||||
public static class Staffing
|
||||
{
|
||||
public const string TeacherPosition = "Teacher";
|
||||
|
||||
public static StaffingOutcome UnknownSchool() =>
|
||||
Fail(StaffingError.UnknownSchool, new Roster([], [], []), ApplicantPool.Empty, allocated: 0, payroll: 0, attempted: 0);
|
||||
|
||||
public static float MonthlyBase(StaffingDef rules, float hourlyAsk) =>
|
||||
Round(hourlyAsk * rules.BaseWeeklyHours * rules.WeeksPerMonth);
|
||||
|
||||
public static float MonthlyPay(StaffingDef rules, float hourlyAsk, int subjectCount)
|
||||
{
|
||||
var extras = Math.Max(0, subjectCount - 1);
|
||||
return Round(MonthlyBase(rules, hourlyAsk) * (1f + (rules.ExtraSubjectSurcharge * extras)));
|
||||
}
|
||||
|
||||
public static float Payroll(StaffingDef rules, IEnumerable<Person> people)
|
||||
{
|
||||
var total = 0f;
|
||||
foreach (var person in people)
|
||||
{
|
||||
if (!person.IsStaff || person.HourlyWageAsk is not { } ask)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
total += MonthlyPay(rules, ask, person.Subjects.Count);
|
||||
}
|
||||
|
||||
return Round(total);
|
||||
}
|
||||
|
||||
public static IReadOnlyList<SubjectDef> Uncovered(DefCatalog catalog, Roster roster)
|
||||
{
|
||||
var years = new HashSet<int>();
|
||||
foreach (var schoolClass in roster.Classes)
|
||||
{
|
||||
years.Add(schoolClass.Year);
|
||||
}
|
||||
|
||||
var taught = new HashSet<string>(StringComparer.Ordinal);
|
||||
foreach (var person in roster.People)
|
||||
{
|
||||
if (!person.IsStaff)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
foreach (var subject in person.Subjects)
|
||||
{
|
||||
taught.Add(subject);
|
||||
}
|
||||
}
|
||||
|
||||
var uncovered = new List<SubjectDef>();
|
||||
foreach (var subject in catalog.Subjects.Values)
|
||||
{
|
||||
if (subject.Abstract || !TouchesYears(subject, years) || taught.Contains(subject.DefName))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
uncovered.Add(subject);
|
||||
}
|
||||
|
||||
return uncovered;
|
||||
}
|
||||
|
||||
public static StaffingOutcome Hire(
|
||||
DefCatalog catalog,
|
||||
MapLayout? map,
|
||||
Roster roster,
|
||||
ApplicantPool pool,
|
||||
string personId,
|
||||
string position,
|
||||
float allocated)
|
||||
{
|
||||
var rules = RequireRules(catalog);
|
||||
var payroll = Payroll(rules, roster.People);
|
||||
|
||||
if (roster.People.Any(person => person.Id.Equals(personId, StringComparison.Ordinal) && person.IsStaff))
|
||||
{
|
||||
return Fail(StaffingError.AlreadyHired, roster, pool, allocated, payroll, payroll);
|
||||
}
|
||||
|
||||
var applicant = pool.Applicants.FirstOrDefault(candidate => candidate.Person.Id.Equals(personId, StringComparison.Ordinal));
|
||||
if (applicant is null)
|
||||
{
|
||||
return Fail(StaffingError.UnknownApplicant, roster, pool, allocated, payroll, payroll);
|
||||
}
|
||||
|
||||
if (!IsConcretePosition(catalog, position))
|
||||
{
|
||||
return Fail(StaffingError.UnknownPosition, roster, pool, allocated, payroll, payroll);
|
||||
}
|
||||
|
||||
if (!TryWorkplace(catalog, map, roster, position, out var workplace))
|
||||
{
|
||||
return Fail(StaffingError.NoOpening, roster, pool, allocated, payroll, payroll);
|
||||
}
|
||||
|
||||
var pay = MonthlyPay(rules, applicant.HourlyWageAsk, subjectCount: 0);
|
||||
var attempted = Round(payroll + pay);
|
||||
if (attempted > allocated)
|
||||
{
|
||||
return Fail(StaffingError.PayrollExceeded, roster, pool, allocated, payroll, attempted);
|
||||
}
|
||||
|
||||
var hired = applicant.Person with
|
||||
{
|
||||
IsStaff = true,
|
||||
Position = position,
|
||||
WorkplaceRoomId = workplace,
|
||||
HourlyWageAsk = applicant.HourlyWageAsk,
|
||||
Subjects = [],
|
||||
};
|
||||
|
||||
var nextRoster = UpsertStaff(roster, hired);
|
||||
var nextPool = pool.Without(personId);
|
||||
return Ok(nextRoster, nextPool, allocated, Payroll(rules, nextRoster.People));
|
||||
}
|
||||
|
||||
public static StaffingOutcome AssignSubject(
|
||||
DefCatalog catalog,
|
||||
Roster roster,
|
||||
ApplicantPool pool,
|
||||
string personId,
|
||||
string subject,
|
||||
float allocated)
|
||||
{
|
||||
var rules = RequireRules(catalog);
|
||||
var payroll = Payroll(rules, roster.People);
|
||||
var index = IndexOf(roster, personId);
|
||||
if (index < 0)
|
||||
{
|
||||
return Fail(StaffingError.NotStaff, roster, pool, allocated, payroll, payroll);
|
||||
}
|
||||
|
||||
var person = roster.People[index];
|
||||
if (!person.IsStaff)
|
||||
{
|
||||
return Fail(StaffingError.NotStaff, roster, pool, allocated, payroll, payroll);
|
||||
}
|
||||
|
||||
if (!TeacherPosition.Equals(person.Position, StringComparison.Ordinal))
|
||||
{
|
||||
return Fail(StaffingError.NotTeacher, roster, pool, allocated, payroll, payroll);
|
||||
}
|
||||
|
||||
if (!IsConcreteSubject(catalog, subject))
|
||||
{
|
||||
return Fail(StaffingError.UnknownSubject, roster, pool, allocated, payroll, payroll);
|
||||
}
|
||||
|
||||
if (person.Subjects.Contains(subject, StringComparer.Ordinal))
|
||||
{
|
||||
return Fail(StaffingError.AlreadyAssigned, roster, pool, allocated, payroll, payroll);
|
||||
}
|
||||
|
||||
var ask = person.HourlyWageAsk ?? ApplicantPool.HourlyAsk(catalog, person);
|
||||
var nextSubjects = person.Subjects.Append(subject).ToArray();
|
||||
var currentPay = person.HourlyWageAsk is float currentAsk
|
||||
? MonthlyPay(rules, currentAsk, person.Subjects.Count)
|
||||
: MonthlyPay(rules, ask, person.Subjects.Count);
|
||||
var nextPay = MonthlyPay(rules, ask, nextSubjects.Length);
|
||||
var attempted = Round(payroll - currentPay + nextPay);
|
||||
if (attempted > allocated)
|
||||
{
|
||||
return Fail(StaffingError.PayrollExceeded, roster, pool, allocated, payroll, attempted);
|
||||
}
|
||||
|
||||
var nextRoster = Replace(roster, index, person with { HourlyWageAsk = ask, Subjects = nextSubjects });
|
||||
return Ok(nextRoster, pool, allocated, Payroll(rules, nextRoster.People));
|
||||
}
|
||||
|
||||
public static StaffingOutcome UnassignSubject(
|
||||
DefCatalog catalog,
|
||||
Roster roster,
|
||||
ApplicantPool pool,
|
||||
string personId,
|
||||
string subject,
|
||||
float allocated)
|
||||
{
|
||||
var rules = RequireRules(catalog);
|
||||
var payroll = Payroll(rules, roster.People);
|
||||
var index = IndexOf(roster, personId);
|
||||
if (index < 0)
|
||||
{
|
||||
return Fail(StaffingError.NotStaff, roster, pool, allocated, payroll, payroll);
|
||||
}
|
||||
|
||||
var person = roster.People[index];
|
||||
if (!person.IsStaff)
|
||||
{
|
||||
return Fail(StaffingError.NotStaff, roster, pool, allocated, payroll, payroll);
|
||||
}
|
||||
|
||||
if (!person.Subjects.Contains(subject, StringComparer.Ordinal))
|
||||
{
|
||||
return Fail(StaffingError.SubjectNotAssigned, roster, pool, allocated, payroll, payroll);
|
||||
}
|
||||
|
||||
var nextSubjects = person.Subjects.Where(name => !name.Equals(subject, StringComparison.Ordinal)).ToArray();
|
||||
var nextRoster = Replace(roster, index, person with { Subjects = nextSubjects });
|
||||
return Ok(nextRoster, pool, allocated, Payroll(rules, nextRoster.People));
|
||||
}
|
||||
|
||||
private static StaffingOutcome Ok(Roster roster, ApplicantPool pool, float allocated, float payroll) =>
|
||||
new(StaffingError.None, roster, pool, allocated, payroll, Remaining(allocated, payroll), payroll);
|
||||
|
||||
private static StaffingOutcome Fail(
|
||||
StaffingError error,
|
||||
Roster roster,
|
||||
ApplicantPool pool,
|
||||
float allocated,
|
||||
float payroll,
|
||||
float attempted) =>
|
||||
new(error, roster, pool, allocated, payroll, Remaining(allocated, payroll), attempted);
|
||||
|
||||
private static float Remaining(float allocated, float payroll) => Round(MathF.Max(0f, allocated - payroll));
|
||||
|
||||
private static float Round(float value) => MathF.Round(value, 2);
|
||||
|
||||
private static StaffingDef RequireRules(DefCatalog catalog) =>
|
||||
catalog.StaffingRules ?? throw new InvalidOperationException("The catalog has no StaffingDef.");
|
||||
|
||||
private static bool IsConcretePosition(DefCatalog catalog, string position) =>
|
||||
catalog.Positions.TryGetValue(position, out var def) && !def.Abstract;
|
||||
|
||||
private static bool IsConcreteSubject(DefCatalog catalog, string subject) =>
|
||||
catalog.Subjects.TryGetValue(subject, out var def) && !def.Abstract;
|
||||
|
||||
private static bool TouchesYears(SubjectDef subject, HashSet<int> years)
|
||||
{
|
||||
for (var year = subject.Grades.Min; year <= subject.Grades.Max; year++)
|
||||
{
|
||||
if (years.Contains(year))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private static bool TryWorkplace(
|
||||
DefCatalog catalog,
|
||||
MapLayout? map,
|
||||
Roster roster,
|
||||
string position,
|
||||
out string? workplace)
|
||||
{
|
||||
workplace = null;
|
||||
if (TeacherPosition.Equals(position, StringComparison.Ordinal))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
if (map is null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
var taken = new HashSet<string>(StringComparer.Ordinal);
|
||||
foreach (var person in roster.People)
|
||||
{
|
||||
if (person.IsStaff
|
||||
&& position.Equals(person.Position, StringComparison.Ordinal)
|
||||
&& person.WorkplaceRoomId is { } roomId)
|
||||
{
|
||||
taken.Add(roomId);
|
||||
}
|
||||
}
|
||||
|
||||
foreach (var opening in SchoolDemand.From(catalog, map).Staff)
|
||||
{
|
||||
if (opening.Position.Equals(position, StringComparison.Ordinal) && taken.Add(opening.RoomId))
|
||||
{
|
||||
workplace = opening.RoomId;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private static int IndexOf(Roster roster, string personId)
|
||||
{
|
||||
for (var i = 0; i < roster.People.Count; i++)
|
||||
{
|
||||
if (roster.People[i].Id.Equals(personId, StringComparison.Ordinal))
|
||||
{
|
||||
return i;
|
||||
}
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
private static Roster Replace(Roster roster, int index, Person person)
|
||||
{
|
||||
var people = roster.People.ToArray();
|
||||
people[index] = person;
|
||||
return roster with { People = people };
|
||||
}
|
||||
|
||||
private static Roster UpsertStaff(Roster roster, Person hired)
|
||||
{
|
||||
var index = IndexOf(roster, hired.Id);
|
||||
if (index >= 0)
|
||||
{
|
||||
return Replace(roster, index, hired);
|
||||
}
|
||||
|
||||
var people = roster.People.Append(hired).ToArray();
|
||||
if (roster.Families.Any(family => family.Id.Equals(hired.FamilyId, StringComparison.Ordinal)))
|
||||
{
|
||||
return roster with { People = people };
|
||||
}
|
||||
|
||||
var families = roster.Families.Append(new Family(hired.FamilyId, [hired.Id], [])).ToArray();
|
||||
return roster with { People = people, Families = families };
|
||||
}
|
||||
}
|
||||
@@ -169,3 +169,123 @@ internal static class PeopleListMapper
|
||||
return new PeopleFilterOptionsResponse(years, letters, positions);
|
||||
}
|
||||
}
|
||||
|
||||
internal sealed record HireStaffRequest(string? PersonId, string? Position);
|
||||
|
||||
internal sealed record AssignSubjectRequest(string? Subject);
|
||||
|
||||
internal sealed record StaffingResponse(
|
||||
float Allocated,
|
||||
float Payroll,
|
||||
float Remaining,
|
||||
IReadOnlyList<UncoveredSubjectResponse> Uncovered,
|
||||
IReadOnlyList<ApplicantResponse> Applicants,
|
||||
IReadOnlyList<StaffMemberResponse> Staff);
|
||||
|
||||
internal sealed record UncoveredSubjectResponse(
|
||||
string DefName,
|
||||
string Label,
|
||||
int GradeMin,
|
||||
int GradeMax,
|
||||
int HoursPerWeek);
|
||||
|
||||
internal sealed record ApplicantResponse(
|
||||
string Id,
|
||||
string FullName,
|
||||
bool Female,
|
||||
int Age,
|
||||
bool IsParent,
|
||||
float HourlyWageAsk,
|
||||
float MonthlyBase);
|
||||
|
||||
internal sealed record StaffMemberResponse(
|
||||
string Id,
|
||||
string FullName,
|
||||
bool Female,
|
||||
int Age,
|
||||
bool IsParent,
|
||||
string Position,
|
||||
string PositionLabel,
|
||||
float HourlyWageAsk,
|
||||
float MonthlyPay,
|
||||
IReadOnlyList<DefLabelResponse> Subjects);
|
||||
|
||||
internal static class StaffingMapper
|
||||
{
|
||||
public static StaffingResponse From(
|
||||
Roster? roster,
|
||||
ApplicantPool? pool,
|
||||
DefCatalog? catalog,
|
||||
DateTime asOf,
|
||||
float allocated,
|
||||
string locale)
|
||||
{
|
||||
roster ??= new Roster([], [], []);
|
||||
pool ??= ApplicantPool.Empty;
|
||||
var rules = catalog?.StaffingRules;
|
||||
var payroll = rules is null ? 0f : Staffing.Payroll(rules, roster.People);
|
||||
var remaining = MathF.Round(MathF.Max(0f, allocated - payroll), 2);
|
||||
|
||||
var uncovered = catalog is null
|
||||
? Array.Empty<UncoveredSubjectResponse>()
|
||||
: Staffing.Uncovered(catalog, roster)
|
||||
.Select(subject => new UncoveredSubjectResponse(
|
||||
subject.DefName,
|
||||
catalog.Label(locale, subject),
|
||||
subject.Grades.Min,
|
||||
subject.Grades.Max,
|
||||
subject.HoursPerWeek))
|
||||
.ToArray();
|
||||
|
||||
var applicants = pool.Applicants
|
||||
.Select(applicant => new ApplicantResponse(
|
||||
applicant.Person.Id,
|
||||
applicant.Person.Name.Full,
|
||||
applicant.Person.Female,
|
||||
applicant.Person.AgeOn(asOf),
|
||||
applicant.Person.IsParent,
|
||||
applicant.HourlyWageAsk,
|
||||
rules is null ? 0f : Staffing.MonthlyBase(rules, applicant.HourlyWageAsk)))
|
||||
.ToArray();
|
||||
|
||||
var staff = roster.People
|
||||
.Where(person => person.IsStaff)
|
||||
.OrderBy(person => person.Name.Surname, StringComparer.Ordinal)
|
||||
.ThenBy(person => person.Id, StringComparer.Ordinal)
|
||||
.Select(person => Member(person, catalog, rules, locale, asOf))
|
||||
.ToArray();
|
||||
|
||||
return new StaffingResponse(allocated, payroll, remaining, uncovered, applicants, staff);
|
||||
}
|
||||
|
||||
private static StaffMemberResponse Member(
|
||||
Person person,
|
||||
DefCatalog? catalog,
|
||||
StaffingDef? rules,
|
||||
string locale,
|
||||
DateTime asOf)
|
||||
{
|
||||
var ask = person.HourlyWageAsk ?? 0f;
|
||||
var pay = rules is null ? 0f : Staffing.MonthlyPay(rules, ask, person.Subjects.Count);
|
||||
var subjects = person.Subjects
|
||||
.Select(name => new DefLabelResponse(
|
||||
name,
|
||||
catalog is not null && catalog.Subjects.TryGetValue(name, out var def)
|
||||
? catalog.Label(locale, def)
|
||||
: name))
|
||||
.ToArray();
|
||||
|
||||
return new StaffMemberResponse(
|
||||
person.Id,
|
||||
person.Name.Full,
|
||||
person.Female,
|
||||
person.AgeOn(asOf),
|
||||
person.IsParent,
|
||||
person.Position ?? string.Empty,
|
||||
PeopleListMapper.PositionLabel(catalog, locale, person.Position) ?? person.Position ?? string.Empty,
|
||||
ask,
|
||||
pay,
|
||||
subjects);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -6,8 +6,8 @@ using HSchool.Simulation;
|
||||
namespace HSchool.Server.Api;
|
||||
|
||||
/// <summary>
|
||||
/// The main menu talks to these: list, create, delete. The people list reads a published roster
|
||||
/// snapshot; the person card goes through the school's mailbox because needs are live.
|
||||
/// The main menu talks to these: list, create, delete. People list and staffing read published
|
||||
/// snapshots; the person card, hire and subject changes go through the school's mailbox.
|
||||
/// </summary>
|
||||
internal static class SchoolEndpoints
|
||||
{
|
||||
@@ -156,6 +156,86 @@ internal static class SchoolEndpoints
|
||||
};
|
||||
})
|
||||
.WithName("GetSchoolPerson");
|
||||
|
||||
schools.MapGet("/{id:int}/staffing", (
|
||||
int id,
|
||||
string? lang,
|
||||
GameLoopService loop) =>
|
||||
{
|
||||
var published = loop.FindPeople(id);
|
||||
if (published is null)
|
||||
{
|
||||
return Problem(StatusCodes.Status404NotFound, "unknown-school", "That school does not exist.");
|
||||
}
|
||||
|
||||
return Results.Ok(MapStaffing(published, loop.Options.MonthlyPayrollCap, ParseLocale(lang)));
|
||||
})
|
||||
.WithName("GetSchoolStaffing");
|
||||
|
||||
schools.MapPost("/{id:int}/staff/hire", async (
|
||||
int id,
|
||||
HireStaffRequest request,
|
||||
string? lang,
|
||||
GameCommandQueue commands,
|
||||
GameLoopService loop,
|
||||
CancellationToken cancellationToken) =>
|
||||
{
|
||||
if (!TryPersonId(request.PersonId, out var personId, out var error)
|
||||
|| !TryDefName(request.Position, "position", out var position, out error))
|
||||
{
|
||||
return Problem(StatusCodes.Status400BadRequest, "invalid-query", error);
|
||||
}
|
||||
|
||||
var command = new GameCommand.HireStaff(id, personId, position, NewCompletion<StaffingOutcome>());
|
||||
commands.Enqueue(command);
|
||||
var outcome = await command.Result.Task.WaitAsync(CommandTimeout, cancellationToken);
|
||||
return StaffingResult(id, outcome, loop, ParseLocale(lang));
|
||||
})
|
||||
.WithName("HireSchoolStaff");
|
||||
|
||||
schools.MapPost("/{id:int}/staff/{personId}/subjects", async (
|
||||
int id,
|
||||
string personId,
|
||||
AssignSubjectRequest request,
|
||||
string? lang,
|
||||
GameCommandQueue commands,
|
||||
GameLoopService loop,
|
||||
CancellationToken cancellationToken) =>
|
||||
{
|
||||
if (!TryPersonId(personId, out var idValue, out var error)
|
||||
|| !TryDefName(request.Subject, "subject", out var subject, out error))
|
||||
{
|
||||
return Problem(StatusCodes.Status400BadRequest, "invalid-query", error);
|
||||
}
|
||||
|
||||
var command = new GameCommand.AssignSubject(id, idValue, subject, NewCompletion<StaffingOutcome>());
|
||||
commands.Enqueue(command);
|
||||
var outcome = await command.Result.Task.WaitAsync(CommandTimeout, cancellationToken);
|
||||
return StaffingResult(id, outcome, loop, ParseLocale(lang));
|
||||
})
|
||||
.WithName("AssignSchoolSubject");
|
||||
|
||||
schools.MapDelete("/{id:int}/staff/{personId}/subjects/{subject}", async (
|
||||
int id,
|
||||
string personId,
|
||||
string subject,
|
||||
string? lang,
|
||||
GameCommandQueue commands,
|
||||
GameLoopService loop,
|
||||
CancellationToken cancellationToken) =>
|
||||
{
|
||||
if (!TryPersonId(personId, out var idValue, out var error)
|
||||
|| !TryDefName(subject, "subject", out var subjectName, out error))
|
||||
{
|
||||
return Problem(StatusCodes.Status400BadRequest, "invalid-query", error);
|
||||
}
|
||||
|
||||
var command = new GameCommand.UnassignSubject(id, idValue, subjectName, NewCompletion<StaffingOutcome>());
|
||||
commands.Enqueue(command);
|
||||
var outcome = await command.Result.Task.WaitAsync(CommandTimeout, cancellationToken);
|
||||
return StaffingResult(id, outcome, loop, ParseLocale(lang));
|
||||
})
|
||||
.WithName("UnassignSchoolSubject");
|
||||
}
|
||||
|
||||
/// <summary>The supervisor must never be blocked by a continuation of a waiting request.</summary>
|
||||
@@ -279,11 +359,100 @@ internal static class SchoolEndpoints
|
||||
return true;
|
||||
}
|
||||
|
||||
private static IResult Problem(int statusCode, string code, string detail) =>
|
||||
Results.Problem(detail: detail, statusCode: statusCode, title: code, extensions: new Dictionary<string, object?>
|
||||
private static StaffingResponse MapStaffing(PublishedSchoolPeople published, float allocated, string locale) =>
|
||||
StaffingMapper.From(
|
||||
published.Roster,
|
||||
published.Applicants,
|
||||
published.Catalog,
|
||||
published.School.GameTime,
|
||||
allocated,
|
||||
locale);
|
||||
|
||||
private static IResult StaffingResult(int schoolId, StaffingOutcome outcome, GameLoopService loop, string locale)
|
||||
{
|
||||
if (outcome.Error != StaffingError.None)
|
||||
{
|
||||
["code"] = code,
|
||||
});
|
||||
return StaffingProblem(outcome);
|
||||
}
|
||||
|
||||
var published = loop.FindPeople(schoolId);
|
||||
if (published is null)
|
||||
{
|
||||
return Problem(StatusCodes.Status404NotFound, "unknown-school", "That school does not exist.");
|
||||
}
|
||||
|
||||
return Results.Ok(MapStaffing(published, loop.Options.MonthlyPayrollCap, locale));
|
||||
}
|
||||
|
||||
private static IResult StaffingProblem(StaffingOutcome outcome) =>
|
||||
outcome.Error switch
|
||||
{
|
||||
StaffingError.UnknownApplicant =>
|
||||
Problem(StatusCodes.Status404NotFound, "unknown-applicant", "That person is not in the applicant pool."),
|
||||
StaffingError.AlreadyHired =>
|
||||
Problem(StatusCodes.Status409Conflict, "already-hired", "That person is already on staff."),
|
||||
StaffingError.UnknownPosition =>
|
||||
Problem(StatusCodes.Status400BadRequest, "unknown-position", "That position is not in the catalog."),
|
||||
StaffingError.UnknownSubject =>
|
||||
Problem(StatusCodes.Status400BadRequest, "unknown-subject", "That subject is not in the catalog."),
|
||||
StaffingError.NotStaff =>
|
||||
Problem(StatusCodes.Status400BadRequest, "not-staff", "That person is not on staff."),
|
||||
StaffingError.NotTeacher =>
|
||||
Problem(StatusCodes.Status400BadRequest, "not-teacher", "Only a teacher can be assigned a subject."),
|
||||
StaffingError.AlreadyAssigned =>
|
||||
Problem(StatusCodes.Status409Conflict, "already-assigned", "That subject is already assigned to this person."),
|
||||
StaffingError.SubjectNotAssigned =>
|
||||
Problem(StatusCodes.Status404NotFound, "unknown-assignment", "That subject is not assigned to this person."),
|
||||
StaffingError.PayrollExceeded =>
|
||||
Problem(
|
||||
StatusCodes.Status409Conflict,
|
||||
"payroll-exceeded",
|
||||
$"That change would take payroll from {outcome.Payroll} to {outcome.Attempted} against an allocation of {outcome.Allocated}.",
|
||||
outcome),
|
||||
StaffingError.NoOpening =>
|
||||
Problem(StatusCodes.Status409Conflict, "no-opening", "There is no free opening for that position."),
|
||||
_ => Problem(StatusCodes.Status404NotFound, "unknown-school", "That school does not exist."),
|
||||
};
|
||||
|
||||
private static bool TryPersonId(string? value, out string personId, out string error)
|
||||
{
|
||||
personId = value?.Trim() ?? string.Empty;
|
||||
if (personId.Length is < 1 or > 64)
|
||||
{
|
||||
error = "The person id is not valid.";
|
||||
return false;
|
||||
}
|
||||
|
||||
error = string.Empty;
|
||||
return true;
|
||||
}
|
||||
|
||||
private static bool TryDefName(string? value, string field, out string name, out string error)
|
||||
{
|
||||
name = value?.Trim() ?? string.Empty;
|
||||
if (name.Length is < 1 or > 64)
|
||||
{
|
||||
error = $"{field} is not valid.";
|
||||
return false;
|
||||
}
|
||||
|
||||
error = string.Empty;
|
||||
return true;
|
||||
}
|
||||
|
||||
private static IResult Problem(int statusCode, string code, string detail, StaffingOutcome? staffing = null)
|
||||
{
|
||||
var extensions = new Dictionary<string, object?> { ["code"] = code };
|
||||
if (staffing is not null)
|
||||
{
|
||||
extensions["allocated"] = staffing.Allocated;
|
||||
extensions["payroll"] = staffing.Payroll;
|
||||
extensions["remaining"] = staffing.Remaining;
|
||||
extensions["attempted"] = staffing.Attempted;
|
||||
}
|
||||
|
||||
return Results.Problem(detail: detail, statusCode: statusCode, title: code, extensions: extensions);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Body of <c>POST /api/schools</c>. The start date is a game calendar date, not a real one.</summary>
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using HSchool.Content;
|
||||
using HSchool.People;
|
||||
using HSchool.Server.Api;
|
||||
using HSchool.Simulation;
|
||||
|
||||
@@ -50,4 +51,22 @@ internal abstract record GameCommand
|
||||
string PersonId,
|
||||
string Locale,
|
||||
TaskCompletionSource<PersonCardResult> Result) : GameCommand;
|
||||
|
||||
internal sealed record HireStaff(
|
||||
int SchoolId,
|
||||
string PersonId,
|
||||
string Position,
|
||||
TaskCompletionSource<StaffingOutcome> Result) : GameCommand;
|
||||
|
||||
internal sealed record AssignSubject(
|
||||
int SchoolId,
|
||||
string PersonId,
|
||||
string Subject,
|
||||
TaskCompletionSource<StaffingOutcome> Result) : GameCommand;
|
||||
|
||||
internal sealed record UnassignSubject(
|
||||
int SchoolId,
|
||||
string PersonId,
|
||||
string Subject,
|
||||
TaskCompletionSource<StaffingOutcome> Result) : GameCommand;
|
||||
}
|
||||
|
||||
@@ -171,6 +171,27 @@ internal sealed class GameLoopService(
|
||||
case GameCommand.GetPerson getPerson:
|
||||
HandleGetPerson(getPerson);
|
||||
break;
|
||||
|
||||
case GameCommand.HireStaff hire:
|
||||
HandleStaffing(
|
||||
hire.SchoolId,
|
||||
new WorkerCommand.HireStaff(hire.PersonId, hire.Position, hire.Result),
|
||||
hire.Result);
|
||||
break;
|
||||
|
||||
case GameCommand.AssignSubject assign:
|
||||
HandleStaffing(
|
||||
assign.SchoolId,
|
||||
new WorkerCommand.AssignSubject(assign.PersonId, assign.Subject, assign.Result),
|
||||
assign.Result);
|
||||
break;
|
||||
|
||||
case GameCommand.UnassignSubject unassign:
|
||||
HandleStaffing(
|
||||
unassign.SchoolId,
|
||||
new WorkerCommand.UnassignSubject(unassign.PersonId, unassign.Subject, unassign.Result),
|
||||
unassign.Result);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -183,6 +204,14 @@ internal sealed class GameLoopService(
|
||||
}
|
||||
}
|
||||
|
||||
private void HandleStaffing(int schoolId, WorkerCommand command, TaskCompletionSource<StaffingOutcome> result)
|
||||
{
|
||||
if (!_workers.TryGetValue(schoolId, out var worker) || !worker.Post(command))
|
||||
{
|
||||
result.TrySetResult(Staffing.UnknownSchool());
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A school's thread died. Drop it from the table so the menu stops drawing a card whose clock
|
||||
/// never moves again, and tell anybody watching it to go back to the menu. The save file stays
|
||||
|
||||
@@ -342,10 +342,7 @@ internal sealed class SchoolWorker
|
||||
{
|
||||
while (_mailbox.Reader.TryRead(out var orphan))
|
||||
{
|
||||
if (orphan is WorkerCommand.GetPerson getPerson)
|
||||
{
|
||||
getPerson.Result.TrySetResult(new PersonCardResult(null, PersonLookupError.UnknownSchool));
|
||||
}
|
||||
CompleteOrphan(orphan);
|
||||
}
|
||||
|
||||
return;
|
||||
@@ -393,14 +390,23 @@ internal sealed class SchoolWorker
|
||||
? new PersonCardResult(null, PersonLookupError.UnknownPerson)
|
||||
: new PersonCardResult(card, PersonLookupError.None));
|
||||
break;
|
||||
|
||||
case WorkerCommand.HireStaff hire:
|
||||
hire.Result.TrySetResult(ApplyHire(school, hire.PersonId, hire.Position));
|
||||
break;
|
||||
|
||||
case WorkerCommand.AssignSubject assign:
|
||||
assign.Result.TrySetResult(ApplyAssign(school, assign.PersonId, assign.Subject));
|
||||
break;
|
||||
|
||||
case WorkerCommand.UnassignSubject unassign:
|
||||
unassign.Result.TrySetResult(ApplyUnassign(school, unassign.PersonId, unassign.Subject));
|
||||
break;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
if (command is WorkerCommand.GetPerson failed)
|
||||
{
|
||||
failed.Result.TrySetException(ex);
|
||||
}
|
||||
FailCommand(command, ex);
|
||||
|
||||
_logger.LogError(
|
||||
ex,
|
||||
@@ -421,6 +427,76 @@ internal sealed class SchoolWorker
|
||||
}
|
||||
}
|
||||
|
||||
private static void CompleteOrphan(WorkerCommand command)
|
||||
{
|
||||
switch (command)
|
||||
{
|
||||
case WorkerCommand.GetPerson getPerson:
|
||||
getPerson.Result.TrySetResult(new PersonCardResult(null, PersonLookupError.UnknownSchool));
|
||||
break;
|
||||
case WorkerCommand.HireStaff hire:
|
||||
hire.Result.TrySetResult(Staffing.UnknownSchool());
|
||||
break;
|
||||
case WorkerCommand.AssignSubject assign:
|
||||
assign.Result.TrySetResult(Staffing.UnknownSchool());
|
||||
break;
|
||||
case WorkerCommand.UnassignSubject unassign:
|
||||
unassign.Result.TrySetResult(Staffing.UnknownSchool());
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private static void FailCommand(WorkerCommand command, Exception exception)
|
||||
{
|
||||
switch (command)
|
||||
{
|
||||
case WorkerCommand.GetPerson getPerson:
|
||||
getPerson.Result.TrySetException(exception);
|
||||
break;
|
||||
case WorkerCommand.HireStaff hire:
|
||||
hire.Result.TrySetException(exception);
|
||||
break;
|
||||
case WorkerCommand.AssignSubject assign:
|
||||
assign.Result.TrySetException(exception);
|
||||
break;
|
||||
case WorkerCommand.UnassignSubject unassign:
|
||||
unassign.Result.TrySetException(exception);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private StaffingOutcome ApplyHire(School school, string personId, string position) =>
|
||||
ApplyStaffingChange(school, (catalog, roster, pool) =>
|
||||
Staffing.Hire(catalog, school.Map, roster, pool, personId, position, _options.MonthlyPayrollCap));
|
||||
|
||||
private StaffingOutcome ApplyAssign(School school, string personId, string subject) =>
|
||||
ApplyStaffingChange(school, (catalog, roster, pool) =>
|
||||
Staffing.AssignSubject(catalog, roster, pool, personId, subject, _options.MonthlyPayrollCap));
|
||||
|
||||
private StaffingOutcome ApplyUnassign(School school, string personId, string subject) =>
|
||||
ApplyStaffingChange(school, (catalog, roster, pool) =>
|
||||
Staffing.UnassignSubject(catalog, roster, pool, personId, subject, _options.MonthlyPayrollCap));
|
||||
|
||||
private StaffingOutcome ApplyStaffingChange(
|
||||
School school,
|
||||
Func<DefCatalog, Roster, ApplicantPool, StaffingOutcome> apply)
|
||||
{
|
||||
if (school.Roster is null || school.Applicants is null || school.Catalog is null)
|
||||
{
|
||||
return Staffing.UnknownSchool();
|
||||
}
|
||||
|
||||
var outcome = apply(school.Catalog, school.Roster, school.Applicants);
|
||||
if (outcome.Error == StaffingError.None)
|
||||
{
|
||||
school.ApplyStaffing(outcome.Roster, outcome.Pool);
|
||||
PersistPeople();
|
||||
PublishSnapshot();
|
||||
}
|
||||
|
||||
return outcome;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Writes pause/speed changes, at most once per <see cref="SimulationOptions.MinSaveInterval"/>.
|
||||
/// A single click still lands within that window; a burst collapses into one write.
|
||||
@@ -459,7 +535,7 @@ internal sealed class SchoolWorker
|
||||
|
||||
/// <summary>
|
||||
/// Writes the composition file. Not called from the 30-second clock save — the roster and
|
||||
/// applicant pool change on create, weekly refresh and yearly intake, not every tick.
|
||||
/// applicant pool change on create, hire, weekly refresh and yearly intake, not every tick.
|
||||
/// </summary>
|
||||
private void PersistPeople()
|
||||
{
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
using HSchool.People;
|
||||
using HSchool.Server.Api;
|
||||
using HSchool.Server.Net;
|
||||
|
||||
@@ -21,4 +22,19 @@ internal abstract record WorkerCommand
|
||||
string PersonId,
|
||||
string Locale,
|
||||
TaskCompletionSource<PersonCardResult> Result) : WorkerCommand;
|
||||
|
||||
internal sealed record HireStaff(
|
||||
string PersonId,
|
||||
string Position,
|
||||
TaskCompletionSource<StaffingOutcome> Result) : WorkerCommand;
|
||||
|
||||
internal sealed record AssignSubject(
|
||||
string PersonId,
|
||||
string Subject,
|
||||
TaskCompletionSource<StaffingOutcome> Result) : WorkerCommand;
|
||||
|
||||
internal sealed record UnassignSubject(
|
||||
string PersonId,
|
||||
string Subject,
|
||||
TaskCompletionSource<StaffingOutcome> Result) : WorkerCommand;
|
||||
}
|
||||
|
||||
@@ -21,6 +21,7 @@ builder.Services
|
||||
.Validate(options => !string.IsNullOrWhiteSpace(options.SavesDirectory), "Simulation:SavesDirectory must be set.")
|
||||
.Validate(options => !string.IsNullOrWhiteSpace(options.ModsDirectory), "Simulation:ModsDirectory must be set.")
|
||||
.Validate(options => options.SaveIntervalSeconds is > 0 and <= 3600, "Simulation:SaveIntervalSeconds must be between 1 and 3600.")
|
||||
.Validate(options => options.MonthlyPayrollCap > 0, "Simulation:MonthlyPayrollCap must be positive.")
|
||||
.ValidateOnStart();
|
||||
|
||||
builder.Services.AddSingleton<GameCommandQueue>();
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
"DefaultStartDate": "2012-03-31T06:00:00",
|
||||
"SavesDirectory": "saves",
|
||||
"ModsDirectory": "mods",
|
||||
"SaveIntervalSeconds": 30
|
||||
"SaveIntervalSeconds": 30,
|
||||
"MonthlyPayrollCap": 10000
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,4 +5,7 @@
|
||||
"parentChance": 0.35,
|
||||
"hourlyWageBase": 30,
|
||||
"hourlyWagePerSkill": 0.6,
|
||||
"baseWeeklyHours": 20,
|
||||
"weeksPerMonth": 4,
|
||||
"extraSubjectSurcharge": 0.25,
|
||||
}
|
||||
|
||||
@@ -91,6 +91,21 @@ public sealed class School : IDisposable
|
||||
RosterSpawner.Spawn(World, roster);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Replaces the roster and applicant pool after hire or a subject change. The World is rebuilt
|
||||
/// so the people list and card match; needs reset to the roster snapshot, same as yearly intake.
|
||||
/// </summary>
|
||||
public void ApplyStaffing(Roster roster, ApplicantPool applicants)
|
||||
{
|
||||
ObjectDisposedException.ThrowIf(_disposed, this);
|
||||
ArgumentNullException.ThrowIfNull(roster);
|
||||
ArgumentNullException.ThrowIfNull(applicants);
|
||||
|
||||
Roster = roster;
|
||||
Applicants = applicants;
|
||||
RosterSpawner.Replace(World, roster);
|
||||
}
|
||||
|
||||
/// <summary>Runs one fixed step of the school: calendar, yearly intake, applicant refresh, then need decay.</summary>
|
||||
/// <returns><see langword="true"/> when the roster or the applicant pool changed this step.</returns>
|
||||
public bool Tick(double deltaTime, double gameMinutesPerRealSecond)
|
||||
|
||||
@@ -49,6 +49,12 @@ public sealed class SimulationOptions
|
||||
/// </summary>
|
||||
public int MinSaveIntervalMilliseconds { get; set; } = 1000;
|
||||
|
||||
/// <summary>
|
||||
/// Monthly payroll the player may commit. Hire and subject assignment that would exceed it
|
||||
/// are rejected immediately; money itself does not move.
|
||||
/// </summary>
|
||||
public float MonthlyPayrollCap { get; set; } = 10_000f;
|
||||
|
||||
/// <summary>Length of one fixed step.</summary>
|
||||
public double FixedDeltaTime => 1d / TickRate;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user