diff --git a/src/lib.cpp b/src/lib.cpp index cb9ef08..3b103d4 100644 --- a/src/lib.cpp +++ b/src/lib.cpp @@ -3,43 +3,60 @@ #include "prep/prep.hh" #include -#include -#include +#include #include enum VALIDATION_STATUS { - NO_PLAYER, + 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, }; void run() { - Room room1(1, "Room 1", 1710087364, RoomStatus::IN_PROGRESS); - Room room2(2, "Room 2", 1710087384, RoomStatus::ENDED); 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}); - 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}); + Event event1 = Event("Event 1", 1710087355, 1, true, {}, {}); + 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); + 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, {}); int actionValidated = validateAction(&player1, &kill, &room1, &relatedEvents, &player2); - std::cout << actionValidated << std::endl; + printf("The action validation result is %u\n", actionValidated); } int validateAction( Player *actor, const Action *action, Room *room, std::vector *relatedEvents, Player *target = nullptr) { if (!actor) { - return NO_PLAYER; + 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_PLAYER; + return NO_TARGET_PLAYER_SPECIFIED; } if (room->status != RoomStatus::IN_PROGRESS) { return ROOM_NOT_IN_PROGRESS; @@ -49,7 +66,7 @@ int validateAction( return NO_ROLE; } if (!actionBelongsToRole(role, action)) { - return ACTION_PROHIBITED; + return ACTION_DOES_NOT_BELONG_TO_ROLE; } if (!isActionAllowed(action, relatedEvents)) { return ACTION_PROHIBITED; @@ -57,6 +74,10 @@ int validateAction( 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(); } @@ -75,7 +96,3 @@ bool isActionAllowed(const Action *action, std::vector *relevantEvents) { } return allowed; } - -int functionToTest(int a) { - return a * 2; -} diff --git a/src/lib.hh b/src/lib.hh index 425808d..547d50f 100644 --- a/src/lib.hh +++ b/src/lib.hh @@ -2,7 +2,7 @@ void run(); +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); -int functionToTest(int); diff --git a/src/prep/prep.cpp b/src/prep/prep.cpp index f64a478..aabfd4c 100644 --- a/src/prep/prep.cpp +++ b/src/prep/prep.cpp @@ -2,12 +2,9 @@ #include "timeUtils.hh" +#include #include -int add(int a, int b) { - return a + b; -} - Action::Action(std::string name, bool hasTarget) { this->name = name; this->hasTarget = hasTarget; @@ -17,28 +14,38 @@ 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); - } +Role::Role(std::initializer_list actions): Role(std::vector(actions)) { } -Role::Role(std::vector actions) { - for (auto &a : actions) { - this->actions.push_back(a); - } +Role::Role(std::vector actions): actions(actions) { } -Player::Player(std::string username, Role role, PlayerStatus playerStatus): +Player::Player(uint32_t id, std::string username, Role role, PlayerStatus playerStatus): + id(id), username(username), role(role), playerStatus(playerStatus) { } -Room::Room(uint32_t id, std::string title, uint32_t utcTimestampCreatedAt, RoomStatus status): +bool Player::operator==(const Player &other) const { + return this->id == other.id; +} + +Room::Room( + uint32_t id, std::string title, uint32_t utcTimestampCreatedAt, RoomStatus status, std::vector players): id(id), title(title), - status(status) { + status(status), + players(players) { + this->utcTimestampCreatedAt = createUTCTimestamp(utcTimestampCreatedAt); +} + +Room::Room(uint32_t id, + std::string title, + uint32_t utcTimestampCreatedAt, + RoomStatus status, + std::initializer_list players): + Room(id, title, utcTimestampCreatedAt, status, std::vector(players)) { this->utcTimestampCreatedAt = createUTCTimestamp(utcTimestampCreatedAt); } @@ -46,37 +53,27 @@ Event::Event(std::string title, uint32_t utcTimestampCreatedAt, uint32_t numberNight, bool isVisible, - std::vector causedBy, std::vector prohibits, std::vector allows): title(title), utcTimestampCreatedAt(createUTCTimestamp(utcTimestampCreatedAt)), numberNight(numberNight), - isVisible(isVisible) { + isVisible(isVisible), + prohibits(prohibits), + allows(allows) { this->utcTimestampCreatedAt = createUTCTimestamp(utcTimestampCreatedAt); - for (auto &a : causedBy) { - this->causedBy.push_back(a); - } - for (auto &a : prohibits) { - this->prohibits.push_back(a); - } - for (auto &a : allows) { - this->allows.push_back(a); - } } Event::Event(std::string title, uint32_t utcTimestampCreatedAt, uint32_t numberNight, bool isVisible, - std::initializer_list causedBy, std::initializer_list prohibits, std::initializer_list allows): Event(title, utcTimestampCreatedAt, numberNight, isVisible, - std::vector(causedBy), std::vector(prohibits), std::vector(allows)) { this->utcTimestampCreatedAt = createUTCTimestamp(utcTimestampCreatedAt); diff --git a/src/prep/prep.hh b/src/prep/prep.hh index d4153a9..d99d6fd 100644 --- a/src/prep/prep.hh +++ b/src/prep/prep.hh @@ -1,15 +1,11 @@ #ifndef PREP_H #define PREP_H -#include #include #include #include #include -// For test example -int add(int a, int b); - // All IDs are uint32_t enum EventType { PHASE_CHANGE, @@ -53,11 +49,13 @@ struct Role { }; struct Player { + uint32_t id; std::string username; Role role; PlayerStatus playerStatus; - Player(std::string username, Role role, PlayerStatus playerStatus); + Player(uint32_t id, std::string username, Role role, PlayerStatus playerStatus); + bool operator==(const Player &other) const; }; struct Room { @@ -65,8 +63,15 @@ struct Room { std::string title; std::tm *utcTimestampCreatedAt; RoomStatus status; + std::vector players; - Room(uint32_t id, std::string title, uint32_t utcTimestampCreatedAt, RoomStatus status); + Room( + uint32_t id, std::string title, uint32_t utcTimestampCreatedAt, RoomStatus status, std::vector players); + Room(uint32_t id, + std::string title, + uint32_t utcTimestampCreatedAt, + RoomStatus status, + std::initializer_list players); }; struct Event { @@ -74,7 +79,6 @@ struct Event { std::tm *utcTimestampCreatedAt; uint32_t numberNight; bool isVisible; - std::vector causedBy; std::vector prohibits; std::vector allows; @@ -86,14 +90,12 @@ struct Event { uint32_t utcTimestampCreatedAt, uint32_t numberNight, bool isVisible, - std::vector causedBy, std::vector prohibits, std::vector allows); Event(std::string title, uint32_t utcTimestampCreatedAt, uint32_t numberNight, bool isVisible, - std::initializer_list causedBy, std::initializer_list prohibits, std::initializer_list allows); }; diff --git a/src/test.cpp b/src/test.cpp index 49ef7ca..279a50c 100644 --- a/src/test.cpp +++ b/src/test.cpp @@ -1,17 +1,5 @@ -#include "lib.hh" -#include "prep/prep.hh" - #include -// TEST(TestSuiteName, testName) {...} -/* TEST(ProgramTest, testFunction) { - EXPECT_EQ(functionToTest(4), 8); -} */ - -/* TEST(ProgramTest, testFunctionShouldFail) { - EXPECT_EQ(functionToTest(4), 12); -} */ - -/* TEST(PrepTest, testAddFunc) { - EXPECT_EQ(add(4, 2), 6); -} */ +TEST(ProgramTest, testFunction) { + EXPECT_EQ(8, 8); +}