mirror of
https://github.com/jorenchik/testing-spring-2024.git
synced 2025-10-21 20:10:36 +00:00
refactor(enum): use enum class
refactor(enum): convert validation enum to enum class
This commit is contained in:
parent
4e63f81655
commit
7fd0c887ec
@ -12,11 +12,10 @@ int main(int argc, char *argv[]) {
|
|||||||
Event event2 = Event("Event 2", 1710087363, 1, true, {kill}, {});
|
Event event2 = Event("Event 2", 1710087363, 1, true, {kill}, {});
|
||||||
Event event3 = Event("Event 3", 1710087369, 1, true, {}, {kill});
|
Event event3 = Event("Event 3", 1710087369, 1, true, {}, {kill});
|
||||||
std::vector<Event> relatedEvents({event2, event3});
|
std::vector<Event> relatedEvents({event2, event3});
|
||||||
Player player1 = Player(69, "player1", role1, player::Alive);
|
Player player1 = Player(69, "player1", role1, PlayerStatus::Alive);
|
||||||
Player player2 = Player(420, "player2", role1, player::Alive);
|
Player player2 = Player(420, "player2", role1, PlayerStatus::Alive);
|
||||||
Room room1(1, "Room 1", 1710087364, room::InProgress, {player1, player2});
|
Room room1(1, "Room 1", 1710087364, RoomStatus::InProgress, {player1, player2});
|
||||||
Room room2(2, "Room 2", 1710087384, room::Ended, {});
|
Room room2(2, "Room 2", 1710087384, RoomStatus::Ended, {});
|
||||||
int validated_action = validate_action(&player1, &kill, &room1, &relatedEvents, &player2);
|
|
||||||
printf("The action validation result is %u\n", validated_action);
|
printf("The action validation result is %u\n", validated_action);
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -8,14 +8,12 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
namespace event {
|
enum EventType {
|
||||||
enum Type {
|
PhaseChange,
|
||||||
PhaseChange,
|
EventAction,
|
||||||
EventAction,
|
RoomStateChange,
|
||||||
RoomStateChange,
|
PlayerStateChange,
|
||||||
PlayerStateChange,
|
};
|
||||||
};
|
|
||||||
} // namespace event
|
|
||||||
|
|
||||||
struct Event {
|
struct Event {
|
||||||
std::string title;
|
std::string title;
|
||||||
|
|||||||
@ -4,7 +4,7 @@
|
|||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
Player::Player(uint32_t id, std::string username, Role role, player::Status status):
|
Player::Player(uint32_t id, std::string username, Role role, PlayerStatus status):
|
||||||
id(id),
|
id(id),
|
||||||
username(username),
|
username(username),
|
||||||
role(role),
|
role(role),
|
||||||
|
|||||||
@ -4,21 +4,19 @@
|
|||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
namespace player {
|
enum class PlayerStatus {
|
||||||
enum Status {
|
Kicked,
|
||||||
Kicked,
|
Alive,
|
||||||
Alive,
|
Dead,
|
||||||
Dead,
|
VotedOut,
|
||||||
VotedOut,
|
};
|
||||||
};
|
|
||||||
} // namespace player
|
|
||||||
|
|
||||||
struct Player {
|
struct Player {
|
||||||
uint32_t id;
|
uint32_t id;
|
||||||
std::string username;
|
std::string username;
|
||||||
Role role;
|
Role role;
|
||||||
player::Status status;
|
PlayerStatus status;
|
||||||
|
|
||||||
Player(uint32_t id, std::string username, Role role, player::Status status);
|
Player(uint32_t id, std::string username, Role role, PlayerStatus status);
|
||||||
bool operator==(const Player &other) const;
|
bool operator==(const Player &other) const;
|
||||||
};
|
};
|
||||||
|
|||||||
@ -7,7 +7,7 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
Room::Room(uint32_t id, std::string title, uint32_t created_at, room::Status status, std::vector<Player> players):
|
Room::Room(uint32_t id, std::string title, uint32_t created_at, RoomStatus status, std::vector<Player> players):
|
||||||
id(id),
|
id(id),
|
||||||
title(title),
|
title(title),
|
||||||
status(status),
|
status(status),
|
||||||
@ -16,7 +16,7 @@ Room::Room(uint32_t id, std::string title, uint32_t created_at, room::Status sta
|
|||||||
}
|
}
|
||||||
|
|
||||||
Room::Room(
|
Room::Room(
|
||||||
uint32_t id, std::string title, uint32_t created_at, room::Status status, std::initializer_list<Player> players):
|
uint32_t id, std::string title, uint32_t created_at, RoomStatus status, std::initializer_list<Player> players):
|
||||||
id(id),
|
id(id),
|
||||||
title(title),
|
title(title),
|
||||||
status(status),
|
status(status),
|
||||||
|
|||||||
@ -8,23 +8,20 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
namespace room {
|
enum class RoomStatus {
|
||||||
enum Status {
|
AwaitingStart,
|
||||||
AwaitingStart,
|
InProgress,
|
||||||
InProgress,
|
Stopped,
|
||||||
Stopped,
|
Ended,
|
||||||
Ended,
|
};
|
||||||
};
|
|
||||||
} // namespace room
|
|
||||||
|
|
||||||
struct Room {
|
struct Room {
|
||||||
uint32_t id;
|
uint32_t id;
|
||||||
std::string title;
|
std::string title;
|
||||||
std::tm *created_at;
|
std::tm *created_at;
|
||||||
room::Status status;
|
RoomStatus status;
|
||||||
std::vector<Player> players;
|
std::vector<Player> players;
|
||||||
|
|
||||||
Room(uint32_t id, std::string title, uint32_t created_at, room::Status status, std::vector<Player> players);
|
Room(uint32_t id, std::string title, uint32_t created_at, RoomStatus status, std::vector<Player> players);
|
||||||
Room(
|
Room(uint32_t id, std::string title, uint32_t created_at, RoomStatus status, std::initializer_list<Player> players);
|
||||||
uint32_t id, std::string title, uint32_t created_at, room::Status status, std::initializer_list<Player> players);
|
|
||||||
};
|
};
|
||||||
|
|||||||
@ -60,38 +60,38 @@ bool is_action_allowed(const Action *action, const std::vector<Event> *relevant_
|
|||||||
* @param target Pointer to the target player (optional, defaults to `nullptr`).
|
* @param target Pointer to the target player (optional, defaults to `nullptr`).
|
||||||
* @return An integer representing the validation status.
|
* @return An integer representing the validation status.
|
||||||
*/
|
*/
|
||||||
int validate_action(
|
ValidationStatus validate_action(
|
||||||
Player *actor, const Action *action, Room *room, std::vector<Event> *related_events, Player *target = nullptr) {
|
Player *actor, const Action *action, Room *room, std::vector<Event> *related_events, Player *target = nullptr) {
|
||||||
if (!actor) {
|
if (!actor) {
|
||||||
return validation::NoActor;
|
return ValidationStatus::NoActor;
|
||||||
}
|
}
|
||||||
if (!action) {
|
if (!action) {
|
||||||
return validation::NoAction;
|
return ValidationStatus::NoAction;
|
||||||
}
|
}
|
||||||
if (!room) {
|
if (!room) {
|
||||||
return validation::NoRoom;
|
return ValidationStatus::NoRoom;
|
||||||
}
|
}
|
||||||
if (!related_events) {
|
if (!related_events) {
|
||||||
return validation::NoRelatedEvents;
|
return ValidationStatus::NoRelatedEvents;
|
||||||
}
|
}
|
||||||
if (!player_belongs_to_room(actor, room)) {
|
if (!player_belongs_to_room(actor, room)) {
|
||||||
return validation::PlayerNotInRoom;
|
return ValidationStatus::PlayerNotInRoom;
|
||||||
}
|
}
|
||||||
if (action->has_target && !target) {
|
if (action->has_target && !target) {
|
||||||
return validation::NoTargetPlayerSpecified;
|
return ValidationStatus::NoTargetPlayerSpecified;
|
||||||
}
|
}
|
||||||
if (room->status != room::Status::InProgress) {
|
if (room->status != RoomStatus::InProgress) {
|
||||||
return validation::RoomNotInProgress;
|
return ValidationStatus::RoomNotInProgress;
|
||||||
}
|
}
|
||||||
Role *role = &actor->role;
|
Role *role = &actor->role;
|
||||||
if (!role) {
|
if (!role) {
|
||||||
return validation::NoRole;
|
return ValidationStatus::NoRole;
|
||||||
}
|
}
|
||||||
if (!action_belongs_to_role(role, action)) {
|
if (!action_belongs_to_role(role, action)) {
|
||||||
return validation::ActionDoesNotBelongToRole;
|
return ValidationStatus::ActionDoesNotBelongToRole;
|
||||||
}
|
}
|
||||||
if (!is_action_allowed(action, related_events)) {
|
if (!is_action_allowed(action, related_events)) {
|
||||||
return validation::ActionProhibited;
|
return ValidationStatus::ActionProhibited;
|
||||||
}
|
}
|
||||||
return validation::ActionProhibited;
|
return ValidationStatus::ActionProhibited;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -5,20 +5,20 @@
|
|||||||
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
namespace validation {
|
enum class ValidationStatus {
|
||||||
enum Status {
|
PlayerNotInRoom,
|
||||||
PlayerNotInRoom,
|
NoTargetPlayerSpecified,
|
||||||
NoTargetPlayerSpecified,
|
RoomNotInProgress,
|
||||||
RoomNotInProgress,
|
ActionDoesNotBelongToRole,
|
||||||
ActionDoesNotBelongToRole,
|
ActionProhibited,
|
||||||
ActionProhibited,
|
NoActor,
|
||||||
NoActor,
|
NoAction,
|
||||||
NoAction,
|
NoRole,
|
||||||
NoRole,
|
NoRoom,
|
||||||
NoRoom,
|
NoRelatedEvents,
|
||||||
NoRelatedEvents,
|
ActionValid,
|
||||||
ActionValid,
|
};
|
||||||
};
|
|
||||||
} // namespace validation
|
|
||||||
|
|
||||||
int validate_action(Player *actor, const Action *action, Room *room, std::vector<Event> *relatedEvents, Player *target);
|
|
||||||
|
ValidationStatus
|
||||||
|
validate_action(Player *actor, const Action *action, Room *room, std::vector<Event> *relatedEvents, Player *target);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user