From db4a4be369559dc2daf3069c8685929b59c915d1 Mon Sep 17 00:00:00 2001 From: Jorens Shtekels Date: Tue, 12 Mar 2024 21:01:44 +0200 Subject: [PATCH 01/12] feat: remove caused by from event --- src/lib.cpp | 6 +++--- src/prep/prep.cpp | 6 ------ src/prep/prep.hh | 3 --- 3 files changed, 3 insertions(+), 12 deletions(-) diff --git a/src/lib.cpp b/src/lib.cpp index cb9ef08..1cdc3db 100644 --- a/src/lib.cpp +++ b/src/lib.cpp @@ -23,9 +23,9 @@ void run() { 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); diff --git a/src/prep/prep.cpp b/src/prep/prep.cpp index f64a478..93580fe 100644 --- a/src/prep/prep.cpp +++ b/src/prep/prep.cpp @@ -46,7 +46,6 @@ Event::Event(std::string title, uint32_t utcTimestampCreatedAt, uint32_t numberNight, bool isVisible, - std::vector causedBy, std::vector prohibits, std::vector allows): title(title), @@ -54,9 +53,6 @@ Event::Event(std::string title, numberNight(numberNight), isVisible(isVisible) { this->utcTimestampCreatedAt = createUTCTimestamp(utcTimestampCreatedAt); - for (auto &a : causedBy) { - this->causedBy.push_back(a); - } for (auto &a : prohibits) { this->prohibits.push_back(a); } @@ -69,14 +65,12 @@ 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..093aa79 100644 --- a/src/prep/prep.hh +++ b/src/prep/prep.hh @@ -74,7 +74,6 @@ struct Event { std::tm *utcTimestampCreatedAt; uint32_t numberNight; bool isVisible; - std::vector causedBy; std::vector prohibits; std::vector allows; @@ -86,14 +85,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); }; From 23345ba89f6b99f489b4d09676cad6d4d10f95c6 Mon Sep 17 00:00:00 2001 From: Jorens Shtekels Date: Tue, 12 Mar 2024 21:51:05 +0200 Subject: [PATCH 02/12] feat: add player to the room --- src/lib.cpp | 4 ++-- src/prep/prep.cpp | 15 +++++++++++++-- src/prep/prep.hh | 9 ++++++++- 3 files changed, 23 insertions(+), 5 deletions(-) diff --git a/src/lib.cpp b/src/lib.cpp index 1cdc3db..53e0857 100644 --- a/src/lib.cpp +++ b/src/lib.cpp @@ -16,8 +16,6 @@ enum VALIDATION_STATUS { }; 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); @@ -29,6 +27,8 @@ void run() { std::vector relatedEvents({event2, event3}); Player player1 = Player("player1", role1, PlayerStatus::ALIVE); Player player2 = Player("player2", role1, PlayerStatus::ALIVE); + Room room1(1, "Room 1", 1710087364, RoomStatus::IN_PROGRESS, {player1, player2}); + Room room2(2, "Room 2", 1710087384, RoomStatus::ENDED, {player1, player2}); int actionValidated = validateAction(&player1, &kill, &room1, &relatedEvents, &player2); std::cout << actionValidated << std::endl; } diff --git a/src/prep/prep.cpp b/src/prep/prep.cpp index 93580fe..5ec4935 100644 --- a/src/prep/prep.cpp +++ b/src/prep/prep.cpp @@ -35,10 +35,21 @@ Player::Player(std::string username, Role role, PlayerStatus playerStatus): playerStatus(playerStatus) { } -Room::Room(uint32_t id, std::string title, uint32_t utcTimestampCreatedAt, RoomStatus status): +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); } diff --git a/src/prep/prep.hh b/src/prep/prep.hh index 093aa79..65a70ac 100644 --- a/src/prep/prep.hh +++ b/src/prep/prep.hh @@ -65,8 +65,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 { From 23c1c23858fa60639a001d917784d60ddb188d99 Mon Sep 17 00:00:00 2001 From: Jorens Shtekels Date: Tue, 12 Mar 2024 22:02:22 +0200 Subject: [PATCH 03/12] feat: player belongs to a room with a check --- src/lib.cpp | 14 +++++++++++--- src/lib.hh | 1 + src/prep/prep.cpp | 8 +++++++- src/prep/prep.hh | 4 +++- 4 files changed, 22 insertions(+), 5 deletions(-) diff --git a/src/lib.cpp b/src/lib.cpp index 53e0857..c4197d2 100644 --- a/src/lib.cpp +++ b/src/lib.cpp @@ -8,6 +8,7 @@ #include enum VALIDATION_STATUS { + PLAYER_NOT_IN_ROOM, NO_PLAYER, ROOM_NOT_IN_PROGRESS, ACTION_PROHIBITED, @@ -25,9 +26,9 @@ void run() { 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); - Room room1(1, "Room 1", 1710087364, RoomStatus::IN_PROGRESS, {player1, player2}); + 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, {}); Room room2(2, "Room 2", 1710087384, RoomStatus::ENDED, {player1, player2}); int actionValidated = validateAction(&player1, &kill, &room1, &relatedEvents, &player2); std::cout << actionValidated << std::endl; @@ -35,6 +36,9 @@ void run() { int validateAction( Player *actor, const Action *action, Room *room, std::vector *relatedEvents, Player *target = nullptr) { + if (!playerBelongsToRoom(actor, room)) { + return PLAYER_NOT_IN_ROOM; + } if (!actor) { return NO_PLAYER; } @@ -57,6 +61,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(); } diff --git a/src/lib.hh b/src/lib.hh index 425808d..bf9ec23 100644 --- a/src/lib.hh +++ b/src/lib.hh @@ -2,6 +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); diff --git a/src/prep/prep.cpp b/src/prep/prep.cpp index 5ec4935..52253dc 100644 --- a/src/prep/prep.cpp +++ b/src/prep/prep.cpp @@ -2,6 +2,7 @@ #include "timeUtils.hh" +#include #include int add(int a, int b) { @@ -29,12 +30,17 @@ Role::Role(std::vector 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) { } +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), diff --git a/src/prep/prep.hh b/src/prep/prep.hh index 65a70ac..890ab17 100644 --- a/src/prep/prep.hh +++ b/src/prep/prep.hh @@ -53,11 +53,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 { From ec828dfaa3014256aa6f8cc52c95f67c498c6574 Mon Sep 17 00:00:00 2001 From: Jorens Shtekels Date: Tue, 12 Mar 2024 22:03:00 +0200 Subject: [PATCH 04/12] feat: rename target player not specified --- src/lib.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/lib.cpp b/src/lib.cpp index c4197d2..e772912 100644 --- a/src/lib.cpp +++ b/src/lib.cpp @@ -10,6 +10,7 @@ enum VALIDATION_STATUS { PLAYER_NOT_IN_ROOM, NO_PLAYER, + NO_TARGET_PLAYER_SPECIFIED, ROOM_NOT_IN_PROGRESS, ACTION_PROHIBITED, NO_ROLE, @@ -43,7 +44,7 @@ int validateAction( return NO_PLAYER; } if (action->hasTarget && !target) { - return NO_PLAYER; + return NO_TARGET_PLAYER_SPECIFIED; } if (room->status != RoomStatus::IN_PROGRESS) { return ROOM_NOT_IN_PROGRESS; From 2f585a3318340876c4c5a5b02b57700f617370a8 Mon Sep 17 00:00:00 2001 From: Jorens Shtekels Date: Tue, 12 Mar 2024 22:04:55 +0200 Subject: [PATCH 05/12] feat: detailed the printed info --- src/lib.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/lib.cpp b/src/lib.cpp index e772912..d72ac8d 100644 --- a/src/lib.cpp +++ b/src/lib.cpp @@ -3,6 +3,7 @@ #include "prep/prep.hh" #include +#include #include #include #include @@ -32,7 +33,7 @@ void run() { Room room1(1, "Room 1", 1710087364, RoomStatus::IN_PROGRESS, {}); Room room2(2, "Room 2", 1710087384, RoomStatus::ENDED, {player1, player2}); int actionValidated = validateAction(&player1, &kill, &room1, &relatedEvents, &player2); - std::cout << actionValidated << std::endl; + printf("The action validation result is %u\n", actionValidated); } int validateAction( From 871faaa2a39290e959e1090e214f8ab5322afd35 Mon Sep 17 00:00:00 2001 From: Jorens Shtekels Date: Tue, 12 Mar 2024 22:31:52 +0200 Subject: [PATCH 06/12] feat: null checks and rename some options --- src/lib.cpp | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/src/lib.cpp b/src/lib.cpp index d72ac8d..5f0062c 100644 --- a/src/lib.cpp +++ b/src/lib.cpp @@ -4,17 +4,19 @@ #include #include -#include -#include #include enum VALIDATION_STATUS { PLAYER_NOT_IN_ROOM, - NO_PLAYER, 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, }; @@ -30,20 +32,31 @@ void run() { 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, {}); - Room room2(2, "Room 2", 1710087384, RoomStatus::ENDED, {player1, player2}); + 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); printf("The action validation result is %u\n", actionValidated); } int validateAction( Player *actor, const Action *action, Room *room, std::vector *relatedEvents, Player *target = nullptr) { + // null checks + if (!actor) { + return NO_ACTOR; + } + if (!action) { + return NO_ACTION; + } + if (!room) { + return NO_ROOM; + } + if (!relatedEvents) { + return NO_RELATED_EVENTS; + } + // actual validation if (!playerBelongsToRoom(actor, room)) { return PLAYER_NOT_IN_ROOM; } - if (!actor) { - return NO_PLAYER; - } if (action->hasTarget && !target) { return NO_TARGET_PLAYER_SPECIFIED; } @@ -55,7 +68,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; From 93791b4ef626d132c34791d94f4575b87a132ac4 Mon Sep 17 00:00:00 2001 From: Jorens Shtekels Date: Tue, 12 Mar 2024 22:39:08 +0200 Subject: [PATCH 07/12] feat: remove unneeded comment --- src/lib.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/lib.cpp b/src/lib.cpp index 5f0062c..1e4acbd 100644 --- a/src/lib.cpp +++ b/src/lib.cpp @@ -40,7 +40,6 @@ void run() { int validateAction( Player *actor, const Action *action, Room *room, std::vector *relatedEvents, Player *target = nullptr) { - // null checks if (!actor) { return NO_ACTOR; } @@ -53,7 +52,6 @@ int validateAction( if (!relatedEvents) { return NO_RELATED_EVENTS; } - // actual validation if (!playerBelongsToRoom(actor, room)) { return PLAYER_NOT_IN_ROOM; } From 423669ab42d9f8f4691163ef54fec08df645b806 Mon Sep 17 00:00:00 2001 From: Jorens Shtekels Date: Tue, 12 Mar 2024 22:39:51 +0200 Subject: [PATCH 08/12] feat: remove functionToTest --- src/lib.cpp | 4 ---- src/lib.hh | 1 - 2 files changed, 5 deletions(-) diff --git a/src/lib.cpp b/src/lib.cpp index 1e4acbd..3b103d4 100644 --- a/src/lib.cpp +++ b/src/lib.cpp @@ -96,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 bf9ec23..547d50f 100644 --- a/src/lib.hh +++ b/src/lib.hh @@ -6,4 +6,3 @@ 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); From cb7927a6fc1837b9366e34a3e2757220c0621523 Mon Sep 17 00:00:00 2001 From: Jorens Shtekels Date: Tue, 12 Mar 2024 22:41:47 +0200 Subject: [PATCH 09/12] feat: removed push initialization of vectors --- src/prep/prep.cpp | 20 +++++--------------- 1 file changed, 5 insertions(+), 15 deletions(-) diff --git a/src/prep/prep.cpp b/src/prep/prep.cpp index 52253dc..c389448 100644 --- a/src/prep/prep.cpp +++ b/src/prep/prep.cpp @@ -18,16 +18,10 @@ 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(uint32_t id, std::string username, Role role, PlayerStatus playerStatus): @@ -68,14 +62,10 @@ Event::Event(std::string title, title(title), utcTimestampCreatedAt(createUTCTimestamp(utcTimestampCreatedAt)), numberNight(numberNight), - isVisible(isVisible) { + isVisible(isVisible), + prohibits(prohibits), + allows(allows) { this->utcTimestampCreatedAt = createUTCTimestamp(utcTimestampCreatedAt); - for (auto &a : prohibits) { - this->prohibits.push_back(a); - } - for (auto &a : allows) { - this->allows.push_back(a); - } } Event::Event(std::string title, From b2e2e44599f28e9e86ecdd6599ada7409d2294b8 Mon Sep 17 00:00:00 2001 From: Jorens Shtekels Date: Tue, 12 Mar 2024 22:42:20 +0200 Subject: [PATCH 10/12] feat: remove add function --- src/prep/prep.cpp | 4 ---- src/prep/prep.hh | 5 +---- 2 files changed, 1 insertion(+), 8 deletions(-) diff --git a/src/prep/prep.cpp b/src/prep/prep.cpp index c389448..aabfd4c 100644 --- a/src/prep/prep.cpp +++ b/src/prep/prep.cpp @@ -5,10 +5,6 @@ #include #include -int add(int a, int b) { - return a + b; -} - Action::Action(std::string name, bool hasTarget) { this->name = name; this->hasTarget = hasTarget; diff --git a/src/prep/prep.hh b/src/prep/prep.hh index 890ab17..0d53e6f 100644 --- a/src/prep/prep.hh +++ b/src/prep/prep.hh @@ -7,9 +7,6 @@ #include #include -// For test example -int add(int a, int b); - // All IDs are uint32_t enum EventType { PHASE_CHANGE, @@ -58,7 +55,7 @@ struct Player { Role role; PlayerStatus playerStatus; - Player(uint32_t id, std::string username, Role role, PlayerStatus playerStatus); + Player(uint32_t id, std : string username, Role role, PlayerStatus playerStatus); bool operator==(const Player &other) const; }; From bba9ddc04a881faf28588f34c9db66028d9f9c66 Mon Sep 17 00:00:00 2001 From: Jorens Shtekels Date: Tue, 12 Mar 2024 22:43:28 +0200 Subject: [PATCH 11/12] feat: simple test example --- src/test.cpp | 18 +++--------------- 1 file changed, 3 insertions(+), 15 deletions(-) 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); +} From 37f78c20c424817ee72f0ee29526c839e28852eb Mon Sep 17 00:00:00 2001 From: Jorens Shtekels Date: Tue, 12 Mar 2024 22:45:51 +0200 Subject: [PATCH 12/12] fix: Player constructor --- src/prep/prep.hh | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/prep/prep.hh b/src/prep/prep.hh index 0d53e6f..d99d6fd 100644 --- a/src/prep/prep.hh +++ b/src/prep/prep.hh @@ -1,7 +1,6 @@ #ifndef PREP_H #define PREP_H -#include #include #include #include @@ -55,7 +54,7 @@ struct Player { Role role; PlayerStatus playerStatus; - Player(uint32_t id, std : string username, Role role, PlayerStatus playerStatus); + Player(uint32_t id, std::string username, Role role, PlayerStatus playerStatus); bool operator==(const Player &other) const; };