using Arch.Core; using HSchool.Ai; using HSchool.Content; using HSchool.People; using HSchool.Schedule; namespace HSchool.Simulation.Tests; public class DiseaseSimulationTests { 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 ColdWeather_RaisesRespiratoryOnsetVsWarmControl() { using var coldSchool = OpenStaffed(); using var warmSchool = OpenStaffed(); coldSchool.ForceWeather(new OutdoorWeather(-10f, Precipitation.Snow)); warmSchool.ForceWeather(new OutdoorWeather(18f, Precipitation.None)); var coldHits = CountRespiratoryOnset(coldSchool); var warmHits = CountRespiratoryOnset(warmSchool); Assert.True(coldHits > warmHits); Assert.True(coldHits > 0); } [Fact] public void HeavyInfluenza_MarksAbsenceAsIllness() { var (school, _, pupilId, _) = StaffedMath(); using (school) { var pupil = school.Roster!.People.First(row => row.Id == pupilId); HealthConditions.Add( pupil, new HealthCondition { DefName = "Influenza", Severity = 0.85f, Progress = 0.1f, Source = "cold", StartedAt = LessonStart.AddDays(-3), }); Assert.True(DiseaseEffects.StayHomeChance(pupil, school.Catalog!, LessonStart) >= 0.5f); school.PlanDay = null; AdvanceTo(school, LessonStart.AddMinutes(-1)); SetPlace(school, pupilId, null); school.Clock.JumpTo(LessonStart); AttendanceSystem.Apply(school); school.Clock.JumpTo(LessonStart.AddMinutes(45)); AttendanceSystem.Apply(school); var row = school.Roster.People.First(p => p.Id == pupilId).Attendance?.LastOrDefault(); Assert.NotNull(row); Assert.Equal(AttendanceStatuses.Absent, row!.Status); Assert.Equal(AbsenceReasons.Illness, row.AbsenceReason); Assert.NotEqual(AbsenceReasons.Truancy, row.AbsenceReason); } } private static int CountRespiratoryOnset(School school) { // Force the day-onset pass with fixed weather. school.LastDiseaseDay = int.MinValue; DiseaseSystem.Apply(school, gameMinutes: 0); return school.Roster!.People.Count(person => person.Conditions is not null && person.Conditions.Any(row => row.DefName is "CommonCold" or "Influenza" or "Otitis")); } private static void SetPlace(School school, string personId, string? nodeId) { var query = new QueryDescription().WithAll(); school.World.Query( in query, (ref PersonIdentity identity, ref Presence presence) => { if (!identity.Id.Equals(personId, StringComparison.Ordinal)) { return; } presence = nodeId is null ? Presence.OffCampus : new Presence(nodeId, 0f, nodeId, false, []); }); } private static void AdvanceTo(School school, DateTime until) { while (school.Clock.Time < until) { school.Tick(0.2d, 5d); } } private static (School School, string Room, 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 School OpenStaffed() { var start = TuesdayMorning; var (catalog, map) = Vanilla(); var roster = RosterGenerator.Generate(catalog, map, schoolSeed: 42, "Russia", start); var pool = ApplicantPool.Create(catalog, roster, schoolSeed: 42, "Russia", start); var school = School.Create(1, "Болезни", start, catalog, map); school.InstallPeople(roster, seed: 42, "Russia", pool); school.ConfigurePresence(weekDays: 5, maxDecisionsPerTick: 10_000); return school; } 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); } }