mirror of
https://github.com/kierankihn/uno-game.git
synced 2025-12-27 02:13:18 +08:00
refactor(network): replace HandCard with std::multiset<Card> in InitGamePayload
- Updated `InitGamePayload` to use `std::multiset<Card>` for `handCard`. - Removed `serializeHandCard` method and replaced its usage with `serializeCards`. - Adjusted `deserializeHandCard` to return `std::multiset<Card>` and simplified insertion logic. - Cleaned up unused declarations in `MessageSerializer`.
This commit is contained in:
@@ -35,7 +35,7 @@ namespace UNO::NETWORK {
|
|||||||
|
|
||||||
struct InitGamePayload {
|
struct InitGamePayload {
|
||||||
GAME::DiscardPile discardPile;
|
GAME::DiscardPile discardPile;
|
||||||
GAME::HandCard handCard;
|
std::multiset<GAME::Card> handCard;
|
||||||
size_t currentPlayerIndex;
|
size_t currentPlayerIndex;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -28,12 +28,6 @@ namespace UNO::NETWORK {
|
|||||||
return serializeCards(cards.begin(), cards.end());
|
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)
|
nlohmann::json MessageSerializer::serializePayload(const std::monostate &payload)
|
||||||
{
|
{
|
||||||
return nullptr;
|
return nullptr;
|
||||||
@@ -62,7 +56,7 @@ namespace UNO::NETWORK {
|
|||||||
nlohmann::json MessageSerializer::serializePayload(const InitGamePayload &payload)
|
nlohmann::json MessageSerializer::serializePayload(const InitGamePayload &payload)
|
||||||
{
|
{
|
||||||
return {{"discard_pile", serializeDiscardPile(payload.discardPile)},
|
return {{"discard_pile", serializeDiscardPile(payload.discardPile)},
|
||||||
{"hand_card", serializeHandCard(payload.handCard)},
|
{"hand_card", serializeCards(payload.handCard.begin(), payload.handCard.end())},
|
||||||
{"current_player", payload.currentPlayerIndex}};
|
{"current_player", payload.currentPlayerIndex}};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -206,15 +200,15 @@ namespace UNO::NETWORK {
|
|||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
GAME::HandCard MessageSerializer::deserializeHandCard(const nlohmann::json &handCard)
|
std::multiset<GAME::Card> MessageSerializer::deserializeHandCard(const nlohmann::json &handCard)
|
||||||
{
|
{
|
||||||
if (handCard.is_array() == false) {
|
if (handCard.is_array() == false) {
|
||||||
throw std::invalid_argument("Invalid hand_card format: expected JSON array");
|
throw std::invalid_argument("Invalid hand_card format: expected JSON array");
|
||||||
}
|
}
|
||||||
|
|
||||||
GAME::HandCard res;
|
std::multiset<GAME::Card> res;
|
||||||
for (const auto &i : std::views::reverse(handCard)) {
|
for (const auto &i : std::views::reverse(handCard)) {
|
||||||
res.draw(deserializeCard(i));
|
res.insert(deserializeCard(i));
|
||||||
}
|
}
|
||||||
|
|
||||||
return res;
|
return res;
|
||||||
|
|||||||
@@ -24,7 +24,6 @@ namespace UNO::NETWORK {
|
|||||||
static nlohmann::json serializeCards(Iterator begin, Iterator end);
|
static nlohmann::json serializeCards(Iterator begin, Iterator end);
|
||||||
|
|
||||||
static nlohmann::json serializeDiscardPile(const GAME::DiscardPile &discardPile);
|
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 std::monostate &payload);
|
||||||
static nlohmann::json serializePayload(const JoinGamePayload &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::CardType deserializeCardType(const std::string &cardType);
|
||||||
static GAME::Card deserializeCard(const nlohmann::json &card);
|
static GAME::Card deserializeCard(const nlohmann::json &card);
|
||||||
static GAME::DiscardPile deserializeDiscardPile(const nlohmann::json &discardPile);
|
static GAME::DiscardPile deserializeDiscardPile(const nlohmann::json &discardPile);
|
||||||
static GAME::HandCard deserializeHandCard(const nlohmann::json &handCard);
|
static std::multiset<GAME::Card> deserializeHandCard(const nlohmann::json &handCard);
|
||||||
|
|
||||||
static std::monostate deserializeEmptyPayload(const nlohmann::json &payload);
|
static std::monostate deserializeEmptyPayload(const nlohmann::json &payload);
|
||||||
static JoinGamePayload deserializeJoinGamePayload(const nlohmann::json &payload);
|
static JoinGamePayload deserializeJoinGamePayload(const nlohmann::json &payload);
|
||||||
|
|||||||
Reference in New Issue
Block a user