Merge pull request #6 from jorenchik/develop

validation logic
This commit is contained in:
Jorens Shtekels 2024-03-10 21:12:05 +02:00 committed by GitHub
commit b4ad74aa87
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 59 additions and 16 deletions

View File

@ -2,6 +2,7 @@
#include "prep/prep.hh" #include "prep/prep.hh"
#include <algorithm>
#include <vector> #include <vector>
enum VALIDATION_STATUS { enum VALIDATION_STATUS {
@ -16,9 +17,15 @@ enum VALIDATION_STATUS {
void run() { void run() {
Room room1(1, "Room 1", 1710087364, RoomStatus::IN_PROGRESS); Room room1(1, "Room 1", 1710087364, RoomStatus::IN_PROGRESS);
Room room2(2, "Room 2", 1710087384, RoomStatus::ENDED); Room room2(2, "Room 2", 1710087384, RoomStatus::ENDED);
Role role1({Action::KILL, Action::HEAL}); const Action kill = Action("kill", true);
std::vector<Action> actions = {Action::HEAL}; const Action heal = Action("heal", true);
Role role2(actions); 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( int validateAction(
@ -26,7 +33,9 @@ int validateAction(
if (!actor) { if (!actor) {
return NO_PLAYER; return NO_PLAYER;
} }
// TODO: Check if action has a target if (action->hasTarget && !target) {
return NO_PLAYER;
}
if (room->status != RoomStatus::IN_PROGRESS) { if (room->status != RoomStatus::IN_PROGRESS) {
return ROOM_NOT_IN_PROGRESS; return ROOM_NOT_IN_PROGRESS;
} }
@ -37,7 +46,7 @@ int validateAction(
if (!actionBelongsToRole(role, action)) { if (!actionBelongsToRole(role, action)) {
return ACTION_NOT_ALLOWED; return ACTION_NOT_ALLOWED;
} }
if (!isActionAllowed(relatedEvents)) { if (!isActionAllowed(action, relatedEvents)) {
return ACTION_PROHIBITED; return ACTION_PROHIBITED;
} }
return ACTION_VALID; return ACTION_VALID;
@ -49,9 +58,18 @@ bool actionBelongsToRole(Role *role, Action *action) {
return belongs; return belongs;
} }
bool isActionAllowed(std::vector<Event> *relevantEvents) { bool isActionAllowed(Action *action, std::vector<Event> *relevantEvents) {
// TODO: implement // actions are disabled by default
bool allowed = false; 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; return allowed;
} }

View File

@ -3,6 +3,6 @@
void run(); void run();
bool actionBelongsToRole(Role *role, Action *action); 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 validateAction(Player *actor, Action *action, Room *room, std::vector<Event> *relatedEvents, Player *target);
int functionToTest(int); int functionToTest(int);

View File

@ -8,6 +8,15 @@ int add(int a, int b) {
return a + b; return a + b;
} }
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<Action> actions) { Role::Role(std::initializer_list<Action> actions) {
for (auto &a : actions) { for (auto &a : actions) {
this->actions.push_back(a); this->actions.push_back(a);
@ -72,3 +81,15 @@ Event::Event(std::string title,
std::vector<Action>(allows)) { std::vector<Action>(allows)) {
this->utcTimestampCreatedAt = createUTCTimestamp(utcTimestampCreatedAt); 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;
}

View File

@ -18,14 +18,6 @@ enum EventType {
PLAYER_STATE_CHANGE, PLAYER_STATE_CHANGE,
}; };
enum Action {
KILL,
VOTE,
INVESTIGATE,
HEAL,
PROTECT,
};
enum RoomStatus { enum RoomStatus {
AWAITING_START, AWAITING_START,
IN_PROGRESS, IN_PROGRESS,
@ -45,6 +37,14 @@ struct Player;
struct Room; struct Room;
struct Event; struct Event;
struct Action {
std::string name;
bool hasTarget;
Action(std::string name, bool hasTarget);
bool operator==(const Action &other) const;
};
struct Role { struct Role {
std::vector<Action> actions; std::vector<Action> actions;
@ -78,6 +78,10 @@ struct Event {
std::vector<Action> prohibits; std::vector<Action> prohibits;
std::vector<Action> allows; 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, Event(std::string title,
uint32_t utcTimestampCreatedAt, uint32_t utcTimestampCreatedAt,
uint32_t numberNight, uint32_t numberNight,