Skip to content
Snippets Groups Projects
Commit 49c112d6 authored by Aidan Meara's avatar Aidan Meara Committed by marie3003
Browse files

finished json write and from, modified vector utils for cards vector in trick

parent f5510343
No related branches found
No related tags found
2 merge requests!17finalize common,!5Draft: Resolve "Adapt Game State Class and add class RoundState"
......@@ -15,10 +15,10 @@ trick::trick(std::string id) : unique_serializable(id) {
this->_trump_color = new serializable_value<int>(0);
}
trick::trick(std::string id, std::vector<std::pair<card*, player*>> cards, int &trick_color, int &trump_color)
trick::trick(std::string id, std::vector<std::pair<card*, player*>> &cards, serializable_value<int>* trick_color, serializable_value<int>* trump_color)
: unique_serializable(id), _cards(cards) {
this->_trick_color = new serializable_value<int>(trick_color);
this->_trump_color = new serializable_value<int>(trump_color);
this->_trick_color = trick_color;
this->_trump_color = trump_color;
}
trick::trick(const int trump)
......@@ -144,14 +144,26 @@ void trick::set_trick_color(int color) {
// for creating updated instance of trick
trick *trick::from_json(const rapidjson::Value &json) {
if (json.HasMember("id") && json.HasMember("cards") && json.HasMember("trump_color")
if (json.HasMember("id")
&& json.HasMember("cards")
&& json.HasMember("trump_color")
&& json.HasMember("trick_color")) {
auto deserialized_cards = std::vector<serializable_value<std::pair<card*, player*>>*>();
auto deserialized_cards = std::vector<std::pair<card*, player*>>();
for (auto &serialized_card : json["cards"].GetArray()) {
deserialized_cards.push_back(card::from_json(serialized_card.GetObject()));
// Deserialize the card
card* deserialized_card = card::from_json(serialized_card["card"]);
// Deserialize the player
player* deserialized_player = player::from_json(serialized_card["player"]);
// Add the pair to the vector
deserialized_cards.emplace_back(deserialized_card, deserialized_player);
}
return new trick(json["id"].GetString(), deserialized_cards);
auto trump_color = serializable_value<int>::from_json(json["trump_color"].GetObject());
auto trick_color = serializable_value<int>::from_json(json["trick_color"].GetObject());
return new trick(json["id"].GetString(), deserialized_cards, trick_color, trump_color);
} else {
throw WizardException("Could not parse trick from json. 'id' or 'cards' were missing.");
}
......@@ -160,5 +172,16 @@ trick *trick::from_json(const rapidjson::Value &json) {
void trick::write_into_json(rapidjson::Value &json,
rapidjson::MemoryPoolAllocator<rapidjson::CrtAllocator> &allocator) const {
unique_serializable::write_into_json(json, allocator);
json.AddMember("cards", vector_utils::serialize_vector(_cards, allocator), allocator);
json.AddMember("cards", vector_utils::serialize_cards_vector(_cards, allocator), allocator);
rapidjson::Value trump_color(rapidjson::kObjectType);
_trump_color->write_into_json(trump_color, allocator);
json.AddMember("trump_color", trump_color, allocator);
rapidjson::Value trick_color(rapidjson::kObjectType);
_trick_color->write_into_json(trick_color, allocator);
json.AddMember("trick_color", trick_color, allocator);
}
......@@ -21,6 +21,8 @@ private:
serializable_value<int>* _trick_color;
serializable_value<int>* _trump_color;
std::vector<std::pair<card*, player*>> _cards;
explicit trick(std::string id);
trick(std::string id,
......@@ -33,10 +35,9 @@ public:
explicit trick(int trump);
~trick();
// accessors
[[nodiscard]] int get_trick_color() const;
// #ifdef WIZARD_SERVER
#ifdef WIZARD_SERVER
// state update functions
player* wrap_up_trick(std::string& err); // determines winner
void set_up_round(std::string& err, int trump); // resets attributes
......@@ -44,7 +45,9 @@ public:
bool add_card(card* played_card, player* current_player, std::string& err);
// setters
void set_trick_color(int color);
// #endif
// accessors
[[nodiscard]] int get_trick_color() const;
#endif
// serializable interface
......
......@@ -10,6 +10,7 @@
class serializable {
public:
virtual ~serializable() = default;
virtual rapidjson::Document* to_json() const {
rapidjson::Document* json = new rapidjson::Document();
......
......@@ -32,6 +32,27 @@ namespace vector_utils {
return arr_val;
}
static rapidjson::Value serialize_cards_vector(
const std::vector<std::pair<card*, player*>>& cards,
rapidjson::Document::AllocatorType& allocator) {
rapidjson::Value arr_val(rapidjson::kArrayType);
for (const auto& pair : cards) {
rapidjson::Value obj(rapidjson::kObjectType);
// Serialize the pair componentsss
rapidjson::Value card_val(rapidjson::kObjectType);
pair.first->write_into_json(card_val, allocator);
obj.AddMember("card", card_val, allocator);
rapidjson::Value player_val(rapidjson::kObjectType);
pair.second->write_into_json(player_val, allocator);
obj.AddMember("player", player_val, allocator);
arr_val.PushBack(obj, allocator);
}
return arr_val;
}
}
#endif //WIZARD_VECTOR_UTILS_H
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment