Update decision-making phase and enhance localization for presence tracking
ci / server (push) Failing after 3m40s
ci / client (push) Successful in 14s

- Marked tasks as complete in the decision-making phase documentation, indicating readiness for implementation.
- Updated the README to reflect the completion status of the decision-making phase.
- Enhanced localization strings to include new presence tracking features, improving user experience.
- Revised the game screen logic to display real-time presence status, including walking states for individuals.
- Added tests to validate the new localization strings and presence functionalities, ensuring robust performance.
This commit is contained in:
Leonid Pershin
2026-08-19 21:00:12 +03:00
parent b135a9caad
commit 5cd5a6d258
21 changed files with 1839 additions and 72 deletions
@@ -0,0 +1,238 @@
using HSchool.Content;
namespace HSchool.Ai.Tests;
public class DecisionPlannerTests
{
[Fact]
public void ZeroToilet_BeatsALesson()
{
var (catalog, map, walks) = World();
var duty = map.Rooms.First(room => room.Def == "Classroom").Id;
var decision = DecisionPlanner.Decide(
catalog,
map,
walks,
Actor(duty, boundToLesson: true, duty, new Dictionary<string, float>(StringComparer.Ordinal)
{
["Toilet"] = 0f,
["Hunger"] = 1f,
["Social"] = 1f,
["Sleep"] = 1f,
}),
(_, _) => 0);
Assert.Equal(GoalKind.Need, decision.Intent.Kind);
Assert.Equal("Toilet", decision.Intent.Id);
Assert.Equal("UseToilet", decision.Intent.ActionId);
Assert.NotNull(decision.WalkTo);
Assert.Equal("Restroom", map.Rooms.First(room => room.Id == decision.WalkTo).Def);
}
[Fact]
public void NeedJustBelowThreshold_DoesNotLeaveClass()
{
var (catalog, map, walks) = World();
var duty = map.Rooms.First(room => room.Def == "Classroom").Id;
var decision = DecisionPlanner.Decide(
catalog,
map,
walks,
Actor(duty, boundToLesson: true, duty, new Dictionary<string, float>(StringComparer.Ordinal)
{
["Toilet"] = 0.34f,
["Hunger"] = 1f,
["Social"] = 1f,
["Sleep"] = 1f,
}),
(_, _) => 0);
Assert.Equal(GoalKind.Duty, decision.Intent.Kind);
Assert.Null(decision.StartAction);
Assert.True(decision.WalkTo is null || decision.WalkTo == duty);
}
[Fact]
public void EqualNeeds_FinishTheCurrentAction()
{
var (catalog, map, walks) = World();
var restroom = map.Rooms.First(room => room.Def == "Restroom").Id;
var intent = new Intent(GoalKind.Need, "Toilet", DecisionPlanner.NeedWeightAtZero, "UseToilet");
var decision = DecisionPlanner.Decide(
catalog,
map,
walks,
Actor(
restroom,
boundToLesson: true,
dutyRoom: map.Rooms.First(room => room.Def == "Classroom").Id,
needs: new Dictionary<string, float>(StringComparer.Ordinal)
{
["Toilet"] = 0f,
["Hunger"] = 0f,
["Social"] = 1f,
["Sleep"] = 1f,
},
intent: intent,
activityActive: true),
(_, _) => 0);
Assert.Equal("Toilet", decision.Intent.Id);
Assert.Null(decision.WalkTo);
Assert.Null(decision.StartAction);
}
[Fact]
public void BreakAtDutyRoom_PicksLeisureEvenWithAStaleLessonIntent()
{
var (catalog, map, walks) = World();
var duty = map.Rooms.First(room => room.Def == "Classroom").Id;
var decision = DecisionPlanner.Decide(
catalog,
map,
walks,
Actor(
duty,
boundToLesson: false,
duty,
FullNeeds(),
intent: new Intent(GoalKind.Duty, duty, DecisionPlanner.DutyLessonWeight, null)),
(_, _) => 0);
Assert.Equal(GoalKind.Leisure, decision.Intent.Kind);
}
[Fact]
public void LeisureOnABreak_IsNotYankedBackToTheNextHomeroom()
{
var (catalog, map, walks) = World();
var classroom = "classroom-101";
var corridor = "corridor-1";
var decision = DecisionPlanner.Decide(
catalog,
map,
walks,
Actor(
corridor,
boundToLesson: false,
classroom,
FullNeeds(),
intent: new Intent(GoalKind.Leisure, "Chat", 3f, "Chat")),
(_, _) => 0);
Assert.Equal(GoalKind.Leisure, decision.Intent.Kind);
Assert.NotEqual(classroom, decision.WalkTo);
}
[Fact]
public void BreakAtDutyRoom_PicksLeisure_LessonDoesNot()
{
var (catalog, map, walks) = World();
var duty = map.Rooms.First(room => room.Def == "Classroom").Id;
var onBreak = DecisionPlanner.Decide(
catalog,
map,
walks,
Actor(duty, boundToLesson: false, duty, FullNeeds()),
(_, _) => 0);
var inLesson = DecisionPlanner.Decide(
catalog,
map,
walks,
Actor(duty, boundToLesson: true, duty, FullNeeds()),
(_, _) => 0);
Assert.Equal(GoalKind.Leisure, onBreak.Intent.Kind);
Assert.NotNull(onBreak.Intent.ActionId);
Assert.True(catalog.Actions[onBreak.Intent.ActionId!].Weight > 0);
Assert.Equal(GoalKind.Duty, inLesson.Intent.Kind);
Assert.Null(inLesson.StartAction);
}
[Fact]
public void BreakAwayFromNextRoom_WalksThereInsteadOfChatting()
{
var (catalog, map, walks) = World();
var classroom = map.Rooms.First(room => room.Id == "classroom-101").Id;
var gym = "gym-hall";
var fromIdle = DecisionPlanner.Decide(
catalog,
map,
walks,
Actor(classroom, boundToLesson: false, gym, FullNeeds()),
(_, _) => 0);
var fromStaleLesson = DecisionPlanner.Decide(
catalog,
map,
walks,
Actor(
classroom,
boundToLesson: false,
gym,
FullNeeds(),
intent: new Intent(GoalKind.Duty, classroom, DecisionPlanner.DutyLessonWeight, null)),
(_, _) => 0);
Assert.Equal(GoalKind.Duty, fromIdle.Intent.Kind);
Assert.Equal(gym, fromIdle.WalkTo);
Assert.Equal(gym, fromStaleLesson.WalkTo);
Assert.Equal(gym, fromStaleLesson.Intent.Id);
}
[Fact]
public void ArrivedAtNextRoomThisBreak_StaysInsteadOfChatting()
{
var (catalog, map, walks) = World();
var gym = "gym-hall";
var decision = DecisionPlanner.Decide(
catalog,
map,
walks,
Actor(
gym,
boundToLesson: false,
gym,
FullNeeds(),
intent: new Intent(GoalKind.Duty, gym, DecisionPlanner.DutyTravelWeight, null)),
(_, _) => 0);
Assert.Equal(GoalKind.Duty, decision.Intent.Kind);
Assert.Equal(gym, decision.Intent.Id);
Assert.Null(decision.WalkTo);
Assert.Null(decision.StartAction);
}
private static ActorState Actor(
string node,
bool boundToLesson,
string dutyRoom,
IReadOnlyDictionary<string, float> needs,
Intent? intent = null,
bool activityActive = false) =>
new(
node,
node,
IsWalking: false,
activityActive,
IsStudent: true,
IsStaff: false,
IsParent: false,
boundToLesson,
dutyRoom,
needs,
intent ?? Intent.None);
private static Dictionary<string, float> FullNeeds() => new(StringComparer.Ordinal)
{
["Toilet"] = 1f,
["Hunger"] = 1f,
["Social"] = 1f,
["Sleep"] = 1f,
};
private static (DefCatalog Catalog, MapLayout Map, WalkGraph Walks) World()
{
var (catalog, map) = Fixtures.Vanilla();
return (catalog, map, WalkGraph.Build(catalog, map));
}
}
@@ -0,0 +1,32 @@
using HSchool.Content;
namespace HSchool.Ai.Tests;
public class LessonLearningTests
{
private static readonly SkillDef Math = new()
{
DefName = "Mathematics",
Range = new IntRange { Min = 0, Max = 100 },
};
[Fact]
public void HungryLearnsLessThanFull()
{
var full = LessonLearning.Gain(50, Math, share: 1, lessonSkillPerHour: 0.05f, hours: 0.75f, hunger: 1f, traitOffset: 0);
var hungry = LessonLearning.Gain(50, Math, share: 1, lessonSkillPerHour: 0.05f, hours: 0.75f, hunger: 0.1f, traitOffset: 0);
Assert.True(full > 50);
Assert.True(hungry > 50);
Assert.True(full - 50 > hungry - 50);
}
[Fact]
public void DiligentOffset_RaisesTheGain()
{
var plain = LessonLearning.Gain(50, Math, 1, 0.05f, 1f, 1f, 0);
var diligent = LessonLearning.Gain(50, Math, 1, 0.05f, 1f, 1f, 8);
Assert.True(diligent > plain);
}
}
@@ -180,6 +180,46 @@ public class SchoolApiTests(AppHostFixture fixture)
Assert.Contains(ru.Holidays, holiday => holiday.DefName == "SpringBreak");
}
[Fact]
public async Task Catalog_WithAnUnknownMod_IsRejected()
{
using var client = fixture.App.CreateHttpClient("server");
using var response = await client.GetAsync("/api/catalog?mods=no-such-mod", TestContext.Current.CancellationToken);
Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
Assert.Equal("unknown-mod", await ProblemCodeAsync(response));
}
/// <summary>
/// A pack id is a folder name that came from a browser. Anything outside the safe alphabet is
/// refused as unknown before it can be joined onto a path — on both endpoints that take one.
/// </summary>
[Fact]
public async Task ModId_ThatEscapesTheModsFolder_IsRejectedOnBothEndpoints()
{
using var client = fixture.App.CreateHttpClient("server");
await ResetAsync(client);
using var catalog = await client.GetAsync("/api/catalog?mods=..%2F..%2Fsaves", TestContext.Current.CancellationToken);
Assert.Equal(HttpStatusCode.BadRequest, catalog.StatusCode);
Assert.Equal("unknown-mod", await ProblemCodeAsync(catalog));
using var create = await client.PostAsJsonAsync(
"/api/schools",
new
{
name = "Побег из mods",
startDate = ExpectedDefaultStart,
modIds = new[] { "../../saves" },
},
TestContext.Current.CancellationToken);
Assert.Equal(HttpStatusCode.BadRequest, create.StatusCode);
Assert.Equal("unknown-mod", await ProblemCodeAsync(create));
}
[Fact]
public async Task CreateSchool_WithABrokenMap_IsRejected()
{
@@ -0,0 +1,343 @@
using Arch.Core;
using HSchool.Content;
using HSchool.People;
using HSchool.Schedule;
namespace HSchool.Simulation.Tests;
public class DecisionTests
{
private static readonly DateTime TuesdayMorning = new(2012, 4, 3, 6, 0, 0, DateTimeKind.Utc);
[Fact]
public void ZeroToilet_LeavesClass_ReachesRestroom_AndReturns()
{
var (school, homeroom, pupilId) = StaffedFirstFloorClass();
using (school)
{
AdvanceTo(school, new DateTime(2012, 4, 3, 8, 30, 0, DateTimeKind.Utc));
SetNeed(school, pupilId, "Toilet", 0f);
Assert.Equal(0f, NeedOf(school, pupilId, "Toilet"));
school.QueueDecision(pupilId);
if (!WaitUntil(school, pupilId, row => school.Map!.NodeDef(row.NodeId ?? "") == "Restroom", 25))
{
var row = school.CapturePresence().Single(item => item.PersonId == pupilId);
Assert.Fail(
$"never reached a restroom: node={row.NodeId} dest={row.DestinationId} goal={row.GoalKind}/{row.GoalId}/{row.GoalAction} action={row.ActionId} toilet={NeedOf(school, pupilId, "Toilet")} path={string.Join(",", row.Path)}");
}
Assert.True(WaitUntil(school, pupilId, row => row.ActionId == "UseToilet", 8));
Assert.True(WaitUntil(
school,
pupilId,
row => row.NodeId == homeroom && row.Path.Count == 0 && row.RemainingMinutes <= 0 && row.ActionId is null,
25));
}
}
[Fact]
public void NeedJustBelowThreshold_StaysInClass()
{
var (school, homeroom, pupilId) = StaffedFirstFloorClass();
using (school)
{
AdvanceTo(school, new DateTime(2012, 4, 3, 8, 30, 0, DateTimeKind.Utc));
SetNeed(school, pupilId, "Toilet", 0.34f);
school.QueueDecision(pupilId);
AdvanceTo(school, new DateTime(2012, 4, 3, 8, 38, 0, DateTimeKind.Utc));
var row = school.CapturePresence().Single(item => item.PersonId == pupilId);
Assert.Equal(homeroom, row.NodeId);
Assert.Empty(row.Path);
Assert.NotEqual("UseToilet", row.ActionId);
Assert.Equal("Duty", row.GoalKind);
}
}
[Fact]
public void TwoEqualNeeds_FinishTheToiletAction()
{
var (school, _, pupilId) = StaffedFirstFloorClass();
using (school)
{
AdvanceTo(school, new DateTime(2012, 4, 3, 8, 30, 0, DateTimeKind.Utc));
SetNeed(school, pupilId, "Toilet", 0f);
school.QueueDecision(pupilId);
Assert.True(WaitUntil(school, pupilId, row => row.ActionId == "UseToilet", 25));
SetNeed(school, pupilId, "Hunger", 0f);
school.QueueDecision(pupilId);
school.Tick(0.2d, 5d);
Assert.Equal("UseToilet", ActivityOf(school, pupilId));
}
}
[Fact]
public void BreakPicksLeisure_LessonDoesNot()
{
var (school, homeroom, pupilId) = TwoHomeroomLessons();
using (school)
{
AdvanceTo(school, new DateTime(2012, 4, 3, 8, 45, 0, DateTimeKind.Utc));
var inLesson = school.CapturePresence().Single(item => item.PersonId == pupilId);
Assert.Equal(homeroom, inLesson.NodeId);
Assert.Equal("Duty", inLesson.GoalKind);
Assert.Null(inLesson.ActionId);
AdvanceTo(school, new DateTime(2012, 4, 3, 9, 18, 0, DateTimeKind.Utc));
var onBreak = school.CapturePresence().Single(item => item.PersonId == pupilId);
Assert.Equal("Leisure", onBreak.GoalKind);
Assert.True(
school.Map!.NodeDef(onBreak.NodeId ?? "") is "Corridor"
|| school.Map.NodeDef(onBreak.DestinationId ?? "") is "Corridor"
|| onBreak.ActionId is "Chat" or "RecessRest" or "WalkCorridor");
Assert.True(
school.Map!.NodeDef(onBreak.NodeId ?? "") is "Corridor"
|| school.Map.NodeDef(onBreak.DestinationId ?? "") is "Corridor"
|| onBreak.ActionId is "Chat" or "RecessRest" or "WalkCorridor");
}
}
[Fact]
public void HungryPupil_GainsLessSkillDuringTheLesson()
{
var (school, _, firstId) = StaffedFirstFloorClass();
using (school)
{
var schoolClass = school.Roster!.Classes.First(row => row.PupilIds.Contains(firstId));
var secondId = schoolClass.PupilIds.First(id => id != firstId);
AdvanceTo(school, new DateTime(2012, 4, 3, 8, 30, 0, DateTimeKind.Utc));
SetNeed(school, firstId, "Hunger", 0.2f);
SetNeed(school, secondId, "Hunger", 1f);
var hungryBefore = SkillOf(school, firstId, "Mathematics");
var fullBefore = SkillOf(school, secondId, "Mathematics");
AdvanceTo(school, new DateTime(2012, 4, 3, 9, 15, 0, DateTimeKind.Utc));
var hungryGain = SkillOf(school, firstId, "Mathematics") - hungryBefore;
var fullGain = SkillOf(school, secondId, "Mathematics") - fullBefore;
Assert.True(fullGain > 0);
Assert.True(hungryGain > 0);
Assert.True(fullGain > hungryGain);
}
}
[Fact]
public void DecisionCap_DefersTheOverflow()
{
var (school, _, firstId) = StaffedFirstFloorClass();
using (school)
{
var secondId = school.Roster!.People.First(person => person.IsStudent && person.Id != firstId).Id;
AdvanceTo(school, new DateTime(2012, 4, 3, 8, 45, 0, DateTimeKind.Utc));
school.Tick(0.2d, 5d);
Assert.Equal(0, school.PendingDecisionCount);
school.ConfigurePresence(weekDays: 5, maxDecisionsPerTick: 1);
school.QueueDecision(firstId);
school.QueueDecision(secondId);
Assert.Equal(2, school.PendingDecisionCount);
school.Tick(0.2d, 5d);
Assert.Equal(1, school.PendingDecisionCount);
}
}
[Fact]
public void SameSeedAndActions_MatchAfterAWeek()
{
var (catalog, map) = Vanilla();
using var a = OpenStaffed(catalog, map, seed: 9);
using var b = OpenStaffed(catalog, map, seed: 9);
var until = new DateTime(2012, 4, 10, 9, 20, 0, DateTimeKind.Utc);
PlayTo(a, until);
PlayTo(b, until);
Assert.Equal(StateFingerprint(a), StateFingerprint(b));
}
private static (School School, string Homeroom, string PupilId) StaffedFirstFloorClass()
{
var (catalog, map) = Vanilla();
var school = OpenStaffed(catalog, map, seed: 1);
var homeroomClass = school.Roster!.Classes.First(row =>
row.RoomId is "classroom-101" or "classroom-102" or "classroom-103" or "classroom-104");
var pupil = homeroomClass.PupilIds
.Select(id => school.Roster.People.First(person => person.Id == id))
.First(person => !person.Traits.Contains("Lazy"));
return (school, homeroomClass.RoomId, pupil.Id);
}
private static (School School, string Homeroom, string PupilId) TwoHomeroomLessons()
{
var (catalog, map) = Vanilla();
var roster = RosterGenerator.Generate(catalog, map, schoolSeed: 1, "Slavic", TuesdayMorning);
var pool = ApplicantPool.Create(catalog, roster, schoolSeed: 1, "Slavic", TuesdayMorning);
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, "Slavic", pool);
school.SetTimetable(new Timetable(
[
new LessonPlacement(schoolClass.Id, "Mathematics", "t1", schoolClass.RoomId, Day: 1, Period: 1),
new LessonPlacement(schoolClass.Id, "Mathematics", "t1", schoolClass.RoomId, Day: 1, Period: 2),
],
[]));
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);
}
private static School OpenStaffed(DefCatalog catalog, MapLayout map, int seed)
{
var roster = RosterGenerator.Generate(catalog, map, seed, "Slavic", TuesdayMorning);
var pool = ApplicantPool.Create(catalog, roster, seed, "Slavic", TuesdayMorning);
var schoolClass = roster.Classes.First(row =>
row.RoomId is "classroom-101" or "classroom-102" or "classroom-103" or "classroom-104");
var school = School.Create(seed, "Решения", TuesdayMorning, catalog, map);
school.InstallPeople(roster, seed, "Slavic", 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 void AdvanceTo(School school, DateTime until)
{
while (school.Clock.Time < until)
{
school.Tick(0.2d, 5d);
}
}
private static void PlayTo(School school, DateTime until)
{
while (school.Clock.Time < until)
{
if (school.PeekSkipEmpty().Allowed)
{
school.TrySkipEmpty();
continue;
}
school.Tick(0.2d, 5d);
}
}
private static bool WaitUntil(School school, string personId, Func<PresenceSnapshot, bool> match, int minutes)
{
for (var i = 0; i < minutes; i++)
{
school.Tick(0.2d, 5d);
var row = school.CapturePresence().Single(item => item.PersonId == personId);
if (match(row))
{
return true;
}
}
return false;
}
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 float NeedOf(School school, string personId, string need)
{
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.GetValueOrDefault(need, float.NaN);
}
});
return value;
}
private static float SkillOf(School school, string personId, string skill)
{
var value = float.NaN;
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))
{
value = skills.Values.GetValueOrDefault(skill);
}
});
return value;
}
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))
{
found = activity.ActionId;
}
});
return found;
}
private static string StateFingerprint(School school)
{
var needs = new Dictionary<string, string>(StringComparer.Ordinal);
var skills = new Dictionary<string, string>(StringComparer.Ordinal);
var needQuery = new QueryDescription().WithAll<PersonIdentity, PersonNeeds, PersonSkills>();
school.World.Query(in needQuery, (ref PersonIdentity identity, ref PersonNeeds personNeeds, ref PersonSkills personSkills) =>
{
needs[identity.Id] = string.Join(",", personNeeds.Values.OrderBy(pair => pair.Key, StringComparer.Ordinal).Select(pair => $"{pair.Key}:{pair.Value:0.###}"));
skills[identity.Id] = string.Join(",", personSkills.Values.OrderBy(pair => pair.Key, StringComparer.Ordinal).Select(pair => $"{pair.Key}:{pair.Value:0.###}"));
});
return string.Join(
"|",
school.CapturePresence()
.OrderBy(row => row.PersonId, StringComparer.Ordinal)
.Select(row =>
$"{row.PersonId}:{row.NodeId ?? "-"}:{row.RemainingMinutes:0.###}:{row.DestinationId ?? "-"}:{(row.HeadingHome ? "1" : "0")}:{string.Join(",", row.Path)}:{row.ActionId ?? "-"}:{row.GoalKind ?? "-"}:{needs.GetValueOrDefault(row.PersonId)}:{skills.GetValueOrDefault(row.PersonId)}"));
}
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);
}
}