From 03933af4b25a157d25dd0724ed3f8f6624dcd9cc Mon Sep 17 00:00:00 2001 From: Kristofers Solo Date: Thu, 14 Mar 2024 00:27:18 +0200 Subject: [PATCH 01/19] refactor: move `run` function to `main` --- src/lib.cpp | 18 ------------------ src/lib.hh | 2 -- src/main.cpp | 16 +++++++++++++++- 3 files changed, 15 insertions(+), 21 deletions(-) diff --git a/src/lib.cpp b/src/lib.cpp index 3b103d4..3175e00 100644 --- a/src/lib.cpp +++ b/src/lib.cpp @@ -20,24 +20,6 @@ enum VALIDATION_STATUS { ACTION_VALID, }; -void run() { - 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, {}, {}); - 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, {}); - 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) { if (!actor) { diff --git a/src/lib.hh b/src/lib.hh index 547d50f..72ad37b 100644 --- a/src/lib.hh +++ b/src/lib.hh @@ -1,7 +1,5 @@ #include "prep/prep.hh" -void run(); - bool playerBelongsToRoom(Player *player, Room *room); bool actionBelongsToRole(Role *role, const Action *action); bool isActionAllowed(const Action *action, std::vector *relevantEvents); diff --git a/src/main.cpp b/src/main.cpp index 81a2ad1..17e0f09 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3,6 +3,20 @@ #include int main(int argc, char *argv[]) { - run(); + 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, {}, {}); + 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, {}); + int actionValidated = validateAction(&player1, &kill, &room1, &relatedEvents, &player2); + printf("The action validation result is %u\n", actionValidated); return EXIT_SUCCESS; } From acf1bcc2c7dfa100cfb71954556532e1e55fda99 Mon Sep 17 00:00:00 2001 From: Kristofers Solo Date: Thu, 14 Mar 2024 00:28:52 +0200 Subject: [PATCH 02/19] refactor: change cpp files to cc --- CMakeLists.txt | 8 ++++---- src/cppunit/CMakeLists.txt | 2 +- src/cppunit/{cppunit.cpp => cppunit.cc} | 0 src/{lib.cpp => lib.cc} | 0 src/{main.cpp => main.cc} | 0 src/prep/CMakeLists.txt | 2 +- src/prep/{prep.cpp => prep.cc} | 0 src/prep/{timeUtils.cpp => timeUtils.cc} | 0 src/{test.cpp => test.cc} | 0 9 files changed, 6 insertions(+), 6 deletions(-) rename src/cppunit/{cppunit.cpp => cppunit.cc} (100%) rename src/{lib.cpp => lib.cc} (100%) rename src/{main.cpp => main.cc} (100%) rename src/prep/{prep.cpp => prep.cc} (100%) rename src/prep/{timeUtils.cpp => timeUtils.cc} (100%) rename src/{test.cpp => test.cc} (100%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 78cea40..de19ed8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,11 +9,11 @@ project( set(CMAKE_EXPORT_COMPILE_COMMANDS ON) -add_subdirectory(src/cppunit) +add_subdirectory(src/ccunit) add_subdirectory(src/prep) -add_executable(main src/main.cpp) -add_library(lib src/lib.cpp src/lib.hh) +add_executable(main src/main.cc) +add_library(lib src/lib.cc src/lib.hh) target_link_libraries(main PUBLIC lib) target_link_libraries(lib PUBLIC prepLib) target_include_directories(main PUBLIC "${PROJECT_BINARY_DIR}" @@ -28,7 +28,7 @@ FetchContent_Declare( ) FetchContent_MakeAvailable(googletest) -add_executable(runtests src/test.cpp) +add_executable(runtests src/test.cc) target_link_libraries(runtests PUBLIC lib gtest gtest_main) target_include_directories(runtests PUBLIC "${PROJECT_BINARY_DIR}") diff --git a/src/cppunit/CMakeLists.txt b/src/cppunit/CMakeLists.txt index 3e8a2ba..9d4a5a9 100644 --- a/src/cppunit/CMakeLists.txt +++ b/src/cppunit/CMakeLists.txt @@ -1 +1 @@ -add_library(cppunit cppunit.cpp cppunit.hh) +add_library(ccunit ccunit.cc ccunit.hh) diff --git a/src/cppunit/cppunit.cpp b/src/cppunit/cppunit.cc similarity index 100% rename from src/cppunit/cppunit.cpp rename to src/cppunit/cppunit.cc diff --git a/src/lib.cpp b/src/lib.cc similarity index 100% rename from src/lib.cpp rename to src/lib.cc diff --git a/src/main.cpp b/src/main.cc similarity index 100% rename from src/main.cpp rename to src/main.cc diff --git a/src/prep/CMakeLists.txt b/src/prep/CMakeLists.txt index 6d9cc1d..599d6df 100644 --- a/src/prep/CMakeLists.txt +++ b/src/prep/CMakeLists.txt @@ -1 +1 @@ -add_library(prepLib prep.cpp prep.hh timeUtils.cpp timeUtils.hh) +add_library(prepLib prep.cc prep.hh timeUtils.cc timeUtils.hh) diff --git a/src/prep/prep.cpp b/src/prep/prep.cc similarity index 100% rename from src/prep/prep.cpp rename to src/prep/prep.cc diff --git a/src/prep/timeUtils.cpp b/src/prep/timeUtils.cc similarity index 100% rename from src/prep/timeUtils.cpp rename to src/prep/timeUtils.cc diff --git a/src/test.cpp b/src/test.cc similarity index 100% rename from src/test.cpp rename to src/test.cc From f9e6644ebc26d935a972bea54dadc83f9441ba45 Mon Sep 17 00:00:00 2001 From: Kristofers Solo Date: Thu, 14 Mar 2024 00:36:06 +0200 Subject: [PATCH 03/19] refactor(action): move `action` to separate file --- src/action.cc | 10 ++++++++++ src/action.hh | 13 +++++++++++++ src/lib.hh | 1 + src/prep/prep.cc | 9 --------- src/prep/prep.hh | 8 -------- 5 files changed, 24 insertions(+), 17 deletions(-) create mode 100644 src/action.cc create mode 100644 src/action.hh diff --git a/src/action.cc b/src/action.cc new file mode 100644 index 0000000..ebe63bf --- /dev/null +++ b/src/action.cc @@ -0,0 +1,10 @@ +#include "action.hh" + +Action::Action(std::string name, bool has_target) { + this->name = name; + this->has_target = has_target; +} + +bool Action::operator==(const Action &other) const { + return this->name == other.name; +} diff --git a/src/action.hh b/src/action.hh new file mode 100644 index 0000000..3a9503c --- /dev/null +++ b/src/action.hh @@ -0,0 +1,13 @@ +#ifndef ACTION_HH +#define ACTION_HH +#include + +struct Action { + std::string name; + bool has_target; + + Action(std::string name, bool has_target); + bool operator==(const Action &other) const; +}; + +#endif // ACTION_HH diff --git a/src/lib.hh b/src/lib.hh index 72ad37b..8ecf9a8 100644 --- a/src/lib.hh +++ b/src/lib.hh @@ -1,3 +1,4 @@ +#include "action.hh" #include "prep/prep.hh" bool playerBelongsToRoom(Player *player, Room *room); diff --git a/src/prep/prep.cc b/src/prep/prep.cc index aabfd4c..271aaf4 100644 --- a/src/prep/prep.cc +++ b/src/prep/prep.cc @@ -5,15 +5,6 @@ #include #include -Action::Action(std::string name, bool hasTarget) { - this->name = name; - this->hasTarget = hasTarget; -} - -bool Action::operator==(const Action &other) const { - return this->name == other.name; -} - Role::Role(std::initializer_list actions): Role(std::vector(actions)) { } diff --git a/src/prep/prep.hh b/src/prep/prep.hh index d99d6fd..5637e12 100644 --- a/src/prep/prep.hh +++ b/src/prep/prep.hh @@ -33,14 +33,6 @@ struct Player; struct Room; struct Event; -struct Action { - std::string name; - bool hasTarget; - - Action(std::string name, bool hasTarget); - bool operator==(const Action &other) const; -}; - struct Role { std::vector actions; From 9a4b8831ee8469bef91e38663c96efeabc7bc359 Mon Sep 17 00:00:00 2001 From: Kristofers Solo Date: Thu, 14 Mar 2024 00:40:35 +0200 Subject: [PATCH 04/19] refactor(role): move `role` to separate file --- src/action.cc | 2 ++ src/lib.hh | 1 + src/prep/prep.cc | 6 ------ src/prep/prep.hh | 8 -------- src/role.cc | 10 ++++++++++ src/role.hh | 15 +++++++++++++++ 6 files changed, 28 insertions(+), 14 deletions(-) create mode 100644 src/role.cc create mode 100644 src/role.hh diff --git a/src/action.cc b/src/action.cc index ebe63bf..5ad180c 100644 --- a/src/action.cc +++ b/src/action.cc @@ -1,5 +1,7 @@ #include "action.hh" +#include + Action::Action(std::string name, bool has_target) { this->name = name; this->has_target = has_target; diff --git a/src/lib.hh b/src/lib.hh index 8ecf9a8..ac5d9df 100644 --- a/src/lib.hh +++ b/src/lib.hh @@ -1,5 +1,6 @@ #include "action.hh" #include "prep/prep.hh" +#include "role.hh" bool playerBelongsToRoom(Player *player, Room *room); bool actionBelongsToRole(Role *role, const Action *action); diff --git a/src/prep/prep.cc b/src/prep/prep.cc index 271aaf4..ff0baa1 100644 --- a/src/prep/prep.cc +++ b/src/prep/prep.cc @@ -5,12 +5,6 @@ #include #include -Role::Role(std::initializer_list actions): Role(std::vector(actions)) { -} - -Role::Role(std::vector actions): actions(actions) { -} - Player::Player(uint32_t id, std::string username, Role role, PlayerStatus playerStatus): id(id), username(username), diff --git a/src/prep/prep.hh b/src/prep/prep.hh index 5637e12..35e43a5 100644 --- a/src/prep/prep.hh +++ b/src/prep/prep.hh @@ -28,18 +28,10 @@ enum PlayerStatus { VOTED_OUT, }; -struct Role; struct Player; struct Room; struct Event; -struct Role { - std::vector actions; - - Role(std::initializer_list actions); - Role(std::vector actions); -}; - struct Player { uint32_t id; std::string username; diff --git a/src/role.cc b/src/role.cc new file mode 100644 index 0000000..9ed7d94 --- /dev/null +++ b/src/role.cc @@ -0,0 +1,10 @@ +#include "role.hh" + +#include +#include + +Role::Role(std::initializer_list actions): Role(std::vector(actions)) { +} + +Role::Role(std::vector actions): actions(actions) { +} diff --git a/src/role.hh b/src/role.hh new file mode 100644 index 0000000..d52330f --- /dev/null +++ b/src/role.hh @@ -0,0 +1,15 @@ +#ifndef ROLE_HH +#define ROLE_HH + +#include "action.hh" + +#include +#include + +struct Role { + std::vector actions; + Role(std::initializer_list actions); + Role(std::vector actions); +}; + +#endif // ROLE_HH From b4b01c4a119de0ffbeae9066968043cee1754187 Mon Sep 17 00:00:00 2001 From: Kristofers Solo Date: Thu, 14 Mar 2024 00:48:16 +0200 Subject: [PATCH 05/19] refactor(player): move `player` to separate file --- src/action.hh | 2 +- src/lib.hh | 1 + src/player.cc | 16 ++++++++++++++++ src/player.hh | 26 ++++++++++++++++++++++++++ src/prep/prep.cc | 11 ----------- src/prep/prep.hh | 18 ------------------ src/role.hh | 2 +- 7 files changed, 45 insertions(+), 31 deletions(-) create mode 100644 src/player.cc create mode 100644 src/player.hh diff --git a/src/action.hh b/src/action.hh index 3a9503c..a22b709 100644 --- a/src/action.hh +++ b/src/action.hh @@ -10,4 +10,4 @@ struct Action { bool operator==(const Action &other) const; }; -#endif // ACTION_HH +#endif // !ACTION_HH diff --git a/src/lib.hh b/src/lib.hh index ac5d9df..a250d22 100644 --- a/src/lib.hh +++ b/src/lib.hh @@ -1,4 +1,5 @@ #include "action.hh" +#include "player.hh" #include "prep/prep.hh" #include "role.hh" diff --git a/src/player.cc b/src/player.cc new file mode 100644 index 0000000..9764df2 --- /dev/null +++ b/src/player.cc @@ -0,0 +1,16 @@ +#include "player.hh" + +#include "role.hh" + +#include + +Player::Player(uint32_t id, std::string username, Role role, PlayerStatus status): + id(id), + username(username), + role(role), + status(status) { +} + +bool Player::operator==(const Player &other) const { + return this->id == other.id; +} diff --git a/src/player.hh b/src/player.hh new file mode 100644 index 0000000..a2760b4 --- /dev/null +++ b/src/player.hh @@ -0,0 +1,26 @@ +#ifndef PLAYER_HH +#define PLAYER_HH + +#include "role.hh" + +#include +#include + +enum PlayerStatus { + Kicked, + Alive, + Dead, + VotedOut, +}; + +struct Player { + uint32_t id; + std::string username; + Role role; + PlayerStatus status; + + Player(uint32_t id, std::string username, Role role, PlayerStatus status); + bool operator==(const Player &other) const; +}; + +#endif // !PLAYER_HH diff --git a/src/prep/prep.cc b/src/prep/prep.cc index ff0baa1..40252fe 100644 --- a/src/prep/prep.cc +++ b/src/prep/prep.cc @@ -5,17 +5,6 @@ #include #include -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 35e43a5..91e738b 100644 --- a/src/prep/prep.hh +++ b/src/prep/prep.hh @@ -21,27 +21,9 @@ enum RoomStatus { ENDED, }; -enum PlayerStatus { - KICKED, - ALIVE, - DEAD, - VOTED_OUT, -}; - -struct Player; struct Room; struct Event; -struct 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; -}; - struct Room { uint32_t id; std::string title; diff --git a/src/role.hh b/src/role.hh index d52330f..a209895 100644 --- a/src/role.hh +++ b/src/role.hh @@ -12,4 +12,4 @@ struct Role { Role(std::vector actions); }; -#endif // ROLE_HH +#endif // !ROLE_HH From cad8e6d0321a4af2f8884d5cd8cbfc4a9492a5d7 Mon Sep 17 00:00:00 2001 From: Kristofers Solo Date: Thu, 14 Mar 2024 00:56:19 +0200 Subject: [PATCH 06/19] refactor(room): move `room` to separate file --- src/prep/prep.cc | 18 ------------------ src/prep/prep.hh | 24 ------------------------ src/room.cc | 23 +++++++++++++++++++++++ src/room.hh | 30 ++++++++++++++++++++++++++++++ 4 files changed, 53 insertions(+), 42 deletions(-) create mode 100644 src/room.cc create mode 100644 src/room.hh diff --git a/src/prep/prep.cc b/src/prep/prep.cc index 40252fe..ec5d4a3 100644 --- a/src/prep/prep.cc +++ b/src/prep/prep.cc @@ -5,24 +5,6 @@ #include #include -Room::Room( - uint32_t id, std::string title, uint32_t utcTimestampCreatedAt, RoomStatus status, std::vector players): - id(id), - title(title), - 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); -} - Event::Event(std::string title, uint32_t utcTimestampCreatedAt, uint32_t numberNight, diff --git a/src/prep/prep.hh b/src/prep/prep.hh index 91e738b..b199396 100644 --- a/src/prep/prep.hh +++ b/src/prep/prep.hh @@ -14,32 +14,8 @@ enum EventType { PLAYER_STATE_CHANGE, }; -enum RoomStatus { - AWAITING_START, - IN_PROGRESS, - STOPPED, - ENDED, -}; - -struct Room; struct Event; -struct Room { - uint32_t id; - std::string title; - std::tm *utcTimestampCreatedAt; - RoomStatus status; - std::vector players; - - 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 { std::string title; std::tm *utcTimestampCreatedAt; diff --git a/src/room.cc b/src/room.cc new file mode 100644 index 0000000..a3dec91 --- /dev/null +++ b/src/room.cc @@ -0,0 +1,23 @@ +#include "room.hh" + +#include +#include +#include +#include + +Room::Room(uint32_t id, std::string title, uint32_t created_at, RoomStatus status, std::vector players): + id(id), + title(title), + created_at(createUTCTimestamp(created_at)), + status(status), + players(players) { +} + +Room::Room( + uint32_t id, std::string title, uint32_t created_at, RoomStatus status, std::initializer_list players): + id(id), + title(title), + created_at(createUTCTimestamp(created_at)), + status(status), + players(players) { +} diff --git a/src/room.hh b/src/room.hh new file mode 100644 index 0000000..39cb12d --- /dev/null +++ b/src/room.hh @@ -0,0 +1,30 @@ +#ifndef ROOM_HH +#define ROOM_HH + +#include "player.hh" + +#include +#include +#include +#include +#include + +enum RoomStatus { + AwaitingStart, + InProgress, + Stopped, + Ended, +}; + +struct Room { + uint32_t id; + std::string title; + std::tm *created_at; + RoomStatus status; + std::vector players; + + Room(uint32_t id, std::string title, uint32_t created_at, RoomStatus status, std::vector players); + Room(uint32_t id, std::string title, uint32_t created_at, RoomStatus status, std::initializer_list players); +}; + +#endif // !ROOM_HH From 5238b348c6d6ce3c1ab4303f96d459c7eee5e819 Mon Sep 17 00:00:00 2001 From: Kristofers Solo Date: Thu, 14 Mar 2024 01:01:19 +0200 Subject: [PATCH 07/19] refactor(time): move `time` to root --- src/lib.hh | 1 + src/prep/timeUtils.hh | 9 --------- src/room.cc | 6 ++++-- src/{prep/timeUtils.cc => time.cc} | 4 +--- src/time.hh | 9 +++++++++ 5 files changed, 15 insertions(+), 14 deletions(-) delete mode 100644 src/prep/timeUtils.hh rename src/{prep/timeUtils.cc => time.cc} (84%) create mode 100644 src/time.hh diff --git a/src/lib.hh b/src/lib.hh index a250d22..15d0946 100644 --- a/src/lib.hh +++ b/src/lib.hh @@ -2,6 +2,7 @@ #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); diff --git a/src/prep/timeUtils.hh b/src/prep/timeUtils.hh deleted file mode 100644 index 74060e2..0000000 --- a/src/prep/timeUtils.hh +++ /dev/null @@ -1,9 +0,0 @@ -#include -#include - -#ifndef TIMEUTILS_H -# define TIMEUTILS_H - -std::tm *createUTCTimestamp(uint32_t timestamp); - -#endif diff --git a/src/room.cc b/src/room.cc index a3dec91..d914c55 100644 --- a/src/room.cc +++ b/src/room.cc @@ -1,5 +1,7 @@ #include "room.hh" +#include "time.hh" + #include #include #include @@ -8,7 +10,7 @@ Room::Room(uint32_t id, std::string title, uint32_t created_at, RoomStatus status, std::vector players): id(id), title(title), - created_at(createUTCTimestamp(created_at)), + created_at(create_utc_timestamp(created_at)), status(status), players(players) { } @@ -17,7 +19,7 @@ Room::Room( uint32_t id, std::string title, uint32_t created_at, RoomStatus status, std::initializer_list players): id(id), title(title), - created_at(createUTCTimestamp(created_at)), + created_at(create_utc_timestamp(created_at)), status(status), players(players) { } diff --git a/src/prep/timeUtils.cc b/src/time.cc similarity index 84% rename from src/prep/timeUtils.cc rename to src/time.cc index e52033c..155abc5 100644 --- a/src/prep/timeUtils.cc +++ b/src/time.cc @@ -1,10 +1,8 @@ -#include "timeUtils.hh" - #include #include #include -std::tm *createUTCTimestamp(uint32_t timestamp) { +std::tm *create_utc_imestamp(uint32_t timestamp) { // Convert the timestamp into a time_point object std::chrono::seconds sec(timestamp); std::chrono::time_point tp(sec); diff --git a/src/time.hh b/src/time.hh new file mode 100644 index 0000000..3a746e5 --- /dev/null +++ b/src/time.hh @@ -0,0 +1,9 @@ +#ifndef TIME_HH +#define TIME_HH + +#include +#include + +std::tm *create_utc_timestamp(uint32_t timestamp); + +#endif // !TIME_HH From 09d17e2d0a149f76133518fa4c3337d190990251 Mon Sep 17 00:00:00 2001 From: Kristofers Solo Date: Thu, 14 Mar 2024 01:20:39 +0200 Subject: [PATCH 08/19] refactor(event): move `event` to separate file --- src/event.cc | 48 ++++++++++++++++++++++++++++++++++ src/{prep/prep.hh => event.hh} | 45 +++++++++++++++---------------- src/player.hh | 18 +++++++------ src/prep/prep.cc | 48 ---------------------------------- src/room.hh | 21 ++++++++------- 5 files changed, 93 insertions(+), 87 deletions(-) create mode 100644 src/event.cc rename src/{prep/prep.hh => event.hh} (53%) delete mode 100644 src/prep/prep.cc diff --git a/src/event.cc b/src/event.cc new file mode 100644 index 0000000..1c24b4d --- /dev/null +++ b/src/event.cc @@ -0,0 +1,48 @@ +#include "event.hh" + +#include "action.hh" +#include "time.hh" + +#include +#include +#include + +Event::Event(std::string title, + uint32_t created_at, + uint32_t night_number, + bool is_visible, + std::vector prohibits, + std::vector allows): + title(title), + created_at(create_utc_timestamp(created_at)), + night_number(night_number), + is_visible(is_visible), + prohibits(prohibits), + allows(allows) { +} + +Event::Event(std::string title, + uint32_t created_at, + uint32_t night_number, + bool is_visible, + std::initializer_list prohibits, + std::initializer_list allows): + title(title), + created_at(create_utc_timestamp(created_at)), + night_number(night_number), + is_visible(is_visible), + prohibits(prohibits), + allows(allows) { +} + +bool Event::operator<(const Event &right) const { + return this->created_at < right.created_at; +} + +bool Event::operator==(const Event &right) const { + return this->created_at == right.created_at; +} + +bool Event::operator>(const Event &right) const { + return this->created_at > right.created_at; +} diff --git a/src/prep/prep.hh b/src/event.hh similarity index 53% rename from src/prep/prep.hh rename to src/event.hh index b199396..75199c8 100644 --- a/src/prep/prep.hh +++ b/src/event.hh @@ -1,45 +1,46 @@ -#ifndef PREP_H -#define PREP_H +#ifndef EVENT_HH +#define EVENT_HH + +#include "action.hh" #include #include +#include #include #include -// All IDs are uint32_t -enum EventType { - PHASE_CHANGE, - ACTION, - ROOM_STATE_CHANGE, - PLAYER_STATE_CHANGE, -}; - -struct Event; +namespace event { + enum Type { + PhaseChange, + EventAction, + RoomStateChange, + PlayerStateChange, + }; +} struct Event { std::string title; - std::tm *utcTimestampCreatedAt; - uint32_t numberNight; - bool isVisible; + std::tm *created_at; + uint32_t night_number; + bool is_visible; 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, - bool isVisible, + uint32_t created_at, + uint32_t night_number, + bool is_visible, std::vector prohibits, std::vector allows); Event(std::string title, - uint32_t utcTimestampCreatedAt, - uint32_t numberNight, - bool isVisible, + uint32_t created_at, + uint32_t night_number, + bool is_visible, std::initializer_list prohibits, std::initializer_list allows); }; -#endif +#endif // !EVENT_HH diff --git a/src/player.hh b/src/player.hh index a2760b4..588d17b 100644 --- a/src/player.hh +++ b/src/player.hh @@ -6,20 +6,22 @@ #include #include -enum PlayerStatus { - Kicked, - Alive, - Dead, - VotedOut, -}; +namespace player { + enum Status { + Kicked, + Alive, + Dead, + VotedOut, + }; +} struct Player { uint32_t id; std::string username; Role role; - PlayerStatus status; + player::Status status; - Player(uint32_t id, std::string username, Role role, PlayerStatus status); + Player(uint32_t id, std::string username, Role role, player::Status status); bool operator==(const Player &other) const; }; diff --git a/src/prep/prep.cc b/src/prep/prep.cc deleted file mode 100644 index ec5d4a3..0000000 --- a/src/prep/prep.cc +++ /dev/null @@ -1,48 +0,0 @@ -#include "prep.hh" - -#include "timeUtils.hh" - -#include -#include - -Event::Event(std::string title, - uint32_t utcTimestampCreatedAt, - uint32_t numberNight, - bool isVisible, - std::vector prohibits, - std::vector allows): - title(title), - utcTimestampCreatedAt(createUTCTimestamp(utcTimestampCreatedAt)), - numberNight(numberNight), - isVisible(isVisible), - prohibits(prohibits), - allows(allows) { - this->utcTimestampCreatedAt = createUTCTimestamp(utcTimestampCreatedAt); -} - -Event::Event(std::string title, - uint32_t utcTimestampCreatedAt, - uint32_t numberNight, - bool isVisible, - std::initializer_list prohibits, - std::initializer_list allows): - Event(title, - utcTimestampCreatedAt, - numberNight, - isVisible, - std::vector(prohibits), - 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/room.hh b/src/room.hh index 39cb12d..e9ce478 100644 --- a/src/room.hh +++ b/src/room.hh @@ -9,22 +9,25 @@ #include #include -enum RoomStatus { - AwaitingStart, - InProgress, - Stopped, - Ended, -}; +namespace room { + enum Status { + AwaitingStart, + InProgress, + Stopped, + Ended, + }; +} struct Room { uint32_t id; std::string title; std::tm *created_at; - RoomStatus status; + room::Status status; std::vector players; - Room(uint32_t id, std::string title, uint32_t created_at, RoomStatus status, std::vector players); - Room(uint32_t id, std::string title, uint32_t created_at, RoomStatus status, std::initializer_list players); + Room(uint32_t id, std::string title, uint32_t created_at, room::Status status, std::vector players); + Room( + uint32_t id, std::string title, uint32_t created_at, room::Status status, std::initializer_list players); }; #endif // !ROOM_HH From 9f0b32ed8c9fa60043ebf1907cfe841b64d4187c Mon Sep 17 00:00:00 2001 From: Kristofers Solo Date: Thu, 14 Mar 2024 01:31:32 +0200 Subject: [PATCH 09/19] refactor(validation): move `validation` to separate file --- src/lib.cc | 80 ----------------------------------------------- src/lib.hh | 11 +------ src/main.cc | 8 ++--- src/player.cc | 2 +- src/room.cc | 4 +-- src/validation.cc | 64 +++++++++++++++++++++++++++++++++++++ src/validation.hh | 26 +++++++++++++++ 7 files changed, 98 insertions(+), 97 deletions(-) create mode 100644 src/validation.cc create mode 100644 src/validation.hh 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 From 6ed14fd2c0c983cc366949b59da9dddc3344d71f Mon Sep 17 00:00:00 2001 From: Kristofers Solo Date: Thu, 14 Mar 2024 01:43:19 +0200 Subject: [PATCH 10/19] refactor(modules): move files to `modules` dir --- CMakeLists.txt | 8 ++++---- src/cppunit/CMakeLists.txt | 2 +- src/lib.hh | 2 +- src/main.cc | 2 +- src/modules/CMakeLists.txt | 14 ++++++++++++++ src/{ => modules}/action.cc | 2 +- src/{ => modules}/action.hh | 0 src/{ => modules}/event.cc | 6 +++--- src/{ => modules}/event.hh | 4 ++-- src/{ => modules}/player.cc | 4 ++-- src/{ => modules}/player.hh | 4 ++-- src/{ => modules}/role.cc | 2 +- src/{ => modules}/role.hh | 4 ++-- src/{ => modules}/room.cc | 4 ++-- src/{ => modules}/room.hh | 4 ++-- src/{ => modules}/time.cc | 0 src/{ => modules}/time.hh | 0 src/prep/CMakeLists.txt | 1 - src/validation.cc | 4 ++-- src/validation.hh | 10 ++++++---- 20 files changed, 46 insertions(+), 31 deletions(-) create mode 100644 src/modules/CMakeLists.txt rename src/{ => modules}/action.cc (90%) rename src/{ => modules}/action.hh (100%) rename src/{ => modules}/event.cc (94%) rename src/{ => modules}/event.hh (95%) rename src/{ => modules}/player.cc (85%) rename src/{ => modules}/player.hh (90%) rename src/{ => modules}/role.cc (90%) rename src/{ => modules}/role.hh (91%) rename src/{ => modules}/room.cc (93%) rename src/{ => modules}/room.hh (93%) rename src/{ => modules}/time.cc (100%) rename src/{ => modules}/time.hh (100%) delete mode 100644 src/prep/CMakeLists.txt diff --git a/CMakeLists.txt b/CMakeLists.txt index de19ed8..027c017 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,13 +9,13 @@ project( set(CMAKE_EXPORT_COMPILE_COMMANDS ON) -add_subdirectory(src/ccunit) -add_subdirectory(src/prep) +add_subdirectory(src/cppunit) +add_subdirectory(src/modules) add_executable(main src/main.cc) -add_library(lib src/lib.cc src/lib.hh) +add_library(lib src/lib.cc src/lib.hh src/validation.cc src/validation.hh) target_link_libraries(main PUBLIC lib) -target_link_libraries(lib PUBLIC prepLib) +target_link_libraries(lib PUBLIC modules) target_include_directories(main PUBLIC "${PROJECT_BINARY_DIR}" "${PROJECT_SOURCE_DIR}/prep") target_include_directories(lib PUBLIC "${PROJECT_BINARY_DIR}" diff --git a/src/cppunit/CMakeLists.txt b/src/cppunit/CMakeLists.txt index 9d4a5a9..eaebf71 100644 --- a/src/cppunit/CMakeLists.txt +++ b/src/cppunit/CMakeLists.txt @@ -1 +1 @@ -add_library(ccunit ccunit.cc ccunit.hh) +add_library(cppunit cppunit.cc cppunit.hh) diff --git a/src/lib.hh b/src/lib.hh index fc3ec1f..46aa2bf 100644 --- a/src/lib.hh +++ b/src/lib.hh @@ -1 +1 @@ -#include "validation.hh" +#include "./validation.hh" diff --git a/src/main.cc b/src/main.cc index 50b95bc..755ede2 100644 --- a/src/main.cc +++ b/src/main.cc @@ -1,4 +1,4 @@ -#include "lib.hh" +#include "./lib.hh" #include diff --git a/src/modules/CMakeLists.txt b/src/modules/CMakeLists.txt new file mode 100644 index 0000000..84ae806 --- /dev/null +++ b/src/modules/CMakeLists.txt @@ -0,0 +1,14 @@ +add_library( + modules + action.cc + action.hh + event.cc + event.hh + player.cc + player.hh + role.cc + role.hh + room.cc + room.hh + time.cc + time.hh) diff --git a/src/action.cc b/src/modules/action.cc similarity index 90% rename from src/action.cc rename to src/modules/action.cc index 5ad180c..b698933 100644 --- a/src/action.cc +++ b/src/modules/action.cc @@ -1,4 +1,4 @@ -#include "action.hh" +#include "./action.hh" #include diff --git a/src/action.hh b/src/modules/action.hh similarity index 100% rename from src/action.hh rename to src/modules/action.hh diff --git a/src/event.cc b/src/modules/event.cc similarity index 94% rename from src/event.cc rename to src/modules/event.cc index 1c24b4d..d6e6950 100644 --- a/src/event.cc +++ b/src/modules/event.cc @@ -1,7 +1,7 @@ -#include "event.hh" +#include "./event.hh" -#include "action.hh" -#include "time.hh" +#include "./action.hh" +#include "./time.hh" #include #include diff --git a/src/event.hh b/src/modules/event.hh similarity index 95% rename from src/event.hh rename to src/modules/event.hh index 75199c8..5997743 100644 --- a/src/event.hh +++ b/src/modules/event.hh @@ -1,7 +1,7 @@ #ifndef EVENT_HH #define EVENT_HH -#include "action.hh" +#include "./action.hh" #include #include @@ -16,7 +16,7 @@ namespace event { RoomStateChange, PlayerStateChange, }; -} +} // namespace event struct Event { std::string title; diff --git a/src/player.cc b/src/modules/player.cc similarity index 85% rename from src/player.cc rename to src/modules/player.cc index 8e40a81..d67f05c 100644 --- a/src/player.cc +++ b/src/modules/player.cc @@ -1,6 +1,6 @@ -#include "player.hh" +#include "./player.hh" -#include "role.hh" +#include "./role.hh" #include diff --git a/src/player.hh b/src/modules/player.hh similarity index 90% rename from src/player.hh rename to src/modules/player.hh index 588d17b..8f0b25c 100644 --- a/src/player.hh +++ b/src/modules/player.hh @@ -1,7 +1,7 @@ #ifndef PLAYER_HH #define PLAYER_HH -#include "role.hh" +#include "./role.hh" #include #include @@ -13,7 +13,7 @@ namespace player { Dead, VotedOut, }; -} +} // namespace player struct Player { uint32_t id; diff --git a/src/role.cc b/src/modules/role.cc similarity index 90% rename from src/role.cc rename to src/modules/role.cc index 9ed7d94..5b3e1a9 100644 --- a/src/role.cc +++ b/src/modules/role.cc @@ -1,4 +1,4 @@ -#include "role.hh" +#include "./role.hh" #include #include diff --git a/src/role.hh b/src/modules/role.hh similarity index 91% rename from src/role.hh rename to src/modules/role.hh index a209895..e98e134 100644 --- a/src/role.hh +++ b/src/modules/role.hh @@ -1,15 +1,15 @@ #ifndef ROLE_HH #define ROLE_HH -#include "action.hh" +#include "./action.hh" #include #include struct Role { std::vector actions; - Role(std::initializer_list actions); Role(std::vector actions); + Role(std::initializer_list actions); }; #endif // !ROLE_HH diff --git a/src/room.cc b/src/modules/room.cc similarity index 93% rename from src/room.cc rename to src/modules/room.cc index 64dd270..fd9dd39 100644 --- a/src/room.cc +++ b/src/modules/room.cc @@ -1,6 +1,6 @@ -#include "room.hh" +#include "./room.hh" -#include "time.hh" +#include "./time.hh" #include #include diff --git a/src/room.hh b/src/modules/room.hh similarity index 93% rename from src/room.hh rename to src/modules/room.hh index e9ce478..ff0e98c 100644 --- a/src/room.hh +++ b/src/modules/room.hh @@ -1,7 +1,7 @@ #ifndef ROOM_HH #define ROOM_HH -#include "player.hh" +#include "./player.hh" #include #include @@ -16,7 +16,7 @@ namespace room { Stopped, Ended, }; -} +} // namespace room struct Room { uint32_t id; diff --git a/src/time.cc b/src/modules/time.cc similarity index 100% rename from src/time.cc rename to src/modules/time.cc diff --git a/src/time.hh b/src/modules/time.hh similarity index 100% rename from src/time.hh rename to src/modules/time.hh diff --git a/src/prep/CMakeLists.txt b/src/prep/CMakeLists.txt deleted file mode 100644 index 599d6df..0000000 --- a/src/prep/CMakeLists.txt +++ /dev/null @@ -1 +0,0 @@ -add_library(prepLib prep.cc prep.hh timeUtils.cc timeUtils.hh) diff --git a/src/validation.cc b/src/validation.cc index cc0f231..e7ae0b5 100644 --- a/src/validation.cc +++ b/src/validation.cc @@ -1,6 +1,6 @@ -#include "validation.hh" +#include "./validation.hh" -#include "room.hh" +#include "modules/room.hh" #include diff --git a/src/validation.hh b/src/validation.hh index f07b3fe..32f94b3 100644 --- a/src/validation.hh +++ b/src/validation.hh @@ -1,9 +1,11 @@ #ifndef VALIDATION_HH #define VALIDATION_HH -#include "event.hh" -#include "player.hh" -#include "room.hh" +#include "modules/event.hh" +#include "modules/player.hh" +#include "modules/room.hh" + +#include namespace validation { enum Status { @@ -19,7 +21,7 @@ namespace validation { NoRelatedEvents, ActionValid, }; -} +} // namespace validation int validateAction(Player *actor, const Action *action, Room *room, std::vector *relatedEvents, Player *target); From 29f9e60a331b8112dd714dc3c16cc65deb0c4b74 Mon Sep 17 00:00:00 2001 From: Kristofers Solo Date: Thu, 14 Mar 2024 01:58:43 +0200 Subject: [PATCH 11/19] refactor(test): remove tests (for now) --- CMakeLists.txt | 27 ++----------- src/cppunit/CMakeLists.txt | 1 - src/cppunit/cppunit.cc | 0 src/cppunit/cppunit.hh | 83 -------------------------------------- src/lib.cc | 0 src/lib.hh | 1 - src/main.cc | 6 +-- src/modules/room.cc | 4 +- src/modules/time.cc | 4 +- src/test.cc | 5 --- src/validation.cc | 2 +- src/validation.hh | 2 +- 12 files changed, 14 insertions(+), 121 deletions(-) delete mode 100644 src/cppunit/CMakeLists.txt delete mode 100644 src/cppunit/cppunit.cc delete mode 100644 src/cppunit/cppunit.hh delete mode 100644 src/lib.cc delete mode 100644 src/lib.hh delete mode 100644 src/test.cc diff --git a/CMakeLists.txt b/CMakeLists.txt index 027c017..75c054c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,7 +1,5 @@ cmake_minimum_required(VERSION 3.1...3.28) -enable_testing() - project( Template VERSION 0.1.0 @@ -9,28 +7,11 @@ project( set(CMAKE_EXPORT_COMPILE_COMMANDS ON) -add_subdirectory(src/cppunit) add_subdirectory(src/modules) add_executable(main src/main.cc) -add_library(lib src/lib.cc src/lib.hh src/validation.cc src/validation.hh) -target_link_libraries(main PUBLIC lib) -target_link_libraries(lib PUBLIC modules) +add_library(validation src/validation.cc src/validation.hh) +target_link_libraries(main PUBLIC validation) +target_link_libraries(validation PUBLIC modules) target_include_directories(main PUBLIC "${PROJECT_BINARY_DIR}" - "${PROJECT_SOURCE_DIR}/prep") -target_include_directories(lib PUBLIC "${PROJECT_BINARY_DIR}" - "${PROJECT_SOURCE_DIR}/prep") - -include(FetchContent) -FetchContent_Declare( - googletest - URL https://github.com/google/googletest/archive/5376968f6948923e2411081fd9372e71a59d8e77.zip -) -FetchContent_MakeAvailable(googletest) - -add_executable(runtests src/test.cc) -target_link_libraries(runtests PUBLIC lib gtest gtest_main) -target_include_directories(runtests PUBLIC "${PROJECT_BINARY_DIR}") - -include(GoogleTest) -gtest_discover_tests(runtests) + "${PROJECT_SOURCE_DIR}/modules") diff --git a/src/cppunit/CMakeLists.txt b/src/cppunit/CMakeLists.txt deleted file mode 100644 index eaebf71..0000000 --- a/src/cppunit/CMakeLists.txt +++ /dev/null @@ -1 +0,0 @@ -add_library(cppunit cppunit.cc cppunit.hh) diff --git a/src/cppunit/cppunit.cc b/src/cppunit/cppunit.cc deleted file mode 100644 index e69de29..0000000 diff --git a/src/cppunit/cppunit.hh b/src/cppunit/cppunit.hh deleted file mode 100644 index ba51b1c..0000000 --- a/src/cppunit/cppunit.hh +++ /dev/null @@ -1,83 +0,0 @@ - -#ifndef CPPUNIT_H -#define CPPUNIT_H - -// Required headers, or just use #include -#include -#include -#include -#include -#include - -// CPlusPlusUnit - C++ Unit testing TDD framework (github.com/cppunit/cppunit) -class Cppunit { - public: -#define CHECK(a, b) check(a, b, #a, #b, __FILE__, __LINE__, __FUNCTION__); -#define CHECKT(a) check(a, true, #a, "true", __FILE__, __LINE__, __FUNCTION__); -#define CHECKS(a, b) check(a, b, #a, #b, __FILE__, __LINE__, __FUNCTION__); - typedef const std::string &cs; - int checks, fails; - std::ostringstream serr; - std::istringstream *in; - - Cppunit() { - checks = fails = 0; - } - - void test_cin(cs s) { - in = new std::istringstream(s); - std::cin.rdbuf(in->rdbuf()); - } - - void fail_hdr(cs stra, cs strb, cs file, int line, cs func) { - serr << "==================================================" << std::endl; - serr << "FAIL: " << func << std::endl; - serr << "--------------------------------------------------" << std::endl; - serr << "File \"" << file << "\", line " << line << " in " << func << std::endl; - serr << " Checking " << stra << " == " << strb << std::endl; - } - - template void check(T a, T b, cs stra, cs strb, cs file, int line, cs func) { - checks++; - if (a == b) { - std::cout << "."; - return; - } - fails++; - std::cout << "F"; - fail_hdr(stra, strb, file, line, func); - serr << " Error: \"" << a << "\" ! = \"" << b << "\"" << std::endl << std::endl; - } - - virtual void single_test() { - } - - virtual void test_list() { - single_test(); - } - - double dclock() { - return double(clock()) / CLOCKS_PER_SEC; - } - - int status() { - std::cout << std::endl; - if (fails) std::cout << serr.str(); - std::cout << "--------------------------------------------------" << std::endl; - std::cout << "Ran " << checks << " checks in " << dclock() << "s" << std::endl << std::endl; - if (fails) - std::cout << "FAILED (failures=" << fails << ")"; - else - std::cout << "OK" << std::endl; - return fails > 0; - } - - int run() { - std::streambuf *ocin = std::cin.rdbuf(); - test_list(); - std::cin.rdbuf(ocin); - return status(); - } -}; - -#endif // CPPUNIT_H diff --git a/src/lib.cc b/src/lib.cc deleted file mode 100644 index e69de29..0000000 diff --git a/src/lib.hh b/src/lib.hh deleted file mode 100644 index 46aa2bf..0000000 --- a/src/lib.hh +++ /dev/null @@ -1 +0,0 @@ -#include "./validation.hh" diff --git a/src/main.cc b/src/main.cc index 755ede2..f6e678f 100644 --- a/src/main.cc +++ b/src/main.cc @@ -1,4 +1,4 @@ -#include "./lib.hh" +#include "./validation.hh" #include @@ -16,7 +16,7 @@ int main(int argc, char *argv[]) { 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); + int validated_action = validate_action(&player1, &kill, &room1, &relatedEvents, &player2); + printf("The action validation result is %u\n", validated_action); return EXIT_SUCCESS; } diff --git a/src/modules/room.cc b/src/modules/room.cc index fd9dd39..626bcd7 100644 --- a/src/modules/room.cc +++ b/src/modules/room.cc @@ -10,16 +10,16 @@ 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)), status(status), players(players) { + this->created_at = create_utc_timestamp(created_at); } Room::Room( 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)), status(status), players(players) { + this->created_at = create_utc_timestamp(created_at); } diff --git a/src/modules/time.cc b/src/modules/time.cc index 155abc5..fc6edec 100644 --- a/src/modules/time.cc +++ b/src/modules/time.cc @@ -1,8 +1,10 @@ +#include "./time.hh" + #include #include #include -std::tm *create_utc_imestamp(uint32_t timestamp) { +std::tm *create_utc_timestamp(uint32_t timestamp) { // Convert the timestamp into a time_point object std::chrono::seconds sec(timestamp); std::chrono::time_point tp(sec); diff --git a/src/test.cc b/src/test.cc deleted file mode 100644 index 279a50c..0000000 --- a/src/test.cc +++ /dev/null @@ -1,5 +0,0 @@ -#include - -TEST(ProgramTest, testFunction) { - EXPECT_EQ(8, 8); -} diff --git a/src/validation.cc b/src/validation.cc index e7ae0b5..c0684ed 100644 --- a/src/validation.cc +++ b/src/validation.cc @@ -27,7 +27,7 @@ bool is_action_allowed(const Action *action, std::vector *relevantEvents) return allowed; } -int validateAction( +int validate_action( Player *actor, const Action *action, Room *room, std::vector *relatedEvents, Player *target = nullptr) { if (!actor) { return validation::NoActor; diff --git a/src/validation.hh b/src/validation.hh index 32f94b3..9033f27 100644 --- a/src/validation.hh +++ b/src/validation.hh @@ -23,6 +23,6 @@ namespace validation { }; } // namespace validation -int validateAction(Player *actor, const Action *action, Room *room, std::vector *relatedEvents, Player *target); +int validate_action(Player *actor, const Action *action, Room *room, std::vector *relatedEvents, Player *target); #endif // !VALIDATION_HH From 6b7f41e41a0ec7350de7ff431187f903dfa21843 Mon Sep 17 00:00:00 2001 From: Kristofers Solo Date: Thu, 14 Mar 2024 02:24:16 +0200 Subject: [PATCH 12/19] test: add google tests --- CMakeLists.txt | 28 +++++++++++++++++++++++++++- src/test_validation.cc | 10 ++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 src/test_validation.cc diff --git a/CMakeLists.txt b/CMakeLists.txt index 75c054c..a16f545 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,11 +7,37 @@ project( set(CMAKE_EXPORT_COMPILE_COMMANDS ON) +# GoogleTest requires at least C++14 +set(CMAKE_CXX_STANDARD 14) +set(CMAKE_CXX_STANDARD_REQUIRED ON) +enable_testing() + add_subdirectory(src/modules) add_executable(main src/main.cc) +add_executable(test_validation src/test_validation.cc) add_library(validation src/validation.cc src/validation.hh) target_link_libraries(main PUBLIC validation) -target_link_libraries(validation PUBLIC modules) +target_link_libraries(validation PRIVATE modules) + target_include_directories(main PUBLIC "${PROJECT_BINARY_DIR}" "${PROJECT_SOURCE_DIR}/modules") + +target_include_directories( + test_validation PUBLIC "${PROJECT_BINARY_DIR}" + "${PROJECT_SOURCE_DIR}/modules") + +include(FetchContent) +FetchContent_Declare( + googletest + URL https://github.com/google/googletest/archive/03597a01ee50ed33e9dfd640b249b4be3799d395.zip +) +# For Windows: Prevent overriding the parent project's compiler/linker settings +set(gtest_force_shared_crt + ON + CACHE BOOL "" FORCE) +FetchContent_MakeAvailable(googletest) + +include(GoogleTest) +target_link_libraries(test_validation PRIVATE GTest::gtest_main) +gtest_discover_tests(test_validation) diff --git a/src/test_validation.cc b/src/test_validation.cc new file mode 100644 index 0000000..b79b28f --- /dev/null +++ b/src/test_validation.cc @@ -0,0 +1,10 @@ +#include "gtest/gtest.h" + +TEST(ExampleTest, Example) { + EXPECT_EQ(1, 1); +} + +int main(int argc, char *argv[]) { + testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +} From a343dcbe78628016d85e5337f0ae008d7bafcd7b Mon Sep 17 00:00:00 2001 From: Kristofers Solo Date: Thu, 14 Mar 2024 02:47:00 +0200 Subject: [PATCH 13/19] docs: add docstrings --- src/modules/role.hh | 2 +- src/validation.cc | 50 +++++++++++++++++++++++++++++++++++++-------- 2 files changed, 43 insertions(+), 9 deletions(-) diff --git a/src/modules/role.hh b/src/modules/role.hh index e98e134..e2e19da 100644 --- a/src/modules/role.hh +++ b/src/modules/role.hh @@ -8,7 +8,7 @@ struct Role { std::vector actions; - Role(std::vector actions); + explicit Role(std::vector actions); Role(std::initializer_list actions); }; diff --git a/src/validation.cc b/src/validation.cc index c0684ed..2f4b6f0 100644 --- a/src/validation.cc +++ b/src/validation.cc @@ -1,22 +1,46 @@ #include "./validation.hh" +#include "modules/action.hh" +#include "modules/event.hh" +#include "modules/player.hh" +#include "modules/role.hh" #include "modules/room.hh" #include +/** + * Check if a player belongs to a given room. + * + * @param player Pointer to the player object. + * @param room Pointer to the room object. + * @return `true` if the player belongs to the room, otherwise `false`. + */ bool player_belongs_to_room(Player *player, Room *room) { return std::find(room->players.begin(), room->players.end(), *player) != room->players.end(); } +/** + * Check if an action belongs to a given role. + * + * @param role Pointer to the role object. + * @param action Pointer to the action object. + * @return `true` if the action belongs to the role, otherwise `false`. + */ 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) { +/** + * Check if an action is allowed based on relevant events. + * + * @param action Pointer to the action object. + * @param relevantEvents Pointer to the vector of relevant events. + * @return `true` if the action is allowed, otherwise `false`. + */ +bool is_action_allowed(const Action *action, std::vector *relevant_events) { + bool allowed = false; // actions are disabled by default + std::sort(relevant_events->begin(), relevant_events->end()); + for (auto &event : *relevant_events) { if (std::find(event.prohibits.begin(), event.prohibits.end(), *action) != event.prohibits.end()) { allowed = false; } @@ -27,8 +51,18 @@ bool is_action_allowed(const Action *action, std::vector *relevantEvents) return allowed; } +/** + * Validate if an action is valid for a player in a room based on related events. + * + * @param actor Pointer to the player performing the action. + * @param action Pointer to the action to validate. + * @param room Pointer to the room where the action is taking place. + * @param related_events Pointer to the vector of related events. + * @param target Pointer to the target player (optional, defaults to `nullptr`). + * @return An integer representing the validation status. + */ int validate_action( - Player *actor, const Action *action, Room *room, std::vector *relatedEvents, Player *target = nullptr) { + Player *actor, const Action *action, Room *room, std::vector *related_events, Player *target = nullptr) { if (!actor) { return validation::NoActor; } @@ -38,7 +72,7 @@ int validate_action( if (!room) { return validation::NoRoom; } - if (!relatedEvents) { + if (!related_events) { return validation::NoRelatedEvents; } if (!player_belongs_to_room(actor, room)) { @@ -57,7 +91,7 @@ int validate_action( if (!action_belongs_to_role(role, action)) { return validation::ActionDoesNotBelongToRole; } - if (!is_action_allowed(action, relatedEvents)) { + if (!is_action_allowed(action, related_events)) { return validation::ActionProhibited; } return validation::ActionProhibited; From c62cd4c4133b061843a2549a3af0656546711805 Mon Sep 17 00:00:00 2001 From: Kristofers Solo Date: Thu, 14 Mar 2024 02:51:07 +0200 Subject: [PATCH 14/19] refactor: use `pragma once` --- src/modules/action.hh | 5 +---- src/modules/event.hh | 5 +---- src/modules/player.hh | 6 +----- src/modules/role.hh | 5 +---- src/modules/room.hh | 5 +---- src/modules/time.hh | 5 +---- src/validation.hh | 6 +----- 7 files changed, 7 insertions(+), 30 deletions(-) diff --git a/src/modules/action.hh b/src/modules/action.hh index a22b709..51f298b 100644 --- a/src/modules/action.hh +++ b/src/modules/action.hh @@ -1,5 +1,4 @@ -#ifndef ACTION_HH -#define ACTION_HH +#pragma once #include struct Action { @@ -9,5 +8,3 @@ struct Action { Action(std::string name, bool has_target); bool operator==(const Action &other) const; }; - -#endif // !ACTION_HH diff --git a/src/modules/event.hh b/src/modules/event.hh index 5997743..98f7ba8 100644 --- a/src/modules/event.hh +++ b/src/modules/event.hh @@ -1,5 +1,4 @@ -#ifndef EVENT_HH -#define EVENT_HH +#pragma once #include "./action.hh" @@ -42,5 +41,3 @@ struct Event { std::initializer_list prohibits, std::initializer_list allows); }; - -#endif // !EVENT_HH diff --git a/src/modules/player.hh b/src/modules/player.hh index 8f0b25c..98abb51 100644 --- a/src/modules/player.hh +++ b/src/modules/player.hh @@ -1,6 +1,4 @@ -#ifndef PLAYER_HH -#define PLAYER_HH - +#pragma once #include "./role.hh" #include @@ -24,5 +22,3 @@ struct Player { Player(uint32_t id, std::string username, Role role, player::Status status); bool operator==(const Player &other) const; }; - -#endif // !PLAYER_HH diff --git a/src/modules/role.hh b/src/modules/role.hh index e2e19da..4483c40 100644 --- a/src/modules/role.hh +++ b/src/modules/role.hh @@ -1,5 +1,4 @@ -#ifndef ROLE_HH -#define ROLE_HH +#pragma once #include "./action.hh" @@ -11,5 +10,3 @@ struct Role { explicit Role(std::vector actions); Role(std::initializer_list actions); }; - -#endif // !ROLE_HH diff --git a/src/modules/room.hh b/src/modules/room.hh index ff0e98c..4592267 100644 --- a/src/modules/room.hh +++ b/src/modules/room.hh @@ -1,5 +1,4 @@ -#ifndef ROOM_HH -#define ROOM_HH +#pragma once #include "./player.hh" @@ -29,5 +28,3 @@ struct Room { Room( uint32_t id, std::string title, uint32_t created_at, room::Status status, std::initializer_list players); }; - -#endif // !ROOM_HH diff --git a/src/modules/time.hh b/src/modules/time.hh index 3a746e5..73f0fa8 100644 --- a/src/modules/time.hh +++ b/src/modules/time.hh @@ -1,9 +1,6 @@ -#ifndef TIME_HH -#define TIME_HH +#pragma once #include #include std::tm *create_utc_timestamp(uint32_t timestamp); - -#endif // !TIME_HH diff --git a/src/validation.hh b/src/validation.hh index 9033f27..9b183c5 100644 --- a/src/validation.hh +++ b/src/validation.hh @@ -1,6 +1,4 @@ -#ifndef VALIDATION_HH -#define VALIDATION_HH - +#pragma once #include "modules/event.hh" #include "modules/player.hh" #include "modules/room.hh" @@ -24,5 +22,3 @@ namespace validation { } // namespace validation int validate_action(Player *actor, const Action *action, Room *room, std::vector *relatedEvents, Player *target); - -#endif // !VALIDATION_HH From 4e63f81655636f743f8d264cfb48a79dec583628 Mon Sep 17 00:00:00 2001 From: Kristofers Solo Date: Thu, 14 Mar 2024 03:06:23 +0200 Subject: [PATCH 15/19] refactor(validation): set params as const --- src/validation.cc | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/validation.cc b/src/validation.cc index 2f4b6f0..40d77cf 100644 --- a/src/validation.cc +++ b/src/validation.cc @@ -15,7 +15,7 @@ * @param room Pointer to the room object. * @return `true` if the player belongs to the room, otherwise `false`. */ -bool player_belongs_to_room(Player *player, Room *room) { +bool player_belongs_to_room(const Player *player, const Room *room) { return std::find(room->players.begin(), room->players.end(), *player) != room->players.end(); } @@ -26,7 +26,7 @@ bool player_belongs_to_room(Player *player, Room *room) { * @param action Pointer to the action object. * @return `true` if the action belongs to the role, otherwise `false`. */ -bool action_belongs_to_role(Role *role, const Action *action) { +bool action_belongs_to_role(const Role *role, const Action *action) { return std::find(role->actions.begin(), role->actions.end(), *action) != role->actions.end(); } @@ -37,12 +37,11 @@ bool action_belongs_to_role(Role *role, const Action *action) { * @param relevantEvents Pointer to the vector of relevant events. * @return `true` if the action is allowed, otherwise `false`. */ -bool is_action_allowed(const Action *action, std::vector *relevant_events) { - bool allowed = false; // actions are disabled by default - std::sort(relevant_events->begin(), relevant_events->end()); - for (auto &event : *relevant_events) { +bool is_action_allowed(const Action *action, const std::vector *relevant_events) { + bool allowed = false; // Actions are disabled by default + for (const auto &event : *relevant_events) { if (std::find(event.prohibits.begin(), event.prohibits.end(), *action) != event.prohibits.end()) { - allowed = false; + return false; // If action is prohibited, return false immediately } if (std::find(event.allows.begin(), event.allows.end(), *action) != event.allows.end()) { allowed = true; From 7fd0c887ec3b79c1b92b6a86361b9b05a738b0c8 Mon Sep 17 00:00:00 2001 From: Kristofers Solo Date: Thu, 14 Mar 2024 03:06:44 +0200 Subject: [PATCH 16/19] refactor(enum): use `enum class` refactor(enum): convert validation enum to enum class --- src/main.cc | 9 ++++----- src/modules/event.hh | 14 ++++++-------- src/modules/player.cc | 2 +- src/modules/player.hh | 18 ++++++++---------- src/modules/room.cc | 4 ++-- src/modules/room.hh | 21 +++++++++------------ src/validation.cc | 26 +++++++++++++------------- src/validation.hh | 32 ++++++++++++++++---------------- 8 files changed, 59 insertions(+), 67 deletions(-) diff --git a/src/main.cc b/src/main.cc index f6e678f..f9e059e 100644 --- a/src/main.cc +++ b/src/main.cc @@ -12,11 +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, 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 validated_action = validate_action(&player1, &kill, &room1, &relatedEvents, &player2); + Player player1 = Player(69, "player1", role1, PlayerStatus::Alive); + Player player2 = Player(420, "player2", role1, PlayerStatus::Alive); + Room room1(1, "Room 1", 1710087364, RoomStatus::InProgress, {player1, player2}); + Room room2(2, "Room 2", 1710087384, RoomStatus::Ended, {}); printf("The action validation result is %u\n", validated_action); return EXIT_SUCCESS; } diff --git a/src/modules/event.hh b/src/modules/event.hh index 98f7ba8..ae5cb78 100644 --- a/src/modules/event.hh +++ b/src/modules/event.hh @@ -8,14 +8,12 @@ #include #include -namespace event { - enum Type { - PhaseChange, - EventAction, - RoomStateChange, - PlayerStateChange, - }; -} // namespace event +enum EventType { + PhaseChange, + EventAction, + RoomStateChange, + PlayerStateChange, +}; struct Event { std::string title; diff --git a/src/modules/player.cc b/src/modules/player.cc index d67f05c..6f98124 100644 --- a/src/modules/player.cc +++ b/src/modules/player.cc @@ -4,7 +4,7 @@ #include -Player::Player(uint32_t id, std::string username, Role role, player::Status status): +Player::Player(uint32_t id, std::string username, Role role, PlayerStatus status): id(id), username(username), role(role), diff --git a/src/modules/player.hh b/src/modules/player.hh index 98abb51..7d9e09c 100644 --- a/src/modules/player.hh +++ b/src/modules/player.hh @@ -4,21 +4,19 @@ #include #include -namespace player { - enum Status { - Kicked, - Alive, - Dead, - VotedOut, - }; -} // namespace player +enum class PlayerStatus { + Kicked, + Alive, + Dead, + VotedOut, +}; struct Player { uint32_t id; std::string username; Role role; - player::Status status; + PlayerStatus status; - Player(uint32_t id, std::string username, Role role, player::Status status); + Player(uint32_t id, std::string username, Role role, PlayerStatus status); bool operator==(const Player &other) const; }; diff --git a/src/modules/room.cc b/src/modules/room.cc index 626bcd7..f2de501 100644 --- a/src/modules/room.cc +++ b/src/modules/room.cc @@ -7,7 +7,7 @@ #include #include -Room::Room(uint32_t id, std::string title, uint32_t created_at, room::Status status, std::vector players): +Room::Room(uint32_t id, std::string title, uint32_t created_at, RoomStatus status, std::vector players): id(id), title(title), status(status), @@ -16,7 +16,7 @@ Room::Room(uint32_t id, std::string title, uint32_t created_at, room::Status sta } Room::Room( - uint32_t id, std::string title, uint32_t created_at, room::Status status, std::initializer_list players): + uint32_t id, std::string title, uint32_t created_at, RoomStatus status, std::initializer_list players): id(id), title(title), status(status), diff --git a/src/modules/room.hh b/src/modules/room.hh index 4592267..dfd6460 100644 --- a/src/modules/room.hh +++ b/src/modules/room.hh @@ -8,23 +8,20 @@ #include #include -namespace room { - enum Status { - AwaitingStart, - InProgress, - Stopped, - Ended, - }; -} // namespace room +enum class RoomStatus { + AwaitingStart, + InProgress, + Stopped, + Ended, +}; struct Room { uint32_t id; std::string title; std::tm *created_at; - room::Status status; + RoomStatus status; std::vector players; - Room(uint32_t id, std::string title, uint32_t created_at, room::Status status, std::vector players); - Room( - uint32_t id, std::string title, uint32_t created_at, room::Status status, std::initializer_list players); + Room(uint32_t id, std::string title, uint32_t created_at, RoomStatus status, std::vector players); + Room(uint32_t id, std::string title, uint32_t created_at, RoomStatus status, std::initializer_list players); }; diff --git a/src/validation.cc b/src/validation.cc index 40d77cf..7bd5d1a 100644 --- a/src/validation.cc +++ b/src/validation.cc @@ -60,38 +60,38 @@ bool is_action_allowed(const Action *action, const std::vector *relevant_ * @param target Pointer to the target player (optional, defaults to `nullptr`). * @return An integer representing the validation status. */ -int validate_action( +ValidationStatus validate_action( Player *actor, const Action *action, Room *room, std::vector *related_events, Player *target = nullptr) { if (!actor) { - return validation::NoActor; + return ValidationStatus::NoActor; } if (!action) { - return validation::NoAction; + return ValidationStatus::NoAction; } if (!room) { - return validation::NoRoom; + return ValidationStatus::NoRoom; } if (!related_events) { - return validation::NoRelatedEvents; + return ValidationStatus::NoRelatedEvents; } if (!player_belongs_to_room(actor, room)) { - return validation::PlayerNotInRoom; + return ValidationStatus::PlayerNotInRoom; } if (action->has_target && !target) { - return validation::NoTargetPlayerSpecified; + return ValidationStatus::NoTargetPlayerSpecified; } - if (room->status != room::Status::InProgress) { - return validation::RoomNotInProgress; + if (room->status != RoomStatus::InProgress) { + return ValidationStatus::RoomNotInProgress; } Role *role = &actor->role; if (!role) { - return validation::NoRole; + return ValidationStatus::NoRole; } if (!action_belongs_to_role(role, action)) { - return validation::ActionDoesNotBelongToRole; + return ValidationStatus::ActionDoesNotBelongToRole; } if (!is_action_allowed(action, related_events)) { - return validation::ActionProhibited; + return ValidationStatus::ActionProhibited; } - return validation::ActionProhibited; + return ValidationStatus::ActionProhibited; } diff --git a/src/validation.hh b/src/validation.hh index 9b183c5..dcad507 100644 --- a/src/validation.hh +++ b/src/validation.hh @@ -5,20 +5,20 @@ #include -namespace validation { - enum Status { - PlayerNotInRoom, - NoTargetPlayerSpecified, - RoomNotInProgress, - ActionDoesNotBelongToRole, - ActionProhibited, - NoActor, - NoAction, - NoRole, - NoRoom, - NoRelatedEvents, - ActionValid, - }; -} // namespace validation +enum class ValidationStatus { + PlayerNotInRoom, + NoTargetPlayerSpecified, + RoomNotInProgress, + ActionDoesNotBelongToRole, + ActionProhibited, + NoActor, + NoAction, + NoRole, + NoRoom, + NoRelatedEvents, + ActionValid, +}; -int validate_action(Player *actor, const Action *action, Room *room, std::vector *relatedEvents, Player *target); + +ValidationStatus + validate_action(Player *actor, const Action *action, Room *room, std::vector *relatedEvents, Player *target); From e316beedbc94a0471f69adc127b927c16f991a99 Mon Sep 17 00:00:00 2001 From: Kristofers Solo Date: Thu, 14 Mar 2024 03:22:20 +0200 Subject: [PATCH 17/19] feat: add `to_string` to `ValidationStatus` --- src/main.cc | 5 ++++- src/validation.cc | 18 ++++++++++++++++++ src/validation.hh | 7 +++++++ 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/main.cc b/src/main.cc index f9e059e..bb76d4b 100644 --- a/src/main.cc +++ b/src/main.cc @@ -1,6 +1,7 @@ #include "./validation.hh" #include +#include int main(int argc, char *argv[]) { const Action kill = Action("kill", true); @@ -16,6 +17,8 @@ int main(int argc, char *argv[]) { Player player2 = Player(420, "player2", role1, PlayerStatus::Alive); Room room1(1, "Room 1", 1710087364, RoomStatus::InProgress, {player1, player2}); Room room2(2, "Room 2", 1710087384, RoomStatus::Ended, {}); - printf("The action validation result is %u\n", validated_action); + ValidationStatus validated_action = validate_action(&player1, &kill, &room1, &relatedEvents, &player2); + std::string validated_action_str = ValidationStatusUtils::to_string(validated_action); + printf("The action validation result is \"%s\"\n", validated_action_str.c_str()); return EXIT_SUCCESS; } diff --git a/src/validation.cc b/src/validation.cc index 7bd5d1a..692c13f 100644 --- a/src/validation.cc +++ b/src/validation.cc @@ -7,6 +7,7 @@ #include "modules/room.hh" #include +#include /** * Check if a player belongs to a given room. @@ -95,3 +96,20 @@ ValidationStatus validate_action( } return ValidationStatus::ActionProhibited; } + +std::string ValidationStatusUtils::to_string(ValidationStatus status) { + switch (status) { + case ValidationStatus::PlayerNotInRoom: return "player not in room"; + case ValidationStatus::NoTargetPlayerSpecified: return "no target player specified"; + case ValidationStatus::RoomNotInProgress: return "room not in progress"; + case ValidationStatus::ActionDoesNotBelongToRole: return "action does not belong to role"; + case ValidationStatus::ActionProhibited: return "action prohibited"; + case ValidationStatus::NoActor: return "no actor"; + case ValidationStatus::NoAction: return "no action"; + case ValidationStatus::NoRole: return "no role"; + case ValidationStatus::NoRoom: return "no room"; + case ValidationStatus::NoRelatedEvents: return "no relevant events"; + case ValidationStatus::ActionValid: return "action valid"; + default: return "unknown validation status"; + } +} diff --git a/src/validation.hh b/src/validation.hh index dcad507..712ce4f 100644 --- a/src/validation.hh +++ b/src/validation.hh @@ -3,6 +3,7 @@ #include "modules/player.hh" #include "modules/room.hh" +#include #include enum class ValidationStatus { @@ -19,6 +20,12 @@ enum class ValidationStatus { ActionValid, }; +std::string validation_status_to_string(ValidationStatus status); ValidationStatus validate_action(Player *actor, const Action *action, Room *room, std::vector *relatedEvents, Player *target); + +class ValidationStatusUtils { + public: + static std::string to_string(ValidationStatus status); +}; From 72b89de4ce162de43e14647f4119b14d98337b69 Mon Sep 17 00:00:00 2001 From: Kristofers Solo Date: Thu, 14 Mar 2024 03:28:22 +0200 Subject: [PATCH 18/19] fix: invalid validation result --- src/main.cc | 2 +- src/modules/event.hh | 2 +- src/validation.cc | 7 ++++--- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/main.cc b/src/main.cc index bb76d4b..4aef986 100644 --- a/src/main.cc +++ b/src/main.cc @@ -19,6 +19,6 @@ int main(int argc, char *argv[]) { Room room2(2, "Room 2", 1710087384, RoomStatus::Ended, {}); ValidationStatus validated_action = validate_action(&player1, &kill, &room1, &relatedEvents, &player2); std::string validated_action_str = ValidationStatusUtils::to_string(validated_action); - printf("The action validation result is \"%s\"\n", validated_action_str.c_str()); + printf("The validation result is \"%s\"\n", validated_action_str.c_str()); return EXIT_SUCCESS; } diff --git a/src/modules/event.hh b/src/modules/event.hh index ae5cb78..675f538 100644 --- a/src/modules/event.hh +++ b/src/modules/event.hh @@ -8,7 +8,7 @@ #include #include -enum EventType { +enum class EventType { PhaseChange, EventAction, RoomStateChange, diff --git a/src/validation.cc b/src/validation.cc index 692c13f..2c73902 100644 --- a/src/validation.cc +++ b/src/validation.cc @@ -38,11 +38,12 @@ bool action_belongs_to_role(const Role *role, const Action *action) { * @param relevantEvents Pointer to the vector of relevant events. * @return `true` if the action is allowed, otherwise `false`. */ -bool is_action_allowed(const Action *action, const std::vector *relevant_events) { +bool is_action_allowed(const Action *action, std::vector *relevant_events) { bool allowed = false; // Actions are disabled by default + std::sort(relevant_events->begin(), relevant_events->end()); for (const auto &event : *relevant_events) { if (std::find(event.prohibits.begin(), event.prohibits.end(), *action) != event.prohibits.end()) { - return false; // If action is prohibited, return false immediately + allowed = false; } if (std::find(event.allows.begin(), event.allows.end(), *action) != event.allows.end()) { allowed = true; @@ -94,7 +95,7 @@ ValidationStatus validate_action( if (!is_action_allowed(action, related_events)) { return ValidationStatus::ActionProhibited; } - return ValidationStatus::ActionProhibited; + return ValidationStatus::ActionValid; } std::string ValidationStatusUtils::to_string(ValidationStatus status) { From 340674487266313f2f2cb886be4d965508696496 Mon Sep 17 00:00:00 2001 From: Kristofers Solo Date: Thu, 14 Mar 2024 03:42:50 +0200 Subject: [PATCH 19/19] docs(readme): update description --- README-lv.md | 28 +++++++++++++++++----------- README.md | 17 ++++++++++------- 2 files changed, 27 insertions(+), 18 deletions(-) diff --git a/README-lv.md b/README-lv.md index fa62eb2..f68fda5 100644 --- a/README-lv.md +++ b/README-lv.md @@ -5,20 +5,21 @@ ## Projekts Šis ir vienkāršs C++ projekts, kas ietver funkciju mūsu kolēģiem -testēšanai. Funkcija (t.i., galvenā funkcija un tajā izmantotās apakšfunkcijas) -atrodas `lib.cpp`. +testēšanai. Funkcija, t.i., apakšfunkcijas, kuru nepieciešams testēt +atrodas `validation.cc`. ## Papildu bibliotēkas un izpildāmā programma -Mūsu bibliotēka `prep` ir saistīta ar datu sagatavošanu pirms funkcijas -izsaukuma. `test.cpp` ir viens testa fails, kurā būs mūsu kolēģu testi -un mūsu pašu izstrādes testi. GTest tiek izmantots testiem. +Direktorija `modules` ir saistīta ar datu sagatavošanu pirms funkcijas +izsaukuma. `test_validation.cc` ir viens testa fails, kas satur testus. +[GTest](https://github.com/google/googletest) tiek izmantots testiem. ## Lietojums ## Kompilācija uz Linux un MacOS -Kompilācijai operētājsistēmā Linux ir nepieciešams cmake un CXX kompilators (e.g., g++). +Kompilācijai operētājsistēmā Linux ir nepieciešams cmake un +CXX kompilators (e.g., g++). ### MacOS @@ -59,10 +60,15 @@ make ### Windows un/vai VSCode -Uzstādiet [CMake](https://cmake.org/download/) un VSCode [CMake Tools](https://marketplace.visualstudio.com/items?itemName=ms-vscode.cmake-tools) paplašinājumu. +Uzstādiet [CMake](https://cmake.org/download/) un VSCode +[CMake Tools](https://marketplace.visualstudio.com/items?itemName=ms-vscode.cmake-tools) +paplašinājumu. -- Atveriet projektu kā saknes direktoriju VSCode. -- Izvēlaties **View->Command palete...** vai `Ctrl+Shift+P` un palaidiet `CMake: Build`. +- Atveriet projektu kā saknes direktoriju + VSCode. +- Izvēlaties **View->Command palete...** vai + `Ctrl+Shift+P` un palaidiet `CMake: Build`. -Rezultātā iegūtie binārie faili ir `build/main` - programma un `build/runtests`, lai -palaistu punktos norādītos testus iekš `test.cpp`. +Rezultātā iegūtie binārie faili ir `build/main` -- programma un +`build/test_validation`, lai palaistu norādītos testus +no `test_validation.cc` faila. diff --git a/README.md b/README.md index da1f4c4..27c19c2 100644 --- a/README.md +++ b/README.md @@ -7,14 +7,14 @@ ## Project This is a simple C++ project that includes the function for our colleagues to -test. The function (i.e., the main function and the subfunctions used in it) is -located in `lib.cpp`. +test. The function (i.e., the subfunctions that are used in main function) is +located in `validation.cc`. ## Additional libraries and executable -Our library `prep` is concerned with preparation of data prior to the function -call. `test.cpp` is a single test file that will contain the tests of our colleagues -and our own development tests. GTest is used for tests. +Directory `modules` is concerned with preparation of data prior to the function +call. `test_validation.cc` is a single test file that contain the tests. +[GTest](https://github.com/google/googletest) is used for tests. ## Usage @@ -61,9 +61,12 @@ make ### Windows and/or VSCode -Install [CMake](https://cmake.org/download/) and VSCode [CMake Tools](https://marketplace.visualstudio.com/items?itemName=ms-vscode.cmake-tools) extension. +Install [CMake](https://cmake.org/download/) and VSCode +[CMake Tools](https://marketplace.visualstudio.com/items?itemName=ms-vscode.cmake-tools) +extension. - Open project as root in VSCode. - Press **View->Command palette...** or `Ctrl+Shift+P` and run `CMake: Build`. -Resulting binaries are `build/main` -- the program and `build/runtests` to run tests specified in `test.cpp`. +Resulting binaries are `build/main` -- the program and `build/test_validation` +to run tests specified in `test_validation.cc`.