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 event3 = Event("Event 3", 1710087369, 1, true, {}, {kill});
|
||||
std::vector<Event> relatedEvents({event2, event3});
|
||||
Player player1 = Player(69, "player1", role1, player::Alive);
|
||||
Player player2 = Player(420, "player2", role1, player::Alive);
|
||||
Room room1(1, "Room 1", 1710087364, room::InProgress, {player1, player2});
|
||||
Room room2(2, "Room 2", 1710087384, room::Ended, {});
|
||||
int validated_action = validate_action(&player1, &kill, &room1, &relatedEvents, &player2);
|
||||
Player player1 = Player(69, "player1", role1, PlayerStatus::Alive);
|
||||
Player player2 = Player(420, "player2", role1, PlayerStatus::Alive);
|
||||
Room room1(1, "Room 1", 1710087364, RoomStatus::InProgress, {player1, player2});
|
||||
Room room2(2, "Room 2", 1710087384, RoomStatus::Ended, {});
|
||||
printf("The action validation result is %u\n", validated_action);
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
@ -8,14 +8,12 @@
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace event {
|
||||
enum Type {
|
||||
PhaseChange,
|
||||
EventAction,
|
||||
RoomStateChange,
|
||||
PlayerStateChange,
|
||||
};
|
||||
} // namespace event
|
||||
enum EventType {
|
||||
PhaseChange,
|
||||
EventAction,
|
||||
RoomStateChange,
|
||||
PlayerStateChange,
|
||||
};
|
||||
|
||||
struct Event {
|
||||
std::string title;
|
||||
|
||||
@ -4,7 +4,7 @@
|
||||
|
||||
#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),
|
||||
username(username),
|
||||
role(role),
|
||||
|
||||
@ -4,21 +4,19 @@
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
|
||||
namespace player {
|
||||
enum Status {
|
||||
Kicked,
|
||||
Alive,
|
||||
Dead,
|
||||
VotedOut,
|
||||
};
|
||||
} // namespace player
|
||||
enum class PlayerStatus {
|
||||
Kicked,
|
||||
Alive,
|
||||
Dead,
|
||||
VotedOut,
|
||||
};
|
||||
|
||||
struct Player {
|
||||
uint32_t id;
|
||||
std::string username;
|
||||
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;
|
||||
};
|
||||
|
||||
@ -7,7 +7,7 @@
|
||||
#include <string>
|
||||
#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),
|
||||
title(title),
|
||||
status(status),
|
||||
@ -16,7 +16,7 @@ Room::Room(uint32_t id, std::string title, uint32_t created_at, room::Status sta
|
||||
}
|
||||
|
||||
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),
|
||||
title(title),
|
||||
status(status),
|
||||
|
||||
@ -8,23 +8,20 @@
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace room {
|
||||
enum Status {
|
||||
AwaitingStart,
|
||||
InProgress,
|
||||
Stopped,
|
||||
Ended,
|
||||
};
|
||||
} // namespace room
|
||||
enum class RoomStatus {
|
||||
AwaitingStart,
|
||||
InProgress,
|
||||
Stopped,
|
||||
Ended,
|
||||
};
|
||||
|
||||
struct Room {
|
||||
uint32_t id;
|
||||
std::string title;
|
||||
std::tm *created_at;
|
||||
room::Status status;
|
||||
RoomStatus status;
|
||||
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, room::Status status, std::initializer_list<Player> players);
|
||||
Room(uint32_t id, std::string title, uint32_t created_at, RoomStatus status, std::vector<Player> players);
|
||||
Room(uint32_t id, std::string title, uint32_t created_at, RoomStatus 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`).
|
||||
* @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) {
|
||||
if (!actor) {
|
||||
return validation::NoActor;
|
||||
return ValidationStatus::NoActor;
|
||||
}
|
||||
if (!action) {
|
||||
return validation::NoAction;
|
||||
return ValidationStatus::NoAction;
|
||||
}
|
||||
if (!room) {
|
||||
return validation::NoRoom;
|
||||
return ValidationStatus::NoRoom;
|
||||
}
|
||||
if (!related_events) {
|
||||
return validation::NoRelatedEvents;
|
||||
return ValidationStatus::NoRelatedEvents;
|
||||
}
|
||||
if (!player_belongs_to_room(actor, room)) {
|
||||
return validation::PlayerNotInRoom;
|
||||
return ValidationStatus::PlayerNotInRoom;
|
||||
}
|
||||
if (action->has_target && !target) {
|
||||
return validation::NoTargetPlayerSpecified;
|
||||
return ValidationStatus::NoTargetPlayerSpecified;
|
||||
}
|
||||
if (room->status != room::Status::InProgress) {
|
||||
return validation::RoomNotInProgress;
|
||||
if (room->status != RoomStatus::InProgress) {
|
||||
return ValidationStatus::RoomNotInProgress;
|
||||
}
|
||||
Role *role = &actor->role;
|
||||
if (!role) {
|
||||
return validation::NoRole;
|
||||
return ValidationStatus::NoRole;
|
||||
}
|
||||
if (!action_belongs_to_role(role, action)) {
|
||||
return validation::ActionDoesNotBelongToRole;
|
||||
return ValidationStatus::ActionDoesNotBelongToRole;
|
||||
}
|
||||
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>
|
||||
|
||||
namespace validation {
|
||||
enum Status {
|
||||
PlayerNotInRoom,
|
||||
NoTargetPlayerSpecified,
|
||||
RoomNotInProgress,
|
||||
ActionDoesNotBelongToRole,
|
||||
ActionProhibited,
|
||||
NoActor,
|
||||
NoAction,
|
||||
NoRole,
|
||||
NoRoom,
|
||||
NoRelatedEvents,
|
||||
ActionValid,
|
||||
};
|
||||
} // namespace validation
|
||||
enum class ValidationStatus {
|
||||
PlayerNotInRoom,
|
||||
NoTargetPlayerSpecified,
|
||||
RoomNotInProgress,
|
||||
ActionDoesNotBelongToRole,
|
||||
ActionProhibited,
|
||||
NoActor,
|
||||
NoAction,
|
||||
NoRole,
|
||||
NoRoom,
|
||||
NoRelatedEvents,
|
||||
ActionValid,
|
||||
};
|
||||
|
||||
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