simplify functions

This commit is contained in:
Maximilian Keßler 2023-11-16 16:03:42 +01:00
parent 04e071c97c
commit 43b4bec7c6
Signed by: max
GPG key ID: BCC5A619923C0BA5
3 changed files with 29 additions and 14 deletions

View file

@ -25,7 +25,9 @@ namespace Download {
* draw pile hits the given size, whichever comes first * draw pile hits the given size, whichever comes first
* *
*/ */
Hanabi::Game get_game(std::variant<int, std::string> game_spec, std::optional<uint8_t> score_goal); Hanabi::Game get_game(int game_id, std::optional<uint8_t> score_goal);
Hanabi::Game get_game(const std::string& filename, std::optional<uint8_t> score_goal);
} // namespace Download } // namespace Download

View file

@ -50,7 +50,17 @@ namespace Hanabi {
quiet_os.precision(10); quiet_os.precision(10);
// Load game, either from file or from hanab.live // Load game, either from file or from hanab.live
Game game = Download::get_game(parms.game, convert_optional(parms.score_goal)); Game game = [&parms]{
if (std::holds_alternative<int>(parms.game))
{
return Download::get_game(std::get<int>(parms.game), convert_optional(parms.score_goal));
}
else
{
return Download::get_game(std::get<std::string>(parms.game), convert_optional(parms.score_goal));
}
}();
if (not game.holds_state()) if (not game.holds_state())
{ {
if(std::holds_alternative<int>(parms.game)) { if(std::holds_alternative<int>(parms.game)) {

View file

@ -27,22 +27,25 @@ namespace Download {
return boost::json::parse(game_json).as_object(); return boost::json::parse(game_json).as_object();
} }
Hanabi::Game get_game(int game_id, std::optional<uint8_t> score_goal)
Hanabi::Game get_game(std::variant<int, std::string> game_spec, std::optional<uint8_t> score_goal){ {
const std::optional<boost::json::object> game_json_opt = [&game_spec]() { std::optional<boost::json::object> const game_json = download_game_json(game_id);
if (game_spec.index() == 0) { if (!game_json.has_value() or game_json.value().empty()) {
return download_game_json(std::get<int>(game_spec));
} else {
return open_game_json(std::get<std::string>(game_spec).c_str());
}
}();
if (!game_json_opt.has_value() or game_json_opt.value().empty()) {
return {nullptr, {}}; return {nullptr, {}};
} }
Hanabi::GameInfo game_info = Parsing::parse_game(game_json_opt.value()); Hanabi::GameInfo game_info = Parsing::parse_game(game_json.value());
return {make_game_state(game_info.num_suits, game_info.num_players, game_info.deck, score_goal), game_info}; return {make_game_state(game_info.num_suits, game_info.num_players, game_info.deck, score_goal), game_info};
} }
Hanabi::Game get_game(std::string const & filename, std::optional<uint8_t> score_goal)
{
std::optional<boost::json::object> const game_json = open_game_json(filename.c_str());
if (!game_json.has_value() or game_json.value().empty()) {
return {nullptr, {}};
}
Hanabi::GameInfo game_info = Parsing::parse_game(game_json.value());
return {make_game_state(game_info.num_suits, game_info.num_players, game_info.deck, score_goal), game_info};
}
} // namespace Download } // namespace Download