diff --git a/include/download.h b/include/download.h index 469bb72..7890ec3 100644 --- a/include/download.h +++ b/include/download.h @@ -88,20 +88,19 @@ namespace Download { return boost::json::value_to>(action_json); } - boost::json::object download_game_json(int game_id) { + std::optional download_game_json(int game_id) { std::string request_str = "https://hanab.live/export/" + std::to_string(game_id); cpr::Response r = cpr::Get(cpr::Url(request_str)); if (r.header["content-type"] != "application/json; charset=utf-8") { - return {}; + return std::nullopt; } return boost::json::parse(r.text).as_object(); } - boost::json::object open_game_json(const char *filename) { + std::optional open_game_json(const char *filename) { std::ifstream file(filename); if (!file.is_open()) { - std::cout << "Failed to open " << filename << "." << std::endl; - return {}; + return std::nullopt; } std::string game_json((std::istreambuf_iterator(file)), std::istreambuf_iterator()); return boost::json::parse(game_json).as_object(); @@ -145,7 +144,7 @@ namespace Download { } std::unique_ptr get_game(std::variant game_spec, unsigned turn, size_t draw_pile_break = 0) { - const boost::json::object game_json = [&game_spec]() { + const std::optional game_json_opt = [&game_spec]() { if (game_spec.index() == 0) { return download_game_json(std::get(game_spec)); } else { @@ -153,10 +152,12 @@ namespace Download { } }(); - if(game_json.empty()) { + if (!game_json_opt.has_value() or game_json_opt.value().empty()) { return nullptr; } + const boost::json::object& game_json = game_json_opt.value(); + const auto [deck, num_suits] = parse_deck(game_json.at("deck")); const std::vector actions = parse_actions(game_json.at("actions")); const size_t num_players = game_json.at("players").as_array().size(); diff --git a/src/main.cpp b/src/main.cpp index b83feeb..23bf641 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -18,6 +18,11 @@ namespace Hanabi { void analyze_game_and_start_cli(std::variant game_id, int turn) { auto game = Download::get_game(game_id, turn - 1); if (game == nullptr) { + if(game_id.index() == 0) { + std::cout << "Failed to download game " << std::get(game_id) << " from hanab.live." << std::endl; + } else { + std::cout << "Failed to open file " << std::get(game_id) << "." << std::endl; + } return; }