Mark phase 84 wet-aftereffect in progress.

This commit is contained in:
Leonid Pershin
2026-08-21 20:14:00 +03:00
24 changed files with 763 additions and 14 deletions
@@ -0,0 +1,242 @@
using Arch.Core;
using HSchool.Ai;
using HSchool.Content;
using HSchool.People;
using HSchool.Schedule;
namespace HSchool.Simulation.Tests;
public class GradeTrailCouplingTests
{
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 WhisperOnLesson_MarkWorseThanQuiet()
{
var quiet = RunMathMark(whisper: false);
var whispered = RunMathMark(whisper: true);
Assert.True(quiet > whispered);
}
[Fact]
public void NoTextbook_MarkWorseThanWithTextbook()
{
var withBook = RunMathMark(whisper: false, textbook: true);
var without = RunMathMark(whisper: false, textbook: false);
Assert.True(withBook > without);
}
[Fact]
public void PoorMarkTrail_EmitsNotice()
{
var (school, _, pupilId, _) = StaffedMath();
using (school)
{
var rules = school.Catalog!.BehaviorRules!;
var person = school.Roster!.People.First(row => row.Id == pupilId);
var t0 = LessonStart;
Assert.True(LessonMarkMemory.Record(person, "Mathematics", 2, t0, 1, rules));
Assert.True(LessonMarkMemory.Record(person, "Literature", 2, t0.AddHours(1), 2, rules));
Assert.True(LessonMarkMemory.Record(person, "History", 2, t0.AddHours(2), 3, rules));
school.DrainWorldEvents();
GradeTrailSystem.AfterMark(school, person);
Assert.Contains(
school.DrainWorldEvents(),
row => row.Trigger.Equals(EventTriggers.PoorGradeTrail, StringComparison.Ordinal)
&& row.PersonKey.Equals(pupilId, StringComparison.Ordinal));
}
}
[Fact]
public void IllnessAbsent_DoesNotAddTruancyOffense()
{
var (school, _, pupilId, _) = StaffedMath();
using (school)
{
var person = school.Roster!.People.First(row => row.Id == pupilId);
person.Offenses = null;
GradeTrailSystem.AfterAbsent(school, person, AbsenceReasons.Illness);
Assert.True(person.Offenses is null || person.Offenses.Count == 0);
GradeTrailSystem.AfterAbsent(school, person, AbsenceReasons.Truancy);
Assert.NotNull(person.Offenses);
Assert.Contains(person.Offenses!, row => row.Kind == OffenseKinds.Truancy);
}
}
private static int RunMathMark(bool whisper, bool textbook = true)
{
var (school, room, pupilId, teacherId) = StaffedMath();
using (school)
{
AdvanceTo(school, LessonStart);
school.ResetDayLog();
var pupil = school.Roster!.People.First(row => row.Id == pupilId);
pupil.LessonMarks = null;
SetPlace(school, pupilId, room);
SetPlace(school, teacherId, room);
SetSkill(school, teacherId, "Mathematics", 100f);
SetNeed(school, pupilId, "Hunger", 1f);
SetNeed(school, pupilId, "Warmth", 1f);
if (textbook)
{
PlaceTextbook(school, pupilId, "Mathematics");
}
else
{
ClearTextbooks(school, pupilId);
}
if (whisper)
{
var partner = school.Roster!.Classes
.SelectMany(row => row.PupilIds)
.First(id => id != pupilId);
SetPlace(school, partner, room);
PlaceTextbook(school, partner, "Mathematics");
Assert.True(school.TryStartAction(pupilId, TalkActions.Whisper));
}
LessonLearningSystem.Apply(school, 45);
var mark = MarkOf(school, pupilId);
Assert.NotNull(mark);
return mark!.Value;
}
}
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()
{
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 PlaceTextbook(School school, string personId, string subject, string location = ItemLocations.Bag)
{
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}.");
}
ClearTextbooks(school, personId);
items.Add(new InventoryItem("Textbook", Color: null, Condition: 1f, location, subject));
}
private static void ClearTextbooks(School school, string personId)
{
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);
}
}
}
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)
{
TalkCircleSystem.Interrupt(school, personId);
school.TalkCircleByPerson.Remove(personId);
var query = new QueryDescription().WithAll<PersonIdentity, Presence, PersonActivity, Intent>();
school.World.Query(
in query,
(ref PersonIdentity identity, ref Presence presence, ref PersonActivity activity, ref Intent intent) =>
{
if (identity.Id.Equals(personId, StringComparison.Ordinal))
{
presence = new Presence(node, remaining, node, HeadingHome: false, path ?? []);
activity = PersonActivity.Idle;
intent = Intent.None;
}
});
}
private static void SetSkill(School school, string personId, string skill, float value)
{
var query = new QueryDescription().WithAll<PersonIdentity, PersonSkills>();
school.World.Query(
in query,
(ref PersonIdentity identity, ref PersonSkills skills) =>
{
if (identity.Id.Equals(personId, StringComparison.Ordinal))
{
skills.Values[skill] = value;
}
});
}
private static void SetNeed(School school, string personId, string need, float value)
{
var query = new QueryDescription().WithAll<PersonIdentity, PersonNeeds>();
school.World.Query(
in query,
(ref PersonIdentity identity, ref PersonNeeds needs) =>
{
if (identity.Id.Equals(personId, StringComparison.Ordinal))
{
needs.Values[need] = value;
}
});
}
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);
}
}