mirror of
https://github.com/jorenchik/testing-spring-2024.git
synced 2025-10-21 20:10:36 +00:00
feat: event prohibition
This commit is contained in:
parent
9a7bc42db6
commit
ce9e6be7e7
20
src/lib.cpp
20
src/lib.cpp
@ -2,6 +2,7 @@
|
||||
|
||||
#include "prep/prep.hh"
|
||||
|
||||
#include <algorithm>
|
||||
#include <vector>
|
||||
|
||||
enum VALIDATION_STATUS {
|
||||
@ -21,6 +22,10 @@ void run() {
|
||||
const Action vote = Action("vote", true);
|
||||
Role role1({vote, kill, heal});
|
||||
Role role2({heal});
|
||||
Event event1 = Event("Event 1", 1710087364, 1, true, {vote}, {}, {});
|
||||
Event event2 = Event("Event 2", 1710087364, 1, true, {}, {}, {kill});
|
||||
Event event3 = Event("Event 3", 1710087364, 1, true, {}, {kill}, {});
|
||||
std::vector<Event> relatedEvents({event1, event3});
|
||||
}
|
||||
|
||||
int validateAction(
|
||||
@ -41,7 +46,7 @@ int validateAction(
|
||||
if (!actionBelongsToRole(role, action)) {
|
||||
return ACTION_NOT_ALLOWED;
|
||||
}
|
||||
if (!isActionAllowed(relatedEvents)) {
|
||||
if (!isActionAllowed(action, relatedEvents)) {
|
||||
return ACTION_PROHIBITED;
|
||||
}
|
||||
return ACTION_VALID;
|
||||
@ -53,9 +58,18 @@ bool actionBelongsToRole(Role *role, Action *action) {
|
||||
return belongs;
|
||||
}
|
||||
|
||||
bool isActionAllowed(std::vector<Event> *relevantEvents) {
|
||||
// TODO: implement
|
||||
bool isActionAllowed(Action *action, std::vector<Event> *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;
|
||||
}
|
||||
|
||||
|
||||
@ -3,6 +3,6 @@
|
||||
void run();
|
||||
|
||||
bool actionBelongsToRole(Role *role, Action *action);
|
||||
bool isActionAllowed(std::vector<Event> *relevantEvents);
|
||||
bool isActionAllowed(Action *action, std::vector<Event> *relevantEvents);
|
||||
int validateAction(Player *actor, Action *action, Room *room, std::vector<Event> *relatedEvents, Player *target);
|
||||
int functionToTest(int);
|
||||
|
||||
@ -13,6 +13,10 @@ Action::Action(std::string name, bool hasTarget) {
|
||||
this->hasTarget = hasTarget;
|
||||
}
|
||||
|
||||
bool Action::operator==(const Action &other) const {
|
||||
return this->name == other.name;
|
||||
}
|
||||
|
||||
Role::Role(std::initializer_list<Action> actions) {
|
||||
for (auto &a : actions) {
|
||||
this->actions.push_back(a);
|
||||
@ -77,3 +81,15 @@ Event::Event(std::string title,
|
||||
std::vector<Action>(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;
|
||||
}
|
||||
|
||||
@ -42,6 +42,7 @@ struct Action {
|
||||
bool hasTarget;
|
||||
|
||||
Action(std::string name, bool hasTarget);
|
||||
bool operator==(const Action &other) const;
|
||||
};
|
||||
|
||||
struct Role {
|
||||
@ -77,6 +78,10 @@ struct Event {
|
||||
std::vector<Action> prohibits;
|
||||
std::vector<Action> 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,
|
||||
|
||||
Loading…
Reference in New Issue
Block a user