using System.Linq;
using BlaxLaws;
using RimWorld;
using Verse;
namespace BlaxLawsTests
{
///
/// "Jail for N days", holds: a pawn that gets an ideoligion role while its case waits for a prisoner bed
/// is held once the bed exists, not arrested (Ideology). The mechanitor variant is JailTests.HoldAfterOtherWait.
///
public static class HoldRoleTests
{
[BlaxTest("jail: a pawn that gets a role while its case waits for a bed is held, not arrested, once the bed is built (needs Ideology)")]
public static void HoldRoleAfterBedWait()
{
T.NeedsDlc("Ideology", ModsConfig.IdeologyActive);
Pawn o = OffenderWithRoleOpen(out Precept_Role role);
EnforceFixtures.Warden(EnforceFixtures.CellNear(o.Position, 2f, 6f));
LawCase c = EnforceFixtures.ActCase(o, "Jail", 3);
EnforceFixtures.Dispatch(c, LawStage.DidIt);
EnforceFixtures.CheckWait(c, WaitReasons.NoBed, "fixture: the wait without a prisoner bed");
AssignRole(o, role);
EnforceFixtures.PrisonCell();
JailFlow.TryArrest(c, ignoreHold: false);
T.Equal(WaitReasons.Hold, c.waitReason, "the wait after the pawn got a role");
T.Check(!JailFlow.ArrestRuns(c) && !o.IsPrisonerOfColony, "Laws arrested a role holder on its own after a bed wait");
T.Check(!EnforceFixtures.AnyEnforcementJobOn(o), "an enforcement job on the role holder after the bed wait");
T.Check(!c.holdLifted, "the hold counts as lifted without Arrest now");
}
[BlaxTest("jail: a pawn that gets a role while its case waits for a bed: the sentence sweep after the bed is built holds it with one hold letter, and Arrest now arrests (needs Ideology)")]
public static void HoldRoleAfterBedWaitSweep()
{
T.NeedsDlc("Ideology", ModsConfig.IdeologyActive);
Pawn o = OffenderWithRoleOpen(out Precept_Role role);
EnforceFixtures.Warden(EnforceFixtures.CellNear(o.Position, 2f, 6f));
LawCase c = EnforceFixtures.ActCase(o, "Jail", 3);
string holdLabel = "BlaxLaws_LetterHoldLabel".Translate(c.OffenderLabel);
string noBedLabel = "BlaxLaws_LetterNoBedLabel".Translate(c.OffenderLabel);
EnforceFixtures.Dispatch(c, LawStage.DidIt);
EnforceFixtures.CheckWait(c, WaitReasons.NoBed, "fixture: the wait without a prisoner bed");
T.Equal(0, EnforceFixtures.LettersLabeled(holdLabel), "fixture: hold letters before the role");
AssignRole(o, role);
EnforceFixtures.PrisonCell();
// The no-bed wait retries only at the sentence sweep (PunishmentWorker_Jail.Sweep); a second one
// shows the hold stays and its letter goes out once.
EnforceFixtures.TickPastSentenceSweep();
T.Equal(WaitReasons.Hold, c.waitReason, "the wait at the sentence sweep after the bed was built");
T.Check(!EnforceFixtures.AnyEnforcementJobOn(o) && !o.IsPrisonerOfColony, "the sweep arrested a role holder after a bed wait");
EnforceFixtures.TickPastSentenceSweep();
T.Check(!EnforceFixtures.AnyEnforcementJobOn(o) && !o.IsPrisonerOfColony, "a later sweep arrested the held role holder");
T.Equal(WaitReasons.Hold, c.waitReason, "the wait at the second sentence sweep");
T.Equal(1, EnforceFixtures.LettersLabeled(holdLabel), "hold letters");
T.Equal(1, EnforceFixtures.LettersLabeled(noBedLabel), "no-bed letters");
T.Check(Sentences.CanArrestNow(c), "Arrest now isn't offered for the held case");
Sentences.ArrestNow(c);
T.Check(JailFlow.ArrestRuns(c), "Arrest now started no arrest; wait " + c.waitReason);
}
///
/// A fresh offender with an active role of its ideoligion whose requirements it meets, a multi role
/// when there is one, not yet assigned. Skips when ten generated colonists fit no role.
///
private static Pawn OffenderWithRoleOpen(out Precept_Role role)
{
role = null;
for (int i = 0; i < 10; i++)
{
Pawn candidate = EnforceFixtures.Offender();
role = RoleFor(candidate);
if (role != null)
return candidate;
}
T.Skip("ten generated colonists met the requirements of no role of their ideoligion");
return null;
}
private static Precept_Role RoleFor(Pawn o)
{
Ideo ideo = o.Ideo;
if (ideo == null)
return null;
return ideo.PreceptsListForReading.OfType().FirstOrDefault(r => r.Active && r.RequirementsMet(o))
?? (Precept_Role)ideo.PreceptsListForReading.OfType().FirstOrDefault(r => r.Active && r.RequirementsMet(o));
}
///
/// Assigns the role, undone before the cleanup, with a single role's previous holder put back. A tick
/// later the pawn must still hold it: a role whose requirements it misses is taken away on the role
/// tick (Precept_RoleSingle.cs:66-70; Precept_Role.cs:508-520).
///
private static void AssignRole(Pawn o, Precept_Role role)
{
Pawn previous = (role as Precept_RoleSingle)?.ChosenPawnValue;
role.Assign(o, addThoughts: false);
Fixtures.OnCleanup(() =>
{
if (role.IsAssigned(o))
role.Unassign(o, generateThoughts: false);
if (previous != null && !previous.Destroyed && role is Precept_RoleSingle single && single.ChosenPawnValue == null)
single.Assign(previous, addThoughts: false);
});
Fixtures.Tick(1);
T.Check(DlcSupport.HoldsRole(o), "fixture: the colonist holds no role a tick after the assignment");
}
}
}