2023-08-12 19:15:05 +02:00
|
|
|
#include <boost/json/src.hpp>
|
2023-08-12 19:43:22 +02:00
|
|
|
#include <cpr/cpr.h>
|
|
|
|
|
2023-11-15 21:47:50 +01:00
|
|
|
#include "parse_game.h"
|
2023-11-16 15:54:50 +01:00
|
|
|
#include "make_state.h"
|
2023-08-12 19:15:05 +02:00
|
|
|
|
2023-11-15 21:47:50 +01:00
|
|
|
#include "download.h"
|
2023-08-12 19:15:05 +02:00
|
|
|
|
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-11-12 17:30:44 +01:00
|
|
|
Hanabi::Game get_game(std::variant<int, std::string> game_spec, std::optional<uint8_t> score_goal){
|
2023-11-10 20:35:05 +01:00
|
|
|
const std::optional<boost::json::object> game_json_opt = [&game_spec]() {
|
|
|
|
if (game_spec.index() == 0) {
|
|
|
|
return download_game_json(std::get<int>(game_spec));
|
|
|
|
} else {
|
2023-11-12 17:30:44 +01:00
|
|
|
return open_game_json(std::get<std::string>(game_spec).c_str());
|
2023-11-10 20:35:05 +01:00
|
|
|
}
|
|
|
|
}();
|
|
|
|
|
|
|
|
if (!game_json_opt.has_value() or game_json_opt.value().empty()) {
|
2023-11-15 23:23:21 +01:00
|
|
|
return {nullptr, {}};
|
2023-11-10 20:35:05 +01:00
|
|
|
}
|
|
|
|
|
2023-11-15 23:23:21 +01:00
|
|
|
Hanabi::GameInfo game_info = Parsing::parse_game(game_json_opt.value());
|
2023-11-16 15:54:50 +01:00
|
|
|
return {make_game_state(game_info.num_suits, game_info.num_players, game_info.deck, score_goal), game_info};
|
2023-11-10 20:35:05 +01:00
|
|
|
}
|
|
|
|
|
2023-08-12 19:15:05 +02:00
|
|
|
} // namespace Download
|