70 lines
2.3 KiB
C++
70 lines
2.3 KiB
C++
#include <boost/json/src.hpp>
|
|
#include <cpr/cpr.h>
|
|
|
|
#include "parse_game.h"
|
|
#include "make_state.h"
|
|
|
|
#include "download.h"
|
|
|
|
|
|
namespace Download
|
|
{
|
|
|
|
std::optional<boost::json::object> 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<boost::json::object> open_game_json(const char *filename)
|
|
{
|
|
std::ifstream file(filename);
|
|
if (!file.is_open())
|
|
{
|
|
return std::nullopt;
|
|
}
|
|
std::string game_json((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
|
|
return boost::json::parse(game_json).as_object();
|
|
}
|
|
|
|
Hanabi::Game get_game(int game_id, std::optional<uint8_t> score_goal, bool save_memory)
|
|
{
|
|
std::optional<boost::json::object> 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.value());
|
|
|
|
Hanabi::HanabiStateConfig config;
|
|
config.num_clues_gained_on_discard_or_stack_finished = game_info.num_clues_gained_per_discard_or_stack_finished;
|
|
config.save_memory = save_memory;
|
|
config.score_goal = score_goal;
|
|
|
|
return {make_game_state(game_info.num_suits, game_info.num_players, game_info.deck, config), game_info};
|
|
}
|
|
|
|
Hanabi::Game get_game(std::string const & filename, std::optional<uint8_t> score_goal, bool save_memory)
|
|
{
|
|
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());
|
|
|
|
Hanabi::HanabiStateConfig config;
|
|
config.num_clues_gained_on_discard_or_stack_finished = game_info.num_clues_gained_per_discard_or_stack_finished;
|
|
config.save_memory = save_memory;
|
|
config.score_goal = score_goal;
|
|
|
|
return {make_game_state(game_info.num_suits, game_info.num_players, game_info.deck, config), game_info};
|
|
}
|
|
} // namespace Download
|