Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correctly import attackers and defenders in wars. #614

Merged
merged 1 commit into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 15 additions & 17 deletions src/vic3_world/wars/war_importer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@ vic3::WarImporter::WarImporter()
war_parser_.registerKeyword("initiator", [this](std::istream& input) {
initiator_ = commonItems::getInt(input);
});
war_parser_.registerKeyword("initiators", [this](std::istream& input) {
additional_initiators_ = commonItems::getInts(input);
});
war_parser_.registerKeyword("target", [this](std::istream& input) {
target_ = commonItems::getInt(input);
});
war_parser_.registerKeyword("involved", [this](std::istream& input) {
involved_ = commonItems::getInts(input);
war_parser_.registerKeyword("targets", [this](std::istream& input) {
additional_targets_ = commonItems::getInts(input);
});
war_parser_.registerKeyword("war", [this](std::istream& input) {
war_ = commonItems::getLlong(input);
Expand All @@ -29,8 +32,9 @@ vic3::WarImporter::WarImporter()
std::optional<vic3::War> vic3::WarImporter::ImportWar(std::istream& input)
{
initiator_.reset();
additional_initiators_.clear();
target_.reset();
involved_.clear();
additional_targets_.clear();
war_.reset();

war_parser_.parseStream(input);
Expand All @@ -41,22 +45,16 @@ std::optional<vic3::War> vic3::WarImporter::ImportWar(std::istream& input)
}

std::set<int> attackers;
attackers.insert(*initiator_);
attackers.insert(additional_initiators_.begin(), additional_initiators_.end());
std::set<int> defenders;
defenders.insert(*target_);
defenders.insert(additional_targets_.begin(), additional_targets_.end());

auto involved_itr = involved_.begin();
while (involved_itr != involved_.end() && *involved_itr != *target_)
{
attackers.insert(*involved_itr);
++involved_itr;
}
while (involved_itr != involved_.end())
{
defenders.insert(*involved_itr);
++involved_itr;
}

return vic3::War({.original_attacker = *initiator_,
return vic3::War({
.original_attacker = *initiator_,
.attackers = attackers,
.original_defender = *target_,
.defenders = defenders});
.defenders = defenders,
});
}
3 changes: 2 additions & 1 deletion src/vic3_world/wars/war_importer.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ class WarImporter
commonItems::parser war_parser_;

std::optional<int> initiator_;
std::vector<int> additional_initiators_;
std::optional<int> target_;
std::vector<int> involved_;
std::vector<int> additional_targets_;
std::optional<int64_t> war_;
};

Expand Down
16 changes: 13 additions & 3 deletions src/vic3_world/wars/war_importer_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,13 @@ TEST(Vic3WorldWarImporter, WarCanBeImported)
)";
std::optional<War> war = importer.ImportWar(input);
EXPECT_TRUE(war.has_value());
EXPECT_EQ(war.value_or(War({})), War({.original_attacker = 144, .original_defender = 169}));
EXPECT_EQ(war.value_or(War({})),
War({
.original_attacker = 144,
.attackers = {144},
.original_defender = 169,
.defenders = {169},
}));
}


Expand Down Expand Up @@ -95,8 +101,10 @@ TEST(Vic3WorldWarImporter, AttackersAreExtracted)
{
initiator=144
target=169
initiators={
149 154 159 164
}
war=196
involved={ 144 149 154 159 164 169 }
}
)";
std::optional<War> war = importer.ImportWar(input);
Expand All @@ -114,7 +122,9 @@ TEST(Vic3WorldWarImporter, DefendersAreExtracted)
initiator=144
target=169
war=196
involved={ 144 169 170 171 172 }
targets={
170 171 172
}
}
)";
std::optional<War> war = importer.ImportWar(input);
Expand Down
14 changes: 12 additions & 2 deletions src/vic3_world/wars/wars_importer_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,18 @@ database={

const std::vector<War> wars = ImportWars(input);
EXPECT_THAT(wars,
testing::ElementsAre(War({.original_attacker = 144, .original_defender = 169}),
War({.original_attacker = 12, .original_defender = 13})));
testing::ElementsAre(War({
.original_attacker = 144,
.attackers = {144},
.original_defender = 169,
.defenders = {169},
}),
War({
.original_attacker = 12,
.attackers = {12},
.original_defender = 13,
.defenders = {13},
})));
}


Expand Down