#include #include #include "parse_game.h" #include "make_state.h" #include "download.h" namespace Download { 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 std::nullopt; } return boost::json::parse(r.text).as_object(); } std::optional open_game_json(const char *filename) { std::ifstream file(filename); if (!file.is_open()) { return std::nullopt; } std::string game_json((std::istreambuf_iterator(file)), std::istreambuf_iterator()); 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()) { return {nullptr, {}}; } Hanabi::GameInfo game_info = Parsing::parse_game(game_json_opt.value()); return {make_game_state(game_info.num_suits, game_info.num_players, game_info.deck, score_goal), game_info}; } } // namespace Download