2023-11-15 21:47:50 +01:00
|
|
|
#ifndef DYNAMIC_PROGRAM_PARSE_GAME_H
|
|
|
|
#define DYNAMIC_PROGRAM_PARSE_GAME_H
|
|
|
|
|
|
|
|
#include <boost/json.hpp>
|
|
|
|
|
2023-11-15 23:07:39 +01:00
|
|
|
#include "game_interface.h"
|
2023-11-15 21:47:50 +01:00
|
|
|
|
2023-11-16 16:20:04 +01:00
|
|
|
namespace Hanabi
|
|
|
|
{
|
2023-11-15 21:47:50 +01:00
|
|
|
// These are overloads that the boost/json library uses for parsing.
|
|
|
|
// They convert a Card from/to json.
|
|
|
|
// This has to be in the same namespace as Hanabi::Card.
|
2023-11-16 16:20:04 +01:00
|
|
|
Card tag_invoke(boost::json::value_to_tag<Card>, boost::json::value const & jv);
|
|
|
|
|
|
|
|
void tag_invoke(boost::json::value_from_tag, boost::json::value & jv, Hanabi::Card const & card);
|
2023-11-15 21:47:50 +01:00
|
|
|
}
|
|
|
|
|
2023-11-16 16:20:04 +01:00
|
|
|
namespace Parsing
|
|
|
|
{
|
2023-11-15 21:47:50 +01:00
|
|
|
/**
|
|
|
|
* Represents a single action (turn) in a Hanab game.
|
|
|
|
* Note that this is slightly differen than Hanabi::Action,
|
|
|
|
* since this uses indices for specifying the discarded/played cards.
|
|
|
|
* We only want to work with this type while parsing, converting to Hanabi::Action after.
|
|
|
|
*/
|
2023-11-16 16:20:04 +01:00
|
|
|
struct HanabLiveAction
|
|
|
|
{
|
2023-11-15 21:47:50 +01:00
|
|
|
Hanabi::ActionType type{};
|
|
|
|
/**
|
|
|
|
* In case the action is of type discard or play,
|
|
|
|
* this value refers to the index of the discarded/played card in the deck.
|
|
|
|
*/
|
|
|
|
int8_t target{};
|
|
|
|
};
|
|
|
|
|
|
|
|
// Overload for parsing from json to HanabLiveAction
|
2023-11-16 16:20:04 +01:00
|
|
|
HanabLiveAction tag_invoke(boost::json::value_to_tag<HanabLiveAction>, boost::json::value const & jv);
|
2023-11-15 21:47:50 +01:00
|
|
|
|
2023-11-16 16:29:41 +01:00
|
|
|
/**
|
2023-11-15 21:47:50 +01:00
|
|
|
* @brief Parse deck from hanab.live format
|
|
|
|
* @return List of cards (in order) and number of suits
|
|
|
|
*/
|
2023-11-16 16:20:04 +01:00
|
|
|
std::pair<std::vector<Hanabi::Card>, Hanabi::suit_t> parse_deck(const boost::json::value & deck_json);
|
2023-11-15 21:47:50 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Parse actions from hanab.live format.
|
|
|
|
* @return List of actions
|
|
|
|
*/
|
2023-11-16 16:20:04 +01:00
|
|
|
std::vector<HanabLiveAction> parse_actions(const boost::json::value & action_json);
|
2023-11-15 21:47:50 +01:00
|
|
|
|
2023-11-16 16:29:41 +01:00
|
|
|
/**
|
|
|
|
* @brief Convert actions from hanab.live format.
|
|
|
|
* @return List of actions with concrete cards instead of their indices.
|
|
|
|
*/
|
2023-11-15 21:47:50 +01:00
|
|
|
std::vector<Hanabi::Action> convert_actions(
|
2023-11-16 16:20:04 +01:00
|
|
|
std::vector<HanabLiveAction> const & hanab_live_actions, std::vector<Hanabi::Card> const & deck
|
2023-11-15 21:47:50 +01:00
|
|
|
);
|
|
|
|
|
2023-11-15 23:23:21 +01:00
|
|
|
Hanabi::GameInfo parse_game(boost::json::object const & game_json);
|
2023-11-15 21:47:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endif //DYNAMIC_PROGRAM_PARSE_GAME_H
|