feat(network): add Message class with payload types

- Introduced `Message` class in `src/network` for handling game-related message payloads (e.g., `JOIN_GAME`, `START_GAME`, etc.).
- Added `Message.cpp` and `Message.h` to `CMakeLists.txt`.
This commit is contained in:
Kieran Kihn
2025-11-21 22:49:23 +08:00
parent 6393a5b311
commit 820358e0a3
3 changed files with 89 additions and 0 deletions

View File

@@ -11,6 +11,7 @@ add_library(uno-game-lib
src/game/Player.cpp
src/game/GameState.cpp
src/common/Utils.cpp
src/network/Message.cpp
)
add_executable(uno-game src/main.cpp)

28
src/network/Message.cpp Normal file
View File

@@ -0,0 +1,28 @@
/**
* @file
*
* @author Yuzhe Guo
* @date 2025.11.18
*/
#include "Message.h"
#include <utility>
namespace UNO::NETWORK {
Message::Message(MessagePayloadType messagePayloadType, MessagePayload messagePayload) :
messagePayloadType_(messagePayloadType), messagePayload_(std::move(messagePayload))
{
}
MessagePayloadType Message::getMessagePayloadType() const
{
return this->messagePayloadType_;
}
MessagePayload Message::getMessagePayload() const
{
return this->messagePayload_;
}
} // namespace UNO::NETWORK

60
src/network/Message.h Normal file
View File

@@ -0,0 +1,60 @@
/**
* @file
*
* @author Yuzhe Guo
* @date 2025.11.18
*/
#ifndef UNO_GAME_MESSAGE_H
#define UNO_GAME_MESSAGE_H
#include "../game/Card.h"
#include "../game/CardTile.h"
#include "../game/Player.h"
#include <string>
#include <variant>
namespace UNO::NETWORK {
enum class MessagePayloadType { JOIN_GAME, START_GAME, DRAW_CARD, PLAY_CARD, INIT_GAME, END_GAME };
struct JoinGamePayload {
std::string playerName;
};
struct StartGamePayload {};
struct DrawCardPayload {
int drawCount;
};
struct PlayCardPayload {
GAME::Card card;
};
struct InitGamePayload {
GAME::DiscardPile discardPile;
GAME::HandCard handCard;
size_t currentPlayerIndex;
};
struct EndGamePayload {};
using MessagePayload =
std::variant<std::monostate, JoinGamePayload, StartGamePayload, DrawCardPayload, PlayCardPayload, InitGamePayload, EndGamePayload>;
class Message {
private:
MessagePayloadType messagePayloadType_;
MessagePayload messagePayload_;
public:
Message(MessagePayloadType messagePayloadType, MessagePayload messagePayload);
[[nodiscard]] MessagePayloadType getMessagePayloadType() const;
[[nodiscard]] MessagePayload getMessagePayload() const;
};
} // namespace UNO::NETWORK
#endif // UNO_GAME_MESSAGE_H