Files
h-school/tests/HSchool.Simulation.Tests/OffenseMemorySimulationTests.cs
Leonid Pershin fdcaed0f74 Remember recent quarrels, fights and reprimands on the person.
Short offense list for the pupil card and save, capped by BehaviorDef — not a discipline score.
2026-08-21 09:37:12 +03:00

199 lines
7.7 KiB
C#

using Arch.Core;
using HSchool.Ai;
using HSchool.Content;
using HSchool.People;
using HSchool.Simulation;
namespace HSchool.Simulation.Tests;
public class OffenseMemorySimulationTests
{
private static readonly DateTime TuesdayMorning = new(2012, 4, 3, 6, 0, 0, DateTimeKind.Utc);
[Fact]
public void FightBrokenByStaff_AddsFightAndReprimandToBothPupils()
{
var (school, first, second, staffId) = TwoPupilsAndStaffOnBreak();
using (school)
{
OpinionStore.Set(first, second.Id, -55);
OpinionStore.Set(second, first.Id, -55);
PlaceAt(school, first.Id, "yard");
PlaceAt(school, second.Id, "yard");
PlaceAt(school, staffId, "yard");
Assert.True(school.TryStartAction(first.Id, TalkActions.Fight));
PlaceAt(school, staffId, "yard");
TalkCircleSystem.Apply(school, 1d);
first = school.Roster!.People.First(person => person.Id == first.Id);
second = school.Roster.People.First(person => person.Id == second.Id);
AssertFightAndReprimand(first, second.Id);
AssertFightAndReprimand(second, first.Id);
}
}
[Fact]
public void OverCeiling_InWorld_DropsOldestAfterRepeatedFights()
{
var (school, first, second) = TwoPupilsOnBreak();
using (school)
{
var rules = school.Catalog!.BehaviorRules!;
var max = rules.OffenseMemoryMax;
Assert.True(max > 0);
for (var i = 0; i < max + 1; i++)
{
OpinionStore.Set(first, second.Id, -55);
OpinionStore.Set(second, first.Id, -55);
PlaceAt(school, first.Id, "yard");
PlaceAt(school, second.Id, "yard");
Assert.True(school.TryStartAction(first.Id, TalkActions.Fight));
TickWhile(school, personId => TalkActions.IsFight(ActivityOf(school, personId)), first.Id, second.Id);
first = school.Roster!.People.First(person => person.Id == first.Id);
second = school.Roster.People.First(person => person.Id == second.Id);
}
Assert.NotNull(first.Offenses);
Assert.Equal(max, first.Offenses!.Count);
Assert.All(first.Offenses, row => Assert.Equal(OffenseKinds.Fight, row.Kind));
}
}
private static void AssertFightAndReprimand(Person person, string otherId)
{
Assert.NotNull(person.Offenses);
Assert.Contains(
person.Offenses!,
row => row.Kind == OffenseKinds.Fight && row.OtherPersonId == otherId);
Assert.Contains(
person.Offenses!,
row => row.Kind == OffenseKinds.Reprimand && row.OtherPersonId == otherId);
}
private static (School School, Person First, Person Second) TwoPupilsOnBreak()
{
var (catalog, map) = Vanilla();
var school = OpenSchool(catalog, map, seed: 42, TuesdayMorning, advanceToBreak: true);
var schoolClass = school.Roster!.Classes.First(row => row.RoomId == "classroom-101");
var pupils = schoolClass.PupilIds
.Select(id => school.Roster.People.First(person => person.Id == id))
.Take(2)
.ToArray();
return (school, pupils[0], pupils[1]);
}
private static (School School, Person First, Person Second, string StaffId) TwoPupilsAndStaffOnBreak()
{
var (school, first, second) = TwoPupilsOnBreak();
const float cap = 100_000f;
var roster = school.Roster!;
var pool = school.Applicants!;
var hired = Staffing.Hire(
school.Catalog!,
school.Map!,
roster,
pool,
pool.Applicants[0].Person.Id,
Staffing.TeacherPosition,
cap);
Assert.Equal(StaffingError.None, hired.Error);
school.ApplyStaffing(hired.Roster, hired.Pool);
first = school.Roster!.People.First(person => person.Id == first.Id);
second = school.Roster.People.First(person => person.Id == second.Id);
var staffId = school.Roster.People.First(person => person.IsStaff).Id;
return (school, first, second, staffId);
}
private static School OpenSchool(DefCatalog catalog, MapLayout map, int seed, DateTime start, bool advanceToBreak)
{
var roster = RosterGenerator.Generate(catalog, map, seed, "Russia", start);
var pool = ApplicantPool.Create(catalog, roster, seed, "Russia", start);
var schoolClass = roster.Classes.First(row => row.RoomId == "classroom-101");
var school = School.Create(seed, "OffenseMemory", start, catalog, map);
school.InstallPeople(roster, seed, "Russia", pool);
school.SetTimetable(new HSchool.Schedule.Timetable(
[
new HSchool.Schedule.LessonPlacement(schoolClass.Id, "Mathematics", "t1", schoolClass.RoomId, Day: 1, Period: 1),
],
[]));
school.ConfigurePresence(weekDays: 5, maxDecisionsPerTick: 10_000);
if (advanceToBreak)
{
AdvanceTo(school, new DateTime(2012, 4, 3, 9, 18, 0, DateTimeKind.Utc));
}
return school;
}
private static void AdvanceTo(School school, DateTime until)
{
while (school.Clock.Time < until)
{
school.Tick(0.2d, 5d);
}
}
private static void PlaceAt(School school, string personId, string? nodeId)
{
TalkCircleSystem.Interrupt(school, 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 = nodeId is null ? Presence.OffCampus : new Presence(nodeId, 0f, nodeId, false, []);
activity = PersonActivity.Idle;
intent = Intent.None;
}
});
}
private static void TickWhile(School school, Func<string, bool> stillGoing, params string[] personIds)
{
for (var i = 0; i < 12 && personIds.Any(stillGoing); i++)
{
school.Tick(0.2d, 5d);
}
}
private static string? ActivityOf(School school, string personId)
{
string? found = null;
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.IsActive)
{
found = activity.ActionId;
}
});
return found;
}
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;
}
documents.Add(new ContentDocument(
CatalogLoader.CorePackId,
Path.GetRelativePath(root, path).Replace('\\', '/'),
File.ReadAllText(path)));
}
var catalog = new CatalogLoader().Load([CatalogLoader.CorePackId], documents);
var map = CatalogLoader.LastDefaultMap([CatalogLoader.CorePackId], documents);
Assert.NotNull(map);
return (catalog, map);
}
}