diff --git a/src/lib.cc b/src/lib.cc index 3175e00..e69de29 100644 --- a/src/lib.cc +++ b/src/lib.cc @@ -1,80 +0,0 @@ -#include "lib.hh" - -#include "prep/prep.hh" - -#include -#include -#include - -enum VALIDATION_STATUS { - PLAYER_NOT_IN_ROOM, - NO_TARGET_PLAYER_SPECIFIED, - ROOM_NOT_IN_PROGRESS, - ACTION_DOES_NOT_BELONG_TO_ROLE, - ACTION_PROHIBITED, - NO_ACTOR, - NO_ACTION, - NO_ROLE, - NO_ROOM, - NO_RELATED_EVENTS, - ACTION_VALID, -}; - -int validateAction( - Player *actor, const Action *action, Room *room, std::vector *relatedEvents, Player *target = nullptr) { - if (!actor) { - return NO_ACTOR; - } - if (!action) { - return NO_ACTION; - } - if (!room) { - return NO_ROOM; - } - if (!relatedEvents) { - return NO_RELATED_EVENTS; - } - if (!playerBelongsToRoom(actor, room)) { - return PLAYER_NOT_IN_ROOM; - } - if (action->hasTarget && !target) { - return NO_TARGET_PLAYER_SPECIFIED; - } - if (room->status != RoomStatus::IN_PROGRESS) { - return ROOM_NOT_IN_PROGRESS; - } - Role *role = &actor->role; - if (!role) { - return NO_ROLE; - } - if (!actionBelongsToRole(role, action)) { - return ACTION_DOES_NOT_BELONG_TO_ROLE; - } - if (!isActionAllowed(action, relatedEvents)) { - return ACTION_PROHIBITED; - } - return ACTION_VALID; -} - -bool playerBelongsToRoom(Player *player, Room *room) { - return std::find(room->players.begin(), room->players.end(), *player) != room->players.end(); -} - -bool actionBelongsToRole(Role *role, const Action *action) { - return std::find(role->actions.begin(), role->actions.end(), *action) != role->actions.end(); -} - -bool isActionAllowed(const Action *action, std::vector *relevantEvents) { - // actions are disabled by default - bool allowed = false; - std::sort(relevantEvents->begin(), relevantEvents->end()); - for (auto &event : *relevantEvents) { - if (std::find(event.prohibits.begin(), event.prohibits.end(), *action) != event.prohibits.end()) { - allowed = false; - } - if (std::find(event.allows.begin(), event.allows.end(), *action) != event.allows.end()) { - allowed = true; - } - } - return allowed; -} diff --git a/src/lib.hh b/src/lib.hh index 15d0946..fc3ec1f 100644 --- a/src/lib.hh +++ b/src/lib.hh @@ -1,10 +1 @@ -#include "action.hh" -#include "player.hh" -#include "prep/prep.hh" -#include "role.hh" -#include "room.hh" - -bool playerBelongsToRoom(Player *player, Room *room); -bool actionBelongsToRole(Role *role, const Action *action); -bool isActionAllowed(const Action *action, std::vector *relevantEvents); -int validateAction(Player *actor, const Action *action, Room *room, std::vector *relatedEvents, Player *target); +#include "validation.hh" diff --git a/src/main.cc b/src/main.cc index 17e0f09..50b95bc 100644 --- a/src/main.cc +++ b/src/main.cc @@ -12,10 +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 relatedEvents({event2, event3}); - Player player1 = Player(69, "player1", role1, PlayerStatus::ALIVE); - Player player2 = Player(420, "player2", role1, PlayerStatus::ALIVE); - Room room1(1, "Room 1", 1710087364, RoomStatus::IN_PROGRESS, {player1, player2}); - Room room2(2, "Room 2", 1710087384, RoomStatus::ENDED, {}); + 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 actionValidated = validateAction(&player1, &kill, &room1, &relatedEvents, &player2); printf("The action validation result is %u\n", actionValidated); return EXIT_SUCCESS; diff --git a/src/player.cc b/src/player.cc index 9764df2..8e40a81 100644 --- a/src/player.cc +++ b/src/player.cc @@ -4,7 +4,7 @@ #include -Player::Player(uint32_t id, std::string username, Role role, PlayerStatus status): +Player::Player(uint32_t id, std::string username, Role role, player::Status status): id(id), username(username), role(role), diff --git a/src/room.cc b/src/room.cc index d914c55..64dd270 100644 --- a/src/room.cc +++ b/src/room.cc @@ -7,7 +7,7 @@ #include #include -Room::Room(uint32_t id, std::string title, uint32_t created_at, RoomStatus status, std::vector players): +Room::Room(uint32_t id, std::string title, uint32_t created_at, room::Status status, std::vector players): id(id), title(title), created_at(create_utc_timestamp(created_at)), @@ -16,7 +16,7 @@ Room::Room(uint32_t id, std::string title, uint32_t created_at, RoomStatus statu } Room::Room( - uint32_t id, std::string title, uint32_t created_at, RoomStatus status, std::initializer_list players): + uint32_t id, std::string title, uint32_t created_at, room::Status status, std::initializer_list players): id(id), title(title), created_at(create_utc_timestamp(created_at)), diff --git a/src/validation.cc b/src/validation.cc new file mode 100644 index 0000000..cc0f231 --- /dev/null +++ b/src/validation.cc @@ -0,0 +1,64 @@ +#include "validation.hh" + +#include "room.hh" + +#include + +bool player_belongs_to_room(Player *player, Room *room) { + return std::find(room->players.begin(), room->players.end(), *player) != room->players.end(); +} + +bool action_belongs_to_role(Role *role, const Action *action) { + return std::find(role->actions.begin(), role->actions.end(), *action) != role->actions.end(); +} + +bool is_action_allowed(const Action *action, std::vector *relevantEvents) { + // actions are disabled by default + bool allowed = false; + std::sort(relevantEvents->begin(), relevantEvents->end()); + for (auto &event : *relevantEvents) { + if (std::find(event.prohibits.begin(), event.prohibits.end(), *action) != event.prohibits.end()) { + allowed = false; + } + if (std::find(event.allows.begin(), event.allows.end(), *action) != event.allows.end()) { + allowed = true; + } + } + return allowed; +} + +int validateAction( + Player *actor, const Action *action, Room *room, std::vector *relatedEvents, Player *target = nullptr) { + if (!actor) { + return validation::NoActor; + } + if (!action) { + return validation::NoAction; + } + if (!room) { + return validation::NoRoom; + } + if (!relatedEvents) { + return validation::NoRelatedEvents; + } + if (!player_belongs_to_room(actor, room)) { + return validation::PlayerNotInRoom; + } + if (action->has_target && !target) { + return validation::NoTargetPlayerSpecified; + } + if (room->status != room::Status::InProgress) { + return validation::RoomNotInProgress; + } + Role *role = &actor->role; + if (!role) { + return validation::NoRole; + } + if (!action_belongs_to_role(role, action)) { + return validation::ActionDoesNotBelongToRole; + } + if (!is_action_allowed(action, relatedEvents)) { + return validation::ActionProhibited; + } + return validation::ActionProhibited; +} diff --git a/src/validation.hh b/src/validation.hh new file mode 100644 index 0000000..f07b3fe --- /dev/null +++ b/src/validation.hh @@ -0,0 +1,26 @@ +#ifndef VALIDATION_HH +#define VALIDATION_HH + +#include "event.hh" +#include "player.hh" +#include "room.hh" + +namespace validation { + enum Status { + PlayerNotInRoom, + NoTargetPlayerSpecified, + RoomNotInProgress, + ActionDoesNotBelongToRole, + ActionProhibited, + NoActor, + NoAction, + NoRole, + NoRoom, + NoRelatedEvents, + ActionValid, + }; +} + +int validateAction(Player *actor, const Action *action, Room *room, std::vector *relatedEvents, Player *target); + +#endif // !VALIDATION_HH