Newer
Older

marlehmann
committed
#include "../../serialization/vector_utils.h"
hand::hand() : unique_serializable() { }
hand::hand(const std::string& id) : unique_serializable(id) { }
hand::hand(const std::string& id, const std::vector<card*>& cards) : unique_serializable(id) {
this->_cards = cards;
}
hand::~hand() {
_cards.clear();
}
unsigned int hand::get_nof_cards() const {
std::vector<card*> hand::get_cards() const {
// this function searches for a given card in the hand, and returns whether the card was found or not;
// if the card was found, the variable hand_card (given as reference) is updated
bool hand::try_get_card(const std::string &card_id, card *&hand_card) const {
const auto it = std::ranges::find_if(_cards,
[&card_id](const card* x) { return x->get_id() == card_id;});
if (it < _cards.end()) {
hand_card = *it;
return true;
}
return false;
}
card* hand::remove_card(const int idx) {
return remove_card(_cards.begin() + idx);
}
card* hand::remove_card(card* card) {
const auto pos = std::ranges::find(_cards, card);
card* hand::remove_card(const std::vector<card*>::iterator pos) {
if (pos >= _cards.begin() && pos < _cards.end()) {
card* res = *pos;
_cards.erase(pos);
return res;
}
return nullptr;
}
std::vector<card*>::iterator hand::get_card_iterator() {
return _cards.begin();
}
bool hand::add_card(card* card, std::string &err) {
_cards.push_back(card);

marie3003
committed
bool hand::remove_card(std::string card_id, std::string &err) {
const auto it = std::ranges::find_if(_cards,
[&card_id](const card* x) { return x->get_id() == card_id;});

marie3003
committed
remove_card(it);
return true;
} else {
err = "Could not play card, as the requested card was not on the player's hand.";
return false;
}
}
#endif
void hand::write_into_json(rapidjson::Value &json, rapidjson::Document::AllocatorType& allocator) const {
unique_serializable::write_into_json(json, allocator);
json.AddMember("cards", vector_utils::serialize_vector(_cards, allocator), allocator);
}

mtarnow
committed
hand* hand::from_json(const rapidjson::Value &json) {
if (json.HasMember("id") &&
json.HasMember("cards"))
{
std::vector<card*> deserialized_cards = std::vector<card*>();
for (auto &serialized_card : json["cards"].GetArray()) {
deserialized_cards.push_back(card::from_json(serialized_card.GetObject()));
}
return new hand(json["id"].GetString(), deserialized_cards);
}
throw WizardException("Could not parse hand from json. 'cards' were missing.");