From 016e87288dbe6ee75556efcf5c0543ce459b2e7a Mon Sep 17 00:00:00 2001 From: Kieran Kihn <114803508+kierankihn@users.noreply.github.com> Date: Tue, 2 Dec 2025 12:06:21 +0800 Subject: [PATCH] refactor(network): replace `HandCard` with `std::multiset` in `InitGamePayload` - Updated `InitGamePayload` to use `std::multiset` for `handCard`. - Removed `serializeHandCard` method and replaced its usage with `serializeCards`. - Adjusted `deserializeHandCard` to return `std::multiset` and simplified insertion logic. - Cleaned up unused declarations in `MessageSerializer`. --- src/network/Message.h | 2 +- src/network/MessageSerializer.cpp | 14 ++++---------- src/network/MessageSerializer.h | 3 +-- 3 files changed, 6 insertions(+), 13 deletions(-) diff --git a/src/network/Message.h b/src/network/Message.h index 9517b86..b8ce3e7 100644 --- a/src/network/Message.h +++ b/src/network/Message.h @@ -35,7 +35,7 @@ namespace UNO::NETWORK { struct InitGamePayload { GAME::DiscardPile discardPile; - GAME::HandCard handCard; + std::multiset handCard; size_t currentPlayerIndex; }; diff --git a/src/network/MessageSerializer.cpp b/src/network/MessageSerializer.cpp index 8fbdfc1..e51da2a 100644 --- a/src/network/MessageSerializer.cpp +++ b/src/network/MessageSerializer.cpp @@ -28,12 +28,6 @@ namespace UNO::NETWORK { return serializeCards(cards.begin(), cards.end()); } - nlohmann::json MessageSerializer::serializeHandCard(const GAME::HandCard &handCard) - { - const auto &cards = handCard.getCards(); - return serializeCards(cards.begin(), cards.end()); - } - nlohmann::json MessageSerializer::serializePayload(const std::monostate &payload) { return nullptr; @@ -62,7 +56,7 @@ namespace UNO::NETWORK { nlohmann::json MessageSerializer::serializePayload(const InitGamePayload &payload) { return {{"discard_pile", serializeDiscardPile(payload.discardPile)}, - {"hand_card", serializeHandCard(payload.handCard)}, + {"hand_card", serializeCards(payload.handCard.begin(), payload.handCard.end())}, {"current_player", payload.currentPlayerIndex}}; } @@ -206,15 +200,15 @@ namespace UNO::NETWORK { return res; } - GAME::HandCard MessageSerializer::deserializeHandCard(const nlohmann::json &handCard) + std::multiset MessageSerializer::deserializeHandCard(const nlohmann::json &handCard) { if (handCard.is_array() == false) { throw std::invalid_argument("Invalid hand_card format: expected JSON array"); } - GAME::HandCard res; + std::multiset res; for (const auto &i : std::views::reverse(handCard)) { - res.draw(deserializeCard(i)); + res.insert(deserializeCard(i)); } return res; diff --git a/src/network/MessageSerializer.h b/src/network/MessageSerializer.h index a5f81d9..1dca8a3 100644 --- a/src/network/MessageSerializer.h +++ b/src/network/MessageSerializer.h @@ -24,7 +24,6 @@ namespace UNO::NETWORK { static nlohmann::json serializeCards(Iterator begin, Iterator end); static nlohmann::json serializeDiscardPile(const GAME::DiscardPile &discardPile); - static nlohmann::json serializeHandCard(const GAME::HandCard &handCard); static nlohmann::json serializePayload(const std::monostate &payload); static nlohmann::json serializePayload(const JoinGamePayload &payload); @@ -43,7 +42,7 @@ namespace UNO::NETWORK { static GAME::CardType deserializeCardType(const std::string &cardType); static GAME::Card deserializeCard(const nlohmann::json &card); static GAME::DiscardPile deserializeDiscardPile(const nlohmann::json &discardPile); - static GAME::HandCard deserializeHandCard(const nlohmann::json &handCard); + static std::multiset deserializeHandCard(const nlohmann::json &handCard); static std::monostate deserializeEmptyPayload(const nlohmann::json &payload); static JoinGamePayload deserializeJoinGamePayload(const nlohmann::json &payload);