mirror of
https://github.com/kierankihn/uno-game.git
synced 2025-12-27 02:13:18 +08:00
feat(client): validate and handle wild card type in handlePlayerPlayCard
- Added validation for wild card types and ensured proper color assignment. - Updated `PlayerPlayCardPayload` to use ID and color instead of card object.
This commit is contained in:
@@ -22,7 +22,8 @@ namespace UNO::CLIENT {
|
||||
struct PlayerStartGamePayload {};
|
||||
|
||||
struct PlayerPlayCardPayload {
|
||||
GAME::Card card;
|
||||
size_t id;
|
||||
GAME::CardColor color;
|
||||
};
|
||||
|
||||
struct PlayerDrawCardPayload {};
|
||||
|
||||
@@ -107,8 +107,22 @@ namespace UNO::CLIENT {
|
||||
|
||||
void UnoClient::handlePlayerPlayCard(PlayerPlayCardPayload payload)
|
||||
{
|
||||
NETWORK::PlayCardPayload messagePayload = {payload.card};
|
||||
NETWORK::Message message = {NETWORK::MessageStatus::OK, NETWORK::MessagePayloadType::PLAY_CARD, messagePayload};
|
||||
auto cards = this->clientGameState_->getCards();
|
||||
auto card = cards.begin();
|
||||
for (size_t i = 0; i < payload.id; i++) {
|
||||
card = std::next(card);
|
||||
}
|
||||
|
||||
if ((card->getType() == GAME::CardType::WILD || card->getType() == GAME::CardType::WILDDRAWFOUR)
|
||||
&& card->getColor() != GAME::CardColor::RED) {
|
||||
throw std::invalid_argument("Invalid card played by player");
|
||||
}
|
||||
|
||||
NETWORK::PlayCardPayload messagePayload = {
|
||||
{(card->getType() != GAME::CardType::WILD && card->getType() != GAME::CardType::WILDDRAWFOUR) ? card->getColor()
|
||||
: payload.color,
|
||||
card->getType()}};
|
||||
NETWORK::Message message = {NETWORK::MessageStatus::OK, NETWORK::MessagePayloadType::PLAY_CARD, messagePayload};
|
||||
networkClient_->send(NETWORK::MessageSerializer::serialize(message));
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user