Merge branch 'phase/44-quarrel-fight'
Co-authored-by: Cursor <cursoragent@cursor.com> # Conflicts: # docs/phases/README.md # src/HSchool.Content/PeopleDefs.cs # src/HSchool.People/Roster.cs # src/HSchool.People/Seed.cs
This commit is contained in:
@@ -0,0 +1,349 @@
|
||||
using Arch.Core;
|
||||
using HSchool.Ai;
|
||||
using HSchool.Content;
|
||||
using HSchool.People;
|
||||
using HSchool.Simulation;
|
||||
|
||||
namespace HSchool.Simulation.Tests;
|
||||
|
||||
public class QuarrelFightTests
|
||||
{
|
||||
private static readonly DateTime TuesdayMorning = new(2012, 4, 3, 6, 0, 0, DateTimeKind.Utc);
|
||||
private static readonly DateTime SaturdayMorning = new(2012, 4, 7, 10, 0, 0, DateTimeKind.Utc);
|
||||
|
||||
[Fact]
|
||||
public void EnemiesInYard_QuarrelMoreOftenThanFriends()
|
||||
{
|
||||
var (school, first, second) = TwoPupilsOnBreak();
|
||||
using (school)
|
||||
{
|
||||
PlaceAt(school, first.Id, "yard");
|
||||
PlaceAt(school, second.Id, "yard");
|
||||
|
||||
OpinionStore.Set(first, second.Id, -55);
|
||||
OpinionStore.Set(second, first.Id, -55);
|
||||
Assert.True(school.TryStartAction(first.Id, TalkActions.QuarrelYard));
|
||||
TalkCircleSystem.Interrupt(school, first.Id);
|
||||
|
||||
PlaceAt(school, first.Id, "yard");
|
||||
PlaceAt(school, second.Id, "yard");
|
||||
OpinionStore.Set(first, second.Id, 55);
|
||||
OpinionStore.Set(second, first.Id, 55);
|
||||
Assert.False(school.TryStartAction(first.Id, TalkActions.QuarrelYard));
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Bully_TwoDaysInARow_AimsAtTheSamePerson_IfStillInSchool()
|
||||
{
|
||||
var (school, first, second) = TwoPupilsOnBreak();
|
||||
using (school)
|
||||
{
|
||||
GiveTrait(school, first.Id, "Bully");
|
||||
var bully = school.Roster!.People.First(person => person.Id == first.Id);
|
||||
var roster = school.Roster.People;
|
||||
var day1 = Conflict.ResolveVictim(bully, roster, school.Catalog!, seed: 3);
|
||||
var day2 = Conflict.ResolveVictim(bully, roster, school.Catalog!, seed: 40);
|
||||
Assert.NotNull(day1);
|
||||
Assert.Equal(day1, day2);
|
||||
Assert.Contains(roster, person => person.Id == day1);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Apology_RaisesOpinion_ButNotAbovePreQuarrel()
|
||||
{
|
||||
var (school, first, second) = TwoPupilsOnBreak();
|
||||
using (school)
|
||||
{
|
||||
const int baseline = -50;
|
||||
OpinionStore.Set(first, second.Id, baseline);
|
||||
OpinionStore.Set(second, first.Id, baseline);
|
||||
PlaceAt(school, first.Id, "yard");
|
||||
PlaceAt(school, second.Id, "yard");
|
||||
|
||||
Assert.True(school.TryStartAction(first.Id, TalkActions.QuarrelYard));
|
||||
TickWhile(school, personId => ActivityOf(school, personId) == TalkActions.QuarrelYard, first.Id, second.Id);
|
||||
|
||||
if (!school.DayLog.Any(row => row.Type == PersonLogTypes.Apologized))
|
||||
{
|
||||
PlaceAt(school, first.Id, "yard");
|
||||
PlaceAt(school, second.Id, "yard");
|
||||
Assert.True(school.TryStartAction(first.Id, TalkActions.Apologize));
|
||||
TickWhile(school, personId => ActivityOf(school, personId) == TalkActions.Apologize, first.Id, second.Id);
|
||||
}
|
||||
|
||||
var after = OpinionStore.Get(first, second.Id) ?? 0;
|
||||
Assert.True(after <= baseline);
|
||||
Assert.True(after > baseline + Conflict.QuarrelOpinionDelta(school.Catalog!.BehaviorRules));
|
||||
Assert.Contains(school.DayLog, row => row.Type == PersonLogTypes.Quarreled);
|
||||
Assert.Contains(school.DayLog, row => row.Type == PersonLogTypes.Apologized);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Fight_DoesNotChangeHealthNeed_BecauseThereIsNone()
|
||||
{
|
||||
var (school, first, second) = TwoPupilsOnBreak();
|
||||
using (school)
|
||||
{
|
||||
Assert.False(Conflict.HasHealthNeed(school.Catalog!));
|
||||
OpinionStore.Set(first, second.Id, -55);
|
||||
OpinionStore.Set(second, first.Id, -55);
|
||||
PlaceAt(school, first.Id, "yard");
|
||||
PlaceAt(school, second.Id, "yard");
|
||||
var before = NeedsOf(school, first.Id);
|
||||
|
||||
Assert.True(school.TryStartAction(first.Id, TalkActions.Fight));
|
||||
TickWhile(school, personId => TalkActions.IsFight(ActivityOf(school, personId)), first.Id, second.Id);
|
||||
|
||||
var after = NeedsOf(school, first.Id);
|
||||
Assert.False(after.ContainsKey("Health"));
|
||||
Assert.Equal(before.Keys.OrderBy(key => key, StringComparer.Ordinal), after.Keys.OrderBy(key => key, StringComparer.Ordinal));
|
||||
Assert.Contains(school.DayLog, row => row.Type == PersonLogTypes.Fought);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void StaffInNode_BreaksFight_AndWritesReprimand()
|
||||
{
|
||||
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));
|
||||
Assert.Equal(TalkActions.Fight, ActivityOf(school, first.Id));
|
||||
PlaceAt(school, staffId, "yard");
|
||||
TalkCircleSystem.Apply(school, 1d);
|
||||
|
||||
Assert.Contains(
|
||||
school.DayLog,
|
||||
row => row.Type == PersonLogTypes.Reprimanded
|
||||
&& (row.PersonId == first.Id || row.PersonId == second.Id));
|
||||
Assert.False(IsConflictActivity(school, first.Id));
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SkipEmpty_DoesNotLeaveAFightInState()
|
||||
{
|
||||
var (catalog, map) = Vanilla();
|
||||
var school = OpenSchool(catalog, map, seed: 42, SaturdayMorning, advanceToBreak: false);
|
||||
using (school)
|
||||
{
|
||||
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();
|
||||
OpinionStore.Set(pupils[0], pupils[1].Id, -55);
|
||||
OpinionStore.Set(pupils[1], pupils[0].Id, -55);
|
||||
PlaceAt(school, pupils[0].Id, "yard");
|
||||
PlaceAt(school, pupils[1].Id, "yard");
|
||||
Assert.True(school.TryStartAction(pupils[0].Id, TalkActions.Fight));
|
||||
Assert.True(IsConflictActivity(school, pupils[0].Id));
|
||||
|
||||
foreach (var person in school.Roster.People)
|
||||
{
|
||||
PlaceAt(school, person.Id, nodeId: null);
|
||||
}
|
||||
|
||||
Assert.True(school.IsCampusEmpty());
|
||||
Assert.True(school.TrySkipEmpty().Succeeded);
|
||||
Assert.Empty(school.TalkCirclesById);
|
||||
Assert.DoesNotContain(
|
||||
school.CapturePresence(),
|
||||
row => TalkActions.IsFight(row.ActionId));
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Defender_JoinsQuarrelOnVictimsSide()
|
||||
{
|
||||
var (school, pupils) = ThreePupilsOnBreak();
|
||||
using (school)
|
||||
{
|
||||
var bully = pupils[0];
|
||||
var victim = pupils[1];
|
||||
var defender = pupils[2];
|
||||
OpinionStore.Set(bully, victim.Id, -55);
|
||||
OpinionStore.Set(victim, bully.Id, -55);
|
||||
OpinionStore.Set(defender, victim.Id, 70);
|
||||
PlaceAt(school, bully.Id, "corridor-1");
|
||||
PlaceAt(school, victim.Id, "corridor-1");
|
||||
PlaceAt(school, defender.Id, "corridor-1");
|
||||
|
||||
Assert.True(school.TryStartAction(bully.Id, TalkActions.Quarrel));
|
||||
Assert.Equal(TalkActions.Quarrel, ActivityOf(school, bully.Id));
|
||||
Assert.Equal(TalkActions.Quarrel, ActivityOf(school, victim.Id));
|
||||
Assert.Equal(TalkActions.Quarrel, ActivityOf(school, defender.Id));
|
||||
}
|
||||
}
|
||||
|
||||
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, IReadOnlyList<Person> Pupils) ThreePupilsOnBreak()
|
||||
{
|
||||
var (catalog, map) = Vanilla();
|
||||
var school = OpenSchool(catalog, map, seed: 43, 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(3)
|
||||
.ToArray();
|
||||
Assert.Equal(3, pupils.Length);
|
||||
return (school, pupils);
|
||||
}
|
||||
|
||||
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, "Conflict", 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 void GiveTrait(School school, string personId, string trait)
|
||||
{
|
||||
var people = school.Roster!.People.Select(person =>
|
||||
person.Id.Equals(personId, StringComparison.Ordinal)
|
||||
? person with { Traits = person.Traits.Append(trait).Distinct(StringComparer.Ordinal).ToArray() }
|
||||
: person)
|
||||
.ToArray();
|
||||
var roster = school.Roster with { People = people };
|
||||
school.InstallPeople(roster, school.PeopleSeed, school.CountryId, school.Applicants);
|
||||
school.ConfigurePresence(weekDays: 5, maxDecisionsPerTick: 10_000);
|
||||
AdvanceTo(school, new DateTime(2012, 4, 3, 9, 18, 0, DateTimeKind.Utc));
|
||||
}
|
||||
|
||||
private static bool IsConflictActivity(School school, string personId)
|
||||
{
|
||||
var action = ActivityOf(school, personId);
|
||||
return TalkActions.IsConflict(action);
|
||||
}
|
||||
|
||||
private static string? ActivityOf(School school, string personId) =>
|
||||
school.CapturePresence().Single(row => row.PersonId == personId).ActionId;
|
||||
|
||||
private static Dictionary<string, float> NeedsOf(School school, string personId)
|
||||
{
|
||||
var values = new Dictionary<string, float>(StringComparer.Ordinal);
|
||||
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))
|
||||
{
|
||||
foreach (var (key, value) in needs.Values)
|
||||
{
|
||||
values[key] = value;
|
||||
}
|
||||
}
|
||||
});
|
||||
return values;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user