mirror of
https://github.com/kierankihn/uno-game.git
synced 2025-12-27 02:13:18 +08:00
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:
@@ -11,6 +11,7 @@ add_library(uno-game-lib
|
|||||||
src/game/Player.cpp
|
src/game/Player.cpp
|
||||||
src/game/GameState.cpp
|
src/game/GameState.cpp
|
||||||
src/common/Utils.cpp
|
src/common/Utils.cpp
|
||||||
|
src/network/Message.cpp
|
||||||
)
|
)
|
||||||
|
|
||||||
add_executable(uno-game src/main.cpp)
|
add_executable(uno-game src/main.cpp)
|
||||||
|
|||||||
28
src/network/Message.cpp
Normal file
28
src/network/Message.cpp
Normal 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
60
src/network/Message.h
Normal 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
|
||||||
Reference in New Issue
Block a user