From 9a7bc42db6476dddd640bfee53105260280736a4 Mon Sep 17 00:00:00 2001 From: Jorens Shtekels Date: Sun, 10 Mar 2024 20:42:35 +0200 Subject: [PATCH 1/2] feat: target player bool for action Added validation logic that checks for target player if needed. --- src/lib.cpp | 12 ++++++++---- src/prep/prep.cpp | 5 +++++ src/prep/prep.hh | 15 +++++++-------- 3 files changed, 20 insertions(+), 12 deletions(-) diff --git a/src/lib.cpp b/src/lib.cpp index 128417f..41f76e0 100644 --- a/src/lib.cpp +++ b/src/lib.cpp @@ -16,9 +16,11 @@ enum VALIDATION_STATUS { void run() { Room room1(1, "Room 1", 1710087364, RoomStatus::IN_PROGRESS); Room room2(2, "Room 2", 1710087384, RoomStatus::ENDED); - Role role1({Action::KILL, Action::HEAL}); - std::vector actions = {Action::HEAL}; - Role role2(actions); + const Action kill = Action("kill", true); + const Action heal = Action("heal", true); + const Action vote = Action("vote", true); + Role role1({vote, kill, heal}); + Role role2({heal}); } int validateAction( @@ -26,7 +28,9 @@ int validateAction( if (!actor) { return NO_PLAYER; } - // TODO: Check if action has a target + if (action->hasTarget && !target) { + return NO_PLAYER; + } if (room->status != RoomStatus::IN_PROGRESS) { return ROOM_NOT_IN_PROGRESS; } diff --git a/src/prep/prep.cpp b/src/prep/prep.cpp index 6ef7707..7c2453d 100644 --- a/src/prep/prep.cpp +++ b/src/prep/prep.cpp @@ -8,6 +8,11 @@ int add(int a, int b) { return a + b; } +Action::Action(std::string name, bool hasTarget) { + this->name = name; + this->hasTarget = hasTarget; +} + Role::Role(std::initializer_list actions) { for (auto &a : actions) { this->actions.push_back(a); diff --git a/src/prep/prep.hh b/src/prep/prep.hh index 01969c3..6ba1ee2 100644 --- a/src/prep/prep.hh +++ b/src/prep/prep.hh @@ -18,14 +18,6 @@ enum EventType { PLAYER_STATE_CHANGE, }; -enum Action { - KILL, - VOTE, - INVESTIGATE, - HEAL, - PROTECT, -}; - enum RoomStatus { AWAITING_START, IN_PROGRESS, @@ -45,6 +37,13 @@ struct Player; struct Room; struct Event; +struct Action { + std::string name; + bool hasTarget; + + Action(std::string name, bool hasTarget); +}; + struct Role { std::vector actions; From ce9e6be7e721099ea867f1bec31c9c8927854210 Mon Sep 17 00:00:00 2001 From: Jorens Shtekels Date: Sun, 10 Mar 2024 21:03:34 +0200 Subject: [PATCH 2/2] feat: event prohibition --- src/lib.cpp | 20 +++++++++++++++++--- src/lib.hh | 2 +- src/prep/prep.cpp | 16 ++++++++++++++++ src/prep/prep.hh | 5 +++++ 4 files changed, 39 insertions(+), 4 deletions(-) diff --git a/src/lib.cpp b/src/lib.cpp index 41f76e0..0a8ad8a 100644 --- a/src/lib.cpp +++ b/src/lib.cpp @@ -2,6 +2,7 @@ #include "prep/prep.hh" +#include #include enum VALIDATION_STATUS { @@ -21,6 +22,10 @@ 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}); } int validateAction( @@ -41,7 +46,7 @@ int validateAction( if (!actionBelongsToRole(role, action)) { return ACTION_NOT_ALLOWED; } - if (!isActionAllowed(relatedEvents)) { + if (!isActionAllowed(action, relatedEvents)) { return ACTION_PROHIBITED; } return ACTION_VALID; @@ -53,9 +58,18 @@ bool actionBelongsToRole(Role *role, Action *action) { return belongs; } -bool isActionAllowed(std::vector *relevantEvents) { - // TODO: implement +bool isActionAllowed(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 440093d..098de38 100644 --- a/src/lib.hh +++ b/src/lib.hh @@ -3,6 +3,6 @@ void run(); bool actionBelongsToRole(Role *role, Action *action); -bool isActionAllowed(std::vector *relevantEvents); +bool isActionAllowed(Action *action, std::vector *relevantEvents); int validateAction(Player *actor, Action *action, Room *room, std::vector *relatedEvents, Player *target); int functionToTest(int); diff --git a/src/prep/prep.cpp b/src/prep/prep.cpp index 7c2453d..f64a478 100644 --- a/src/prep/prep.cpp +++ b/src/prep/prep.cpp @@ -13,6 +13,10 @@ Action::Action(std::string name, bool hasTarget) { this->hasTarget = hasTarget; } +bool Action::operator==(const Action &other) const { + return this->name == other.name; +} + Role::Role(std::initializer_list actions) { for (auto &a : actions) { this->actions.push_back(a); @@ -77,3 +81,15 @@ Event::Event(std::string title, std::vector(allows)) { this->utcTimestampCreatedAt = createUTCTimestamp(utcTimestampCreatedAt); } + +bool Event::operator<(const Event &right) const { + return this->utcTimestampCreatedAt < right.utcTimestampCreatedAt; +} + +bool Event::operator==(const Event &right) const { + return this->utcTimestampCreatedAt == right.utcTimestampCreatedAt; +} + +bool Event::operator>(const Event &right) const { + return this->utcTimestampCreatedAt > right.utcTimestampCreatedAt; +} diff --git a/src/prep/prep.hh b/src/prep/prep.hh index 6ba1ee2..d4153a9 100644 --- a/src/prep/prep.hh +++ b/src/prep/prep.hh @@ -42,6 +42,7 @@ struct Action { bool hasTarget; Action(std::string name, bool hasTarget); + bool operator==(const Action &other) const; }; struct Role { @@ -77,6 +78,10 @@ struct Event { std::vector prohibits; std::vector allows; + bool operator<(const Event &other) const; + bool operator==(const Event &other) const; + bool operator>(const Event &other) const; + Event(std::string title, uint32_t utcTimestampCreatedAt, uint32_t numberNight,