Skip to content

Commit

Permalink
Set employer for every unlanded character from Imperator (#417) #minor
Browse files Browse the repository at this point in the history
* HomeCountry

* SetEmployer

* OutputEmployer

* RemoveEmployerIdFromLandedCharacters

* CountryIsNotLinkedWithoutParsedId test
  • Loading branch information
IhateTrains authored Nov 21, 2021
1 parent d149eab commit caa502b
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 5 deletions.
26 changes: 25 additions & 1 deletion ImperatorToCK3.UnitTests/Imperator/Characters/CharacterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public void FieldsCanBeSet() {
var reader = new BufferedReader(
"= {" +
"\tcountry=69" +
"\thome_country=68" +
"\tculture=\"paradoxian\"" +
"\treligion=\"orthodox\"" +
"\tfemale=yes" +
Expand All @@ -34,6 +35,7 @@ public void FieldsCanBeSet() {
"\tdna=\"paradoxianDna\"" +
"\tage=56\n" +
"\tprovince=69" +
"\tprisoner_home=68" +
"}"
);
var character = ImperatorToCK3.Imperator.Characters.Character.Parse(reader, "42", genesDB);
Expand All @@ -47,12 +49,22 @@ public void FieldsCanBeSet() {
Assert.Equal((ulong)42, character.Id);

Assert.Null(character.Country); // we have a country id, but no linked country yet
var countriesReader = new BufferedReader("={ 69={} }");
var countriesReader = new BufferedReader("={ 69={} 68={} }");
var countries = new ImperatorToCK3.Imperator.Countries.Countries(countriesReader);
character.LinkCountry(countries);
Assert.NotNull(character.Country);
Assert.Equal((ulong)69, character.Country.Id);

Assert.Null(character.HomeCountry); // we have a home country id, but no linked home country yet
character.LinkHomeCountry(countries);
Assert.NotNull(character.HomeCountry);
Assert.Equal((ulong)68, character.HomeCountry.Id);

Assert.Null(character.PrisonerHome); // we have a prisoner home id, but no linked prisoner home yet
character.LinkPrisonerHome(countries);
Assert.NotNull(character.PrisonerHome);
Assert.Equal((ulong)68, character.PrisonerHome.Id);

Assert.Equal("paradoxian", character.Culture);
Assert.Equal("orthodox", character.Religion);
Assert.True(character.Female);
Expand Down Expand Up @@ -264,5 +276,17 @@ public void IgnoredTokensAreSaved() {
};
Assert.True(ImperatorToCK3.Imperator.Characters.Character.IgnoredTokens.SetEquals(expectedIgnoredTokens));
}

[Fact]
public void CountryIsNotLinkedWithoutParsedId() {
var character = new ImperatorToCK3.Imperator.Characters.Character(1);
var countries = new ImperatorToCK3.Imperator.Countries.Countries();
character.LinkCountry(countries);
character.LinkHomeCountry(countries);
character.LinkPrisonerHome(countries);
Assert.Null(character.Country);
Assert.Null(character.HomeCountry);
Assert.Null(character.PrisonerHome);
}
}
}
23 changes: 21 additions & 2 deletions ImperatorToCK3/CK3/Characters/Character.cs
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,15 @@ Date ck3BookmarkDate
DeathReason = deathReasonMapper.GetCK3ReasonForImperatorReason(impDeathReason);
}

if (ImperatorCharacter.PrisonerHome is not null) {
// if character is imprisoned, set jailor
SetJailor();
SetEmployer();

void SetJailor() {
if (ImperatorCharacter.PrisonerHome is null) {
return;
}

var prisonCountry = ImperatorCharacter.Country;
if (prisonCountry is null) {
Logger.Warn($"Imperator character {ImperatorCharacter.Id} is imprisoned but has no country!");
Expand All @@ -214,6 +222,16 @@ Date ck3BookmarkDate
jailorId = prisonCountry.CK3Title.GetHolderId(dateOnConversion);
}
}

void SetEmployer() {
var prisonerHome = ImperatorCharacter.PrisonerHome;
var homeCountry = ImperatorCharacter.HomeCountry;
if (prisonerHome?.CK3Title is not null) { // is imprisoned
EmployerId = prisonerHome.CK3Title.GetHolderId(dateOnConversion);
} else if (homeCountry?.CK3Title is not null) {
EmployerId = homeCountry.CK3Title.GetHolderId(dateOnConversion);
}
}
}

