mirror of
https://github.com/kierankihn/uno-game.git
synced 2025-12-27 02:13:18 +08:00
refactor(network): replace PlayerPublicState with ClientPlayerState
- Updated `InitGamePayload` to use `ClientPlayerState` instead of `PlayerPublicState`. - Adjusted serialization and deserialization methods in `MessageSerializer` accordingly.
This commit is contained in:
@@ -8,6 +8,7 @@
|
|||||||
#define UNO_GAME_MESSAGE_H
|
#define UNO_GAME_MESSAGE_H
|
||||||
#include "../game/Card.h"
|
#include "../game/Card.h"
|
||||||
#include "../game/CardTile.h"
|
#include "../game/CardTile.h"
|
||||||
|
#include "../game/GameState.h"
|
||||||
#include "../game/Player.h"
|
#include "../game/Player.h"
|
||||||
|
|
||||||
|
|
||||||
@@ -30,19 +31,13 @@ namespace UNO::NETWORK {
|
|||||||
std::vector<GAME::Card> cards;
|
std::vector<GAME::Card> cards;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct PlayerPublicState {
|
|
||||||
std::string name;
|
|
||||||
size_t remainingCardCount;
|
|
||||||
bool isUno;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct PlayCardPayload {
|
struct PlayCardPayload {
|
||||||
GAME::Card card;
|
GAME::Card card;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct InitGamePayload {
|
struct InitGamePayload {
|
||||||
size_t playerId;
|
size_t playerId;
|
||||||
std::vector<PlayerPublicState> players;
|
std::vector<GAME::ClientPlayerState> players;
|
||||||
GAME::DiscardPile discardPile;
|
GAME::DiscardPile discardPile;
|
||||||
std::multiset<GAME::Card> handCard;
|
std::multiset<GAME::Card> handCard;
|
||||||
size_t currentPlayerIndex;
|
size_t currentPlayerIndex;
|
||||||
|
|||||||
@@ -28,16 +28,16 @@ namespace UNO::NETWORK {
|
|||||||
return serializeCards(cards.begin(), cards.end());
|
return serializeCards(cards.begin(), cards.end());
|
||||||
}
|
}
|
||||||
|
|
||||||
nlohmann::json MessageSerializer::serializePlayerPublicState(const PlayerPublicState &state)
|
nlohmann::json MessageSerializer::serializeClientPlayerState(const GAME::ClientPlayerState &state)
|
||||||
{
|
{
|
||||||
return {{"name", state.name}, {"remaining_cards", state.remainingCardCount}, {"is_uno", state.isUno}};
|
return {{"name", state.getName()}, {"remaining_cards", state.getRemainingCardCount()}, {"is_uno", state.getIsUno()}};
|
||||||
}
|
}
|
||||||
|
|
||||||
nlohmann::json MessageSerializer::serializePlayerPublicStates(const std::vector<PlayerPublicState> &states)
|
nlohmann::json MessageSerializer::serializeClientPlayerStates(const std::vector<GAME::ClientPlayerState> &states)
|
||||||
{
|
{
|
||||||
nlohmann::json result = nlohmann::json::array();
|
nlohmann::json result = nlohmann::json::array();
|
||||||
for (const auto &state : states) {
|
for (const auto &state : states) {
|
||||||
result.push_back(serializePlayerPublicState(state));
|
result.push_back(serializeClientPlayerState(state));
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
@@ -70,7 +70,7 @@ namespace UNO::NETWORK {
|
|||||||
nlohmann::json MessageSerializer::serializePayload(const InitGamePayload &payload)
|
nlohmann::json MessageSerializer::serializePayload(const InitGamePayload &payload)
|
||||||
{
|
{
|
||||||
return {{"player_id", payload.playerId},
|
return {{"player_id", payload.playerId},
|
||||||
{"players", serializePlayerPublicStates(payload.players)},
|
{"players", serializeClientPlayerStates(payload.players)},
|
||||||
{"discard_pile", serializeDiscardPile(payload.discardPile)},
|
{"discard_pile", serializeDiscardPile(payload.discardPile)},
|
||||||
{"hand_card", serializeCards(payload.handCard.begin(), payload.handCard.end())},
|
{"hand_card", serializeCards(payload.handCard.begin(), payload.handCard.end())},
|
||||||
{"current_player", payload.currentPlayerIndex}};
|
{"current_player", payload.currentPlayerIndex}};
|
||||||
@@ -230,7 +230,7 @@ namespace UNO::NETWORK {
|
|||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
PlayerPublicState MessageSerializer::deserializePlayerPublicState(const nlohmann::json &payload)
|
GAME::ClientPlayerState MessageSerializer::deserializeClientPlayerState(const nlohmann::json &payload)
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
if (payload.is_object() == false) {
|
if (payload.is_object() == false) {
|
||||||
@@ -252,15 +252,15 @@ namespace UNO::NETWORK {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<PlayerPublicState> MessageSerializer::deserializePlayerPublicStates(const nlohmann::json &payload)
|
std::vector<GAME::ClientPlayerState> MessageSerializer::deserializeClientPlayerStates(const nlohmann::json &payload)
|
||||||
{
|
{
|
||||||
if (payload.is_array() == false) {
|
if (payload.is_array() == false) {
|
||||||
throw std::invalid_argument("Invalid players field in INIT_GAME payload: expected JSON array");
|
throw std::invalid_argument("Invalid players field in INIT_GAME payload: expected JSON array");
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<PlayerPublicState> players;
|
std::vector<GAME::ClientPlayerState> players;
|
||||||
for (const auto &entry : payload) {
|
for (const auto &entry : payload) {
|
||||||
players.push_back(deserializePlayerPublicState(entry));
|
players.push_back(deserializeClientPlayerState(entry));
|
||||||
}
|
}
|
||||||
return players;
|
return players;
|
||||||
}
|
}
|
||||||
@@ -349,7 +349,7 @@ namespace UNO::NETWORK {
|
|||||||
throw std::invalid_argument("Invalid 'current_player' field in INIT_GAME payload: expected unsigned integer");
|
throw std::invalid_argument("Invalid 'current_player' field in INIT_GAME payload: expected unsigned integer");
|
||||||
}
|
}
|
||||||
return {payload.at("player_id"),
|
return {payload.at("player_id"),
|
||||||
deserializePlayerPublicStates(payload.at("players")),
|
deserializeClientPlayerStates(payload.at("players")),
|
||||||
deserializeDiscardPile(payload.at("discard_pile")),
|
deserializeDiscardPile(payload.at("discard_pile")),
|
||||||
deserializeHandCard(payload.at("hand_card")),
|
deserializeHandCard(payload.at("hand_card")),
|
||||||
payload.at("current_player")};
|
payload.at("current_player")};
|
||||||
|
|||||||
@@ -24,8 +24,8 @@ 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 serializePlayerPublicState(const PlayerPublicState &state);
|
static nlohmann::json serializeClientPlayerState(const GAME::ClientPlayerState &state);
|
||||||
static nlohmann::json serializePlayerPublicStates(const std::vector<PlayerPublicState> &states);
|
static nlohmann::json serializeClientPlayerStates(const std::vector<GAME::ClientPlayerState> &states);
|
||||||
|
|
||||||
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);
|
||||||
@@ -45,8 +45,8 @@ namespace UNO::NETWORK {
|
|||||||
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 std::multiset<GAME::Card> deserializeHandCard(const nlohmann::json &handCard);
|
static std::multiset<GAME::Card> deserializeHandCard(const nlohmann::json &handCard);
|
||||||
static PlayerPublicState deserializePlayerPublicState(const nlohmann::json &payload);
|
static GAME::ClientPlayerState deserializeClientPlayerState(const nlohmann::json &payload);
|
||||||
static std::vector<PlayerPublicState> deserializePlayerPublicStates(const nlohmann::json &payload);
|
static std::vector<GAME::ClientPlayerState> deserializeClientPlayerStates(const nlohmann::json &payload);
|
||||||
|
|
||||||
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