From 0b16d5f92b26f1238ae433860261f0c821edb620 Mon Sep 17 00:00:00 2001 From: Jorens Shtekels Date: Sun, 10 Mar 2024 19:19:00 +0200 Subject: [PATCH 1/5] feat[prep]: role, player constructors --- src/lib.cpp | 7 +++++-- src/prep/prep.cpp | 28 ++++++++++++++++++++++++++++ src/prep/prep.hh | 7 +++++++ 3 files changed, 40 insertions(+), 2 deletions(-) diff --git a/src/lib.cpp b/src/lib.cpp index 75f8cf9..2b58745 100644 --- a/src/lib.cpp +++ b/src/lib.cpp @@ -1,10 +1,13 @@ -#include "lib.hh" - #include "prep/prep.hh" +#include + void run() { Room room1(1, "Room 1", 1710087364, RoomStatus::IN_PROGRESS); Room room2(2, "Room 2", 1710087384, RoomStatus::ENDED); + Role role1({Action::KILL, Action::HEAL}); + std::vector actions = {Action::HEAL}; + Role role2(actions); } int functionToTest(int a) { diff --git a/src/prep/prep.cpp b/src/prep/prep.cpp index 5d4d199..0cd8665 100644 --- a/src/prep/prep.cpp +++ b/src/prep/prep.cpp @@ -2,13 +2,41 @@ #include "timeUtils.hh" +#include + int add(int a, int b) { return a + b; } +Role::Role(std::initializer_list actions) { + for (auto &a : actions) { + this->actions.push_back(a); + } +} + +Role::Role(std::vector actions) { + for (auto &a : actions) { + this->actions.push_back(a); + } +} + +Player::Player(std::string username, Role role, PlayerStatus playerStatus): + username(username), + role(role), + playerStatus(playerStatus) { +} + Room::Room(uint32_t id, std::string title, uint32_t utcTimestampCreatedAt, RoomStatus status): id(id), title(title), status(status) { this->utcTimestampCreatedAt = createUTCTimestamp(utcTimestampCreatedAt); } + +Event::Event(std::string title, uint32_t utcTimestampCreatedAt, uint32_t numberNight, bool isVisible): + title(title), + utcTimestampCreatedAt(createUTCTimestamp(utcTimestampCreatedAt)), + numberNight(numberNight), + isVisible(isVisible) { + this->utcTimestampCreatedAt = createUTCTimestamp(utcTimestampCreatedAt); +} diff --git a/src/prep/prep.hh b/src/prep/prep.hh index 9a8d1aa..eadd4e6 100644 --- a/src/prep/prep.hh +++ b/src/prep/prep.hh @@ -47,12 +47,17 @@ struct Event; struct Role { std::vector actions; + + Role(std::initializer_list actions); + Role(std::vector actions); }; struct Player { std::string username; Role role; PlayerStatus playerStatus; + + Player(std::string username, Role role, PlayerStatus playerStatus); }; struct Room { @@ -71,6 +76,8 @@ struct Event { bool isVisible; std::vector causedBy; std::vector influenced; + + Event(std::string title, uint32_t utcTimestampCreatedAt, uint32_t numberNight, bool isVisible); }; #endif From 505cf1ec1016041056ceb1d7beb1e856ad0164eb Mon Sep 17 00:00:00 2001 From: Kristofers Solo Date: Sun, 10 Mar 2024 18:58:51 +0200 Subject: [PATCH 2/5] build(ninja): add ninja support - Added ninja build support - Updated readme --- .gitignore | 29 +++++++++++++++++++++++++++++ README.md | 22 ++++++++++++++++------ 2 files changed, 45 insertions(+), 6 deletions(-) diff --git a/.gitignore b/.gitignore index 123517b..c6a6574 100644 --- a/.gitignore +++ b/.gitignore @@ -68,6 +68,35 @@ build # Clang .cache +# clangd +/.clangd/ +/compile_commands.json +/.cache/ + +# Ninja output +.ninja_deps +.ninja_log + +*.pyc +*.obj +*.ilk +/build*/ +/build.ninja +/ninja +/ninja.bootstrap +/build_log_perftest +/canon_perftest +/clparser_perftest +/depfile_parser_perftest +/hash_collision_bench +/ninja_test +/manifest_parser_perftest +/graph.png +/doc/manual.html +/doc/doxygen +*.patch +.DS_Store + input.txt output.txt diff --git a/README.md b/README.md index e2205b6..c3a10e5 100644 --- a/README.md +++ b/README.md @@ -4,11 +4,11 @@ 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 `main_lib.cpp`. +located in `lib.cpp`. ## Additional libraries and executable -Our library `Prep` is concerned with preparation of data prior to the function +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. @@ -18,12 +18,22 @@ and our own development tests. GTest is used for tests. Compilation on Linux requires cmake and CXX compiler (e.g., g++). +### ninja + ```bash -cd src -mkdir build +cmake -Gninja -Bbuild cd build -cmake ../ -cmake --build . +ninja +./main +``` + +### Make + +```bash +cmake -Bbuild +cd build +make +./main ``` Resulting binaries are `build/main` - the program and `build/runtests` to From faf6d64ce080cf90eabe8d5dd5cac94018bfce34 Mon Sep 17 00:00:00 2001 From: Jorens Shtekels Date: Sun, 10 Mar 2024 19:28:46 +0200 Subject: [PATCH 3/5] feat[prep]: event prohibits and allows other actions --- src/prep/prep.cpp | 34 +++++++++++++++++++++++++++++++++- src/prep/prep.hh | 18 ++++++++++++++++-- 2 files changed, 49 insertions(+), 3 deletions(-) diff --git a/src/prep/prep.cpp b/src/prep/prep.cpp index 0cd8665..6ef7707 100644 --- a/src/prep/prep.cpp +++ b/src/prep/prep.cpp @@ -33,10 +33,42 @@ Room::Room(uint32_t id, std::string title, uint32_t utcTimestampCreatedAt, RoomS this->utcTimestampCreatedAt = createUTCTimestamp(utcTimestampCreatedAt); } -Event::Event(std::string title, uint32_t utcTimestampCreatedAt, uint32_t numberNight, bool isVisible): +Event::Event(std::string title, + uint32_t utcTimestampCreatedAt, + uint32_t numberNight, + bool isVisible, + std::vector causedBy, + std::vector prohibits, + std::vector allows): title(title), utcTimestampCreatedAt(createUTCTimestamp(utcTimestampCreatedAt)), numberNight(numberNight), isVisible(isVisible) { this->utcTimestampCreatedAt = createUTCTimestamp(utcTimestampCreatedAt); + for (auto &a : causedBy) { + this->causedBy.push_back(a); + } + for (auto &a : prohibits) { + this->prohibits.push_back(a); + } + for (auto &a : allows) { + this->allows.push_back(a); + } +} + +Event::Event(std::string title, + uint32_t utcTimestampCreatedAt, + uint32_t numberNight, + bool isVisible, + std::initializer_list causedBy, + std::initializer_list prohibits, + std::initializer_list allows): + Event(title, + utcTimestampCreatedAt, + numberNight, + isVisible, + std::vector(causedBy), + std::vector(prohibits), + std::vector(allows)) { + this->utcTimestampCreatedAt = createUTCTimestamp(utcTimestampCreatedAt); } diff --git a/src/prep/prep.hh b/src/prep/prep.hh index eadd4e6..01969c3 100644 --- a/src/prep/prep.hh +++ b/src/prep/prep.hh @@ -75,9 +75,23 @@ struct Event { uint32_t numberNight; bool isVisible; std::vector causedBy; - std::vector influenced; + std::vector prohibits; + std::vector allows; - Event(std::string title, uint32_t utcTimestampCreatedAt, uint32_t numberNight, bool isVisible); + Event(std::string title, + 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); }; #endif From fb05b5d6c3c9e5ac7b733b8a911a24fc43644c3e Mon Sep 17 00:00:00 2001 From: Jorens Shtekels Date: Sun, 10 Mar 2024 19:41:59 +0200 Subject: [PATCH 4/5] feat[lib]: function placeholder --- src/lib.cpp | 13 +++++++++++++ src/lib.hh | 4 ++++ 2 files changed, 17 insertions(+) diff --git a/src/lib.cpp b/src/lib.cpp index 2b58745..3ab6cb5 100644 --- a/src/lib.cpp +++ b/src/lib.cpp @@ -10,6 +10,19 @@ void run() { Role role2(actions); } +enum VALIDATION_STATUS { + NO_PLAYER, + ROOM_NOT_IN_PROGRESS, + ACTION_PROHIBITED, + ACTION_NOT_ALLOWED, + ACTION_VALID, +}; + +int validateAction( + Player *actor, Action *action, Room *room, std::vector *relatedEvents, Player *target = nullptr) { + return ACTION_VALID; +} + int functionToTest(int a) { return a * 2; } diff --git a/src/lib.hh b/src/lib.hh index a0bce46..fc6a08b 100644 --- a/src/lib.hh +++ b/src/lib.hh @@ -1,2 +1,6 @@ +#include "prep/prep.hh" + void run(); +int validateAction( + Player *actor, Action *action, Room *room, std::vector *relatedEvents, Player *target = nullptr); int functionToTest(int); From ee12e48a861fa0aaacbac17436d6f7815cd1cd9c Mon Sep 17 00:00:00 2001 From: Jorens Shtekels Date: Sun, 10 Mar 2024 20:15:55 +0200 Subject: [PATCH 5/5] feat[lib]: validate action partial implementation Performed most checks. Added placeholders to functions called. --- src/lib.cpp | 48 ++++++++++++++++++++++++++++++++++++++++-------- src/lib.hh | 6 ++++-- 2 files changed, 44 insertions(+), 10 deletions(-) diff --git a/src/lib.cpp b/src/lib.cpp index 3ab6cb5..128417f 100644 --- a/src/lib.cpp +++ b/src/lib.cpp @@ -1,7 +1,18 @@ +#include "lib.hh" + #include "prep/prep.hh" #include +enum VALIDATION_STATUS { + NO_PLAYER, + ROOM_NOT_IN_PROGRESS, + ACTION_PROHIBITED, + ACTION_NOT_ALLOWED, + NO_ROLE, + ACTION_VALID, +}; + void run() { Room room1(1, "Room 1", 1710087364, RoomStatus::IN_PROGRESS); Room room2(2, "Room 2", 1710087384, RoomStatus::ENDED); @@ -10,19 +21,40 @@ void run() { Role role2(actions); } -enum VALIDATION_STATUS { - NO_PLAYER, - ROOM_NOT_IN_PROGRESS, - ACTION_PROHIBITED, - ACTION_NOT_ALLOWED, - ACTION_VALID, -}; - int validateAction( Player *actor, Action *action, Room *room, std::vector *relatedEvents, Player *target = nullptr) { + if (!actor) { + return NO_PLAYER; + } + // TODO: Check if action has a target + 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_NOT_ALLOWED; + } + if (!isActionAllowed(relatedEvents)) { + return ACTION_PROHIBITED; + } return ACTION_VALID; } +bool actionBelongsToRole(Role *role, Action *action) { + // TODO: implement + bool belongs = false; + return belongs; +} + +bool isActionAllowed(std::vector *relevantEvents) { + // TODO: implement + bool allowed = false; + return allowed; +} + int functionToTest(int a) { return a * 2; } diff --git a/src/lib.hh b/src/lib.hh index fc6a08b..440093d 100644 --- a/src/lib.hh +++ b/src/lib.hh @@ -1,6 +1,8 @@ #include "prep/prep.hh" void run(); -int validateAction( - Player *actor, Action *action, Room *room, std::vector *relatedEvents, Player *target = nullptr); + +bool actionBelongsToRole(Role *role, Action *action); +bool isActionAllowed(std::vector *relevantEvents); +int validateAction(Player *actor, Action *action, Room *room, std::vector *relatedEvents, Player *target); int functionToTest(int);