From 820358e0a370c494e2f1569d3db10073660b9f50 Mon Sep 17 00:00:00 2001 From: Kieran Kihn <114803508+kierankihn@users.noreply.github.com> Date: Fri, 21 Nov 2025 22:49:23 +0800 Subject: [PATCH] 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`. --- CMakeLists.txt | 1 + src/network/Message.cpp | 28 +++++++++++++++++++ src/network/Message.h | 60 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 89 insertions(+) create mode 100644 src/network/Message.cpp create mode 100644 src/network/Message.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 1051393..7660f7e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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) diff --git a/src/network/Message.cpp b/src/network/Message.cpp new file mode 100644 index 0000000..85597b3 --- /dev/null +++ b/src/network/Message.cpp @@ -0,0 +1,28 @@ +/** + * @file + * + * @author Yuzhe Guo + * @date 2025.11.18 + */ +#include "Message.h" + +#include + +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 \ No newline at end of file diff --git a/src/network/Message.h b/src/network/Message.h new file mode 100644 index 0000000..4ed4e8e --- /dev/null +++ b/src/network/Message.h @@ -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 +#include + + +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; + + 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