Merge branch 'phase/77-health-conditions'
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -0,0 +1,144 @@
|
||||
using Arch.Core;
|
||||
using HSchool.Ai;
|
||||
using HSchool.Content;
|
||||
using HSchool.People;
|
||||
using HSchool.Schedule;
|
||||
|
||||
namespace HSchool.Simulation.Tests;
|
||||
|
||||
public class HealthConditionSystemTests
|
||||
{
|
||||
private static readonly DateTime TuesdayLesson = new(2012, 4, 3, 8, 30, 0, DateTimeKind.Utc);
|
||||
|
||||
[Fact]
|
||||
public void SameSeed_SchoolTick_YieldsSameSeverity()
|
||||
{
|
||||
using var first = OpenStaffed();
|
||||
using var second = OpenStaffed();
|
||||
var pupilId = first.Roster!.People.First(person => person.IsStudent && !person.IsParent).Id;
|
||||
HangArtificial(first, pupilId, severity: 0.25f);
|
||||
HangArtificial(second, pupilId, severity: 0.25f);
|
||||
|
||||
TickMinutes(first, 60);
|
||||
TickMinutes(second, 60);
|
||||
|
||||
var a = first.Roster.People.First(person => person.Id == pupilId).Conditions![0].Severity;
|
||||
var b = second.Roster!.People.First(person => person.Id == pupilId).Conditions![0].Severity;
|
||||
Assert.Equal(a, b);
|
||||
Assert.True(a > 0.25f);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Hunger_StillDecaysWhileConditionTicks()
|
||||
{
|
||||
using var school = OpenStaffed();
|
||||
AdvanceTo(school, TuesdayLesson);
|
||||
var onCampus = school.CapturePresence().First(row => row.NodeId is not null);
|
||||
var pupil = school.Roster!.People.First(person => person.Id.Equals(onCampus.PersonId, StringComparison.Ordinal));
|
||||
HangArtificial(school, pupil.Id, severity: 0.2f);
|
||||
|
||||
var hungerBefore = ReadHunger(school, pupil.Id);
|
||||
var severityBefore = pupil.Conditions![0].Severity;
|
||||
|
||||
TickMinutes(school, 60);
|
||||
|
||||
var hungerAfter = ReadHunger(school, pupil.Id);
|
||||
var severityAfter = pupil.Conditions[0].Severity;
|
||||
var catalogHunger = school.Catalog!.Needs["Hunger"];
|
||||
Assert.True(hungerAfter < hungerBefore);
|
||||
Assert.Equal(hungerBefore - catalogHunger.DecayPerHour, hungerAfter, precision: 3);
|
||||
Assert.True(severityAfter > severityBefore);
|
||||
}
|
||||
|
||||
private static void HangArtificial(School school, string personId, float severity)
|
||||
{
|
||||
var person = school.Roster!.People.First(row => row.Id.Equals(personId, StringComparison.Ordinal));
|
||||
HealthConditions.Add(
|
||||
person,
|
||||
new HealthCondition
|
||||
{
|
||||
DefName = "TestCold",
|
||||
Severity = severity,
|
||||
Progress = 0f,
|
||||
Source = "dev",
|
||||
StartedAt = school.Clock.Time,
|
||||
SeverityPerDay = 0.5f,
|
||||
ProgressPerDay = 0.1f,
|
||||
});
|
||||
}
|
||||
|
||||
private static float ReadHunger(School school, string personId)
|
||||
{
|
||||
var value = float.NaN;
|
||||
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))
|
||||
{
|
||||
value = needs.Values["Hunger"];
|
||||
}
|
||||
});
|
||||
Assert.False(float.IsNaN(value));
|
||||
return value;
|
||||
}
|
||||
|
||||
private static void TickMinutes(School school, int minutes)
|
||||
{
|
||||
for (var i = 0; i < minutes; i++)
|
||||
{
|
||||
school.Tick(0.2d, 5d);
|
||||
}
|
||||
}
|
||||
|
||||
private static void AdvanceTo(School school, DateTime until)
|
||||
{
|
||||
while (school.Clock.Time < until)
|
||||
{
|
||||
school.Tick(0.2d, 5d);
|
||||
}
|
||||
}
|
||||
|
||||
private static School OpenStaffed()
|
||||
{
|
||||
var start = new DateTime(2012, 4, 3, 6, 0, 0, DateTimeKind.Utc);
|
||||
var (catalog, map) = Vanilla();
|
||||
var roster = RosterGenerator.Generate(catalog, map, schoolSeed: 1, "Russia", start);
|
||||
var pool = ApplicantPool.Create(catalog, roster, schoolSeed: 1, "Russia", start);
|
||||
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, "Здоровье", start, catalog, map);
|
||||
school.InstallPeople(roster, seed: 1, "Russia", pool);
|
||||
school.SetTimetable(new Timetable(
|
||||
[
|
||||
new LessonPlacement(schoolClass.Id, "Mathematics", "t1", schoolClass.RoomId, Day: 1, Period: 1),
|
||||
new LessonPlacement(schoolClass.Id, "PhysicalEducation", "t2", "gym-hall", Day: 1, Period: 2),
|
||||
],
|
||||
[]));
|
||||
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<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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user