public void BreakAllLinks() {
Expand Down Expand Up @@ -299,7 +317,8 @@ public Character? Father {

public string? DynastyId { get; set; } // not always set

private readonly string? jailorId;
private string? jailorId;
public string? EmployerId { get; set; }

public bool LinkJailor(Characters characters) {
if (jailorId is null) {
Expand Down
11 changes: 9 additions & 2 deletions ImperatorToCK3/CK3/Characters/Characters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,9 @@ private void LinkPrisoners() {
}

public void PurgeLandlessVanillaCharacters(LandedTitles titles, Date ck3BookmarkDate) {
var landedCharacterIdSelect = titles.Values.Select(t => t.GetHolderId(ck3BookmarkDate));
var landedCharacterIds = titles.GetHolderIds(ck3BookmarkDate);
var farewellIds = Keys.Where(
id => !id.StartsWith("imperator") && !landedCharacterIdSelect.Contains(id)
id => !id.StartsWith("imperator") && !landedCharacterIds.Contains(id)
);

foreach (var characterId in farewellIds) {
Expand All @@ -143,5 +143,12 @@ public void PurgeLandlessVanillaCharacters(LandedTitles titles, Date ck3Bookmark
}
Logger.Info($"Purged {farewellIds.Count()} landless vanilla characters.");
}

public void RemoveEmployerIdFromLandedCharacters(LandedTitles titles, Date conversionDate) {
var landedCharacterIds = titles.GetHolderIds(conversionDate);
foreach (var character in Values.Where(character => landedCharacterIds.Contains(character.Id))) {
character.EmployerId = null;
}
}
}
}
4 changes: 4 additions & 0 deletions ImperatorToCK3/CK3/Titles/LandedTitles.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ public void EraseTitle(string name) {
return null;
}

public HashSet<string> GetHolderIds(Date date) {
return new HashSet<string>(Values.Select(t => t.GetHolderId(date)));
}

private void RegisterKeys(Parser parser) {
parser.RegisterRegex(@"(e|k|d|c|b)_[A-Za-z0-9_\-\']+", (reader, titleNameStr) => {
// Pull the titles beneath this one and add them to the lot, overwriting existing ones.
Expand Down
2 changes: 2 additions & 0 deletions ImperatorToCK3/CK3/World.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using commonItems;
using ImperatorToCK3.CK3.Characters;
using ImperatorToCK3.CK3.Dynasties;
using ImperatorToCK3.CK3.Provinces;
using ImperatorToCK3.CK3.Titles;
Expand Down Expand Up @@ -88,6 +89,7 @@ public World(Imperator.World impWorld, Configuration theConfiguration) {
RemoveInvalidLandlessTitles(theConfiguration.Ck3BookmarkDate);

Characters.PurgeLandlessVanillaCharacters(LandedTitles, theConfiguration.Ck3BookmarkDate);
Characters.RemoveEmployerIdFromLandedCharacters(LandedTitles, impWorld.EndDate);
}

private void ClearFeaturedCharactersDescriptions(Date ck3BookmarkDate) {
Expand Down
17 changes: 17 additions & 0 deletions ImperatorToCK3/Imperator/Characters/Character.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ public Character(ulong id) {

private ulong? parsedCountryId;
public Country? Country { get; set; }
private ulong? parsedHomeCountryId;
public Country? HomeCountry { get; set; }
private ulong? parsedPrisonerHomeId;
public Country? PrisonerHome { get; private set; }

Expand Down Expand Up @@ -108,6 +110,7 @@ static Character() {
parsedCharacter.CustomName = characterName.CustomName;
});
parser.RegisterKeyword("country", reader => parsedCharacter.parsedCountryId = ParserHelpers.GetULong(reader));
parser.RegisterKeyword("home_country", reader => parsedCharacter.parsedHomeCountryId = ParserHelpers.GetULong(reader));
parser.RegisterKeyword("province", reader => parsedCharacter.ProvinceId = ParserHelpers.GetULong(reader));
parser.RegisterKeyword("culture", reader => parsedCharacter.culture = ParserHelpers.GetString(reader));
parser.RegisterKeyword("religion", reader => parsedCharacter.Religion = ParserHelpers.GetString(reader));
Expand Down Expand Up @@ -182,6 +185,20 @@ public bool LinkCountry(Countries.Countries countries) {
return false;
}

// Returns whether a country was linked
public bool LinkHomeCountry(Countries.Countries countries) {
if (parsedHomeCountryId is null) {
return false;
}
var homeCountryId = (ulong)parsedHomeCountryId;
if (countries.TryGetValue(homeCountryId, out var countryToLink)) {
HomeCountry = countryToLink;
return true;
}
Logger.Warn($"Country with ID {homeCountryId} has no definition!");
return false;
}

// Returns whether a country was linked
public bool LinkPrisonerHome(Countries.Countries countries) {
if (parsedPrisonerHomeId is null) {
Expand Down
3 changes: 3 additions & 0 deletions ImperatorToCK3/Imperator/Characters/Characters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ public void LinkCountries(Countries.Countries countries) {
var counter = Values.Count(character => character.LinkCountry(countries));
Logger.Info($"{counter} countries linked to characters.");

counter = Values.Count(character => character.LinkHomeCountry(countries));
Logger.Info($"{counter} home countries linked to characters.");

counter = Values.Count(character => character.LinkPrisonerHome(countries));
Logger.Info($"{counter} prisoner homes linked to characters.");
}
Expand Down
8 changes: 8 additions & 0 deletions ImperatorToCK3/Outputter/CharacterOutputter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public static void OutputCharacter(StreamWriter output, Character character, Dat

OutputBirthAndDeathDates(output, character);
OutputPrisoners(output, character, conversionDate);
OutputEmployer(output, character, conversionDate);

output.WriteLine("}");
}
Expand Down Expand Up @@ -92,5 +93,12 @@ private static void OutputPrisoners(TextWriter output, Character character, Date
}
output.WriteLine("\t}");
}
private static void OutputEmployer(TextWriter output, Character character, Date conversionDate) {
if (character.EmployerId is null) {
return;
}

output.WriteLine($"\t{conversionDate}={{employer={character.EmployerId}}}");
}
}
}

0 comments on commit caa502b

Please sign in to comment.