Skip to content

Commit caa502b

Browse files
authored
Set employer for every unlanded character from Imperator (#417) #minor
* HomeCountry * SetEmployer * OutputEmployer * RemoveEmployerIdFromLandedCharacters * CountryIsNotLinkedWithoutParsedId test
1 parent d149eab commit caa502b

File tree

8 files changed

+89
-5
lines changed

8 files changed

+89
-5
lines changed

ImperatorToCK3.UnitTests/Imperator/Characters/CharacterTests.cs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public void FieldsCanBeSet() {
1212
var reader = new BufferedReader(
1313
"= {" +
1414
"\tcountry=69" +
15+
"\thome_country=68" +
1516
"\tculture=\"paradoxian\"" +
1617
"\treligion=\"orthodox\"" +
1718
"\tfemale=yes" +
@@ -34,6 +35,7 @@ public void FieldsCanBeSet() {
3435
"\tdna=\"paradoxianDna\"" +
3536
"\tage=56\n" +
3637
"\tprovince=69" +
38+
"\tprisoner_home=68" +
3739
"}"
3840
);
3941
var character = ImperatorToCK3.Imperator.Characters.Character.Parse(reader, "42", genesDB);
@@ -47,12 +49,22 @@ public void FieldsCanBeSet() {
4749
Assert.Equal((ulong)42, character.Id);
4850

4951
Assert.Null(character.Country); // we have a country id, but no linked country yet
50-
var countriesReader = new BufferedReader("={ 69={} }");
52+
var countriesReader = new BufferedReader("={ 69={} 68={} }");
5153
var countries = new ImperatorToCK3.Imperator.Countries.Countries(countriesReader);
5254
character.LinkCountry(countries);
5355
Assert.NotNull(character.Country);
5456
Assert.Equal((ulong)69, character.Country.Id);
5557

58+
Assert.Null(character.HomeCountry); // we have a home country id, but no linked home country yet
59+
character.LinkHomeCountry(countries);
60+
Assert.NotNull(character.HomeCountry);
61+
Assert.Equal((ulong)68, character.HomeCountry.Id);
62+
63+
Assert.Null(character.PrisonerHome); // we have a prisoner home id, but no linked prisoner home yet
64+
character.LinkPrisonerHome(countries);
65+
Assert.NotNull(character.PrisonerHome);
66+
Assert.Equal((ulong)68, character.PrisonerHome.Id);
67+
5668
Assert.Equal("paradoxian", character.Culture);
5769
Assert.Equal("orthodox", character.Religion);
5870
Assert.True(character.Female);
@@ -264,5 +276,17 @@ public void IgnoredTokensAreSaved() {
264276
};
265277
Assert.True(ImperatorToCK3.Imperator.Characters.Character.IgnoredTokens.SetEquals(expectedIgnoredTokens));
266278
}
279+
280+
[Fact]
281+
public void CountryIsNotLinkedWithoutParsedId() {
282+
var character = new ImperatorToCK3.Imperator.Characters.Character(1);
283+
var countries = new ImperatorToCK3.Imperator.Countries.Countries();
284+
character.LinkCountry(countries);
285+
character.LinkHomeCountry(countries);
286+
character.LinkPrisonerHome(countries);
287+
Assert.Null(character.Country);
288+
Assert.Null(character.HomeCountry);
289+
Assert.Null(character.PrisonerHome);
290+
}
267291
}
268292
}

ImperatorToCK3/CK3/Characters/Character.cs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,15 @@ Date ck3BookmarkDate
204204
DeathReason = deathReasonMapper.GetCK3ReasonForImperatorReason(impDeathReason);
205205
}
206206

207-
if (ImperatorCharacter.PrisonerHome is not null) {
207+
// if character is imprisoned, set jailor
208+
SetJailor();
209+
SetEmployer();
210+
211+
void SetJailor() {
212+
if (ImperatorCharacter.PrisonerHome is null) {
213+
return;
214+
}
215+
208216
var prisonCountry = ImperatorCharacter.Country;
209217
if (prisonCountry is null) {
210218
Logger.Warn($"Imperator character {ImperatorCharacter.Id} is imprisoned but has no country!");
@@ -214,6 +222,16 @@ Date ck3BookmarkDate
214222
jailorId = prisonCountry.CK3Title.GetHolderId(dateOnConversion);
215223
}
216224
}
225+
226+
void SetEmployer() {
227+
var prisonerHome = ImperatorCharacter.PrisonerHome;
228+
var homeCountry = ImperatorCharacter.HomeCountry;
229+
if (prisonerHome?.CK3Title is not null) { // is imprisoned
230+
EmployerId = prisonerHome.CK3Title.GetHolderId(dateOnConversion);
231+
} else if (homeCountry?.CK3Title is not null) {
232+
EmployerId = homeCountry.CK3Title.GetHolderId(dateOnConversion);
233+
}
234+
}
217235
}
218236

