mirror of
https://github.com/kierankihn/uno-game.git
synced 2025-12-27 02:13:18 +08:00
feat(client): add PlayerAction struct and payload types
- Introduced `PlayerAction` to encapsulate player actions, including type and payload. - Added specific payload structures for connect, start game, play card, and draw card actions.
This commit is contained in:
22
src/client/PlayerAction.cpp
Normal file
22
src/client/PlayerAction.cpp
Normal file
@@ -0,0 +1,22 @@
|
||||
/**
|
||||
* @file PlayerAction.cpp
|
||||
*
|
||||
* @author Yuzhe Guo
|
||||
* @date 2025.12.07
|
||||
*/
|
||||
|
||||
#include "PlayerAction.h"
|
||||
|
||||
#include <stdexcept>
|
||||
|
||||
namespace UNO::CLIENT {
|
||||
PlayerAction::PlayerAction(PlayerActionType type, PlayerActionPayload payload) : playerActionType(type), payload(std::move(payload))
|
||||
{
|
||||
if (playerActionType == PlayerActionType::CONNECT && std::holds_alternative<PlayerConnectPayload>(payload) == false
|
||||
|| playerActionType == PlayerActionType::START_GAME && std::holds_alternative<PlayerStartGamePayload>(payload) == false
|
||||
|| playerActionType == PlayerActionType::PLAY_CARD && std::holds_alternative<PlayerPlayCardPayload>(payload) == false
|
||||
|| playerActionType == PlayerActionType::DRAW_CARD && std::holds_alternative<PlayerDrawCardPayload>(payload) == false) {
|
||||
throw std::invalid_argument("Payload type do not match");
|
||||
}
|
||||
}
|
||||
} // namespace UNO::CLIENT
|
||||
38
src/client/PlayerAction.h
Normal file
38
src/client/PlayerAction.h
Normal file
@@ -0,0 +1,38 @@
|
||||
/**
|
||||
* @file PlayerAction.h
|
||||
*
|
||||
* @author Yuzhe Guo
|
||||
* @date 2025.12.07
|
||||
*/
|
||||
#pragma once
|
||||
#include <string>
|
||||
#include <variant>
|
||||
|
||||
#include "../game/Card.h"
|
||||
|
||||
namespace UNO::CLIENT {
|
||||
enum class PlayerActionType { CONNECT, START_GAME, PLAY_CARD, DRAW_CARD };
|
||||
|
||||
struct PlayerConnectPayload {
|
||||
std::string playerName;
|
||||
std::string host;
|
||||
uint16_t port;
|
||||
};
|
||||
|
||||
struct PlayerStartGamePayload {};
|
||||
|
||||
struct PlayerPlayCardPayload {
|
||||
GAME::Card card;
|
||||
};
|
||||
|
||||
struct PlayerDrawCardPayload {};
|
||||
|
||||
using PlayerActionPayload = std::variant<PlayerConnectPayload, PlayerStartGamePayload, PlayerPlayCardPayload, PlayerDrawCardPayload>;
|
||||
|
||||
struct PlayerAction {
|
||||
PlayerActionType playerActionType;
|
||||
PlayerActionPayload payload;
|
||||
|
||||
PlayerAction(PlayerActionType type, PlayerActionPayload payload);
|
||||
};
|
||||
} // namespace UNO::CLIENT
|
||||
Reference in New Issue
Block a user