-
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ability to import a focus. (#626)
- Loading branch information
Showing
8 changed files
with
332 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#ifndef SRC_HOI4WORLD_FOCUSTREES_FOCUS_H | ||
#define SRC_HOI4WORLD_FOCUSTREES_FOCUS_H | ||
|
||
|
||
|
||
#include <optional> | ||
#include <string> | ||
#include <vector> | ||
|
||
|
||
|
||
namespace hoi4 | ||
{ | ||
|
||
struct Focus | ||
{ | ||
std::string id; | ||
std::string icon; | ||
std::optional<std::string> text; | ||
std::vector<std::string> prerequisites; | ||
std::optional<std::string> mutually_exclusive; | ||
std::optional<std::string> bypass; | ||
int x_position = 0; | ||
int y_position = 0; | ||
std::optional<std::string> relative_position_id; | ||
int cost = 0; | ||
bool available_if_capitulated = false; | ||
std::optional<std::string> available; | ||
std::optional<std::string> cancel_if_invalid; | ||
std::optional<std::string> continue_if_invalid; | ||
std::optional<std::string> select_effect; | ||
std::optional<std::string> complete_tooltip; | ||
std::string completion_reward; | ||
std::optional<std::string> ai_will_do; | ||
std::optional<std::string> allow_branch; | ||
}; | ||
|
||
} // namespace hoi4 | ||
|
||
|
||
|
||
#endif // SRC_HOI4WORLD_FOCUSTREES_FOCUS_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
#include "src/hoi4_world/focus_trees/focus_importer.h" | ||
|
||
#include "external/commonItems/ParserHelpers.h" | ||
|
||
|
||
|
||
namespace hoi4 | ||
{ | ||
|
||
FocusImporter::FocusImporter() | ||
{ | ||
focus_parser_.registerKeyword("id", [this](std::istream& input_stream) { | ||
id_ = commonItems::getString(input_stream); | ||
}); | ||
focus_parser_.registerKeyword("icon", [this](std::istream& input_stream) { | ||
icon_ = commonItems::getString(input_stream); | ||
}); | ||
focus_parser_.registerKeyword("text", [this](std::istream& input_stream) { | ||
text_ = commonItems::getString(input_stream); | ||
}); | ||
focus_parser_.registerKeyword("mutually_exclusive", [this](std::istream& input_stream) { | ||
mutually_exclusive_ = commonItems::getString(input_stream); | ||
}); | ||
focus_parser_.registerKeyword("bypass", [this](std::istream& input_stream) { | ||
bypass_ = commonItems::getString(input_stream); | ||
}); | ||
focus_parser_.registerKeyword("x", [this](std::istream& input_stream) { | ||
x_position_ = commonItems::getInt(input_stream); | ||
}); | ||
focus_parser_.registerKeyword("y", [this](std::istream& input_stream) { | ||
y_position_ = commonItems::getInt(input_stream); | ||
}); | ||
focus_parser_.registerKeyword("relative_position_id", [this](std::istream& input_stream) { | ||
relative_position_id_ = commonItems::getString(input_stream); | ||
}); | ||
focus_parser_.registerKeyword("cost", [this](std::istream& input_stream) { | ||
cost_ = commonItems::getInt(input_stream); | ||
}); | ||
focus_parser_.registerKeyword("available_if_capitulated", [this](std::istream& input_stream) { | ||
available_if_capitulated_ = (commonItems::getString(input_stream) == "yes"); | ||
}); | ||
focus_parser_.registerKeyword("available", [this](std::istream& input_stream) { | ||
available_ = commonItems::getString(input_stream); | ||
}); | ||
focus_parser_.registerKeyword("cancel_if_invalid", [this](std::istream& input_stream) { | ||
cancel_if_invalid_ = commonItems::getString(input_stream); | ||
}); | ||
focus_parser_.registerKeyword("continue_if_invalid", [this](std::istream& input_stream) { | ||
continue_if_invalid_ = commonItems::getString(input_stream); | ||
}); | ||
focus_parser_.registerKeyword("select_effect", [this](std::istream& input_stream) { | ||
select_effect_ = commonItems::getString(input_stream); | ||
}); | ||
focus_parser_.registerKeyword("complete_tooltip", [this](std::istream& input_stream) { | ||
complete_tooltip_ = commonItems::getString(input_stream); | ||
}); | ||
focus_parser_.registerKeyword("completion_reward", [this](std::istream& input_stream) { | ||
completion_reward_ = commonItems::getString(input_stream); | ||
}); | ||
focus_parser_.registerKeyword("ai_will_do", [this](std::istream& input_stream) { | ||
ai_will_do_ = commonItems::getString(input_stream); | ||
}); | ||
focus_parser_.registerKeyword("prerequisite", [this](std::istream& input_stream) { | ||
prerequisites_.push_back(commonItems::getString(input_stream)); | ||
}); | ||
focus_parser_.registerKeyword("allow_branch", [this](std::istream& input_stream) { | ||
allow_branch_ = commonItems::stringOfItem(input_stream).getString(); | ||
}); | ||
focus_parser_.IgnoreAndLogUnregisteredItems(); | ||
} | ||
|
||
|
||
Focus FocusImporter::ImportFocus(std::istream& input_stream) | ||
{ | ||
id_.clear(); | ||
icon_.clear(); | ||
text_.reset(); | ||
prerequisites_.clear(); | ||
mutually_exclusive_.reset(); | ||
bypass_.reset(); | ||
x_position_ = 0; | ||
y_position_ = 0; | ||
relative_position_id_.reset(); | ||
cost_ = 0; | ||
available_if_capitulated_ = false; | ||
available_.reset(); | ||
cancel_if_invalid_.reset(); | ||
continue_if_invalid_.reset(); | ||
select_effect_.reset(); | ||
complete_tooltip_.reset(); | ||
completion_reward_.clear(); | ||
ai_will_do_.reset(); | ||
allow_branch_.reset(); | ||
|
||
focus_parser_.parseStream(input_stream); | ||
|
||
return { | ||
.id = id_, | ||
.icon = icon_, | ||
.text = text_, | ||
.prerequisites = prerequisites_, | ||
.mutually_exclusive = mutually_exclusive_, | ||
.bypass = bypass_, | ||
.x_position = x_position_, | ||
.y_position = y_position_, | ||
.relative_position_id = relative_position_id_, | ||
.cost = cost_, | ||
.available_if_capitulated = available_if_capitulated_, | ||
.available = available_, | ||
.cancel_if_invalid = cancel_if_invalid_, | ||
.continue_if_invalid = continue_if_invalid_, | ||
.select_effect = select_effect_, | ||
.complete_tooltip = complete_tooltip_, | ||
.completion_reward = completion_reward_, | ||
.ai_will_do = ai_will_do_, | ||
.allow_branch = allow_branch_, | ||
}; | ||
} | ||
|
||
} // namespace hoi4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
#ifndef SRC_HOI4WORLD_FOCUSTREES_FOCUSIMPORTER_H | ||
#define SRC_HOI4WORLD_FOCUSTREES_FOCUSIMPORTER_H | ||
|
||
|
||
|
||
#include <istream> | ||
#include <optional> | ||
#include <string> | ||
#include <vector> | ||
|
||
#include "external/commonItems/Parser.h" | ||
#include "src/hoi4_world/focus_trees/focus.h" | ||
|
||
|
||
|
||
namespace hoi4 | ||
{ | ||
|
||
class FocusImporter | ||
{ | ||
public: | ||
FocusImporter(); | ||
|
||
[[nodiscard]] Focus ImportFocus(std::istream& input_stream); | ||
|
||
private: | ||
commonItems::parser focus_parser_; | ||
|
||
std::string id_; | ||
std::string icon_; | ||
std::optional<std::string> text_; | ||
std::vector<std::string> prerequisites_; | ||
std::optional<std::string> mutually_exclusive_; | ||
std::optional<std::string> bypass_; | ||
int x_position_ = 0; | ||
int y_position_ = 0; | ||
std::optional<std::string> relative_position_id_; | ||
int cost_ = 0; | ||
bool available_if_capitulated_ = false; | ||
std::optional<std::string> available_; | ||
std::optional<std::string> cancel_if_invalid_; | ||
std::optional<std::string> continue_if_invalid_; | ||
std::optional<std::string> select_effect_; | ||
std::optional<std::string> complete_tooltip_; | ||
std::string completion_reward_; | ||
std::optional<std::string> ai_will_do_; | ||
std::optional<std::string> allow_branch_; | ||
}; | ||
|
||
} // namespace hoi4 | ||
|
||
|
||
|
||
#endif // SRC_HOI4WORLD_FOCUSTREES_FOCUSIMPORTER_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
#include <sstream> | ||
|
||
#include "external/commonItems/external/googletest/googlemock/include/gmock/gmock-matchers.h" | ||
#include "external/commonItems/external/googletest/googletest/include/gtest/gtest.h" | ||
#include "src/hoi4_world/focus_trees/focus_importer.h" | ||
|
||
|
||
|
||
namespace hoi4 | ||
{ | ||
|
||
TEST(Hoi4worldFocustreesFocusimporterTests, DefaultsAreDefaulted) | ||
{ | ||
std::stringstream input; | ||
|
||
FocusImporter importer; | ||
const Focus focus = importer.ImportFocus(input); | ||
|
||
EXPECT_TRUE(focus.id.empty()); | ||
EXPECT_TRUE(focus.icon.empty()); | ||
EXPECT_FALSE(focus.text.has_value()); | ||
EXPECT_TRUE(focus.prerequisites.empty()); | ||
EXPECT_FALSE(focus.mutually_exclusive.has_value()); | ||
EXPECT_FALSE(focus.bypass.has_value()); | ||
EXPECT_EQ(focus.x_position, 0); | ||
EXPECT_EQ(focus.y_position, 0); | ||
EXPECT_FALSE(focus.relative_position_id.has_value()); | ||
EXPECT_EQ(focus.cost, 0); | ||
EXPECT_FALSE(focus.available_if_capitulated); | ||
EXPECT_FALSE(focus.available.has_value()); | ||
EXPECT_FALSE(focus.cancel_if_invalid.has_value()); | ||
EXPECT_FALSE(focus.continue_if_invalid.has_value()); | ||
EXPECT_FALSE(focus.select_effect.has_value()); | ||
EXPECT_FALSE(focus.complete_tooltip.has_value()); | ||
EXPECT_TRUE(focus.completion_reward.empty()); | ||
EXPECT_FALSE(focus.ai_will_do.has_value()); | ||
EXPECT_FALSE(focus.allow_branch.has_value()); | ||
} | ||
|
||
|
||
TEST(Hoi4worldFocustreesFocusimporterTests, ItemsCanBeImported) | ||
{ | ||
std::stringstream input; | ||
input << "={\n"; | ||
input << "\tid = test_id\n"; | ||
input << "\ticon = test_icon\n"; | ||
input << "\ttext = test_text\n"; | ||
input << "\tprerequisite = test_prerequisite_1\n"; | ||
input << "\tprerequisite = test_prerequisite_2\n"; | ||
input << "\tmutually_exclusive = test_mutually_exclusive\n"; | ||
input << "\tbypass = test_bypass\n"; | ||
input << "\tx = 3\n"; | ||
input << "\ty = 5\n"; | ||
input << "\trelative_position_id = test_relative_position\n"; | ||
input << "\tcost = 8\n"; | ||
input << "\tavailable_if_capitulated = yes\n"; | ||
input << "\tavailable = test_available\n"; | ||
input << "\tcancel_if_invalid = test_cancel_if_invalid\n"; | ||
input << "\tcontinue_if_invalid = test_continue_if_invalid\n"; | ||
input << "\tselect_effect = test_select_effect\n"; | ||
input << "\tcomplete_tooltip = test_complete_tooltip\n"; | ||
input << "\tcompletion_reward = test_completion_reward\n"; | ||
input << "\tai_will_do = test_ai_will_do\n"; | ||
input << "\tallow_branch = { test_allow_branch }\n"; | ||
input << "\t}\n"; | ||
input << "}"; | ||
|
||
FocusImporter importer; | ||
const Focus focus = importer.ImportFocus(input); | ||
|
||
EXPECT_EQ(focus.id, "test_id"); | ||
EXPECT_EQ(focus.icon, "test_icon"); | ||
EXPECT_TRUE(focus.text.has_value()); | ||
EXPECT_EQ(focus.text.value_or(""), "test_text"); | ||
EXPECT_THAT(focus.prerequisites, testing::ElementsAre("test_prerequisite_1", "test_prerequisite_2")); | ||
EXPECT_TRUE(focus.mutually_exclusive.has_value()); | ||
EXPECT_EQ(focus.mutually_exclusive.value_or(""), "test_mutually_exclusive"); | ||
EXPECT_EQ(focus.bypass.value_or(""), "test_bypass"); | ||
EXPECT_EQ(focus.x_position, 3); | ||
EXPECT_EQ(focus.y_position, 5); | ||
EXPECT_EQ(focus.relative_position_id.value_or(""), "test_relative_position"); | ||
EXPECT_EQ(focus.cost, 8); | ||
EXPECT_TRUE(focus.available_if_capitulated); | ||
EXPECT_EQ(focus.available.value_or(""), "test_available"); | ||
EXPECT_EQ(focus.cancel_if_invalid.value_or(""), "test_cancel_if_invalid"); | ||
EXPECT_EQ(focus.continue_if_invalid.value_or(""), "test_continue_if_invalid"); | ||
EXPECT_EQ(focus.select_effect.value_or(""), "test_select_effect"); | ||
EXPECT_EQ(focus.complete_tooltip.value_or(""), "test_complete_tooltip"); | ||
EXPECT_EQ(focus.completion_reward, "test_completion_reward"); | ||
EXPECT_EQ(focus.ai_will_do.value_or(""), "test_ai_will_do"); | ||
EXPECT_EQ(focus.allow_branch.value_or(""), "= { test_allow_branch }"); | ||
} | ||
|
||
} // namespace hoi4 |