257 lines
9.4 KiB
C#
257 lines
9.4 KiB
C#
using Arch.Core;
|
|
using HSchool.Ai;
|
|
using HSchool.Content;
|
|
using HSchool.People;
|
|
using HSchool.Schedule;
|
|
|
|
namespace HSchool.Simulation.Tests;
|
|
|
|
public class AttendanceSimulationTests
|
|
{
|
|
private static readonly DateTime TuesdayMorning = new(2012, 4, 3, 6, 0, 0, DateTimeKind.Utc);
|
|
private static readonly DateTime LessonStart = new(2012, 4, 3, 8, 30, 0, DateTimeKind.Utc);
|
|
|
|
[Fact]
|
|
public void InRoomAtStart_IsPresent()
|
|
{
|
|
var (school, room, pupilId, _) = StaffedMath();
|
|
using (school)
|
|
{
|
|
AdvanceTo(school, LessonStart.AddMinutes(-1));
|
|
SetPlace(school, pupilId, room);
|
|
school.Clock.JumpTo(LessonStart);
|
|
AttendanceSystem.Apply(school);
|
|
|
|
var row = RowOf(school, pupilId);
|
|
Assert.NotNull(row);
|
|
Assert.Equal(AttendanceStatuses.Present, row!.Status);
|
|
Assert.Equal("Mathematics", row.Subject);
|
|
Assert.Equal(1, row.Period);
|
|
Assert.Null(row.AbsenceReason);
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public void ArrivesAfterThreshold_IsLate()
|
|
{
|
|
var (school, room, pupilId, _) = StaffedMath();
|
|
using (school)
|
|
{
|
|
Assert.Equal(5f, school.Catalog!.BehaviorRules!.LessonLateMinutes);
|
|
AdvanceTo(school, LessonStart.AddMinutes(-1));
|
|
SetPlace(school, pupilId, null);
|
|
school.Clock.JumpTo(LessonStart);
|
|
AttendanceSystem.Apply(school);
|
|
Assert.Null(RowOf(school, pupilId));
|
|
|
|
school.Clock.JumpTo(LessonStart.AddMinutes(6));
|
|
SetPlace(school, pupilId, room);
|
|
AttendanceSystem.Apply(school);
|
|
|
|
var row = RowOf(school, pupilId);
|
|
Assert.NotNull(row);
|
|
Assert.Equal(AttendanceStatuses.Late, row!.Status);
|
|
Assert.Null(row.AbsenceReason);
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public void OffCampusWholeSlot_IsAbsentTruancy()
|
|
{
|
|
var (school, _, pupilId, _) = StaffedMath();
|
|
using (school)
|
|
{
|
|
AdvanceTo(school, LessonStart.AddMinutes(-1));
|
|
SetPlace(school, pupilId, null);
|
|
school.Clock.JumpTo(LessonStart);
|
|
AttendanceSystem.Apply(school);
|
|
Assert.Null(RowOf(school, pupilId));
|
|
|
|
school.Clock.JumpTo(LessonStart.AddMinutes(45));
|
|
AttendanceSystem.Apply(school);
|
|
|
|
var row = RowOf(school, pupilId);
|
|
Assert.NotNull(row);
|
|
Assert.Equal(AttendanceStatuses.Absent, row!.Status);
|
|
Assert.Equal(AbsenceReasons.Truancy, row.AbsenceReason);
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public void OtherNodeWholeSlot_IsAbsent()
|
|
{
|
|
var (school, room, pupilId, _) = StaffedMath();
|
|
using (school)
|
|
{
|
|
AdvanceTo(school, LessonStart.AddMinutes(-1));
|
|
var other = room.Equals("classroom-101", StringComparison.Ordinal) ? "classroom-102" : "classroom-101";
|
|
SetPlace(school, pupilId, other);
|
|
school.Clock.JumpTo(LessonStart);
|
|
AttendanceSystem.Apply(school);
|
|
school.Clock.JumpTo(LessonStart.AddMinutes(45));
|
|
AttendanceSystem.Apply(school);
|
|
|
|
var row = RowOf(school, pupilId);
|
|
Assert.NotNull(row);
|
|
Assert.Equal(AttendanceStatuses.Absent, row!.Status);
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public void SameDayAndSeed_SameAttendance()
|
|
{
|
|
var first = CapturePresent();
|
|
var second = CapturePresent();
|
|
Assert.Equal(first.Status, second.Status);
|
|
Assert.Equal(first.Subject, second.Subject);
|
|
Assert.Equal(first.Period, second.Period);
|
|
}
|
|
|
|
[Fact]
|
|
public void Late_DoesNotCancelMarkWhenConfigured()
|
|
{
|
|
var (school, room, pupilId, teacherId) = StaffedMath();
|
|
using (school)
|
|
{
|
|
Assert.True(school.Catalog!.BehaviorRules!.LessonMarkWhenLate);
|
|
AdvanceTo(school, LessonStart.AddMinutes(-1));
|
|
SetIdle(school, pupilId);
|
|
SetIdle(school, teacherId);
|
|
school.Clock.JumpTo(LessonStart.AddMinutes(6));
|
|
SetPlace(school, pupilId, room);
|
|
SetPlace(school, teacherId, room);
|
|
PlaceTextbook(school, pupilId, "Mathematics");
|
|
LessonLearningSystem.Apply(school, 5);
|
|
AttendanceSystem.Apply(school);
|
|
|
|
Assert.Equal(AttendanceStatuses.Late, RowOf(school, pupilId)!.Status);
|
|
var marks = school.Roster!.People.First(row => row.Id == pupilId).LessonMarks;
|
|
Assert.NotNull(marks);
|
|
Assert.Single(marks!);
|
|
}
|
|
}
|
|
|
|
private static AttendanceRecord CapturePresent()
|
|
{
|
|
var (school, room, pupilId, _) = StaffedMath();
|
|
using (school)
|
|
{
|
|
AdvanceTo(school, LessonStart.AddMinutes(-1));
|
|
SetPlace(school, pupilId, room);
|
|
school.Clock.JumpTo(LessonStart);
|
|
AttendanceSystem.Apply(school);
|
|
var row = RowOf(school, pupilId);
|
|
Assert.NotNull(row);
|
|
return row!;
|
|
}
|
|
}
|
|
|
|
private static AttendanceRecord? RowOf(School school, string pupilId) =>
|
|
school.Roster!.People.First(row => row.Id == pupilId).Attendance?.LastOrDefault();
|
|
|
|
private static (School School, string Homeroom, string PupilId, string TeacherId) StaffedMath()
|
|
{
|
|
var (catalog, map) = Vanilla();
|
|
var roster = RosterGenerator.Generate(catalog, map, schoolSeed: 1, "Russia", TuesdayMorning);
|
|
var pool = ApplicantPool.Create(catalog, roster, schoolSeed: 1, "Russia", TuesdayMorning);
|
|
var hired = Staffing.Hire(catalog, map, roster, pool, pool.Applicants[0].Person.Id, Staffing.TeacherPosition, 1_000_000f);
|
|
Assert.Equal(StaffingError.None, hired.Error);
|
|
roster = hired.Roster;
|
|
pool = hired.Pool;
|
|
var hiredId = roster.People.First(person => person.IsStaff).Id;
|
|
var schoolClass = roster.Classes.First(row =>
|
|
row.RoomId is "classroom-101" or "classroom-102" or "classroom-103" or "classroom-104");
|
|
var school = School.Create(1, "Явка", TuesdayMorning, catalog, map);
|
|
school.InstallPeople(roster, seed: 1, "Russia", pool);
|
|
school.SetTimetable(new Timetable(
|
|
[new LessonPlacement(schoolClass.Id, "Mathematics", hiredId, schoolClass.RoomId, Day: 1, Period: 1)],
|
|
[]));
|
|
school.ConfigurePresence(weekDays: 5, maxDecisionsPerTick: 10_000);
|
|
var pupil = schoolClass.PupilIds
|
|
.Select(id => school.Roster!.People.First(person => person.Id == id))
|
|
.First(person => !person.Traits.Contains("Lazy"));
|
|
return (school, schoolClass.RoomId, pupil.Id, hiredId);
|
|
}
|
|
|
|
private static void AdvanceTo(School school, DateTime until)
|
|
{
|
|
while (school.Clock.Time < until)
|
|
{
|
|
school.Tick(0.2d, 5d);
|
|
}
|
|
}
|
|
|
|
private static void SetPlace(School school, string personId, string? node, string[]? path = null, float remaining = 0f)
|
|
{
|
|
var query = new QueryDescription().WithAll<PersonIdentity, Presence>();
|
|
school.World.Query(
|
|
in query,
|
|
(ref PersonIdentity identity, ref Presence presence) =>
|
|
{
|
|
if (!identity.Id.Equals(personId, StringComparison.Ordinal))
|
|
{
|
|
return;
|
|
}
|
|
|
|
presence = node is null
|
|
? Presence.OffCampus
|
|
: new Presence(node, remaining, node, HeadingHome: false, path ?? []);
|
|
});
|
|
}
|
|
|
|
private static void SetIdle(School school, string personId)
|
|
{
|
|
var query = new QueryDescription().WithAll<PersonIdentity, PersonActivity>();
|
|
school.World.Query(
|
|
in query,
|
|
(ref PersonIdentity identity, ref PersonActivity activity) =>
|
|
{
|
|
if (identity.Id.Equals(personId, StringComparison.Ordinal))
|
|
{
|
|
activity = PersonActivity.Idle;
|
|
}
|
|
});
|
|
}
|
|
|
|
private static void PlaceTextbook(School school, string personId, string subject)
|
|
{
|
|
var person = school.Roster!.People.First(row => row.Id.Equals(personId, StringComparison.Ordinal));
|
|
if (person.Items is not IList<InventoryItem> items || items.IsReadOnly)
|
|
{
|
|
throw new InvalidOperationException($"Cannot mutate items for {personId}.");
|
|
}
|
|
|
|
for (var i = items.Count - 1; i >= 0; i--)
|
|
{
|
|
if (items[i].Subject is not null)
|
|
{
|
|
items.RemoveAt(i);
|
|
}
|
|
}
|
|
|
|
items.Add(new InventoryItem("Textbook", Color: null, Condition: 1f, ItemLocations.Bag, subject));
|
|
}
|
|
|
|
private static (DefCatalog Catalog, MapLayout Map) Vanilla()
|
|
{
|
|
var root = Path.Combine(AppContext.BaseDirectory, "vanilla");
|
|
var documents = new List<ContentDocument>();
|
|
foreach (var path in Directory.EnumerateFiles(root, "*.*", SearchOption.AllDirectories))
|
|
{
|
|
if (!path.EndsWith(".jsonc", StringComparison.OrdinalIgnoreCase)
|
|
&& !path.EndsWith(".json", StringComparison.OrdinalIgnoreCase))
|
|
{
|
|
continue;
|
|
}
|
|
|
|
var relative = Path.GetRelativePath(root, path).Replace('\\', '/');
|
|
documents.Add(new ContentDocument(CatalogLoader.CorePackId, relative, File.ReadAllText(path)));
|
|
}
|
|
|
|
var catalog = new CatalogLoader().Load([CatalogLoader.CorePackId], documents);
|
|
var map = CatalogLoader.LastDefaultMap([CatalogLoader.CorePackId], documents);
|
|
Assert.NotNull(map);
|
|
return (catalog, map);
|
|
}
|
|
}
|