From 10c486e996dd58c3470a56f2274d84e3ad792972 Mon Sep 17 00:00:00 2001 From: Jorens Shtekels Date: Sun, 10 Mar 2024 21:16:10 +0200 Subject: [PATCH 1/2] feat: action belongs to role check --- src/lib.cpp | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/lib.cpp b/src/lib.cpp index 0a8ad8a..4b3576e 100644 --- a/src/lib.cpp +++ b/src/lib.cpp @@ -53,9 +53,7 @@ int validateAction( } bool actionBelongsToRole(Role *role, Action *action) { - // TODO: implement - bool belongs = false; - return belongs; + return std::find(role->actions.begin(), role->actions.end(), *action) != role->actions.end(); } bool isActionAllowed(Action *action, std::vector *relevantEvents) { From a291411d10eb4fac81f1b45353cf1ab617865062 Mon Sep 17 00:00:00 2001 From: Jorens Shtekels Date: Sun, 10 Mar 2024 21:33:07 +0200 Subject: [PATCH 2/2] feat: calling validation function --- src/lib.cpp | 23 ++++++++++++++--------- src/lib.hh | 6 +++--- 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/src/lib.cpp b/src/lib.cpp index 4b3576e..cb9ef08 100644 --- a/src/lib.cpp +++ b/src/lib.cpp @@ -3,13 +3,14 @@ #include "prep/prep.hh" #include +#include +#include #include enum VALIDATION_STATUS { NO_PLAYER, ROOM_NOT_IN_PROGRESS, ACTION_PROHIBITED, - ACTION_NOT_ALLOWED, NO_ROLE, ACTION_VALID, }; @@ -22,14 +23,18 @@ void run() { const Action vote = Action("vote", true); Role role1({vote, kill, heal}); Role role2({heal}); - Event event1 = Event("Event 1", 1710087364, 1, true, {vote}, {}, {}); - Event event2 = Event("Event 2", 1710087364, 1, true, {}, {}, {kill}); - Event event3 = Event("Event 3", 1710087364, 1, true, {}, {kill}, {}); - std::vector relatedEvents({event1, event3}); + Event event1 = Event("Event 1", 1710087355, 1, true, {vote}, {}, {}); + 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("player1", role1, PlayerStatus::ALIVE); + Player player2 = Player("player2", role1, PlayerStatus::ALIVE); + int actionValidated = validateAction(&player1, &kill, &room1, &relatedEvents, &player2); + std::cout << actionValidated << std::endl; } int validateAction( - Player *actor, Action *action, Room *room, std::vector *relatedEvents, Player *target = nullptr) { + Player *actor, const Action *action, Room *room, std::vector *relatedEvents, Player *target = nullptr) { if (!actor) { return NO_PLAYER; } @@ -44,7 +49,7 @@ int validateAction( return NO_ROLE; } if (!actionBelongsToRole(role, action)) { - return ACTION_NOT_ALLOWED; + return ACTION_PROHIBITED; } if (!isActionAllowed(action, relatedEvents)) { return ACTION_PROHIBITED; @@ -52,11 +57,11 @@ int validateAction( return ACTION_VALID; } -bool actionBelongsToRole(Role *role, Action *action) { +bool actionBelongsToRole(Role *role, const Action *action) { return std::find(role->actions.begin(), role->actions.end(), *action) != role->actions.end(); } -bool isActionAllowed(Action *action, std::vector *relevantEvents) { +bool isActionAllowed(const Action *action, std::vector *relevantEvents) { // actions are disabled by default bool allowed = false; std::sort(relevantEvents->begin(), relevantEvents->end()); diff --git a/src/lib.hh b/src/lib.hh index 098de38..425808d 100644 --- a/src/lib.hh +++ b/src/lib.hh @@ -2,7 +2,7 @@ void run(); -bool actionBelongsToRole(Role *role, Action *action); -bool isActionAllowed(Action *action, std::vector *relevantEvents); -int validateAction(Player *actor, Action *action, Room *room, std::vector *relatedEvents, Player *target); +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); int functionToTest(int);