From 43b4bec7c6af5f65d7d56fbb76dbad5b95902b52 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20Ke=C3=9Fler?= Date: Thu, 16 Nov 2023 16:03:42 +0100 Subject: [PATCH] simplify functions --- include/download.h | 4 +++- src/command_line_interface.cpp | 12 +++++++++++- src/download.cpp | 27 +++++++++++++++------------ 3 files changed, 29 insertions(+), 14 deletions(-) diff --git a/include/download.h b/include/download.h index 834d281..8c9450f 100644 --- a/include/download.h +++ b/include/download.h @@ -25,7 +25,9 @@ namespace Download { * draw pile hits the given size, whichever comes first * */ - Hanabi::Game get_game(std::variant game_spec, std::optional score_goal); + Hanabi::Game get_game(int game_id, std::optional score_goal); + + Hanabi::Game get_game(const std::string& filename, std::optional score_goal); } // namespace Download diff --git a/src/command_line_interface.cpp b/src/command_line_interface.cpp index 5d3686e..b696af1 100644 --- a/src/command_line_interface.cpp +++ b/src/command_line_interface.cpp @@ -50,7 +50,17 @@ namespace Hanabi { quiet_os.precision(10); // 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(parms.game)) + { + return Download::get_game(std::get(parms.game), convert_optional(parms.score_goal)); + } + else + { + return Download::get_game(std::get(parms.game), convert_optional(parms.score_goal)); + } + }(); + if (not game.holds_state()) { if(std::holds_alternative(parms.game)) { diff --git a/src/download.cpp b/src/download.cpp index 8838a2a..9f1ac36 100644 --- a/src/download.cpp +++ b/src/download.cpp @@ -27,22 +27,25 @@ namespace Download { return boost::json::parse(game_json).as_object(); } - - Hanabi::Game get_game(std::variant game_spec, std::optional score_goal){ - const std::optional game_json_opt = [&game_spec]() { - if (game_spec.index() == 0) { - return download_game_json(std::get(game_spec)); - } else { - return open_game_json(std::get(game_spec).c_str()); - } - }(); - - if (!game_json_opt.has_value() or game_json_opt.value().empty()) { + Hanabi::Game get_game(int game_id, std::optional score_goal) + { + std::optional const game_json = download_game_json(game_id); + if (!game_json.has_value() or game_json.value().empty()) { 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}; } + Hanabi::Game get_game(std::string const & filename, std::optional score_goal) + { + std::optional 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