using Arch.Core; using HSchool.Ai; using HSchool.Content; using HSchool.People; using HSchool.Schedule; namespace HSchool.Simulation.Tests; public class LessonMarkSimulationTests { 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 StrongTeacher_MarkIsAtLeastEmptyRoom() { var (withTeacher, room, pupilId, teacherId) = StaffedMath(); using (withTeacher) { AdvanceTo(withTeacher, LessonStart); SetPlace(withTeacher, pupilId, room); SetPlace(withTeacher, teacherId, room); PlaceTextbook(withTeacher, pupilId, "Mathematics", ItemLocations.Bag); LessonLearningSystem.Apply(withTeacher, 45); var strong = MarkOf(withTeacher, pupilId); Assert.NotNull(strong); Assert.InRange(strong!.Value, 2, 5); var (empty, emptyRoom, emptyPupil, emptyTeacher) = StaffedMath(); using (empty) { AdvanceTo(empty, LessonStart); SetPlace(empty, emptyPupil, emptyRoom); SetPlace(empty, emptyTeacher, "restroom-1"); LessonLearningSystem.Apply(empty, 45); var absent = MarkOf(empty, emptyPupil); Assert.NotNull(absent); Assert.Equal(2, absent!.Value); Assert.True(strong.Value >= absent.Value); } } } [Fact] public void OneSlot_DoesNotWriteEveryMinute() { var (school, room, pupilId, teacherId) = StaffedMath(); using (school) { AdvanceTo(school, LessonStart); SetPlace(school, pupilId, room); SetPlace(school, teacherId, room); PlaceTextbook(school, pupilId, "Mathematics", ItemLocations.Bag); LessonLearningSystem.Apply(school, 5); LessonLearningSystem.Apply(school, 5); LessonLearningSystem.Apply(school, 5); var marks = school.Roster!.People.First(row => row.Id == pupilId).LessonMarks; Assert.NotNull(marks); Assert.Single(marks!); } } [Fact] public void NoTeacher_WritesConfiguredMark() { var (school, room, pupilId, teacherId) = StaffedMath(); using (school) { Assert.Equal(2, school.Catalog!.BehaviorRules!.LessonMarkWhenNoTeacher); AdvanceTo(school, LessonStart); SetPlace(school, pupilId, room); SetPlace(school, teacherId, "restroom-1"); LessonLearningSystem.Apply(school, 45); var mark = MarkOf(school, pupilId); Assert.NotNull(mark); Assert.Equal(2, mark!.Value); Assert.Equal("Mathematics", mark.Subject); } } private static LessonMarkRecord? MarkOf(School school, string pupilId) => school.Roster!.People.First(row => row.Id == pupilId).LessonMarks?.LastOrDefault(); private static (School School, string Homeroom, string PupilId, string TeacherId) StaffedMath( string? teacherId = null) { 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", teacherId ?? 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 PlaceTextbook(School school, string personId, string subject, string location) { var person = school.Roster!.People.First(row => row.Id.Equals(personId, StringComparison.Ordinal)); if (person.Items is not IList 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, location, subject)); } 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(); school.World.Query( in query, (ref PersonIdentity identity, ref Presence presence) => { if (identity.Id.Equals(personId, StringComparison.Ordinal)) { presence = new Presence(node, remaining, node, HeadingHome: false, path ?? []); } }); } private static (DefCatalog Catalog, MapLayout Map) Vanilla() { var root = Path.Combine(AppContext.BaseDirectory, "vanilla"); var documents = new List(); 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); } }