Skip to content

Commit

Permalink
Simplify Imperator mother and father code (#415)
Browse files Browse the repository at this point in the history
* Simplify Imperator mother and father code

* Fix test
  • Loading branch information
IhateTrains authored Nov 21, 2021
1 parent 8ccd6d2 commit d149eab
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 71 deletions.
4 changes: 2 additions & 2 deletions ImperatorToCK3.UnitTests/CK3/Characters/CK3CharacterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ public void AllLinksCanBeRemoved() {
var imperatorChild = new ImperatorToCK3.Imperator.Characters.Character(4);
var imperatorSpouse = new ImperatorToCK3.Imperator.Characters.Character(5);

imperatorCharacter.Mother = new(imperatorMother.Id, imperatorMother);
imperatorCharacter.Father = new(imperatorFather.Id, imperatorFather);
imperatorCharacter.Mother = imperatorMother;
imperatorCharacter.Father = imperatorFather;
imperatorCharacter.Children.Add(imperatorChild.Id, imperatorChild);
imperatorCharacter.Spouses.Add(imperatorSpouse.Id, imperatorSpouse);

Expand Down
20 changes: 16 additions & 4 deletions ImperatorToCK3.UnitTests/Imperator/Characters/CharacterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,20 @@ public void FieldsCanBeSet() {
Assert.Equal((ulong)420, item.Key);
}
);
Assert.Equal((ulong)123, character.Mother.Key);
Assert.Equal((ulong)124, character.Father.Key);

Assert.Null(character.Mother); // mother not linked yet
Assert.Null(character.Father); // father not linked yet
var mother = new ImperatorToCK3.Imperator.Characters.Character(123);
var father = new ImperatorToCK3.Imperator.Characters.Character(124);
characters.Add(mother);
characters.Add(father);
character.LinkMother(characters);
character.LinkFather(characters);
Assert.NotNull(character.Mother);
Assert.NotNull(character.Father);
Assert.Equal((ulong)123, character.Mother.Id);
Assert.Equal((ulong)124, character.Father.Id);

Assert.Null(character.Family); // Despite "family=125" in character definition, Family is null until linked.
Assert.Equal(420.5, character.Wealth);
Assert.Equal("Biggus_Dickus", character.Name);
Expand Down Expand Up @@ -122,8 +134,8 @@ public void FieldsDefaultToCorrectValues() {
Assert.Null(character.DeathReason);
Assert.Empty(character.Spouses);
Assert.Empty(character.Children);
Assert.Equal((ulong)0, character.Mother.Key);
Assert.Equal((ulong)0, character.Father.Key);
Assert.Null(character.Mother);
Assert.Null(character.Father);
Assert.Null(character.Family);
Assert.Equal(0, character.Wealth);
Assert.Equal(string.Empty, character.Name);
Expand Down
8 changes: 4 additions & 4 deletions ImperatorToCK3/CK3/Characters/Character.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,12 +142,12 @@ Date ck3BookmarkDate

// Determine valid (not dropped in province mappings) "source province" to be used by religion mapper. Don't give up without a fight.
var impProvForProvinceMapper = ImperatorCharacter.ProvinceId;
if (provinceMapper.GetCK3ProvinceNumbers(impProvForProvinceMapper).Count == 0 && ImperatorCharacter.Father.Value is not null) {
impProvForProvinceMapper = ImperatorCharacter.Father.Value.ProvinceId;
if (provinceMapper.GetCK3ProvinceNumbers(impProvForProvinceMapper).Count == 0 && ImperatorCharacter.Father is not null) {
impProvForProvinceMapper = ImperatorCharacter.Father.ProvinceId;
}

if (provinceMapper.GetCK3ProvinceNumbers(impProvForProvinceMapper).Count == 0 && ImperatorCharacter.Mother.Value is not null) {
impProvForProvinceMapper = ImperatorCharacter.Mother.Value.ProvinceId;
if (provinceMapper.GetCK3ProvinceNumbers(impProvForProvinceMapper).Count == 0 && ImperatorCharacter.Mother is not null) {
impProvForProvinceMapper = ImperatorCharacter.Mother.ProvinceId;
}

if (provinceMapper.GetCK3ProvinceNumbers(impProvForProvinceMapper).Count == 0 && ImperatorCharacter.Spouses.Count > 0) {
Expand Down
4 changes: 2 additions & 2 deletions ImperatorToCK3/CK3/Characters/Characters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ private void LinkMothersAndFathers() {
// imperatorRegnal characters do not have ImperatorCharacter
continue;
}
var impMotherCharacter = ck3Character.ImperatorCharacter.Mother.Value;
var impMotherCharacter = ck3Character.ImperatorCharacter.Mother;
if (impMotherCharacter is not null) {
var ck3MotherCharacter = impMotherCharacter.CK3Character;
ck3Character.Mother = ck3MotherCharacter;
Expand All @@ -93,7 +93,7 @@ private void LinkMothersAndFathers() {
}

// make links between Imperator characters
var impFatherCharacter = ck3Character.ImperatorCharacter.Father.Value;
var impFatherCharacter = ck3Character.ImperatorCharacter.Father;
if (impFatherCharacter is not null) {
var ck3FatherCharacter = impFatherCharacter.CK3Character;
ck3Character.Father = ck3FatherCharacter;
Expand Down
86 changes: 27 additions & 59 deletions ImperatorToCK3/Imperator/Characters/Character.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,10 @@ internal bool LinkFamily(Families.Families families, SortedSet<ulong> missingDef
public Dictionary<ulong, Character> Spouses { get; set; } = new();
private HashSet<ulong> parsedChildrenIds = new();
public Dictionary<ulong, Character> Children { get; set; } = new();
public KeyValuePair<ulong, Character?> Mother { get; set; } = new();
public KeyValuePair<ulong, Character?> Father { get; set; } = new();
private ulong? parsedMotherId;
public Character? Mother { get; set; }
private ulong? parsedFatherId;
public Character? Father { get; set; }
private ulong? parsedFamilyId;
private Family? family;
public Family? Family {
Expand Down Expand Up @@ -105,24 +107,12 @@ static Character() {
parsedCharacter.Name = characterName.Name;
parsedCharacter.CustomName = characterName.CustomName;
});
parser.RegisterKeyword("country", reader => {
parsedCharacter.parsedCountryId = 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);
});
parser.RegisterKeyword("female", reader =>
parsedCharacter.Female = new ParadoxBool(reader)
);
parser.RegisterKeyword("traits", reader => {
parsedCharacter.Traits = ParserHelpers.GetStrings(reader);
});
parser.RegisterKeyword("country", reader => parsedCharacter.parsedCountryId = 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));
parser.RegisterKeyword("female", reader => parsedCharacter.Female = new ParadoxBool(reader));
parser.RegisterKeyword("traits", reader => parsedCharacter.Traits = ParserHelpers.GetStrings(reader));
parser.RegisterKeyword("birth_date", reader => {
var dateStr = ParserHelpers.GetString(reader);
parsedCharacter.BirthDate = new Date(dateStr, true); // converted to AD
Expand All @@ -131,39 +121,17 @@ static Character() {
var dateStr = ParserHelpers.GetString(reader);
parsedCharacter.DeathDate = new Date(dateStr, true); // converted to AD
});
parser.RegisterKeyword("death", reader => {
parsedCharacter.DeathReason = ParserHelpers.GetString(reader);
});
parser.RegisterKeyword("age", reader => {
parsedCharacter.Age = (uint)ParserHelpers.GetInt(reader);
});
parser.RegisterKeyword("nickname", reader => {
parsedCharacter.Nickname = ParserHelpers.GetString(reader);
});
parser.RegisterKeyword("family", reader => {
parsedCharacter.parsedFamilyId = ParserHelpers.GetULong(reader);
});
parser.RegisterKeyword("dna", reader => {
parsedCharacter.DNA = ParserHelpers.GetString(reader);
});
parser.RegisterKeyword("mother", reader => {
parsedCharacter.Mother = new(ParserHelpers.GetULong(reader), null);
});
parser.RegisterKeyword("father", reader => {
parsedCharacter.Father = new(ParserHelpers.GetULong(reader), null);
});
parser.RegisterKeyword("wealth", reader => {
parsedCharacter.Wealth = ParserHelpers.GetDouble(reader);
});
parser.RegisterKeyword("spouse", reader => {
parsedCharacter.parsedSpouseIds = ParserHelpers.GetULongs(reader).ToHashSet();
});
parser.RegisterKeyword("children", reader => {
parsedCharacter.parsedChildrenIds = ParserHelpers.GetULongs(reader).ToHashSet();
});
parser.RegisterKeyword("attributes", reader => {
parsedCharacter.Attributes = CharacterAttributes.Parse(reader);
});
parser.RegisterKeyword("death", reader => parsedCharacter.DeathReason = ParserHelpers.GetString(reader));
parser.RegisterKeyword("age", reader => parsedCharacter.Age = (uint)ParserHelpers.GetInt(reader));
parser.RegisterKeyword("nickname", reader => parsedCharacter.Nickname = ParserHelpers.GetString(reader));
parser.RegisterKeyword("family", reader => parsedCharacter.parsedFamilyId = ParserHelpers.GetULong(reader));
parser.RegisterKeyword("dna", reader => parsedCharacter.DNA = ParserHelpers.GetString(reader));
parser.RegisterKeyword("mother", reader => parsedCharacter.parsedMotherId = ParserHelpers.GetULong(reader));
parser.RegisterKeyword("father", reader => parsedCharacter.parsedFatherId = ParserHelpers.GetULong(reader));
parser.RegisterKeyword("wealth", reader => parsedCharacter.Wealth = ParserHelpers.GetDouble(reader));
parser.RegisterKeyword("spouse", reader => parsedCharacter.parsedSpouseIds = ParserHelpers.GetULongs(reader).ToHashSet());
parser.RegisterKeyword("children", reader => parsedCharacter.parsedChildrenIds = ParserHelpers.GetULongs(reader).ToHashSet());
parser.RegisterKeyword("attributes", reader => parsedCharacter.Attributes = CharacterAttributes.Parse(reader));
parser.RegisterKeyword("prisoner_home", reader => parsedCharacter.parsedPrisonerHomeId = ParserHelpers.GetULong(reader));
parser.RegisterRegex(CommonRegexes.Catchall, (reader, token) => {
IgnoredTokens.Add(token);
Expand Down Expand Up @@ -230,13 +198,13 @@ public bool LinkPrisonerHome(Countries.Countries countries) {

// Returns whether a mother was linked
public bool LinkMother(Characters characters) {
var motherId = Mother.Key;
if (motherId == 0) {
if (parsedMotherId is null) {
return false;
}
ulong motherId = (ulong)parsedMotherId;

if (characters.TryGetValue(motherId, out var motherToLink)) {
Mother = new(motherId, motherToLink);
Mother = motherToLink;
if (!motherToLink.parsedChildrenIds.Contains(Id)) {
Logger.Warn($"Only one-sided link found between character {Id} and mother {motherId}!");
}
Expand All @@ -249,13 +217,13 @@ public bool LinkMother(Characters characters) {

// Returns whether a father was linked
public bool LinkFather(Characters characters) {
var fatherId = Father.Key;
if (fatherId == 0) {
if (parsedFatherId is null) {
return false;
}
ulong fatherId = (ulong)parsedFatherId;

if (characters.TryGetValue(fatherId, out var fatherToLink)) {
Father = new(fatherId, fatherToLink);
Father = fatherToLink;
if (!fatherToLink.parsedChildrenIds.Contains(Id)) {
Logger.Warn($"Only one-sided link found between character {Id} and father {fatherId}!");
}
Expand Down

0 comments on commit d149eab

Please sign in to comment.