219237
public void BreakAllLinks() {
@@ -299,7 +317,8 @@ public Character? Father {
299317

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

302-
private readonly string? jailorId;
320+
private string? jailorId;
321+
public string? EmployerId { get; set; }
303322

304323
public bool LinkJailor(Characters characters) {
305324
if (jailorId is null) {

ImperatorToCK3/CK3/Characters/Characters.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,9 +132,9 @@ private void LinkPrisoners() {
132132
}
133133

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

140140
foreach (var characterId in farewellIds) {
@@ -143,5 +143,12 @@ public void PurgeLandlessVanillaCharacters(LandedTitles titles, Date ck3Bookmark
143143
}
144144
Logger.Info($"Purged {farewellIds.Count()} landless vanilla characters.");
145145
}
146+
147+
public void RemoveEmployerIdFromLandedCharacters(LandedTitles titles, Date conversionDate) {
148+
var landedCharacterIds = titles.GetHolderIds(conversionDate);
149+
foreach (var character in Values.Where(character => landedCharacterIds.Contains(character.Id))) {
150+
character.EmployerId = null;
151+
}
152+
}
146153
}
147154
}

ImperatorToCK3/CK3/Titles/LandedTitles.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ public void EraseTitle(string name) {
6969
return null;
7070
}
7171

72+
public HashSet<string> GetHolderIds(Date date) {
73+
return new HashSet<string>(Values.Select(t => t.GetHolderId(date)));
74+
}
75+
7276
private void RegisterKeys(Parser parser) {
7377
parser.RegisterRegex(@"(e|k|d|c|b)_[A-Za-z0-9_\-\']+", (reader, titleNameStr) => {
7478
// Pull the titles beneath this one and add them to the lot, overwriting existing ones.

ImperatorToCK3/CK3/World.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using commonItems;
2+
using ImperatorToCK3.CK3.Characters;
23
using ImperatorToCK3.CK3.Dynasties;
34
using ImperatorToCK3.CK3.Provinces;
45
using ImperatorToCK3.CK3.Titles;
@@ -88,6 +89,7 @@ public World(Imperator.World impWorld, Configuration theConfiguration) {
8889
RemoveInvalidLandlessTitles(theConfiguration.Ck3BookmarkDate);
8990

9091
Characters.PurgeLandlessVanillaCharacters(LandedTitles, theConfiguration.Ck3BookmarkDate);
92+
Characters.RemoveEmployerIdFromLandedCharacters(LandedTitles, impWorld.EndDate);
9193
}
9294

9395
private void ClearFeaturedCharactersDescriptions(Date ck3BookmarkDate) {

ImperatorToCK3/Imperator/Characters/Character.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ public Character(ulong id) {
1313

1414
private ulong? parsedCountryId;
1515
public Country? Country { get; set; }
16+
private ulong? parsedHomeCountryId;
17+
public Country? HomeCountry { get; set; }
1618
private ulong? parsedPrisonerHomeId;
1719
public Country? PrisonerHome { get; private set; }
1820

@@ -108,6 +110,7 @@ static Character() {
108110
parsedCharacter.CustomName = characterName.CustomName;
109111
});
110112
parser.RegisterKeyword("country", reader => parsedCharacter.parsedCountryId = ParserHelpers.GetULong(reader));
113+
parser.RegisterKeyword("home_country", reader => parsedCharacter.parsedHomeCountryId = ParserHelpers.GetULong(reader));
111114
parser.RegisterKeyword("province", reader => parsedCharacter.ProvinceId = ParserHelpers.GetULong(reader));
112115
parser.RegisterKeyword("culture", reader => parsedCharacter.culture = ParserHelpers.GetString(reader));
113116
parser.RegisterKeyword("religion", reader => parsedCharacter.Religion = ParserHelpers.GetString(reader));
@@ -182,6 +185,20 @@ public bool LinkCountry(Countries.Countries countries) {
182185
return false;
183186
}
184187

188+
// Returns whether a country was linked
189+
public bool LinkHomeCountry(Countries.Countries countries) {
190+
if (parsedHomeCountryId is null) {
191+
return false;
192+
}
193+
var homeCountryId = (ulong)parsedHomeCountryId;
194+
if (countries.TryGetValue(homeCountryId, out var countryToLink)) {
195+
HomeCountry = countryToLink;
196+
return true;
197+
}
198+
Logger.Warn($"Country with ID {homeCountryId} has no definition!");
199+
return false;
200+
}
201+
185202
// Returns whether a country was linked
186203
public bool LinkPrisonerHome(Countries.Countries countries) {
187204
if (parsedPrisonerHomeId is null) {

ImperatorToCK3/Imperator/Characters/Characters.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ public void LinkCountries(Countries.Countries countries) {
5454
var counter = Values.Count(character => character.LinkCountry(countries));
5555
Logger.Info($"{counter} countries linked to characters.");
5656

57+
counter = Values.Count(character => character.LinkHomeCountry(countries));
58+
Logger.Info($"{counter} home countries linked to characters.");
59+
5760
counter = Values.Count(character => character.LinkPrisonerHome(countries));
5861
Logger.Info($"{counter} prisoner homes linked to characters.");
5962
}

ImperatorToCK3/Outputter/CharacterOutputter.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ public static void OutputCharacter(StreamWriter output, Character character, Dat
6262

6363
OutputBirthAndDeathDates(output, character);
6464
OutputPrisoners(output, character, conversionDate);
65+
OutputEmployer(output, character, conversionDate);
6566

6667
output.WriteLine("}");
6768
}
@@ -92,5 +93,12 @@ private static void OutputPrisoners(TextWriter output, Character character, Date
9293
}
9394
output.WriteLine("\t}");
9495
}
96+
private static void OutputEmployer(TextWriter output, Character character, Date conversionDate) {
97+
if (character.EmployerId is null) {
98+
return;
99+
}
100+
101+
output.WriteLine($"\t{conversionDate}={{employer={character.EmployerId}}}");
102+
}
95103
}
96104
}

0 commit comments

Comments
 (0)